# 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.0062310402 -0.0029354222 0.0366061778 0.052132873 4.992294e-02
## 2013-02-28 0.0058912644 -0.0231051588 -0.0129696171 0.016175514 1.267836e-02
## 2013-03-28 0.0009842544 -0.0102350938 0.0129696171 0.040258111 3.726798e-02
## 2013-04-30 0.0096395066 0.0120846787 0.0489676327 0.001222528 1.902982e-02
## 2013-05-31 -0.0202136647 -0.0494835595 -0.0306553634 0.041976522 2.333562e-02
## 2013-06-28 -0.0157784359 -0.0547280873 -0.0271446366 -0.001403170 -1.343443e-02
## 2013-07-31 0.0026879824 0.0131596069 0.0518604186 0.063541429 5.038586e-02
## 2013-08-30 -0.0082983921 -0.0257056066 -0.0197464563 -0.034743347 -3.045063e-02
## 2013-09-30 0.0111438991 0.0695888366 0.0753385931 0.063873394 3.115526e-02
## 2013-10-31 0.0082919522 0.0408613880 0.0320815842 0.034234146 4.526679e-02
## 2013-11-29 -0.0025100731 -0.0025940678 0.0054499549 0.041661073 2.920673e-02
## 2013-12-31 -0.0055828944 -0.0040747038 0.0215280191 0.012891896 2.559635e-02
## 2014-01-31 0.0152916604 -0.0903225258 -0.0534134158 -0.035774753 -3.588469e-02
## 2014-02-28 0.0037569381 0.0332206353 0.0595051336 0.045257198 4.451039e-02
## 2014-03-31 -0.0014815909 0.0380215405 -0.0046029429 0.013315465 8.261373e-03
## 2014-04-30 0.0081831366 0.0077729708 0.0165296891 -0.023184327 6.927501e-03
## 2014-05-30 0.0117218869 0.0290911140 0.0158283807 0.006205079 2.294118e-02
## 2014-06-30 -0.0005762012 0.0237340214 0.0091654436 0.037718855 2.043470e-02
## 2014-07-31 -0.0025119016 0.0135556517 -0.0263797299 -0.052009416 -1.352876e-02
## 2014-08-29 0.0114306578 0.0279046148 0.0018002803 0.043657874 3.870476e-02
## 2014-09-30 -0.0061671817 -0.0808567956 -0.0395983253 -0.061260462 -1.389257e-02
## 2014-10-31 0.0105845067 0.0140964225 -0.0026549644 0.068874854 2.327799e-02
## 2014-11-28 0.0065486625 -0.0155412572 0.0006254644 0.004773712 2.710153e-02
## 2014-12-31 0.0014752737 -0.0404420590 -0.0407468459 0.025295629 -2.539732e-03
## 2015-01-30 0.0203151997 -0.0068959393 0.0062264467 -0.054627944 -3.007702e-02
## 2015-02-27 -0.0089883458 0.0431361569 0.0614506478 0.056914813 5.468175e-02
## 2015-03-31 0.0037397996 -0.0150864832 -0.0143887489 0.010156253 -1.583027e-02
## 2015-04-30 -0.0032323033 0.0662817424 0.0358165171 -0.018417602 9.785723e-03
## 2015-05-29 -0.0043839149 -0.0419113664 0.0019526938 0.007509937 1.277415e-02
## 2015-06-30 -0.0108253406 -0.0297463847 -0.0316788464 0.004171265 -2.052090e-02
## 2015-07-31 0.0085848772 -0.0651780932 0.0201144071 -0.027375327 2.233777e-02
## 2015-08-31 -0.0033644186 -0.0925121365 -0.0771522768 -0.047268134 -6.288667e-02
## 2015-09-30 0.0080819268 -0.0318251522 -0.0451949032 -0.038465169 -2.584717e-02
## 2015-10-30 0.0006854569 0.0618082427 0.0640259263 0.063589915 8.163494e-02
## 2015-11-30 -0.0038982453 -0.0255602149 -0.0075559238 0.024415031 3.648466e-03
## 2015-12-31 -0.0019188207 -0.0389473069 -0.0235950377 -0.052156832 -1.743346e-02
## 2016-01-29 0.0123297053 -0.0516365681 -0.0567578510 -0.060306912 -5.106870e-02
## 2016-02-29 0.0088314803 -0.0082116547 -0.0339138330 0.020605264 -8.263032e-04
## 2016-03-31 0.0087089098 0.1218790034 0.0637456380 0.089910471 6.510009e-02
## 2016-04-29 0.0025464920 0.0040792011 0.0219750423 0.021044249 3.933454e-03
## 2016-05-31 0.0001353587 -0.0376285505 -0.0008560654 0.004397040 1.686854e-02
## 2016-06-30 0.0191667568 0.0445825140 -0.0244913638 0.008292086 3.469756e-03
## 2016-07-29 0.0054297515 0.0524420803 0.0390003109 0.049348307 3.582211e-02
## 2016-08-31 -0.0021566191 0.0087985274 0.0053268052 0.011261391 1.196687e-03
## 2016-09-30 0.0005160837 0.0248727921 0.0132790000 0.008614486 5.811445e-05
## 2016-10-31 -0.0082051531 -0.0083122369 -0.0224036234 -0.038134633 -1.748917e-02
## 2016-11-30 -0.0259896546 -0.0451616817 -0.0179745854 0.125246378 3.617629e-02
## 2016-12-30 0.0025379616 -0.0025300065 0.0267030347 0.031491546 2.006897e-02
## 2017-01-31 0.0021260288 0.0644313386 0.0323817590 -0.012143639 1.773667e-02
## 2017-02-28 0.0064380429 0.0172579347 0.0118365242 0.013428704 3.853908e-02
## 2017-03-31 -0.0005529032 0.0361890439 0.0318056037 -0.006533235 1.249170e-03
## 2017-04-28 0.0090293436 0.0168664912 0.0239523044 0.005108111 9.877225e-03
## 2017-05-31 0.0068474589 0.0280598559 0.0348102134 -0.022862952 1.401413e-02
## 2017-06-30 -0.0001828649 0.0092237832 0.0029558483 0.029151768 6.354652e-03
## 2017-07-31 0.0033346479 0.0565944491 0.0261877340 0.007481570 2.034585e-02
## 2017-08-31 0.0093690731 0.0232437207 -0.0004482462 -0.027564722 2.913531e-03
## 2017-09-29 -0.0057323880 -0.0004460229 0.0233429973 0.082321653 1.994895e-02
## 2017-10-31 0.0009782048 0.0322784387 0.0166535066 0.005916315 2.329092e-02
## 2017-11-30 -0.0014843247 -0.0038970809 0.0068700936 0.036913285 3.010800e-02
## 2017-12-29 0.0047405001 0.0369253525 0.0133984000 -0.003731246 1.205483e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398408e-05 0.0001042118 4.178389e-05 -7.812084e-05 -9.030875e-06
## EEM 1.042118e-04 0.0017547100 1.039016e-03 6.437734e-04 6.795414e-04
## EFA 4.178389e-05 0.0010390159 1.064237e-03 6.490292e-04 6.975392e-04
## IJS -7.812084e-05 0.0006437734 6.490292e-04 1.565448e-03 8.290242e-04
## SPY -9.030875e-06 0.0006795414 6.975392e-04 8.290242e-04 7.408276e-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.0003874189 0.009257145 0.005815633 0.005684461 0.002330246
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.0062310402 -0.0029354222 0.0366061778 0.052132873 4.992294e-02
## 2013-02-28 0.0058912644 -0.0231051588 -0.0129696171 0.016175514 1.267836e-02
## 2013-03-28 0.0009842544 -0.0102350938 0.0129696171 0.040258111 3.726798e-02
## 2013-04-30 0.0096395066 0.0120846787 0.0489676327 0.001222528 1.902982e-02
## 2013-05-31 -0.0202136647 -0.0494835595 -0.0306553634 0.041976522 2.333562e-02
## 2013-06-28 -0.0157784359 -0.0547280873 -0.0271446366 -0.001403170 -1.343443e-02
## 2013-07-31 0.0026879824 0.0131596069 0.0518604186 0.063541429 5.038586e-02
## 2013-08-30 -0.0082983921 -0.0257056066 -0.0197464563 -0.034743347 -3.045063e-02
## 2013-09-30 0.0111438991 0.0695888366 0.0753385931 0.063873394 3.115526e-02
## 2013-10-31 0.0082919522 0.0408613880 0.0320815842 0.034234146 4.526679e-02
## 2013-11-29 -0.0025100731 -0.0025940678 0.0054499549 0.041661073 2.920673e-02
## 2013-12-31 -0.0055828944 -0.0040747038 0.0215280191 0.012891896 2.559635e-02
## 2014-01-31 0.0152916604 -0.0903225258 -0.0534134158 -0.035774753 -3.588469e-02
## 2014-02-28 0.0037569381 0.0332206353 0.0595051336 0.045257198 4.451039e-02
## 2014-03-31 -0.0014815909 0.0380215405 -0.0046029429 0.013315465 8.261373e-03
## 2014-04-30 0.0081831366 0.0077729708 0.0165296891 -0.023184327 6.927501e-03
## 2014-05-30 0.0117218869 0.0290911140 0.0158283807 0.006205079 2.294118e-02
## 2014-06-30 -0.0005762012 0.0237340214 0.0091654436 0.037718855 2.043470e-02
## 2014-07-31 -0.0025119016 0.0135556517 -0.0263797299 -0.052009416 -1.352876e-02
## 2014-08-29 0.0114306578 0.0279046148 0.0018002803 0.043657874 3.870476e-02
## 2014-09-30 -0.0061671817 -0.0808567956 -0.0395983253 -0.061260462 -1.389257e-02
## 2014-10-31 0.0105845067 0.0140964225 -0.0026549644 0.068874854 2.327799e-02
## 2014-11-28 0.0065486625 -0.0155412572 0.0006254644 0.004773712 2.710153e-02
## 2014-12-31 0.0014752737 -0.0404420590 -0.0407468459 0.025295629 -2.539732e-03
## 2015-01-30 0.0203151997 -0.0068959393 0.0062264467 -0.054627944 -3.007702e-02
## 2015-02-27 -0.0089883458 0.0431361569 0.0614506478 0.056914813 5.468175e-02
## 2015-03-31 0.0037397996 -0.0150864832 -0.0143887489 0.010156253 -1.583027e-02
## 2015-04-30 -0.0032323033 0.0662817424 0.0358165171 -0.018417602 9.785723e-03
## 2015-05-29 -0.0043839149 -0.0419113664 0.0019526938 0.007509937 1.277415e-02
## 2015-06-30 -0.0108253406 -0.0297463847 -0.0316788464 0.004171265 -2.052090e-02
## 2015-07-31 0.0085848772 -0.0651780932 0.0201144071 -0.027375327 2.233777e-02
## 2015-08-31 -0.0033644186 -0.0925121365 -0.0771522768 -0.047268134 -6.288667e-02
## 2015-09-30 0.0080819268 -0.0318251522 -0.0451949032 -0.038465169 -2.584717e-02
## 2015-10-30 0.0006854569 0.0618082427 0.0640259263 0.063589915 8.163494e-02
## 2015-11-30 -0.0038982453 -0.0255602149 -0.0075559238 0.024415031 3.648466e-03
## 2015-12-31 -0.0019188207 -0.0389473069 -0.0235950377 -0.052156832 -1.743346e-02
## 2016-01-29 0.0123297053 -0.0516365681 -0.0567578510 -0.060306912 -5.106870e-02
## 2016-02-29 0.0088314803 -0.0082116547 -0.0339138330 0.020605264 -8.263032e-04
## 2016-03-31 0.0087089098 0.1218790034 0.0637456380 0.089910471 6.510009e-02
## 2016-04-29 0.0025464920 0.0040792011 0.0219750423 0.021044249 3.933454e-03
## 2016-05-31 0.0001353587 -0.0376285505 -0.0008560654 0.004397040 1.686854e-02
## 2016-06-30 0.0191667568 0.0445825140 -0.0244913638 0.008292086 3.469756e-03
## 2016-07-29 0.0054297515 0.0524420803 0.0390003109 0.049348307 3.582211e-02
## 2016-08-31 -0.0021566191 0.0087985274 0.0053268052 0.011261391 1.196687e-03
## 2016-09-30 0.0005160837 0.0248727921 0.0132790000 0.008614486 5.811445e-05
## 2016-10-31 -0.0082051531 -0.0083122369 -0.0224036234 -0.038134633 -1.748917e-02
## 2016-11-30 -0.0259896546 -0.0451616817 -0.0179745854 0.125246378 3.617629e-02
## 2016-12-30 0.0025379616 -0.0025300065 0.0267030347 0.031491546 2.006897e-02
## 2017-01-31 0.0021260288 0.0644313386 0.0323817590 -0.012143639 1.773667e-02
## 2017-02-28 0.0064380429 0.0172579347 0.0118365242 0.013428704 3.853908e-02
## 2017-03-31 -0.0005529032 0.0361890439 0.0318056037 -0.006533235 1.249170e-03
## 2017-04-28 0.0090293436 0.0168664912 0.0239523044 0.005108111 9.877225e-03
## 2017-05-31 0.0068474589 0.0280598559 0.0348102134 -0.022862952 1.401413e-02
## 2017-06-30 -0.0001828649 0.0092237832 0.0029558483 0.029151768 6.354652e-03
## 2017-07-31 0.0033346479 0.0565944491 0.0261877340 0.007481570 2.034585e-02
## 2017-08-31 0.0093690731 0.0232437207 -0.0004482462 -0.027564722 2.913531e-03
## 2017-09-29 -0.0057323880 -0.0004460229 0.0233429973 0.082321653 1.994895e-02
## 2017-10-31 0.0009782048 0.0322784387 0.0166535066 0.005916315 2.329092e-02
## 2017-11-30 -0.0014843247 -0.0038970809 0.0068700936 0.036913285 3.010800e-02
## 2017-12-29 0.0047405001 0.0369253525 0.0133984000 -0.003731246 1.205483e-02
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# 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
# 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)