# 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.0062311778 -0.0029352428 0.0366062167 0.052132964 4.992276e-02
## 2013-02-28 0.0058914092 -0.0231051198 -0.0129691125 0.016175087 1.267839e-02
## 2013-03-28 0.0009845721 -0.0102351239 0.0129691125 0.040258234 3.726792e-02
## 2013-04-30 0.0096392475 0.0120849040 0.0489678996 0.001222682 1.903058e-02
## 2013-05-31 -0.0202141852 -0.0494832528 -0.0306553297 0.041975943 2.333469e-02
## 2013-06-28 -0.0157775228 -0.0547286224 -0.0271446482 -0.001402810 -1.343447e-02
## 2013-07-31 0.0026869076 0.0131598843 0.0518603429 0.063541125 5.038602e-02
## 2013-08-30 -0.0082979413 -0.0257057975 -0.0197464637 -0.034742970 -3.045144e-02
## 2013-09-30 0.0111438825 0.0695889591 0.0753385871 0.063873315 3.115600e-02
## 2013-10-31 0.0082922136 0.0408611785 0.0320818750 0.034234154 4.526689e-02
## 2013-11-29 -0.0025098460 -0.0025940275 0.0054493040 0.041661372 2.920669e-02
## 2013-12-31 -0.0055826433 -0.0040743613 0.0215281353 0.012891732 2.559620e-02
## 2014-01-31 0.0152910008 -0.0903223610 -0.0534132249 -0.035775230 -3.588454e-02
## 2014-02-28 0.0037573192 0.0332204073 0.0595051622 0.045257744 4.451031e-02
## 2014-03-31 -0.0014814735 0.0380214029 -0.0046026433 0.013315192 8.261430e-03
## 2014-04-30 0.0081829982 0.0077728593 0.0165294453 -0.023184118 6.927176e-03
## 2014-05-30 0.0117217854 0.0290910789 0.0158283476 0.006205219 2.294161e-02
## 2014-06-30 -0.0005759853 0.0237338365 0.0091655562 0.037718565 2.043459e-02
## 2014-07-31 -0.0025116546 0.0135559008 -0.0263800808 -0.052009344 -1.352872e-02
## 2014-08-29 0.0114304077 0.0279044700 0.0018005750 0.043657778 3.870454e-02
## 2014-09-30 -0.0061676686 -0.0808564624 -0.0395984113 -0.061260262 -1.389196e-02
## 2014-10-31 0.0105846512 0.0140964501 -0.0026548109 0.068874804 2.327752e-02
## 2014-11-28 0.0065488402 -0.0155414089 0.0006252304 0.004773647 2.710126e-02
## 2014-12-31 0.0014752801 -0.0404422264 -0.0407467275 0.025295514 -2.539261e-03
## 2015-01-30 0.0203148531 -0.0068955568 0.0062266688 -0.054627638 -3.007716e-02
## 2015-02-27 -0.0089880538 0.0431359589 0.0614504481 0.056914329 5.468149e-02
## 2015-03-31 0.0037401243 -0.0150861354 -0.0143888617 0.010156750 -1.583014e-02
## 2015-04-30 -0.0032327923 0.0662810801 0.0358166578 -0.018418048 9.785906e-03
## 2015-05-29 -0.0043840612 -0.0419109109 0.0019525724 0.007509806 1.277412e-02
## 2015-06-30 -0.0108249890 -0.0297466142 -0.0316788276 0.004171607 -2.052119e-02
## 2015-07-31 0.0085842853 -0.0651782587 0.0201146365 -0.027375427 2.233786e-02
## 2015-08-31 -0.0033638598 -0.0925122166 -0.0771524800 -0.047268345 -6.288647e-02
## 2015-09-30 0.0080813346 -0.0318248523 -0.0451947203 -0.038464794 -2.584726e-02
## 2015-10-30 0.0006856701 0.0618081665 0.0640256333 0.063589807 8.163492e-02
## 2015-11-30 -0.0038981400 -0.0255603364 -0.0075557175 0.024415310 3.648477e-03
## 2015-12-31 -0.0019189302 -0.0389472441 -0.0235951109 -0.052156952 -1.743373e-02
## 2016-01-29 0.0123302363 -0.0516364590 -0.0567578198 -0.060307023 -5.106844e-02
## 2016-02-29 0.0088313596 -0.0082116798 -0.0339139014 0.020605063 -8.265148e-04
## 2016-03-31 0.0087092134 0.1218789852 0.0637454309 0.089910749 6.510027e-02
## 2016-04-29 0.0025458016 0.0040791497 0.0219754396 0.021044068 3.933751e-03
## 2016-05-31 0.0001352616 -0.0376283058 -0.0008562978 0.004396951 1.686811e-02
## 2016-06-30 0.0191665935 0.0445822665 -0.0244911962 0.008292413 3.470069e-03
## 2016-07-29 0.0054298038 0.0524419681 0.0390001903 0.049348181 3.582198e-02
## 2016-08-31 -0.0021555911 0.0087987881 0.0053266973 0.011261287 1.196701e-03
## 2016-09-30 0.0005151818 0.0248728732 0.0132794022 0.008614720 5.787767e-05
## 2016-10-31 -0.0082047796 -0.0083121295 -0.0224039105 -0.038134748 -1.748876e-02
## 2016-11-30 -0.0259901048 -0.0451619574 -0.0179745666 0.125246148 3.617596e-02
## 2016-12-30 0.0025383814 -0.0025300743 0.0267029452 0.031491790 2.006896e-02
## 2017-01-31 0.0021262507 0.0644316436 0.0323818767 -0.012143599 1.773634e-02
## 2017-02-28 0.0064380692 0.0172578369 0.0118363924 0.013428729 3.853961e-02
## 2017-03-31 -0.0005532870 0.0361888681 0.0318056370 -0.006533506 1.248925e-03
## 2017-04-28 0.0090293148 0.0168663993 0.0239523668 0.005107971 9.877440e-03
## 2017-05-31 0.0068477169 0.0280599865 0.0348101130 -0.022862636 1.401422e-02
## 2017-06-30 -0.0001828723 0.0092238728 0.0029561419 0.029151767 6.354824e-03
## 2017-07-31 0.0033344998 0.0565942299 0.0261876545 0.007481442 2.034557e-02
## 2017-08-31 0.0093688155 0.0232439755 -0.0004483634 -0.027564599 2.913560e-03
## 2017-09-29 -0.0057318325 -0.0004463954 0.0233429150 0.082321751 1.994900e-02
## 2017-10-31 0.0009780035 0.0322783816 0.0166535960 0.005916156 2.329090e-02
## 2017-11-30 -0.0014838908 -0.0038965658 0.0068701724 0.036913231 3.010817e-02
## 2017-12-29 0.0047403732 0.0369252960 0.0133981377 -0.003731306 1.205468e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398281e-05 0.0001042128 4.178357e-05 -7.811953e-05 -9.030481e-06
## EEM 1.042128e-04 0.0017547061 1.039015e-03 6.437719e-04 6.795415e-04
## EFA 4.178357e-05 0.0010390149 1.064237e-03 6.490291e-04 6.975394e-04
## IJS -7.811953e-05 0.0006437719 6.490291e-04 1.565445e-03 8.290221e-04
## SPY -9.030481e-06 0.0006795415 6.975394e-04 8.290221e-04 7.408268e-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.02347489
# 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.000387421 0.009257137 0.005815632 0.005684458 0.002330246
rowSums(component_contribution)
## [1] 0.02347489
# 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.0062311778 -0.0029352428 0.0366062167 0.052132964 4.992276e-02
## 2013-02-28 0.0058914092 -0.0231051198 -0.0129691125 0.016175087 1.267839e-02
## 2013-03-28 0.0009845721 -0.0102351239 0.0129691125 0.040258234 3.726792e-02
## 2013-04-30 0.0096392475 0.0120849040 0.0489678996 0.001222682 1.903058e-02
## 2013-05-31 -0.0202141852 -0.0494832528 -0.0306553297 0.041975943 2.333469e-02
## 2013-06-28 -0.0157775228 -0.0547286224 -0.0271446482 -0.001402810 -1.343447e-02
## 2013-07-31 0.0026869076 0.0131598843 0.0518603429 0.063541125 5.038602e-02
## 2013-08-30 -0.0082979413 -0.0257057975 -0.0197464637 -0.034742970 -3.045144e-02
## 2013-09-30 0.0111438825 0.0695889591 0.0753385871 0.063873315 3.115600e-02
## 2013-10-31 0.0082922136 0.0408611785 0.0320818750 0.034234154 4.526689e-02
## 2013-11-29 -0.0025098460 -0.0025940275 0.0054493040 0.041661372 2.920669e-02
## 2013-12-31 -0.0055826433 -0.0040743613 0.0215281353 0.012891732 2.559620e-02
## 2014-01-31 0.0152910008 -0.0903223610 -0.0534132249 -0.035775230 -3.588454e-02
## 2014-02-28 0.0037573192 0.0332204073 0.0595051622 0.045257744 4.451031e-02
## 2014-03-31 -0.0014814735 0.0380214029 -0.0046026433 0.013315192 8.261430e-03
## 2014-04-30 0.0081829982 0.0077728593 0.0165294453 -0.023184118 6.927176e-03
## 2014-05-30 0.0117217854 0.0290910789 0.0158283476 0.006205219 2.294161e-02
## 2014-06-30 -0.0005759853 0.0237338365 0.0091655562 0.037718565 2.043459e-02
## 2014-07-31 -0.0025116546 0.0135559008 -0.0263800808 -0.052009344 -1.352872e-02
## 2014-08-29 0.0114304077 0.0279044700 0.0018005750 0.043657778 3.870454e-02
## 2014-09-30 -0.0061676686 -0.0808564624 -0.0395984113 -0.061260262 -1.389196e-02
## 2014-10-31 0.0105846512 0.0140964501 -0.0026548109 0.068874804 2.327752e-02
## 2014-11-28 0.0065488402 -0.0155414089 0.0006252304 0.004773647 2.710126e-02
## 2014-12-31 0.0014752801 -0.0404422264 -0.0407467275 0.025295514 -2.539261e-03
## 2015-01-30 0.0203148531 -0.0068955568 0.0062266688 -0.054627638 -3.007716e-02
## 2015-02-27 -0.0089880538 0.0431359589 0.0614504481 0.056914329 5.468149e-02
## 2015-03-31 0.0037401243 -0.0150861354 -0.0143888617 0.010156750 -1.583014e-02
## 2015-04-30 -0.0032327923 0.0662810801 0.0358166578 -0.018418048 9.785906e-03
## 2015-05-29 -0.0043840612 -0.0419109109 0.0019525724 0.007509806 1.277412e-02
## 2015-06-30 -0.0108249890 -0.0297466142 -0.0316788276 0.004171607 -2.052119e-02
## 2015-07-31 0.0085842853 -0.0651782587 0.0201146365 -0.027375427 2.233786e-02
## 2015-08-31 -0.0033638598 -0.0925122166 -0.0771524800 -0.047268345 -6.288647e-02
## 2015-09-30 0.0080813346 -0.0318248523 -0.0451947203 -0.038464794 -2.584726e-02
## 2015-10-30 0.0006856701 0.0618081665 0.0640256333 0.063589807 8.163492e-02
## 2015-11-30 -0.0038981400 -0.0255603364 -0.0075557175 0.024415310 3.648477e-03
## 2015-12-31 -0.0019189302 -0.0389472441 -0.0235951109 -0.052156952 -1.743373e-02
## 2016-01-29 0.0123302363 -0.0516364590 -0.0567578198 -0.060307023 -5.106844e-02
## 2016-02-29 0.0088313596 -0.0082116798 -0.0339139014 0.020605063 -8.265148e-04
## 2016-03-31 0.0087092134 0.1218789852 0.0637454309 0.089910749 6.510027e-02
## 2016-04-29 0.0025458016 0.0040791497 0.0219754396 0.021044068 3.933751e-03
## 2016-05-31 0.0001352616 -0.0376283058 -0.0008562978 0.004396951 1.686811e-02
## 2016-06-30 0.0191665935 0.0445822665 -0.0244911962 0.008292413 3.470069e-03
## 2016-07-29 0.0054298038 0.0524419681 0.0390001903 0.049348181 3.582198e-02
## 2016-08-31 -0.0021555911 0.0087987881 0.0053266973 0.011261287 1.196701e-03
## 2016-09-30 0.0005151818 0.0248728732 0.0132794022 0.008614720 5.787767e-05
## 2016-10-31 -0.0082047796 -0.0083121295 -0.0224039105 -0.038134748 -1.748876e-02
## 2016-11-30 -0.0259901048 -0.0451619574 -0.0179745666 0.125246148 3.617596e-02
## 2016-12-30 0.0025383814 -0.0025300743 0.0267029452 0.031491790 2.006896e-02
## 2017-01-31 0.0021262507 0.0644316436 0.0323818767 -0.012143599 1.773634e-02
## 2017-02-28 0.0064380692 0.0172578369 0.0118363924 0.013428729 3.853961e-02
## 2017-03-31 -0.0005532870 0.0361888681 0.0318056370 -0.006533506 1.248925e-03
## 2017-04-28 0.0090293148 0.0168663993 0.0239523668 0.005107971 9.877440e-03
## 2017-05-31 0.0068477169 0.0280599865 0.0348101130 -0.022862636 1.401422e-02
## 2017-06-30 -0.0001828723 0.0092238728 0.0029561419 0.029151767 6.354824e-03
## 2017-07-31 0.0033344998 0.0565942299 0.0261876545 0.007481442 2.034557e-02
## 2017-08-31 0.0093688155 0.0232439755 -0.0004483634 -0.027564599 2.913560e-03
## 2017-09-29 -0.0057318325 -0.0004463954 0.0233429150 0.082321751 1.994900e-02
## 2017-10-31 0.0009780035 0.0322783816 0.0166535960 0.005916156 2.329090e-02
## 2017-11-30 -0.0014838908 -0.0038965658 0.0068701724 0.036913231 3.010817e-02
## 2017-12-29 0.0047403732 0.0369252960 0.0133981377 -0.003731306 1.205468e-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, .1)) %>%
# Transform to long from
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 from
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(positio = "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 Volatilityand Weight",
y = "Percent",
x = NULL)