# 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.0062305130 -0.0029352592 0.0366063129 0.052133096 4.992271e-02
## 2013-02-28 0.0058902835 -0.0231049967 -0.0129695690 0.016175558 1.267836e-02
## 2013-03-28 0.0009847199 -0.0102354628 0.0129695690 0.040258086 3.726842e-02
## 2013-04-30 0.0096397717 0.0120848940 0.0489676215 0.001222024 1.902938e-02
## 2013-05-31 -0.0202141789 -0.0494834017 -0.0306555009 0.041976604 2.333572e-02
## 2013-06-28 -0.0157785266 -0.0547280833 -0.0271445280 -0.001402903 -1.343419e-02
## 2013-07-31 0.0026873100 0.0131590314 0.0518603956 0.063541427 5.038559e-02
## 2013-08-30 -0.0082974867 -0.0257051958 -0.0197465144 -0.034743478 -3.045134e-02
## 2013-09-30 0.0111443285 0.0695885897 0.0753387023 0.063873622 3.115615e-02
## 2013-10-31 0.0082916040 0.0408613628 0.0320817394 0.034234167 4.526662e-02
## 2013-11-29 -0.0025095221 -0.0025940764 0.0054495994 0.041661243 2.920658e-02
## 2013-12-31 -0.0055835318 -0.0040741220 0.0215280075 0.012892129 2.559637e-02
## 2014-01-31 0.0152914006 -0.0903224612 -0.0534133526 -0.035775390 -3.588455e-02
## 2014-02-28 0.0037577123 0.0332203512 0.0595051668 0.045257180 4.451065e-02
## 2014-03-31 -0.0014821344 0.0380216363 -0.0046027323 0.013315314 8.260999e-03
## 2014-04-30 0.0081833579 0.0077724951 0.0165294236 -0.023184192 6.927457e-03
## 2014-05-30 0.0117214589 0.0290914298 0.0158282903 0.006205286 2.294155e-02
## 2014-06-30 -0.0005758403 0.0237338294 0.0091658006 0.037718971 2.043435e-02
## 2014-07-31 -0.0025121562 0.0135556888 -0.0263799645 -0.052009324 -1.352860e-02
## 2014-08-29 0.0114310019 0.0279048239 0.0018004766 0.043657693 3.870468e-02
## 2014-09-30 -0.0061673533 -0.0808568785 -0.0395984549 -0.061260812 -1.389237e-02
## 2014-10-31 0.0105844580 0.0140965713 -0.0026547696 0.068875138 2.327789e-02
## 2014-11-28 0.0065489891 -0.0155414412 0.0006250021 0.004773416 2.710132e-02
## 2014-12-31 0.0014748790 -0.0404419346 -0.0407463100 0.025295913 -2.539647e-03
## 2015-01-30 0.0203157216 -0.0068959413 0.0062261953 -0.054627809 -3.007746e-02
## 2015-02-27 -0.0089888475 0.0431360857 0.0614506677 0.056914605 5.468215e-02
## 2015-03-31 0.0037408990 -0.0150863626 -0.0143888736 0.010156389 -1.583032e-02
## 2015-04-30 -0.0032328671 0.0662814194 0.0358165820 -0.018417724 9.785962e-03
## 2015-05-29 -0.0043837166 -0.0419106929 0.0019526337 0.007509938 1.277442e-02
## 2015-06-30 -0.0108258754 -0.0297469392 -0.0316789252 0.004171359 -2.052141e-02
## 2015-07-31 0.0085848680 -0.0651780220 0.0201146561 -0.027375267 2.233793e-02
## 2015-08-31 -0.0033641534 -0.0925122563 -0.0771525048 -0.047268098 -6.288681e-02
## 2015-09-30 0.0080815304 -0.0318252450 -0.0451948496 -0.038464823 -2.584726e-02
## 2015-10-30 0.0006855448 0.0618083560 0.0640259951 0.063589505 8.163508e-02
## 2015-11-30 -0.0038984430 -0.0255603366 -0.0075558957 0.024414879 3.648162e-03
## 2015-12-31 -0.0019189930 -0.0389471369 -0.0235951381 -0.052156854 -1.743289e-02
## 2016-01-29 0.0123300213 -0.0516367002 -0.0567577056 -0.060306641 -5.106904e-02
## 2016-02-29 0.0088316786 -0.0082115378 -0.0339138310 0.020605212 -8.261509e-04
## 2016-03-31 0.0087087767 0.1218790966 0.0637456621 0.089910057 6.510025e-02
## 2016-04-29 0.0025461178 0.0040792408 0.0219750163 0.021044553 3.933602e-03
## 2016-05-31 0.0001360457 -0.0376284587 -0.0008561716 0.004397027 1.686808e-02
## 2016-06-30 0.0191664444 0.0445820115 -0.0244915294 0.008292246 3.469846e-03
## 2016-07-29 0.0054295068 0.0524423053 0.0390003306 0.049348280 3.582217e-02
## 2016-08-31 -0.0021562660 0.0087987266 0.0053270996 0.011261344 1.196811e-03
## 2016-09-30 0.0005161298 0.0248724178 0.0132789926 0.008614451 5.827063e-05
## 2016-10-31 -0.0082057180 -0.0083117032 -0.0224038355 -0.038134886 -1.748914e-02
## 2016-11-30 -0.0259892699 -0.0451616839 -0.0179743146 0.125246531 3.617567e-02
## 2016-12-30 0.0025379669 -0.0025302457 0.0267028154 0.031491676 2.006930e-02
## 2017-01-31 0.0021259474 0.0644313559 0.0323819116 -0.012143957 1.773640e-02
## 2017-02-28 0.0064384981 0.0172579587 0.0118362689 0.013428835 3.853933e-02
## 2017-03-31 -0.0005532173 0.0361889731 0.0318058334 -0.006532870 1.249344e-03
## 2017-04-28 0.0090294342 0.0168664077 0.0239520989 0.005107514 9.877006e-03
## 2017-05-31 0.0068471806 0.0280601231 0.0348104463 -0.022862421 1.401420e-02
## 2017-06-30 -0.0001826531 0.0092235478 0.0029558517 0.029151638 6.354794e-03
## 2017-07-31 0.0033344052 0.0565944241 0.0261878079 0.007481652 2.034587e-02
## 2017-08-31 0.0093690704 0.0232439210 -0.0004483592 -0.027564835 2.913498e-03
## 2017-09-29 -0.0057315665 -0.0004463619 0.0233426736 0.082321737 1.994886e-02
## 2017-10-31 0.0009773904 0.0322786990 0.0166539327 0.005916215 2.329075e-02
## 2017-11-30 -0.0014839725 -0.0038970180 0.0068698242 0.036912920 3.010834e-02
## 2017-12-29 0.0047399892 0.0369253201 0.0133984810 -0.003730741 1.205462e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398441e-05 0.0001042091 4.178434e-05 -7.811854e-05 -9.030723e-06
## EEM 1.042091e-04 0.0017547079 1.039016e-03 6.437715e-04 6.795434e-04
## EFA 4.178434e-05 0.0010390164 1.064239e-03 6.490298e-04 6.975421e-04
## IJS -7.811854e-05 0.0006437715 6.490298e-04 1.565448e-03 8.290239e-04
## SPY -9.030723e-06 0.0006795434 6.975421e-04 8.290239e-04 7.408306e-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.02347491
# 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.0003874187 0.009257132 0.005815641 0.005684463 0.002330252
rowSums(component_contribution)
## [1] 0.02347491
# 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.0062305130 -0.0029352592 0.0366063129 0.052133096 4.992271e-02
## 2013-02-28 0.0058902835 -0.0231049967 -0.0129695690 0.016175558 1.267836e-02
## 2013-03-28 0.0009847199 -0.0102354628 0.0129695690 0.040258086 3.726842e-02
## 2013-04-30 0.0096397717 0.0120848940 0.0489676215 0.001222024 1.902938e-02
## 2013-05-31 -0.0202141789 -0.0494834017 -0.0306555009 0.041976604 2.333572e-02
## 2013-06-28 -0.0157785266 -0.0547280833 -0.0271445280 -0.001402903 -1.343419e-02
## 2013-07-31 0.0026873100 0.0131590314 0.0518603956 0.063541427 5.038559e-02
## 2013-08-30 -0.0082974867 -0.0257051958 -0.0197465144 -0.034743478 -3.045134e-02
## 2013-09-30 0.0111443285 0.0695885897 0.0753387023 0.063873622 3.115615e-02
## 2013-10-31 0.0082916040 0.0408613628 0.0320817394 0.034234167 4.526662e-02
## 2013-11-29 -0.0025095221 -0.0025940764 0.0054495994 0.041661243 2.920658e-02
## 2013-12-31 -0.0055835318 -0.0040741220 0.0215280075 0.012892129 2.559637e-02
## 2014-01-31 0.0152914006 -0.0903224612 -0.0534133526 -0.035775390 -3.588455e-02
## 2014-02-28 0.0037577123 0.0332203512 0.0595051668 0.045257180 4.451065e-02
## 2014-03-31 -0.0014821344 0.0380216363 -0.0046027323 0.013315314 8.260999e-03
## 2014-04-30 0.0081833579 0.0077724951 0.0165294236 -0.023184192 6.927457e-03
## 2014-05-30 0.0117214589 0.0290914298 0.0158282903 0.006205286 2.294155e-02
## 2014-06-30 -0.0005758403 0.0237338294 0.0091658006 0.037718971 2.043435e-02
## 2014-07-31 -0.0025121562 0.0135556888 -0.0263799645 -0.052009324 -1.352860e-02
## 2014-08-29 0.0114310019 0.0279048239 0.0018004766 0.043657693 3.870468e-02
## 2014-09-30 -0.0061673533 -0.0808568785 -0.0395984549 -0.061260812 -1.389237e-02
## 2014-10-31 0.0105844580 0.0140965713 -0.0026547696 0.068875138 2.327789e-02
## 2014-11-28 0.0065489891 -0.0155414412 0.0006250021 0.004773416 2.710132e-02
## 2014-12-31 0.0014748790 -0.0404419346 -0.0407463100 0.025295913 -2.539647e-03
## 2015-01-30 0.0203157216 -0.0068959413 0.0062261953 -0.054627809 -3.007746e-02
## 2015-02-27 -0.0089888475 0.0431360857 0.0614506677 0.056914605 5.468215e-02
## 2015-03-31 0.0037408990 -0.0150863626 -0.0143888736 0.010156389 -1.583032e-02
## 2015-04-30 -0.0032328671 0.0662814194 0.0358165820 -0.018417724 9.785962e-03
## 2015-05-29 -0.0043837166 -0.0419106929 0.0019526337 0.007509938 1.277442e-02
## 2015-06-30 -0.0108258754 -0.0297469392 -0.0316789252 0.004171359 -2.052141e-02
## 2015-07-31 0.0085848680 -0.0651780220 0.0201146561 -0.027375267 2.233793e-02
## 2015-08-31 -0.0033641534 -0.0925122563 -0.0771525048 -0.047268098 -6.288681e-02
## 2015-09-30 0.0080815304 -0.0318252450 -0.0451948496 -0.038464823 -2.584726e-02
## 2015-10-30 0.0006855448 0.0618083560 0.0640259951 0.063589505 8.163508e-02
## 2015-11-30 -0.0038984430 -0.0255603366 -0.0075558957 0.024414879 3.648162e-03
## 2015-12-31 -0.0019189930 -0.0389471369 -0.0235951381 -0.052156854 -1.743289e-02
## 2016-01-29 0.0123300213 -0.0516367002 -0.0567577056 -0.060306641 -5.106904e-02
## 2016-02-29 0.0088316786 -0.0082115378 -0.0339138310 0.020605212 -8.261509e-04
## 2016-03-31 0.0087087767 0.1218790966 0.0637456621 0.089910057 6.510025e-02
## 2016-04-29 0.0025461178 0.0040792408 0.0219750163 0.021044553 3.933602e-03
## 2016-05-31 0.0001360457 -0.0376284587 -0.0008561716 0.004397027 1.686808e-02
## 2016-06-30 0.0191664444 0.0445820115 -0.0244915294 0.008292246 3.469846e-03
## 2016-07-29 0.0054295068 0.0524423053 0.0390003306 0.049348280 3.582217e-02
## 2016-08-31 -0.0021562660 0.0087987266 0.0053270996 0.011261344 1.196811e-03
## 2016-09-30 0.0005161298 0.0248724178 0.0132789926 0.008614451 5.827063e-05
## 2016-10-31 -0.0082057180 -0.0083117032 -0.0224038355 -0.038134886 -1.748914e-02
## 2016-11-30 -0.0259892699 -0.0451616839 -0.0179743146 0.125246531 3.617567e-02
## 2016-12-30 0.0025379669 -0.0025302457 0.0267028154 0.031491676 2.006930e-02
## 2017-01-31 0.0021259474 0.0644313559 0.0323819116 -0.012143957 1.773640e-02
## 2017-02-28 0.0064384981 0.0172579587 0.0118362689 0.013428835 3.853933e-02
## 2017-03-31 -0.0005532173 0.0361889731 0.0318058334 -0.006532870 1.249344e-03
## 2017-04-28 0.0090294342 0.0168664077 0.0239520989 0.005107514 9.877006e-03
## 2017-05-31 0.0068471806 0.0280601231 0.0348104463 -0.022862421 1.401420e-02
## 2017-06-30 -0.0001826531 0.0092235478 0.0029558517 0.029151638 6.354794e-03
## 2017-07-31 0.0033344052 0.0565944241 0.0261878079 0.007481652 2.034587e-02
## 2017-08-31 0.0093690704 0.0232439210 -0.0004483592 -0.027564835 2.913498e-03
## 2017-09-29 -0.0057315665 -0.0004463619 0.0233426736 0.082321737 1.994886e-02
## 2017-10-31 0.0009773904 0.0322786990 0.0166539327 0.005916215 2.329075e-02
## 2017-11-30 -0.0014839725 -0.0038970180 0.0068698242 0.036912920 3.010834e-02
## 2017-12-29 0.0047399892 0.0369253201 0.0133984810 -0.003730741 1.205462e-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]
rowSums(component_contribution)
# 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(0.25, 0.25, 0.2, 0.2, 0.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
column chart of component contribution
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.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")
column chart of component contribution and weight
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
#transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# add weight
add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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)) +
scale_fill_tq()+
theme(plot.title = element_text(hjust = 0.5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight",
y = "Percent",
x = NULL)