# 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.0062309366 -0.0029352422 0.0366064924 0.052133087 4.992255e-02
## 2013-02-28 0.0058912501 -0.0231056805 -0.0129695699 0.016175454 1.267880e-02
## 2013-03-28 0.0009845318 -0.0102347844 0.0129695699 0.040258089 3.726773e-02
## 2013-04-30 0.0096393007 0.0120846781 0.0489675453 0.001222264 1.903024e-02
## 2013-05-31 -0.0202140304 -0.0494833829 -0.0306553350 0.041976482 2.333538e-02
## 2013-06-28 -0.0157784659 -0.0547283917 -0.0271445615 -0.001402849 -1.343434e-02
## 2013-07-31 0.0026877549 0.0131598241 0.0518602606 0.063540888 5.038588e-02
## 2013-08-30 -0.0082983986 -0.0257054850 -0.0197462012 -0.034743101 -3.045114e-02
## 2013-09-30 0.0111439865 0.0695885954 0.0753385807 0.063873386 3.115544e-02
## 2013-10-31 0.0082926898 0.0408610743 0.0320815538 0.034234717 4.526691e-02
## 2013-11-29 -0.0025103626 -0.0025933446 0.0054497805 0.041660833 2.920704e-02
## 2013-12-31 -0.0055830853 -0.0040745888 0.0215279752 0.012892044 2.559627e-02
## 2014-01-31 0.0152919892 -0.0903229138 -0.0534133841 -0.035775409 -3.588453e-02
## 2014-02-28 0.0037566638 0.0332204822 0.0595051669 0.045257499 4.450990e-02
## 2014-03-31 -0.0014809777 0.0380219996 -0.0046025662 0.013315117 8.261908e-03
## 2014-04-30 0.0081826431 0.0077729732 0.0165290647 -0.023184283 6.927175e-03
## 2014-05-30 0.0117219631 0.0290908442 0.0158288032 0.006205626 2.294080e-02
## 2014-06-30 -0.0005757591 0.0237338338 0.0091654070 0.037718564 2.043507e-02
## 2014-07-31 -0.0025124754 0.0135556828 -0.0263800828 -0.052009531 -1.352855e-02
## 2014-08-29 0.0114307708 0.0279048943 0.0018007274 0.043658054 3.870482e-02
## 2014-09-30 -0.0061673850 -0.0808570067 -0.0395985666 -0.061260541 -1.389274e-02
## 2014-10-31 0.0105849796 0.0140964533 -0.0026550493 0.068874823 2.327815e-02
## 2014-11-28 0.0065485366 -0.0155411837 0.0006254686 0.004773795 2.710122e-02
## 2014-12-31 0.0014750596 -0.0404421073 -0.0407466482 0.025295678 -2.539662e-03
## 2015-01-30 0.0203151387 -0.0068957359 0.0062265045 -0.054627737 -3.007717e-02
## 2015-02-27 -0.0089877187 0.0431359040 0.0614506124 0.056914405 5.468181e-02
## 2015-03-31 0.0037400403 -0.0150862538 -0.0143887050 0.010156537 -1.583019e-02
## 2015-04-30 -0.0032332689 0.0662814223 0.0358165767 -0.018417818 9.785952e-03
## 2015-05-29 -0.0043831115 -0.0419109062 0.0019525723 0.007509950 1.277439e-02
## 2015-06-30 -0.0108257831 -0.0297468452 -0.0316788252 0.004171371 -2.052161e-02
## 2015-07-31 0.0085844648 -0.0651779538 0.0201145586 -0.027375442 2.233781e-02
## 2015-08-31 -0.0033637457 -0.0925124042 -0.0771525624 -0.047268345 -6.288643e-02
## 2015-09-30 0.0080813422 -0.0318249231 -0.0451948104 -0.038464799 -2.584732e-02
## 2015-10-30 0.0006854946 0.0618082373 0.0640259677 0.063589720 8.163514e-02
## 2015-11-30 -0.0038982207 -0.0255603364 -0.0075558793 0.024415188 3.648448e-03
## 2015-12-31 -0.0019190337 -0.0389471730 -0.0235950275 -0.052156916 -1.743374e-02
## 2016-01-29 0.0123303065 -0.0516366048 -0.0567579916 -0.060306866 -5.106857e-02
## 2016-02-29 0.0088311328 -0.0082114542 -0.0339138130 0.020605375 -8.262107e-04
## 2016-03-31 0.0087090675 0.1218789678 0.0637457739 0.089910323 6.510015e-02
## 2016-04-29 0.0025463209 0.0040789497 0.0219750127 0.021044233 3.933479e-03
## 2016-05-31 0.0001353070 -0.0376283774 -0.0008560460 0.004397115 1.686830e-02
## 2016-06-30 0.0191666959 0.0445824046 -0.0244914502 0.008291954 3.470053e-03
## 2016-07-29 0.0054300076 0.0524421561 0.0390003591 0.049348601 3.582207e-02
## 2016-08-31 -0.0021561129 0.0087984139 0.0053266146 0.011261250 1.196897e-03
## 2016-09-30 0.0005156509 0.0248729989 0.0132791585 0.008614458 5.773221e-05
## 2016-10-31 -0.0082048781 -0.0083123743 -0.0224035838 -0.038134646 -1.748897e-02
## 2016-11-30 -0.0259900561 -0.0451616521 -0.0179746496 0.125246412 3.617622e-02
## 2016-12-30 0.0025381850 -0.0025298821 0.0267029452 0.031491488 2.006878e-02
## 2017-01-31 0.0021258727 0.0644314514 0.0323819564 -0.012143972 1.773669e-02
## 2017-02-28 0.0064384661 0.0172578369 0.0118363126 0.013428750 3.853933e-02
## 2017-03-31 -0.0005532259 0.0361889820 0.0318057133 -0.006533021 1.249160e-03
## 2017-04-28 0.0090291145 0.0168663974 0.0239522905 0.005107959 9.877162e-03
## 2017-05-31 0.0068473331 0.0280599834 0.0348100411 -0.022862500 1.401430e-02
## 2017-06-30 -0.0001825045 0.0092235481 0.0029559987 0.029151746 6.354688e-03
## 2017-07-31 0.0033342335 0.0565945476 0.0261880095 0.007481616 2.034565e-02
## 2017-08-31 0.0093691148 0.0232437740 -0.0004482934 -0.027564918 2.913448e-03
## 2017-09-29 -0.0057316276 -0.0004460965 0.0233426369 0.082322015 1.994921e-02
## 2017-10-31 0.0009776145 0.0322783753 0.0166537315 0.005916315 2.329068e-02
## 2017-11-30 -0.0014839869 -0.0038968556 0.0068700385 0.036912499 3.010768e-02
## 2017-12-29 0.0047404895 0.0369252062 0.0133983361 -0.003730916 1.205532e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398458e-05 0.0001042110 4.178363e-05 -7.811772e-05 -9.030107e-06
## EEM 1.042110e-04 0.0017547101 1.039018e-03 6.437744e-04 6.795440e-04
## EFA 4.178363e-05 0.0010390179 1.064238e-03 6.490299e-04 6.975400e-04
## IJS -7.811772e-05 0.0006437744 6.490299e-04 1.565447e-03 8.290244e-04
## SPY -9.030107e-06 0.0006795440 6.975400e-04 8.290244e-04 7.408277e-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.0003874247 0.009257146 0.005815636 0.005684467 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.0062309366 -0.0029352422 0.0366064924 0.052133087 4.992255e-02
## 2013-02-28 0.0058912501 -0.0231056805 -0.0129695699 0.016175454 1.267880e-02
## 2013-03-28 0.0009845318 -0.0102347844 0.0129695699 0.040258089 3.726773e-02
## 2013-04-30 0.0096393007 0.0120846781 0.0489675453 0.001222264 1.903024e-02
## 2013-05-31 -0.0202140304 -0.0494833829 -0.0306553350 0.041976482 2.333538e-02
## 2013-06-28 -0.0157784659 -0.0547283917 -0.0271445615 -0.001402849 -1.343434e-02
## 2013-07-31 0.0026877549 0.0131598241 0.0518602606 0.063540888 5.038588e-02
## 2013-08-30 -0.0082983986 -0.0257054850 -0.0197462012 -0.034743101 -3.045114e-02
## 2013-09-30 0.0111439865 0.0695885954 0.0753385807 0.063873386 3.115544e-02
## 2013-10-31 0.0082926898 0.0408610743 0.0320815538 0.034234717 4.526691e-02
## 2013-11-29 -0.0025103626 -0.0025933446 0.0054497805 0.041660833 2.920704e-02
## 2013-12-31 -0.0055830853 -0.0040745888 0.0215279752 0.012892044 2.559627e-02
## 2014-01-31 0.0152919892 -0.0903229138 -0.0534133841 -0.035775409 -3.588453e-02
## 2014-02-28 0.0037566638 0.0332204822 0.0595051669 0.045257499 4.450990e-02
## 2014-03-31 -0.0014809777 0.0380219996 -0.0046025662 0.013315117 8.261908e-03
## 2014-04-30 0.0081826431 0.0077729732 0.0165290647 -0.023184283 6.927175e-03
## 2014-05-30 0.0117219631 0.0290908442 0.0158288032 0.006205626 2.294080e-02
## 2014-06-30 -0.0005757591 0.0237338338 0.0091654070 0.037718564 2.043507e-02
## 2014-07-31 -0.0025124754 0.0135556828 -0.0263800828 -0.052009531 -1.352855e-02
## 2014-08-29 0.0114307708 0.0279048943 0.0018007274 0.043658054 3.870482e-02
## 2014-09-30 -0.0061673850 -0.0808570067 -0.0395985666 -0.061260541 -1.389274e-02
## 2014-10-31 0.0105849796 0.0140964533 -0.0026550493 0.068874823 2.327815e-02
## 2014-11-28 0.0065485366 -0.0155411837 0.0006254686 0.004773795 2.710122e-02
## 2014-12-31 0.0014750596 -0.0404421073 -0.0407466482 0.025295678 -2.539662e-03
## 2015-01-30 0.0203151387 -0.0068957359 0.0062265045 -0.054627737 -3.007717e-02
## 2015-02-27 -0.0089877187 0.0431359040 0.0614506124 0.056914405 5.468181e-02
## 2015-03-31 0.0037400403 -0.0150862538 -0.0143887050 0.010156537 -1.583019e-02
## 2015-04-30 -0.0032332689 0.0662814223 0.0358165767 -0.018417818 9.785952e-03
## 2015-05-29 -0.0043831115 -0.0419109062 0.0019525723 0.007509950 1.277439e-02
## 2015-06-30 -0.0108257831 -0.0297468452 -0.0316788252 0.004171371 -2.052161e-02
## 2015-07-31 0.0085844648 -0.0651779538 0.0201145586 -0.027375442 2.233781e-02
## 2015-08-31 -0.0033637457 -0.0925124042 -0.0771525624 -0.047268345 -6.288643e-02
## 2015-09-30 0.0080813422 -0.0318249231 -0.0451948104 -0.038464799 -2.584732e-02
## 2015-10-30 0.0006854946 0.0618082373 0.0640259677 0.063589720 8.163514e-02
## 2015-11-30 -0.0038982207 -0.0255603364 -0.0075558793 0.024415188 3.648448e-03
## 2015-12-31 -0.0019190337 -0.0389471730 -0.0235950275 -0.052156916 -1.743374e-02
## 2016-01-29 0.0123303065 -0.0516366048 -0.0567579916 -0.060306866 -5.106857e-02
## 2016-02-29 0.0088311328 -0.0082114542 -0.0339138130 0.020605375 -8.262107e-04
## 2016-03-31 0.0087090675 0.1218789678 0.0637457739 0.089910323 6.510015e-02
## 2016-04-29 0.0025463209 0.0040789497 0.0219750127 0.021044233 3.933479e-03
## 2016-05-31 0.0001353070 -0.0376283774 -0.0008560460 0.004397115 1.686830e-02
## 2016-06-30 0.0191666959 0.0445824046 -0.0244914502 0.008291954 3.470053e-03
## 2016-07-29 0.0054300076 0.0524421561 0.0390003591 0.049348601 3.582207e-02
## 2016-08-31 -0.0021561129 0.0087984139 0.0053266146 0.011261250 1.196897e-03
## 2016-09-30 0.0005156509 0.0248729989 0.0132791585 0.008614458 5.773221e-05
## 2016-10-31 -0.0082048781 -0.0083123743 -0.0224035838 -0.038134646 -1.748897e-02
## 2016-11-30 -0.0259900561 -0.0451616521 -0.0179746496 0.125246412 3.617622e-02
## 2016-12-30 0.0025381850 -0.0025298821 0.0267029452 0.031491488 2.006878e-02
## 2017-01-31 0.0021258727 0.0644314514 0.0323819564 -0.012143972 1.773669e-02
## 2017-02-28 0.0064384661 0.0172578369 0.0118363126 0.013428750 3.853933e-02
## 2017-03-31 -0.0005532259 0.0361889820 0.0318057133 -0.006533021 1.249160e-03
## 2017-04-28 0.0090291145 0.0168663974 0.0239522905 0.005107959 9.877162e-03
## 2017-05-31 0.0068473331 0.0280599834 0.0348100411 -0.022862500 1.401430e-02
## 2017-06-30 -0.0001825045 0.0092235481 0.0029559987 0.029151746 6.354688e-03
## 2017-07-31 0.0033342335 0.0565945476 0.0261880095 0.007481616 2.034565e-02
## 2017-08-31 0.0093691148 0.0232437740 -0.0004482934 -0.027564918 2.913448e-03
## 2017-09-29 -0.0057316276 -0.0004460965 0.0233426369 0.082322015 1.994921e-02
## 2017-10-31 0.0009776145 0.0322783753 0.0166537315 0.005916315 2.329068e-02
## 2017-11-30 -0.0014839869 -0.0038968556 0.0068700385 0.036912499 3.010768e-02
## 2017-12-29 0.0047404895 0.0369252062 0.0133983361 -0.003730916 1.205532e-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 may vary with those of other assets withing 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, .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)