# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG” from 2012-12-31 to 2017-12-31
symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
Refresh your memory on covariance with this video. Click this link Refresh your memory on matrix multiplication. Click this link
# Transform data into wide form
asset_returns_wide_tbl <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns) %>%
column_to_rownames(var = "date")
asset_returns_wide_tbl
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062310302 -0.0029357944 0.0366061226 0.052132974 4.992330e-02
## 2013-02-28 0.0058912506 -0.0231051224 -0.0129692964 0.016175033 1.267804e-02
## 2013-03-28 0.0009847176 -0.0102348964 0.0129692964 0.040258510 3.726785e-02
## 2013-04-30 0.0096393918 0.0120846767 0.0489679861 0.001222466 1.903006e-02
## 2013-05-31 -0.0202140267 -0.0494833772 -0.0306557725 0.041976280 2.333539e-02
## 2013-06-28 -0.0157785582 -0.0547284477 -0.0271442001 -0.001403043 -1.343435e-02
## 2013-07-31 0.0026881351 0.0131596377 0.0518602514 0.063541264 5.038611e-02
## 2013-08-30 -0.0082982987 -0.0257054247 -0.0197464637 -0.034743377 -3.045137e-02
## 2013-09-30 0.0111438853 0.0695886588 0.0753388338 0.063873833 3.115588e-02
## 2013-10-31 0.0082921216 0.0408614153 0.0320815486 0.034233936 4.526648e-02
## 2013-11-29 -0.0025102685 -0.0025940275 0.0054495420 0.041661342 2.920745e-02
## 2013-12-31 -0.0055823267 -0.0040742469 0.0215279768 0.012891881 2.559577e-02
## 2014-01-31 0.0152911372 -0.0903229138 -0.0534133066 -0.035775161 -3.588454e-02
## 2014-02-28 0.0037575014 0.0332209062 0.0595049358 0.045257171 4.451040e-02
## 2014-03-31 -0.0014820946 0.0380214590 -0.0046026447 0.013315435 8.261317e-03
## 2014-04-30 0.0081835703 0.0077728584 0.0165296788 -0.023184441 6.927763e-03
## 2014-05-30 0.0117212260 0.0290910756 0.0158281240 0.006205867 2.294099e-02
## 2014-06-30 -0.0005755764 0.0237339436 0.0091656332 0.037718555 2.043469e-02
## 2014-07-31 -0.0025124754 0.0135556813 -0.0263800106 -0.052009682 -1.352873e-02
## 2014-08-29 0.0114309519 0.0279047860 0.0018005753 0.043657972 3.870455e-02
## 2014-09-30 -0.0061678396 -0.0808572351 -0.0395982591 -0.061260541 -1.389192e-02
## 2014-10-31 0.0105850727 0.0140967943 -0.0026548903 0.068874978 2.327751e-02
## 2014-11-28 0.0065486274 -0.0155412963 0.0006253098 0.004773794 2.710140e-02
## 2014-12-31 0.0014755964 -0.0404421073 -0.0407467275 0.025295825 -2.539750e-03
## 2015-01-30 0.0203150420 -0.0068959157 0.0062264224 -0.054628117 -3.007699e-02
## 2015-02-27 -0.0089884231 0.0431363135 0.0614505401 0.056914634 5.468163e-02
## 2015-03-31 0.0037400416 -0.0150863669 -0.0143887072 0.010156461 -1.583011e-02
## 2015-04-30 -0.0032326512 0.0662816331 0.0358165822 -0.018417967 9.785865e-03
## 2015-05-29 -0.0043836431 -0.0419113473 0.0019525726 0.007510025 1.277439e-02
## 2015-06-30 -0.0108256963 -0.0297464970 -0.0316787522 0.004171520 -2.052118e-02
## 2015-07-31 0.0085844664 -0.0651779380 0.0201147128 -0.027375361 2.233763e-02
## 2015-08-31 -0.0033636570 -0.0925126544 -0.0771527212 -0.047268737 -6.288669e-02
## 2015-09-30 0.0080814315 -0.0318249940 -0.0451949005 -0.038464554 -2.584686e-02
## 2015-10-30 0.0006857602 0.0618084413 0.0640259784 0.063589794 8.163460e-02
## 2015-11-30 -0.0038984862 -0.0255605379 -0.0075557990 0.024415184 3.648876e-03
## 2015-12-31 -0.0019189447 -0.0389469627 -0.0235948625 -0.052156908 -1.743408e-02
## 2016-01-29 0.0123299537 -0.0516368964 -0.0567581634 -0.060306942 -5.106849e-02
## 2016-02-29 0.0088317452 -0.0082113800 -0.0339135419 0.020604953 -8.263023e-04
## 2016-03-31 0.0087088917 0.1218789097 0.0637455054 0.089910582 6.510024e-02
## 2016-04-29 0.0025456310 0.0040792162 0.0219750146 0.021044159 3.933565e-03
## 2016-05-31 0.0001356518 -0.0376285104 -0.0008560461 0.004397266 1.686863e-02
## 2016-06-30 0.0191668683 0.0445823386 -0.0244915383 0.008292177 3.469632e-03
## 2016-07-29 0.0054297554 0.0524423474 0.0390003656 0.049348308 3.582191e-02
## 2016-08-31 -0.0021560291 0.0087984749 0.0053267801 0.011261181 1.196978e-03
## 2016-09-30 0.0005157352 0.0248729944 0.0132790773 0.008614667 5.781295e-05
## 2016-10-31 -0.0082053034 -0.0083124950 -0.0224035026 -0.038134715 -1.748897e-02
## 2016-11-30 -0.0259897210 -0.0451617770 -0.0179744805 0.125246158 3.617590e-02
## 2016-12-30 0.0025381854 -0.0025298182 0.0267029407 0.031491742 2.006909e-02
## 2017-01-31 0.0021261333 0.0644313313 0.0323816323 -0.012143535 1.773662e-02
## 2017-02-28 0.0064377760 0.0172579571 0.0118365508 0.013428559 3.853934e-02
## 2017-03-31 -0.0005523639 0.0361887542 0.0318057108 -0.006533266 1.249160e-03
## 2017-04-28 0.0090289412 0.0168665132 0.0239520652 0.005108021 9.877162e-03
## 2017-05-31 0.0068475871 0.0280598776 0.0348102620 -0.022862561 1.401416e-02
## 2017-06-30 -0.0001827592 0.0092238738 0.0029559267 0.029151379 6.354832e-03
## 2017-07-31 0.0033343179 0.0565945416 0.0261879397 0.007481983 2.034572e-02
## 2017-08-31 0.0093691971 0.0232437716 -0.0004481536 -0.027564793 2.913448e-03
## 2017-09-29 -0.0057320477 -0.0004462957 0.0233427719 0.082321891 1.994894e-02
## 2017-10-31 0.0009778673 0.0322784749 0.0166535266 0.005916086 2.329089e-02
## 2017-11-30 -0.0014840711 -0.0038967587 0.0068699718 0.036912838 3.010800e-02
## 2017-12-29 0.0047404056 0.0369252960 0.0133984028 -0.003730915 1.205500e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398424e-05 0.0001042128 4.178514e-05 -7.811683e-05 -9.029307e-06
## EEM 1.042128e-04 0.0017547145 1.039018e-03 6.437774e-04 6.795432e-04
## EFA 4.178514e-05 0.0010390181 1.064238e-03 6.490305e-04 6.975397e-04
## IJS -7.811683e-05 0.0006437774 6.490305e-04 1.565450e-03 8.290242e-04
## SPY -9.029307e-06 0.0006795432 6.975397e-04 8.290242e-04 7.408266e-04
# Standard deviation of portfolio
# Summarizes how much each asset's returns vary with those of other assets within the portfolio into a single number
w <- c(0.25, 0.25, 0.2, 0.2, 0.1)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.02347495
# Component contribution
# Similar to the formula for sd_portfolio
# Mathematical trick to summarize the same, sd_portfolio, by asset instead of a single number
component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
component_contribution
## AGG EEM EFA IJS SPY
## [1,] 0.0003874343 0.009257159 0.005815634 0.005684474 0.002330246
rowSums(component_contribution)
## [1] 0.02347495
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AGG 0.017
## 2 EEM 0.394
## 3 EFA 0.248
## 4 IJS 0.242
## 5 SPY 0.099
# Transform data into wide form
asset_returns_wide_tbl <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns) %>%
column_to_rownames(var = "date")
asset_returns_wide_tbl
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062310302 -0.0029357944 0.0366061226 0.052132974 4.992330e-02
## 2013-02-28 0.0058912506 -0.0231051224 -0.0129692964 0.016175033 1.267804e-02
## 2013-03-28 0.0009847176 -0.0102348964 0.0129692964 0.040258510 3.726785e-02
## 2013-04-30 0.0096393918 0.0120846767 0.0489679861 0.001222466 1.903006e-02
## 2013-05-31 -0.0202140267 -0.0494833772 -0.0306557725 0.041976280 2.333539e-02
## 2013-06-28 -0.0157785582 -0.0547284477 -0.0271442001 -0.001403043 -1.343435e-02
## 2013-07-31 0.0026881351 0.0131596377 0.0518602514 0.063541264 5.038611e-02
## 2013-08-30 -0.0082982987 -0.0257054247 -0.0197464637 -0.034743377 -3.045137e-02
## 2013-09-30 0.0111438853 0.0695886588 0.0753388338 0.063873833 3.115588e-02
## 2013-10-31 0.0082921216 0.0408614153 0.0320815486 0.034233936 4.526648e-02
## 2013-11-29 -0.0025102685 -0.0025940275 0.0054495420 0.041661342 2.920745e-02
## 2013-12-31 -0.0055823267 -0.0040742469 0.0215279768 0.012891881 2.559577e-02
## 2014-01-31 0.0152911372 -0.0903229138 -0.0534133066 -0.035775161 -3.588454e-02
## 2014-02-28 0.0037575014 0.0332209062 0.0595049358 0.045257171 4.451040e-02
## 2014-03-31 -0.0014820946 0.0380214590 -0.0046026447 0.013315435 8.261317e-03
## 2014-04-30 0.0081835703 0.0077728584 0.0165296788 -0.023184441 6.927763e-03
## 2014-05-30 0.0117212260 0.0290910756 0.0158281240 0.006205867 2.294099e-02
## 2014-06-30 -0.0005755764 0.0237339436 0.0091656332 0.037718555 2.043469e-02
## 2014-07-31 -0.0025124754 0.0135556813 -0.0263800106 -0.052009682 -1.352873e-02
## 2014-08-29 0.0114309519 0.0279047860 0.0018005753 0.043657972 3.870455e-02
## 2014-09-30 -0.0061678396 -0.0808572351 -0.0395982591 -0.061260541 -1.389192e-02
## 2014-10-31 0.0105850727 0.0140967943 -0.0026548903 0.068874978 2.327751e-02
## 2014-11-28 0.0065486274 -0.0155412963 0.0006253098 0.004773794 2.710140e-02
## 2014-12-31 0.0014755964 -0.0404421073 -0.0407467275 0.025295825 -2.539750e-03
## 2015-01-30 0.0203150420 -0.0068959157 0.0062264224 -0.054628117 -3.007699e-02
## 2015-02-27 -0.0089884231 0.0431363135 0.0614505401 0.056914634 5.468163e-02
## 2015-03-31 0.0037400416 -0.0150863669 -0.0143887072 0.010156461 -1.583011e-02
## 2015-04-30 -0.0032326512 0.0662816331 0.0358165822 -0.018417967 9.785865e-03
## 2015-05-29 -0.0043836431 -0.0419113473 0.0019525726 0.007510025 1.277439e-02
## 2015-06-30 -0.0108256963 -0.0297464970 -0.0316787522 0.004171520 -2.052118e-02
## 2015-07-31 0.0085844664 -0.0651779380 0.0201147128 -0.027375361 2.233763e-02
## 2015-08-31 -0.0033636570 -0.0925126544 -0.0771527212 -0.047268737 -6.288669e-02
## 2015-09-30 0.0080814315 -0.0318249940 -0.0451949005 -0.038464554 -2.584686e-02
## 2015-10-30 0.0006857602 0.0618084413 0.0640259784 0.063589794 8.163460e-02
## 2015-11-30 -0.0038984862 -0.0255605379 -0.0075557990 0.024415184 3.648876e-03
## 2015-12-31 -0.0019189447 -0.0389469627 -0.0235948625 -0.052156908 -1.743408e-02
## 2016-01-29 0.0123299537 -0.0516368964 -0.0567581634 -0.060306942 -5.106849e-02
## 2016-02-29 0.0088317452 -0.0082113800 -0.0339135419 0.020604953 -8.263023e-04
## 2016-03-31 0.0087088917 0.1218789097 0.0637455054 0.089910582 6.510024e-02
## 2016-04-29 0.0025456310 0.0040792162 0.0219750146 0.021044159 3.933565e-03
## 2016-05-31 0.0001356518 -0.0376285104 -0.0008560461 0.004397266 1.686863e-02
## 2016-06-30 0.0191668683 0.0445823386 -0.0244915383 0.008292177 3.469632e-03
## 2016-07-29 0.0054297554 0.0524423474 0.0390003656 0.049348308 3.582191e-02
## 2016-08-31 -0.0021560291 0.0087984749 0.0053267801 0.011261181 1.196978e-03
## 2016-09-30 0.0005157352 0.0248729944 0.0132790773 0.008614667 5.781295e-05
## 2016-10-31 -0.0082053034 -0.0083124950 -0.0224035026 -0.038134715 -1.748897e-02
## 2016-11-30 -0.0259897210 -0.0451617770 -0.0179744805 0.125246158 3.617590e-02
## 2016-12-30 0.0025381854 -0.0025298182 0.0267029407 0.031491742 2.006909e-02
## 2017-01-31 0.0021261333 0.0644313313 0.0323816323 -0.012143535 1.773662e-02
## 2017-02-28 0.0064377760 0.0172579571 0.0118365508 0.013428559 3.853934e-02
## 2017-03-31 -0.0005523639 0.0361887542 0.0318057108 -0.006533266 1.249160e-03
## 2017-04-28 0.0090289412 0.0168665132 0.0239520652 0.005108021 9.877162e-03
## 2017-05-31 0.0068475871 0.0280598776 0.0348102620 -0.022862561 1.401416e-02
## 2017-06-30 -0.0001827592 0.0092238738 0.0029559267 0.029151379 6.354832e-03
## 2017-07-31 0.0033343179 0.0565945416 0.0261879397 0.007481983 2.034572e-02
## 2017-08-31 0.0093691971 0.0232437716 -0.0004481536 -0.027564793 2.913448e-03
## 2017-09-29 -0.0057320477 -0.0004462957 0.0233427719 0.082321891 1.994894e-02
## 2017-10-31 0.0009778673 0.0322784749 0.0166535266 0.005916086 2.329089e-02
## 2017-11-30 -0.0014840711 -0.0038967587 0.0068699718 0.036912838 3.010800e-02
## 2017-12-29 0.0047404056 0.0369252960 0.0133984028 -0.003730915 1.205500e-02
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(.data)
# Standard deviation of portfolio
# Summarizes how much each asset's returns vary with those of other assets within the portfolio into a single number
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
# Component contribution
# Similar to the formula for sd_portfolio
# Mathematical trick to summarize the same, sd_portfolio, by asset instead of a single number
component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
return(component_percentages)
}
asset_returns_wide_tbl %>% calculate_component_contribution(w = c(.25, .25, .2, .2, .1))
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
# Transform to long form
pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution")
plot_data %>%
ggplot(aes(x = Asset, y = Contribution)) +
geom_col(fill = "cornflowerblue") +
scale_y_continuous(labels = scales:: percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
labs(title = "Percent Contribution to Portfolio Volatility")
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
# Transform to long form
pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.25, .25, .2, .2, .1)) %>%
# Transform to long
pivot_longer(cols = c(Contribution, weight), names_to = "type", values_to = "value")
plot_data %>%
ggplot(aes(x = Asset, y = value, fill = type)) +
geom_col(position = "dodge") +
scale_y_continuous(labels = scales:: percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
x = NULL)