# 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.0062311986 -0.0029355225 0.0366063119 0.052132875 4.992320e-02
## 2013-02-28 0.0058916850 -0.0231053305 -0.0129693888 0.016175111 1.267828e-02
## 2013-03-28 0.0009844126 -0.0102345489 0.0129693888 0.040258755 3.726762e-02
## 2013-04-30 0.0096394948 0.0120842173 0.0489675988 0.001222411 1.903058e-02
## 2013-05-31 -0.0202145751 -0.0494832204 -0.0306556806 0.041976129 2.333543e-02
## 2013-06-28 -0.0157770933 -0.0547282959 -0.0271443147 -0.001402677 -1.343444e-02
## 2013-07-31 0.0026874431 0.0131596255 0.0518604105 0.063540987 5.038578e-02
## 2013-08-30 -0.0082981159 -0.0257056520 -0.0197463273 -0.034743556 -3.045073e-02
## 2013-09-30 0.0111437776 0.0695890605 0.0753385331 0.063873586 3.115535e-02
## 2013-10-31 0.0082920092 0.0408613404 0.0320818171 0.034234619 4.526680e-02
## 2013-11-29 -0.0025099641 -0.0025942364 0.0054495471 0.041660627 2.920721e-02
## 2013-12-31 -0.0055824702 -0.0040743456 0.0215278893 0.012892534 2.559584e-02
## 2014-01-31 0.0152910076 -0.0903228841 -0.0534130913 -0.035775157 -3.588454e-02
## 2014-02-28 0.0037564749 0.0332209361 0.0595049933 0.045256715 4.451006e-02
## 2014-03-31 -0.0014815704 0.0380215841 -0.0046027354 0.013315839 8.261709e-03
## 2014-04-30 0.0081837932 0.0077726697 0.0165294481 -0.023184513 6.927467e-03
## 2014-05-30 0.0117215378 0.0290912214 0.0158283763 0.006205591 2.294078e-02
## 2014-06-30 -0.0005759261 0.0237337969 0.0091653144 0.037718318 2.043522e-02
## 2014-07-31 -0.0025122748 0.0135557649 -0.0263794716 -0.052009338 -1.352885e-02
## 2014-08-29 0.0114310531 0.0279046974 0.0018002076 0.043658036 3.870499e-02
## 2014-09-30 -0.0061674642 -0.0808568613 -0.0395982704 -0.061260308 -1.389301e-02
## 2014-10-31 0.0105851325 0.0140964741 -0.0026549203 0.068874630 2.327811e-02
## 2014-11-28 0.0065478731 -0.0155411680 0.0006254062 0.004773720 2.710157e-02
## 2014-12-31 0.0014757108 -0.0404424511 -0.0407466665 0.025295670 -2.540051e-03
## 2015-01-30 0.0203153087 -0.0068958350 0.0062262718 -0.054627860 -3.007700e-02
## 2015-02-27 -0.0089883059 0.0431365078 0.0614507073 0.056914719 5.468203e-02
## 2015-03-31 0.0037400752 -0.0150864341 -0.0143888986 0.010156172 -1.583063e-02
## 2015-04-30 -0.0032328487 0.0662815148 0.0358165749 -0.018417749 9.786224e-03
## 2015-05-29 -0.0043833240 -0.0419113191 0.0019527074 0.007510027 1.277392e-02
## 2015-06-30 -0.0108265831 -0.0297463895 -0.0316787741 0.004171213 -2.052112e-02
## 2015-07-31 0.0085857250 -0.0651784228 0.0201144609 -0.027375270 2.233788e-02
## 2015-08-31 -0.0033640812 -0.0925120914 -0.0771525204 -0.047268304 -6.288667e-02
## 2015-09-30 0.0080807493 -0.0318249506 -0.0451947225 -0.038464679 -2.584706e-02
## 2015-10-30 0.0006858213 0.0618082843 0.0640255940 0.063589553 8.163487e-02
## 2015-11-30 -0.0038981486 -0.0255603828 -0.0075557493 0.024415260 3.648704e-03
## 2015-12-31 -0.0019188519 -0.0389471565 -0.0235949185 -0.052156874 -1.743366e-02
## 2016-01-29 0.0123293083 -0.0516366931 -0.0567577521 -0.060307023 -5.106852e-02
## 2016-02-29 0.0088320681 -0.0082115773 -0.0339139751 0.020605606 -8.266309e-04
## 2016-03-31 0.0087090511 0.1218790627 0.0637457369 0.089910022 6.510042e-02
## 2016-04-29 0.0025458861 0.0040790657 0.0219752071 0.021044305 3.933263e-03
## 2016-05-31 0.0001360761 -0.0376284092 -0.0008561247 0.004397234 1.686866e-02
## 2016-06-30 0.0191665856 0.0445824760 -0.0244917119 0.008292162 3.469925e-03
## 2016-07-29 0.0054296915 0.0524420774 0.0390004701 0.049348215 3.582174e-02
## 2016-08-31 -0.0021564449 0.0087985890 0.0053269209 0.011261188 1.197076e-03
## 2016-09-30 0.0005157787 0.0248728315 0.0132789208 0.008614636 5.774279e-05
## 2016-10-31 -0.0082050031 -0.0083122263 -0.0224036205 -0.038134777 -1.748909e-02
## 2016-11-30 -0.0259901784 -0.0451616221 -0.0179746749 0.125246440 3.617607e-02
## 2016-12-30 0.0025390303 -0.0025302984 0.0267030732 0.031491602 2.006926e-02
## 2017-01-31 0.0021254405 0.0644317220 0.0323818365 -0.012143804 1.773652e-02
## 2017-02-28 0.0064381866 0.0172577360 0.0118366340 0.013429018 3.853932e-02
## 2017-03-31 -0.0005528664 0.0361889664 0.0318054816 -0.006533236 1.249010e-03
## 2017-04-28 0.0090291152 0.0168664223 0.0239522163 0.005107696 9.877038e-03
## 2017-05-31 0.0068468509 0.0280601456 0.0348102252 -0.022862524 1.401432e-02
## 2017-06-30 -0.0001822163 0.0092235584 0.0029558906 0.029151717 6.354644e-03
## 2017-07-31 0.0033345456 0.0565945313 0.0261878297 0.007481786 2.034584e-02
## 2017-08-31 0.0093689424 0.0232435387 -0.0004482007 -0.027564754 2.913595e-03
## 2017-09-29 -0.0057322635 -0.0004461344 0.0233428119 0.082321715 1.994912e-02
## 2017-10-31 0.0009778712 0.0322783760 0.0166535263 0.005916225 2.329081e-02
## 2017-11-30 -0.0014835964 -0.0038968316 0.0068701607 0.036912997 3.010781e-02
## 2017-12-29 0.0047400532 0.0369251455 0.0133984312 -0.003731518 1.205514e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398416e-05 0.0001042095 4.178452e-05 -7.811841e-05 -9.029232e-06
## EEM 1.042095e-04 0.0017547132 1.039016e-03 6.437736e-04 6.795451e-04
## EFA 4.178452e-05 0.0010390163 1.064236e-03 6.490268e-04 6.975400e-04
## IJS -7.811841e-05 0.0006437736 6.490268e-04 1.565445e-03 8.290241e-04
## SPY -9.029232e-06 0.0006795451 6.975400e-04 8.290241e-04 7.408289e-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.0003874211 0.009257151 0.005815628 0.005684457 0.002330252
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.0062311986 -0.0029355225 0.0366063119 0.052132875 4.992320e-02
## 2013-02-28 0.0058916850 -0.0231053305 -0.0129693888 0.016175111 1.267828e-02
## 2013-03-28 0.0009844126 -0.0102345489 0.0129693888 0.040258755 3.726762e-02
## 2013-04-30 0.0096394948 0.0120842173 0.0489675988 0.001222411 1.903058e-02
## 2013-05-31 -0.0202145751 -0.0494832204 -0.0306556806 0.041976129 2.333543e-02
## 2013-06-28 -0.0157770933 -0.0547282959 -0.0271443147 -0.001402677 -1.343444e-02
## 2013-07-31 0.0026874431 0.0131596255 0.0518604105 0.063540987 5.038578e-02
## 2013-08-30 -0.0082981159 -0.0257056520 -0.0197463273 -0.034743556 -3.045073e-02
## 2013-09-30 0.0111437776 0.0695890605 0.0753385331 0.063873586 3.115535e-02
## 2013-10-31 0.0082920092 0.0408613404 0.0320818171 0.034234619 4.526680e-02
## 2013-11-29 -0.0025099641 -0.0025942364 0.0054495471 0.041660627 2.920721e-02
## 2013-12-31 -0.0055824702 -0.0040743456 0.0215278893 0.012892534 2.559584e-02
## 2014-01-31 0.0152910076 -0.0903228841 -0.0534130913 -0.035775157 -3.588454e-02
## 2014-02-28 0.0037564749 0.0332209361 0.0595049933 0.045256715 4.451006e-02
## 2014-03-31 -0.0014815704 0.0380215841 -0.0046027354 0.013315839 8.261709e-03
## 2014-04-30 0.0081837932 0.0077726697 0.0165294481 -0.023184513 6.927467e-03
## 2014-05-30 0.0117215378 0.0290912214 0.0158283763 0.006205591 2.294078e-02
## 2014-06-30 -0.0005759261 0.0237337969 0.0091653144 0.037718318 2.043522e-02
## 2014-07-31 -0.0025122748 0.0135557649 -0.0263794716 -0.052009338 -1.352885e-02
## 2014-08-29 0.0114310531 0.0279046974 0.0018002076 0.043658036 3.870499e-02
## 2014-09-30 -0.0061674642 -0.0808568613 -0.0395982704 -0.061260308 -1.389301e-02
## 2014-10-31 0.0105851325 0.0140964741 -0.0026549203 0.068874630 2.327811e-02
## 2014-11-28 0.0065478731 -0.0155411680 0.0006254062 0.004773720 2.710157e-02
## 2014-12-31 0.0014757108 -0.0404424511 -0.0407466665 0.025295670 -2.540051e-03
## 2015-01-30 0.0203153087 -0.0068958350 0.0062262718 -0.054627860 -3.007700e-02
## 2015-02-27 -0.0089883059 0.0431365078 0.0614507073 0.056914719 5.468203e-02
## 2015-03-31 0.0037400752 -0.0150864341 -0.0143888986 0.010156172 -1.583063e-02
## 2015-04-30 -0.0032328487 0.0662815148 0.0358165749 -0.018417749 9.786224e-03
## 2015-05-29 -0.0043833240 -0.0419113191 0.0019527074 0.007510027 1.277392e-02
## 2015-06-30 -0.0108265831 -0.0297463895 -0.0316787741 0.004171213 -2.052112e-02
## 2015-07-31 0.0085857250 -0.0651784228 0.0201144609 -0.027375270 2.233788e-02
## 2015-08-31 -0.0033640812 -0.0925120914 -0.0771525204 -0.047268304 -6.288667e-02
## 2015-09-30 0.0080807493 -0.0318249506 -0.0451947225 -0.038464679 -2.584706e-02
## 2015-10-30 0.0006858213 0.0618082843 0.0640255940 0.063589553 8.163487e-02
## 2015-11-30 -0.0038981486 -0.0255603828 -0.0075557493 0.024415260 3.648704e-03
## 2015-12-31 -0.0019188519 -0.0389471565 -0.0235949185 -0.052156874 -1.743366e-02
## 2016-01-29 0.0123293083 -0.0516366931 -0.0567577521 -0.060307023 -5.106852e-02
## 2016-02-29 0.0088320681 -0.0082115773 -0.0339139751 0.020605606 -8.266309e-04
## 2016-03-31 0.0087090511 0.1218790627 0.0637457369 0.089910022 6.510042e-02
## 2016-04-29 0.0025458861 0.0040790657 0.0219752071 0.021044305 3.933263e-03
## 2016-05-31 0.0001360761 -0.0376284092 -0.0008561247 0.004397234 1.686866e-02
## 2016-06-30 0.0191665856 0.0445824760 -0.0244917119 0.008292162 3.469925e-03
## 2016-07-29 0.0054296915 0.0524420774 0.0390004701 0.049348215 3.582174e-02
## 2016-08-31 -0.0021564449 0.0087985890 0.0053269209 0.011261188 1.197076e-03
## 2016-09-30 0.0005157787 0.0248728315 0.0132789208 0.008614636 5.774279e-05
## 2016-10-31 -0.0082050031 -0.0083122263 -0.0224036205 -0.038134777 -1.748909e-02
## 2016-11-30 -0.0259901784 -0.0451616221 -0.0179746749 0.125246440 3.617607e-02
## 2016-12-30 0.0025390303 -0.0025302984 0.0267030732 0.031491602 2.006926e-02
## 2017-01-31 0.0021254405 0.0644317220 0.0323818365 -0.012143804 1.773652e-02
## 2017-02-28 0.0064381866 0.0172577360 0.0118366340 0.013429018 3.853932e-02
## 2017-03-31 -0.0005528664 0.0361889664 0.0318054816 -0.006533236 1.249010e-03
## 2017-04-28 0.0090291152 0.0168664223 0.0239522163 0.005107696 9.877038e-03
## 2017-05-31 0.0068468509 0.0280601456 0.0348102252 -0.022862524 1.401432e-02
## 2017-06-30 -0.0001822163 0.0092235584 0.0029558906 0.029151717 6.354644e-03
## 2017-07-31 0.0033345456 0.0565945313 0.0261878297 0.007481786 2.034584e-02
## 2017-08-31 0.0093689424 0.0232435387 -0.0004482007 -0.027564754 2.913595e-03
## 2017-09-29 -0.0057322635 -0.0004461344 0.0233428119 0.082321715 1.994912e-02
## 2017-10-31 0.0009778712 0.0322783760 0.0166535263 0.005916225 2.329081e-02
## 2017-11-30 -0.0014835964 -0.0038968316 0.0068701607 0.036912997 3.010781e-02
## 2017-12-29 0.0047400532 0.0369251455 0.0133984312 -0.003731518 1.205514e-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 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
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 = "Pecent",
x = NULL)