# 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.0062310740 -0.0029357127 0.0366063164 0.052133267 4.992300e-02
## 2013-02-28 0.0058912502 -0.0231052344 -0.0129691910 0.016175114 1.267775e-02
## 2013-03-28 0.0009847449 -0.0102346459 0.0129691910 0.040258434 3.726836e-02
## 2013-04-30 0.0096394317 0.0120846578 0.0489676260 0.001222418 1.903024e-02
## 2013-05-31 -0.0202138089 -0.0494833348 -0.0306556875 0.041976099 2.333514e-02
## 2013-06-28 -0.0157781765 -0.0547287809 -0.0271439691 -0.001402802 -1.343469e-02
## 2013-07-31 0.0026882468 0.0131604990 0.0518602020 0.063541402 5.038618e-02
## 2013-08-30 -0.0082990897 -0.0257062137 -0.0197464212 -0.034743742 -3.045141e-02
## 2013-09-30 0.0111438655 0.0695888981 0.0753384346 0.063873949 3.115616e-02
## 2013-10-31 0.0082921852 0.0408611197 0.0320816599 0.034234087 4.526656e-02
## 2013-11-29 -0.0025097781 -0.0025938424 0.0054497637 0.041661213 2.920688e-02
## 2013-12-31 -0.0055833097 -0.0040743561 0.0215277677 0.012892026 2.559598e-02
## 2014-01-31 0.0152919013 -0.0903227184 -0.0534130285 -0.035775432 -3.588453e-02
## 2014-02-28 0.0037575214 0.0332207328 0.0595049235 0.045257453 4.451028e-02
## 2014-03-31 -0.0014820859 0.0380216317 -0.0046026532 0.013315471 8.261661e-03
## 2014-04-30 0.0081833182 0.0077726130 0.0165296605 -0.023184431 6.927277e-03
## 2014-05-30 0.0117213891 0.0290910768 0.0158282105 0.006205218 2.294151e-02
## 2014-06-30 -0.0005758057 0.0237340576 0.0091656468 0.037718654 2.043425e-02
## 2014-07-31 -0.0025120439 0.0135555761 -0.0263797306 -0.052009444 -1.352880e-02
## 2014-08-29 0.0114312510 0.0279044995 0.0018001624 0.043658113 3.870547e-02
## 2014-09-30 -0.0061683760 -0.0808566713 -0.0395982980 -0.061260518 -1.389275e-02
## 2014-10-31 0.0105850830 0.0140966886 -0.0026548515 0.068874813 2.327795e-02
## 2014-11-28 0.0065491449 -0.0155414412 0.0006254112 0.004773526 2.710098e-02
## 2014-12-31 0.0014748462 -0.0404420568 -0.0407467225 0.025296073 -2.539728e-03
## 2015-01-30 0.0203148716 -0.0068955729 0.0062262805 -0.054628026 -3.007696e-02
## 2015-02-27 -0.0089879820 0.0431359574 0.0614506677 0.056914605 5.468176e-02
## 2015-03-31 0.0037404688 -0.0150864206 -0.0143887929 0.010156426 -1.583018e-02
## 2015-04-30 -0.0032332924 0.0662813596 0.0358163453 -0.018417830 9.785650e-03
## 2015-05-29 -0.0043830020 -0.0419106929 0.0019528674 0.007509980 1.277483e-02
## 2015-06-30 -0.0108260488 -0.0297466985 -0.0316789227 0.004171216 -2.052168e-02
## 2015-07-31 0.0085846300 -0.0651783269 0.0201147332 -0.027375421 2.233791e-02
## 2015-08-31 -0.0033637009 -0.0925122625 -0.0771526622 -0.047268362 -6.288648e-02
## 2015-09-30 0.0080816991 -0.0318249564 -0.0451949385 -0.038464334 -2.584740e-02
## 2015-10-30 0.0006848861 0.0618082062 0.0640260006 0.063589567 8.163529e-02
## 2015-11-30 -0.0038974463 -0.0255603348 -0.0075557282 0.024414906 3.648275e-03
## 2015-12-31 -0.0019195747 -0.0389470612 -0.0235950500 -0.052156700 -1.743370e-02
## 2016-01-29 0.0123296515 -0.0516369228 -0.0567579689 -0.060306786 -5.106845e-02
## 2016-02-29 0.0088320903 -0.0082113836 -0.0339139283 0.020605158 -8.265477e-04
## 2016-03-31 0.0087085118 0.1218788136 0.0637459390 0.089910239 6.510045e-02
## 2016-04-29 0.0025463969 0.0040794464 0.0219749278 0.021044202 3.933451e-03
## 2016-05-31 0.0001355942 -0.0376283878 -0.0008560851 0.004397090 1.686826e-02
## 2016-06-30 0.0191670642 0.0445823473 -0.0244915273 0.008292694 3.469827e-03
## 2016-07-29 0.0054292049 0.0524419629 0.0390002419 0.049347969 3.582223e-02
## 2016-08-31 -0.0021560140 0.0087988536 0.0053268450 0.011261092 1.196764e-03
## 2016-09-30 0.0005157377 0.0248725375 0.0132791635 0.008614844 5.794653e-05
## 2016-10-31 -0.0082052708 -0.0083120768 -0.0224037517 -0.038134794 -1.748905e-02
## 2016-11-30 -0.0259894334 -0.0451619493 -0.0179744890 0.125246274 3.617607e-02
## 2016-12-30 0.0025384689 -0.0025299176 0.0267030747 0.031491871 2.006916e-02
## 2017-01-31 0.0021257815 0.0644317259 0.0323818267 -0.012144125 1.773648e-02
## 2017-02-28 0.0064379000 0.0172578311 0.0118363502 0.013428799 3.853900e-02
## 2017-03-31 -0.0005530252 0.0361889645 0.0318057522 -0.006532991 1.249223e-03
## 2017-04-28 0.0090293528 0.0168661738 0.0239523293 0.005107864 9.877393e-03
## 2017-05-31 0.0068475053 0.0280598995 0.0348099190 -0.022862899 1.401425e-02
## 2017-06-30 -0.0001831494 0.0092235499 0.0029561485 0.029152154 6.354802e-03
## 2017-07-31 0.0033351378 0.0565947503 0.0261878079 0.007481476 2.034577e-02
## 2017-08-31 0.0093684997 0.0232437140 -0.0004483592 -0.027564705 2.913312e-03
## 2017-09-29 -0.0057318448 -0.0004461573 0.0233427441 0.082321410 1.994920e-02
## 2017-10-31 0.0009778448 0.0322784976 0.0166537929 0.005916172 2.329086e-02
## 2017-11-30 -0.0014840257 -0.0038970184 0.0068700310 0.036913435 3.010777e-02
## 2017-12-29 0.0047403290 0.0369254195 0.0133982755 -0.003731168 1.205499e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398453e-05 0.0001042096 4.178342e-05 -0.0000781144 -9.028641e-06
## EEM 1.042096e-04 0.0017547110 1.039017e-03 0.0006437746 6.795462e-04
## EFA 4.178342e-05 0.0010390165 1.064237e-03 0.0006490292 6.975429e-04
## IJS -7.811440e-05 0.0006437746 6.490292e-04 0.0015654485 8.290267e-04
## SPY -9.028641e-06 0.0006795462 6.975429e-04 0.0008290267 7.408316e-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.02347493
# 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.000387429 0.009257141 0.005815629 0.005684475 0.002330258
rowSums(component_contribution)
## [1] 0.02347493
# 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.0062310740 -0.0029357127 0.0366063164 0.052133267 4.992300e-02
## 2013-02-28 0.0058912502 -0.0231052344 -0.0129691910 0.016175114 1.267775e-02
## 2013-03-28 0.0009847449 -0.0102346459 0.0129691910 0.040258434 3.726836e-02
## 2013-04-30 0.0096394317 0.0120846578 0.0489676260 0.001222418 1.903024e-02
## 2013-05-31 -0.0202138089 -0.0494833348 -0.0306556875 0.041976099 2.333514e-02
## 2013-06-28 -0.0157781765 -0.0547287809 -0.0271439691 -0.001402802 -1.343469e-02
## 2013-07-31 0.0026882468 0.0131604990 0.0518602020 0.063541402 5.038618e-02
## 2013-08-30 -0.0082990897 -0.0257062137 -0.0197464212 -0.034743742 -3.045141e-02
## 2013-09-30 0.0111438655 0.0695888981 0.0753384346 0.063873949 3.115616e-02
## 2013-10-31 0.0082921852 0.0408611197 0.0320816599 0.034234087 4.526656e-02
## 2013-11-29 -0.0025097781 -0.0025938424 0.0054497637 0.041661213 2.920688e-02
## 2013-12-31 -0.0055833097 -0.0040743561 0.0215277677 0.012892026 2.559598e-02
## 2014-01-31 0.0152919013 -0.0903227184 -0.0534130285 -0.035775432 -3.588453e-02
## 2014-02-28 0.0037575214 0.0332207328 0.0595049235 0.045257453 4.451028e-02
## 2014-03-31 -0.0014820859 0.0380216317 -0.0046026532 0.013315471 8.261661e-03
## 2014-04-30 0.0081833182 0.0077726130 0.0165296605 -0.023184431 6.927277e-03
## 2014-05-30 0.0117213891 0.0290910768 0.0158282105 0.006205218 2.294151e-02
## 2014-06-30 -0.0005758057 0.0237340576 0.0091656468 0.037718654 2.043425e-02
## 2014-07-31 -0.0025120439 0.0135555761 -0.0263797306 -0.052009444 -1.352880e-02
## 2014-08-29 0.0114312510 0.0279044995 0.0018001624 0.043658113 3.870547e-02
## 2014-09-30 -0.0061683760 -0.0808566713 -0.0395982980 -0.061260518 -1.389275e-02
## 2014-10-31 0.0105850830 0.0140966886 -0.0026548515 0.068874813 2.327795e-02
## 2014-11-28 0.0065491449 -0.0155414412 0.0006254112 0.004773526 2.710098e-02
## 2014-12-31 0.0014748462 -0.0404420568 -0.0407467225 0.025296073 -2.539728e-03
## 2015-01-30 0.0203148716 -0.0068955729 0.0062262805 -0.054628026 -3.007696e-02
## 2015-02-27 -0.0089879820 0.0431359574 0.0614506677 0.056914605 5.468176e-02
## 2015-03-31 0.0037404688 -0.0150864206 -0.0143887929 0.010156426 -1.583018e-02
## 2015-04-30 -0.0032332924 0.0662813596 0.0358163453 -0.018417830 9.785650e-03
## 2015-05-29 -0.0043830020 -0.0419106929 0.0019528674 0.007509980 1.277483e-02
## 2015-06-30 -0.0108260488 -0.0297466985 -0.0316789227 0.004171216 -2.052168e-02
## 2015-07-31 0.0085846300 -0.0651783269 0.0201147332 -0.027375421 2.233791e-02
## 2015-08-31 -0.0033637009 -0.0925122625 -0.0771526622 -0.047268362 -6.288648e-02
## 2015-09-30 0.0080816991 -0.0318249564 -0.0451949385 -0.038464334 -2.584740e-02
## 2015-10-30 0.0006848861 0.0618082062 0.0640260006 0.063589567 8.163529e-02
## 2015-11-30 -0.0038974463 -0.0255603348 -0.0075557282 0.024414906 3.648275e-03
## 2015-12-31 -0.0019195747 -0.0389470612 -0.0235950500 -0.052156700 -1.743370e-02
## 2016-01-29 0.0123296515 -0.0516369228 -0.0567579689 -0.060306786 -5.106845e-02
## 2016-02-29 0.0088320903 -0.0082113836 -0.0339139283 0.020605158 -8.265477e-04
## 2016-03-31 0.0087085118 0.1218788136 0.0637459390 0.089910239 6.510045e-02
## 2016-04-29 0.0025463969 0.0040794464 0.0219749278 0.021044202 3.933451e-03
## 2016-05-31 0.0001355942 -0.0376283878 -0.0008560851 0.004397090 1.686826e-02
## 2016-06-30 0.0191670642 0.0445823473 -0.0244915273 0.008292694 3.469827e-03
## 2016-07-29 0.0054292049 0.0524419629 0.0390002419 0.049347969 3.582223e-02
## 2016-08-31 -0.0021560140 0.0087988536 0.0053268450 0.011261092 1.196764e-03
## 2016-09-30 0.0005157377 0.0248725375 0.0132791635 0.008614844 5.794653e-05
## 2016-10-31 -0.0082052708 -0.0083120768 -0.0224037517 -0.038134794 -1.748905e-02
## 2016-11-30 -0.0259894334 -0.0451619493 -0.0179744890 0.125246274 3.617607e-02
## 2016-12-30 0.0025384689 -0.0025299176 0.0267030747 0.031491871 2.006916e-02
## 2017-01-31 0.0021257815 0.0644317259 0.0323818267 -0.012144125 1.773648e-02
## 2017-02-28 0.0064379000 0.0172578311 0.0118363502 0.013428799 3.853900e-02
## 2017-03-31 -0.0005530252 0.0361889645 0.0318057522 -0.006532991 1.249223e-03
## 2017-04-28 0.0090293528 0.0168661738 0.0239523293 0.005107864 9.877393e-03
## 2017-05-31 0.0068475053 0.0280598995 0.0348099190 -0.022862899 1.401425e-02
## 2017-06-30 -0.0001831494 0.0092235499 0.0029561485 0.029152154 6.354802e-03
## 2017-07-31 0.0033351378 0.0565947503 0.0261878079 0.007481476 2.034577e-02
## 2017-08-31 0.0093684997 0.0232437140 -0.0004483592 -0.027564705 2.913312e-03
## 2017-09-29 -0.0057318448 -0.0004461573 0.0233427441 0.082321410 1.994920e-02
## 2017-10-31 0.0009778448 0.0322784976 0.0166537929 0.005916172 2.329086e-02
## 2017-11-30 -0.0014840257 -0.0038970184 0.0068700310 0.036913435 3.010777e-02
## 2017-12-29 0.0047403290 0.0369254195 0.0133982755 -0.003731168 1.205499e-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(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(.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")
Column Chart of Component Contribution and Weight
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)
## 6 Rolling Component Contribution