# 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.0062309983 -0.0029354859 0.0366062228 0.052133210 4.992304e-02
## 2013-02-28 0.0058912506 -0.0231053479 -0.0129693818 0.016175451 1.267818e-02
## 2013-03-28 0.0009848158 -0.0102348806 0.0129693818 0.040257987 3.726825e-02
## 2013-04-30 0.0096391950 0.0120847766 0.0489681652 0.001222641 1.902987e-02
## 2013-05-31 -0.0202142782 -0.0494834017 -0.0306559493 0.041976094 2.333572e-02
## 2013-06-28 -0.0157780355 -0.0547284694 -0.0271444361 -0.001402705 -1.343466e-02
## 2013-07-31 0.0026877036 0.0131597987 0.0518603060 0.063541236 5.038628e-02
## 2013-08-30 -0.0082982762 -0.0257055769 -0.0197463333 -0.034743290 -3.045156e-02
## 2013-09-30 0.0111438381 0.0695888329 0.0753385261 0.063873346 3.115603e-02
## 2013-10-31 0.0082928762 0.0408613531 0.0320817420 0.034234173 4.526704e-02
## 2013-11-29 -0.0025106932 -0.0025943098 0.0054495999 0.041661333 2.920626e-02
## 2013-12-31 -0.0055827482 -0.0040740045 0.0215282490 0.012892377 2.559626e-02
## 2014-01-31 0.0152916865 -0.0903227073 -0.0534135125 -0.035775981 -3.588476e-02
## 2014-02-28 0.0037563601 0.0332204176 0.0595049284 0.045257933 4.451036e-02
## 2014-03-31 -0.0014809767 0.0380213991 -0.0046023343 0.013315229 8.261699e-03
## 2014-04-30 0.0081834506 0.0077731510 0.0165292640 -0.023184434 6.927159e-03
## 2014-05-30 0.0117210747 0.0290909579 0.0158288312 0.006205122 2.294136e-02
## 2014-06-30 -0.0005757455 0.0237340576 0.0091651831 0.037718899 2.043464e-02
## 2014-07-31 -0.0025117759 0.0135554649 -0.0263798879 -0.052009586 -1.352869e-02
## 2014-08-29 0.0114307137 0.0279047188 0.0018005551 0.043658032 3.870468e-02
## 2014-09-30 -0.0061669737 -0.0808566622 -0.0395986151 -0.061260146 -1.389209e-02
## 2014-10-31 0.0105838902 0.0140965713 -0.0026548517 0.068874552 2.327770e-02
## 2014-11-28 0.0065490813 -0.0155413238 0.0006250840 0.004773416 2.710140e-02
## 2014-12-31 0.0014748787 -0.0404424187 -0.0407463986 0.025296145 -2.539557e-03
## 2015-01-30 0.0203152636 -0.0068955130 0.0062265351 -0.054628043 -3.007737e-02
## 2015-02-27 -0.0089882999 0.0431361421 0.0614504983 0.056914762 5.468222e-02
## 2015-03-31 0.0037401673 -0.0150863009 -0.0143887929 0.010156464 -1.583067e-02
## 2015-04-30 -0.0032328685 0.0662814639 0.0358164232 -0.018418029 9.786137e-03
## 2015-05-29 -0.0043836266 -0.0419112674 0.0019529451 0.007510015 1.277407e-02
## 2015-06-30 -0.0108254143 -0.0297461675 -0.0316789980 0.004171360 -2.052132e-02
## 2015-07-31 0.0085846828 -0.0651786359 0.0201144168 -0.027375348 2.233793e-02
## 2015-08-31 -0.0033637835 -0.0925123454 -0.0771524261 -0.047268187 -6.288709e-02
## 2015-09-30 0.0080814364 -0.0318248178 -0.0451947606 -0.038465003 -2.584661e-02
## 2015-10-30 0.0006859117 0.0618082789 0.0640259062 0.063589771 8.163472e-02
## 2015-11-30 -0.0038987171 -0.0255604049 -0.0075558957 0.024415117 3.648595e-03
## 2015-12-31 -0.0019189002 -0.0389470640 -0.0235949659 -0.052156767 -1.743350e-02
## 2016-01-29 0.0123294699 -0.0516366195 -0.0567577867 -0.060307067 -5.106859e-02
## 2016-02-29 0.0088319512 -0.0082117688 -0.0339140163 0.020605645 -8.267999e-04
## 2016-03-31 0.0087087760 0.1218792426 0.0637455795 0.089910276 6.510062e-02
## 2016-04-29 0.0025458496 0.0040789675 0.0219751066 0.021044009 3.933516e-03
## 2016-05-31 0.0001355098 -0.0376282539 -0.0008559986 0.004397027 1.686825e-02
## 2016-06-30 0.0191673343 0.0445823504 -0.0244914386 0.008292248 3.470100e-03
## 2016-07-29 0.0054295930 0.0524418378 0.0390002385 0.049348575 3.582199e-02
## 2016-08-31 -0.0021561781 0.0087989828 0.0053267596 0.011261058 1.197056e-03
## 2016-09-30 0.0005163042 0.0248726635 0.0132791635 0.008614523 5.769854e-05
## 2016-10-31 -0.0082055376 -0.0083118882 -0.0224037517 -0.038134815 -1.748931e-02
## 2016-11-30 -0.0259899740 -0.0451622001 -0.0179744890 0.125246596 3.617624e-02
## 2016-12-30 0.0025377867 -0.0025297203 0.0267030747 0.031491736 2.006914e-02
## 2017-01-31 0.0021265771 0.0644314669 0.0323817445 -0.012144082 1.773617e-02
## 2017-02-28 0.0064378699 0.0172576504 0.0118364324 0.013428647 3.853949e-02
## 2017-03-31 -0.0005528599 0.0361889731 0.0318056735 -0.006532997 1.249270e-03
## 2017-04-28 0.0090287240 0.0168662927 0.0239524080 0.005107578 9.877007e-03
## 2017-05-31 0.0068475361 0.0280601262 0.0348100674 -0.022862427 1.401464e-02
## 2017-06-30 -0.0001822132 0.0092236596 0.0029560741 0.029151832 6.354576e-03
## 2017-07-31 0.0033340535 0.0565944241 0.0261878060 0.007481590 2.034558e-02
## 2017-08-31 0.0093698531 0.0232440232 -0.0004483592 -0.027564394 2.913569e-03
## 2017-09-29 -0.0057324363 -0.0004464642 0.0233428833 0.082321419 1.994907e-02
## 2017-10-31 0.0009778271 0.0322785008 0.0166536509 0.005916215 2.329081e-02
## 2017-11-30 -0.0014839721 -0.0038971182 0.0068698930 0.036913032 3.010801e-02
## 2017-12-29 0.0047397269 0.0369256184 0.0133984122 -0.003731078 1.205482e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398433e-05 0.0001042115 4.178258e-05 -7.812303e-05 -9.031383e-06
## EEM 1.042115e-04 0.0017547124 1.039017e-03 6.437714e-04 6.795443e-04
## EFA 4.178258e-05 0.0010390172 1.064238e-03 6.490297e-04 6.975419e-04
## IJS -7.812303e-05 0.0006437714 6.490297e-04 1.565450e-03 8.290271e-04
## SPY -9.031383e-06 0.0006795443 6.975419e-04 8.290271e-04 7.408328e-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.0003874108 0.009257151 0.005815636 0.005684458 0.002330255
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.0062309983 -0.0029354859 0.0366062228 0.052133210 4.992304e-02
## 2013-02-28 0.0058912506 -0.0231053479 -0.0129693818 0.016175451 1.267818e-02
## 2013-03-28 0.0009848158 -0.0102348806 0.0129693818 0.040257987 3.726825e-02
## 2013-04-30 0.0096391950 0.0120847766 0.0489681652 0.001222641 1.902987e-02
## 2013-05-31 -0.0202142782 -0.0494834017 -0.0306559493 0.041976094 2.333572e-02
## 2013-06-28 -0.0157780355 -0.0547284694 -0.0271444361 -0.001402705 -1.343466e-02
## 2013-07-31 0.0026877036 0.0131597987 0.0518603060 0.063541236 5.038628e-02
## 2013-08-30 -0.0082982762 -0.0257055769 -0.0197463333 -0.034743290 -3.045156e-02
## 2013-09-30 0.0111438381 0.0695888329 0.0753385261 0.063873346 3.115603e-02
## 2013-10-31 0.0082928762 0.0408613531 0.0320817420 0.034234173 4.526704e-02
## 2013-11-29 -0.0025106932 -0.0025943098 0.0054495999 0.041661333 2.920626e-02
## 2013-12-31 -0.0055827482 -0.0040740045 0.0215282490 0.012892377 2.559626e-02
## 2014-01-31 0.0152916865 -0.0903227073 -0.0534135125 -0.035775981 -3.588476e-02
## 2014-02-28 0.0037563601 0.0332204176 0.0595049284 0.045257933 4.451036e-02
## 2014-03-31 -0.0014809767 0.0380213991 -0.0046023343 0.013315229 8.261699e-03
## 2014-04-30 0.0081834506 0.0077731510 0.0165292640 -0.023184434 6.927159e-03
## 2014-05-30 0.0117210747 0.0290909579 0.0158288312 0.006205122 2.294136e-02
## 2014-06-30 -0.0005757455 0.0237340576 0.0091651831 0.037718899 2.043464e-02
## 2014-07-31 -0.0025117759 0.0135554649 -0.0263798879 -0.052009586 -1.352869e-02
## 2014-08-29 0.0114307137 0.0279047188 0.0018005551 0.043658032 3.870468e-02
## 2014-09-30 -0.0061669737 -0.0808566622 -0.0395986151 -0.061260146 -1.389209e-02
## 2014-10-31 0.0105838902 0.0140965713 -0.0026548517 0.068874552 2.327770e-02
## 2014-11-28 0.0065490813 -0.0155413238 0.0006250840 0.004773416 2.710140e-02
## 2014-12-31 0.0014748787 -0.0404424187 -0.0407463986 0.025296145 -2.539557e-03
## 2015-01-30 0.0203152636 -0.0068955130 0.0062265351 -0.054628043 -3.007737e-02
## 2015-02-27 -0.0089882999 0.0431361421 0.0614504983 0.056914762 5.468222e-02
## 2015-03-31 0.0037401673 -0.0150863009 -0.0143887929 0.010156464 -1.583067e-02
## 2015-04-30 -0.0032328685 0.0662814639 0.0358164232 -0.018418029 9.786137e-03
## 2015-05-29 -0.0043836266 -0.0419112674 0.0019529451 0.007510015 1.277407e-02
## 2015-06-30 -0.0108254143 -0.0297461675 -0.0316789980 0.004171360 -2.052132e-02
## 2015-07-31 0.0085846828 -0.0651786359 0.0201144168 -0.027375348 2.233793e-02
## 2015-08-31 -0.0033637835 -0.0925123454 -0.0771524261 -0.047268187 -6.288709e-02
## 2015-09-30 0.0080814364 -0.0318248178 -0.0451947606 -0.038465003 -2.584661e-02
## 2015-10-30 0.0006859117 0.0618082789 0.0640259062 0.063589771 8.163472e-02
## 2015-11-30 -0.0038987171 -0.0255604049 -0.0075558957 0.024415117 3.648595e-03
## 2015-12-31 -0.0019189002 -0.0389470640 -0.0235949659 -0.052156767 -1.743350e-02
## 2016-01-29 0.0123294699 -0.0516366195 -0.0567577867 -0.060307067 -5.106859e-02
## 2016-02-29 0.0088319512 -0.0082117688 -0.0339140163 0.020605645 -8.267999e-04
## 2016-03-31 0.0087087760 0.1218792426 0.0637455795 0.089910276 6.510062e-02
## 2016-04-29 0.0025458496 0.0040789675 0.0219751066 0.021044009 3.933516e-03
## 2016-05-31 0.0001355098 -0.0376282539 -0.0008559986 0.004397027 1.686825e-02
## 2016-06-30 0.0191673343 0.0445823504 -0.0244914386 0.008292248 3.470100e-03
## 2016-07-29 0.0054295930 0.0524418378 0.0390002385 0.049348575 3.582199e-02
## 2016-08-31 -0.0021561781 0.0087989828 0.0053267596 0.011261058 1.197056e-03
## 2016-09-30 0.0005163042 0.0248726635 0.0132791635 0.008614523 5.769854e-05
## 2016-10-31 -0.0082055376 -0.0083118882 -0.0224037517 -0.038134815 -1.748931e-02
## 2016-11-30 -0.0259899740 -0.0451622001 -0.0179744890 0.125246596 3.617624e-02
## 2016-12-30 0.0025377867 -0.0025297203 0.0267030747 0.031491736 2.006914e-02
## 2017-01-31 0.0021265771 0.0644314669 0.0323817445 -0.012144082 1.773617e-02
## 2017-02-28 0.0064378699 0.0172576504 0.0118364324 0.013428647 3.853949e-02
## 2017-03-31 -0.0005528599 0.0361889731 0.0318056735 -0.006532997 1.249270e-03
## 2017-04-28 0.0090287240 0.0168662927 0.0239524080 0.005107578 9.877007e-03
## 2017-05-31 0.0068475361 0.0280601262 0.0348100674 -0.022862427 1.401464e-02
## 2017-06-30 -0.0001822132 0.0092236596 0.0029560741 0.029151832 6.354576e-03
## 2017-07-31 0.0033340535 0.0565944241 0.0261878060 0.007481590 2.034558e-02
## 2017-08-31 0.0093698531 0.0232440232 -0.0004483592 -0.027564394 2.913569e-03
## 2017-09-29 -0.0057324363 -0.0004464642 0.0233428833 0.082321419 1.994907e-02
## 2017-10-31 0.0009778271 0.0322785008 0.0166536509 0.005916215 2.329081e-02
## 2017-11-30 -0.0014839721 -0.0038971182 0.0068698930 0.036913032 3.010801e-02
## 2017-12-29 0.0047397269 0.0369256184 0.0133984122 -0.003731078 1.205482e-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
Column Chart of Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, .3)) %>%
# 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(.25, .25, .2, .2, .3)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weight
add_column(weight = c(.25, .25, .2, .2, .3)) %>%
# 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)