# 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.0062308196 -0.0029353531 0.0366061226 0.052133425 4.992298e-02
## 2013-02-28 0.0058916374 -0.0231054593 -0.0129693883 0.016175243 1.267811e-02
## 2013-03-28 0.0009854413 -0.0102346701 0.0129693883 0.040258299 3.726833e-02
## 2013-04-30 0.0096386901 0.0120847897 0.0489678996 0.001222264 1.902971e-02
## 2013-05-31 -0.0202138173 -0.0494834901 -0.0306554188 0.041976579 2.333584e-02
## 2013-06-28 -0.0157788145 -0.0547283850 -0.0271447422 -0.001403237 -1.343457e-02
## 2013-07-31 0.0026879081 0.0131598224 0.0518604391 0.063541816 5.038565e-02
## 2013-08-30 -0.0082980988 -0.0257055452 -0.0197464655 -0.034743549 -3.045126e-02
## 2013-09-30 0.0111435129 0.0695886503 0.0753385113 0.063873285 3.115600e-02
## 2013-10-31 0.0082925042 0.0408611832 0.0320818802 0.034234372 4.526679e-02
## 2013-11-29 -0.0025099417 -0.0025940278 0.0054494632 0.041660843 2.920682e-02
## 2013-12-31 -0.0055831864 -0.0040741329 0.0215281353 0.012892452 2.559597e-02
## 2014-01-31 0.0152918052 -0.0903228512 -0.0534133066 -0.035775487 -3.588413e-02
## 2014-02-28 0.0037564575 0.0332208436 0.0595051669 0.045257575 4.451019e-02
## 2014-03-31 -0.0014813665 0.0380216922 -0.0046026436 0.013315036 8.261316e-03
## 2014-04-30 0.0081831846 0.0077727408 0.0165293705 -0.023184038 6.927372e-03
## 2014-05-30 0.0117222743 0.0290908475 0.0158284999 0.006205383 2.294128e-02
## 2014-06-30 -0.0005760563 0.0237341658 0.0091654820 0.037718328 2.043441e-02
## 2014-07-31 -0.0025122021 0.0135556798 -0.0263798541 -0.052009132 -1.352845e-02
## 2014-08-29 0.0114309500 0.0279046777 0.0018003464 0.043657965 3.870465e-02
## 2014-09-30 -0.0061676014 -0.0808572351 -0.0395984935 -0.061260697 -1.389247e-02
## 2014-10-31 0.0105846474 0.0140969069 -0.0026546525 0.068874901 2.327779e-02
## 2014-11-28 0.0065484327 -0.0155414089 0.0006252304 0.004773640 2.710167e-02
## 2014-12-31 0.0014752737 -0.0404419882 -0.0407468101 0.025296056 -2.539661e-03
## 2015-01-30 0.0203153584 -0.0068959149 0.0062265051 -0.054628276 -3.007734e-02
## 2015-02-27 -0.0089888039 0.0431358491 0.0614506945 0.056914793 5.468216e-02
## 2015-03-31 0.0037405113 -0.0150859058 -0.0143887833 0.010156239 -1.583062e-02
## 2015-04-30 -0.0032331180 0.0662811892 0.0358167306 -0.018417442 9.786040e-03
## 2015-05-29 -0.0043835609 -0.0419107925 0.0019524967 0.007509873 1.277414e-02
## 2015-06-30 -0.0108249160 -0.0297469590 -0.0316788252 0.004171296 -2.052119e-02
## 2015-07-31 0.0085846576 -0.0651778287 0.0201143296 -0.027375517 2.233789e-02
## 2015-08-31 -0.0033640847 -0.0925124607 -0.0771523334 -0.047268184 -6.288651e-02
## 2015-09-30 0.0080816430 -0.0318251334 -0.0451947241 -0.038464877 -2.584741e-02
## 2015-10-30 0.0006850315 0.0618084456 0.0640258005 0.063589951 8.163523e-02
## 2015-11-30 -0.0038981627 -0.0255605396 -0.0075558800 0.024414950 3.648191e-03
## 2015-12-31 -0.0019192494 -0.0389469654 -0.0235950294 -0.052157073 -1.743348e-02
## 2016-01-29 0.0123305352 -0.0516369002 -0.0567579081 -0.060306614 -5.106857e-02
## 2016-02-29 0.0088312287 -0.0082112298 -0.0339139044 0.020604867 -8.263022e-04
## 2016-03-31 0.0087089570 0.1218787676 0.0637458653 0.089910428 6.510015e-02
## 2016-04-29 0.0025461144 0.0040794824 0.0219750127 0.021044313 3.933394e-03
## 2016-05-31 0.0001353142 -0.0376285717 -0.0008561299 0.004397191 1.686847e-02
## 2016-06-30 0.0191671850 0.0445822665 -0.0244915383 0.008292177 3.469885e-03
## 2016-07-29 0.0054298641 0.0524422814 0.0390003656 0.049348594 3.582207e-02
## 2016-08-31 -0.0021565982 0.0087984128 0.0053268624 0.011260968 1.196817e-03
## 2016-09-30 0.0005156369 0.0248728142 0.0132791575 0.008614667 5.805518e-05
## 2016-10-31 -0.0082046403 -0.0083122527 -0.0224039142 -0.038134931 -1.748930e-02
## 2016-11-30 -0.0259904785 -0.0451616492 -0.0179745696 0.125246501 3.617638e-02
## 2016-12-30 0.0025383316 -0.0025301382 0.0267031143 0.031491677 2.006878e-02
## 2017-01-31 0.0021264212 0.0644317036 0.0323819564 -0.012143971 1.773662e-02
## 2017-02-28 0.0064374175 0.0172576588 0.0118363914 0.013429056 3.853934e-02
## 2017-03-31 -0.0005523585 0.0361891001 0.0318056345 -0.006533142 1.249160e-03
## 2017-04-28 0.0090294231 0.0168663974 0.0239522160 0.005107527 9.877162e-03
## 2017-05-31 0.0068476883 0.0280597656 0.0348101875 -0.022862504 1.401430e-02
## 2017-06-30 -0.0001826963 0.0092238738 0.0029560702 0.029151813 6.354618e-03
## 2017-07-31 0.0033342148 0.0565946436 0.0261877962 0.007481920 2.034565e-02
## 2017-08-31 0.0093685754 0.0232436697 -0.0004482935 -0.027565162 2.913726e-03
## 2017-09-29 -0.0057320040 -0.0004462957 0.0233427751 0.082322193 1.994901e-02
## 2017-10-31 0.0009785104 0.0322784749 0.0166535289 0.005915628 2.329061e-02
## 2017-11-30 -0.0014842267 -0.0038968556 0.0068702395 0.036913401 3.010800e-02
## 2017-12-29 0.0047402019 0.0369252995 0.0133982693 -0.003731467 1.205487e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398534e-05 0.0001042102 4.178129e-05 -7.812227e-05 -9.032986e-06
## EEM 1.042102e-04 0.0017547113 1.039019e-03 6.437767e-04 6.795436e-04
## EFA 4.178129e-05 0.0010390190 1.064239e-03 6.490319e-04 6.975403e-04
## IJS -7.812227e-05 0.0006437767 6.490319e-04 1.565454e-03 8.290272e-04
## SPY -9.032986e-06 0.0006795436 6.975403e-04 8.290272e-04 7.408298e-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.02347492
# 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.0003874069 0.009257153 0.005815638 0.005684479 0.002330249
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062308196 -0.0029353531 0.0366061226 0.052133425 4.992298e-02
## 2013-02-28 0.0058916374 -0.0231054593 -0.0129693883 0.016175243 1.267811e-02
## 2013-03-28 0.0009854413 -0.0102346701 0.0129693883 0.040258299 3.726833e-02
## 2013-04-30 0.0096386901 0.0120847897 0.0489678996 0.001222264 1.902971e-02
## 2013-05-31 -0.0202138173 -0.0494834901 -0.0306554188 0.041976579 2.333584e-02
## 2013-06-28 -0.0157788145 -0.0547283850 -0.0271447422 -0.001403237 -1.343457e-02
## 2013-07-31 0.0026879081 0.0131598224 0.0518604391 0.063541816 5.038565e-02
## 2013-08-30 -0.0082980988 -0.0257055452 -0.0197464655 -0.034743549 -3.045126e-02
## 2013-09-30 0.0111435129 0.0695886503 0.0753385113 0.063873285 3.115600e-02
## 2013-10-31 0.0082925042 0.0408611832 0.0320818802 0.034234372 4.526679e-02
## 2013-11-29 -0.0025099417 -0.0025940278 0.0054494632 0.041660843 2.920682e-02
## 2013-12-31 -0.0055831864 -0.0040741329 0.0215281353 0.012892452 2.559597e-02
## 2014-01-31 0.0152918052 -0.0903228512 -0.0534133066 -0.035775487 -3.588413e-02
## 2014-02-28 0.0037564575 0.0332208436 0.0595051669 0.045257575 4.451019e-02
## 2014-03-31 -0.0014813665 0.0380216922 -0.0046026436 0.013315036 8.261316e-03
## 2014-04-30 0.0081831846 0.0077727408 0.0165293705 -0.023184038 6.927372e-03
## 2014-05-30 0.0117222743 0.0290908475 0.0158284999 0.006205383 2.294128e-02
## 2014-06-30 -0.0005760563 0.0237341658 0.0091654820 0.037718328 2.043441e-02
## 2014-07-31 -0.0025122021 0.0135556798 -0.0263798541 -0.052009132 -1.352845e-02
## 2014-08-29 0.0114309500 0.0279046777 0.0018003464 0.043657965 3.870465e-02
## 2014-09-30 -0.0061676014 -0.0808572351 -0.0395984935 -0.061260697 -1.389247e-02
## 2014-10-31 0.0105846474 0.0140969069 -0.0026546525 0.068874901 2.327779e-02
## 2014-11-28 0.0065484327 -0.0155414089 0.0006252304 0.004773640 2.710167e-02
## 2014-12-31 0.0014752737 -0.0404419882 -0.0407468101 0.025296056 -2.539661e-03
## 2015-01-30 0.0203153584 -0.0068959149 0.0062265051 -0.054628276 -3.007734e-02
## 2015-02-27 -0.0089888039 0.0431358491 0.0614506945 0.056914793 5.468216e-02
## 2015-03-31 0.0037405113 -0.0150859058 -0.0143887833 0.010156239 -1.583062e-02
## 2015-04-30 -0.0032331180 0.0662811892 0.0358167306 -0.018417442 9.786040e-03
## 2015-05-29 -0.0043835609 -0.0419107925 0.0019524967 0.007509873 1.277414e-02
## 2015-06-30 -0.0108249160 -0.0297469590 -0.0316788252 0.004171296 -2.052119e-02
## 2015-07-31 0.0085846576 -0.0651778287 0.0201143296 -0.027375517 2.233789e-02
## 2015-08-31 -0.0033640847 -0.0925124607 -0.0771523334 -0.047268184 -6.288651e-02
## 2015-09-30 0.0080816430 -0.0318251334 -0.0451947241 -0.038464877 -2.584741e-02
## 2015-10-30 0.0006850315 0.0618084456 0.0640258005 0.063589951 8.163523e-02
## 2015-11-30 -0.0038981627 -0.0255605396 -0.0075558800 0.024414950 3.648191e-03
## 2015-12-31 -0.0019192494 -0.0389469654 -0.0235950294 -0.052157073 -1.743348e-02
## 2016-01-29 0.0123305352 -0.0516369002 -0.0567579081 -0.060306614 -5.106857e-02
## 2016-02-29 0.0088312287 -0.0082112298 -0.0339139044 0.020604867 -8.263022e-04
## 2016-03-31 0.0087089570 0.1218787676 0.0637458653 0.089910428 6.510015e-02
## 2016-04-29 0.0025461144 0.0040794824 0.0219750127 0.021044313 3.933394e-03
## 2016-05-31 0.0001353142 -0.0376285717 -0.0008561299 0.004397191 1.686847e-02
## 2016-06-30 0.0191671850 0.0445822665 -0.0244915383 0.008292177 3.469885e-03
## 2016-07-29 0.0054298641 0.0524422814 0.0390003656 0.049348594 3.582207e-02
## 2016-08-31 -0.0021565982 0.0087984128 0.0053268624 0.011260968 1.196817e-03
## 2016-09-30 0.0005156369 0.0248728142 0.0132791575 0.008614667 5.805518e-05
## 2016-10-31 -0.0082046403 -0.0083122527 -0.0224039142 -0.038134931 -1.748930e-02
## 2016-11-30 -0.0259904785 -0.0451616492 -0.0179745696 0.125246501 3.617638e-02
## 2016-12-30 0.0025383316 -0.0025301382 0.0267031143 0.031491677 2.006878e-02
## 2017-01-31 0.0021264212 0.0644317036 0.0323819564 -0.012143971 1.773662e-02
## 2017-02-28 0.0064374175 0.0172576588 0.0118363914 0.013429056 3.853934e-02
## 2017-03-31 -0.0005523585 0.0361891001 0.0318056345 -0.006533142 1.249160e-03
## 2017-04-28 0.0090294231 0.0168663974 0.0239522160 0.005107527 9.877162e-03
## 2017-05-31 0.0068476883 0.0280597656 0.0348101875 -0.022862504 1.401430e-02
## 2017-06-30 -0.0001826963 0.0092238738 0.0029560702 0.029151813 6.354618e-03
## 2017-07-31 0.0033342148 0.0565946436 0.0261877962 0.007481920 2.034565e-02
## 2017-08-31 0.0093685754 0.0232436697 -0.0004482935 -0.027565162 2.913726e-03
## 2017-09-29 -0.0057320040 -0.0004462957 0.0233427751 0.082322193 1.994901e-02
## 2017-10-31 0.0009785104 0.0322784749 0.0166535289 0.005915628 2.329061e-02
## 2017-11-30 -0.0014842267 -0.0038968556 0.0068702395 0.036913401 3.010800e-02
## 2017-12-29 0.0047402019 0.0369252995 0.0133982693 -0.003731467 1.205487e-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)) +
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)