# 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.0062309983 -0.0029354856 0.0366058345 0.052133205 4.992285e-02
## 2013-02-28 0.0058915397 -0.0231053453 -0.0129693818 0.016175342 1.267856e-02
## 2013-03-28 0.0009844304 -0.0102352316 0.0129693818 0.040257885 3.726788e-02
## 2013-04-30 0.0096391959 0.0120851273 0.0489678087 0.001222538 1.903064e-02
## 2013-05-31 -0.0202137936 -0.0494835176 -0.0306557766 0.041976891 2.333455e-02
## 2013-06-28 -0.0157786224 -0.0547285338 -0.0271443468 -0.001403198 -1.343432e-02
## 2013-07-31 0.0026877041 0.0131598630 0.0518605797 0.063541230 5.038606e-02
## 2013-08-30 -0.0082981785 -0.0257056421 -0.0197463298 -0.034743287 -3.045111e-02
## 2013-09-30 0.0111447239 0.0695886549 0.0753385976 0.063873430 3.115581e-02
## 2013-10-31 0.0082913099 0.0408614795 0.0320813234 0.034234254 4.526727e-02
## 2013-11-29 -0.0025100110 -0.0025940761 0.0054499275 0.041661072 2.920647e-02
## 2013-12-31 -0.0055829453 -0.0040745915 0.0215280857 0.012891965 2.559595e-02
## 2014-01-31 0.0152916910 -0.0903222373 -0.0534135125 -0.035775655 -3.588435e-02
## 2014-02-28 0.0037576147 0.0332202932 0.0595050873 0.045257774 4.451015e-02
## 2014-03-31 -0.0014819410 0.0380217630 -0.0046026528 0.013315232 8.261201e-03
## 2014-04-30 0.0081826843 0.0077725550 0.0165295022 -0.023184439 6.927754e-03
## 2014-05-30 0.0117219356 0.0290914298 0.0158285209 0.006205615 2.294107e-02
## 2014-06-30 -0.0005752718 0.0237338294 0.0091651086 0.037718732 2.043464e-02
## 2014-07-31 -0.0025125341 0.0135558000 -0.0263796602 -0.052009582 -1.352879e-02
## 2014-08-29 0.0114304342 0.0279047127 0.0018003198 0.043657791 3.870524e-02
## 2014-09-30 -0.0061668821 -0.0808569957 -0.0395983828 -0.061260156 -1.389236e-02
## 2014-10-31 0.0105843615 0.0140965730 -0.0026547701 0.068874957 2.327789e-02
## 2014-11-28 0.0065483378 -0.0155413256 0.0006254113 0.004773494 2.710149e-02
## 2014-12-31 0.0014756214 -0.0404421179 -0.0407466407 0.025295678 -2.540269e-03
## 2015-01-30 0.0203151709 -0.0068953886 0.0062265346 -0.054627567 -3.007710e-02
## 2015-02-27 -0.0089882999 0.0431357164 0.0614503340 0.056914286 5.468205e-02
## 2015-03-31 0.0037408066 -0.0150864224 -0.0143887940 0.010156466 -1.583014e-02
## 2015-04-30 -0.0032338744 0.0662814793 0.0358165820 -0.018417570 9.785522e-03
## 2015-05-29 -0.0043830760 -0.0419108097 0.0019527893 0.007509707 1.277442e-02
## 2015-06-30 -0.0108254123 -0.0297468223 -0.0316788399 0.004171436 -2.052115e-02
## 2015-07-31 0.0085842200 -0.0651781505 0.0201144152 -0.027375424 2.233776e-02
## 2015-08-31 -0.0033639695 -0.0925121983 -0.0771522498 -0.047268843 -6.288654e-02
## 2015-09-30 0.0080820828 -0.0318250291 -0.0451951046 -0.038464347 -2.584744e-02
## 2015-10-30 0.0006846272 0.0618082789 0.0640260786 0.063590090 8.163508e-02
## 2015-11-30 -0.0038979846 -0.0255604049 -0.0075562313 0.024414797 3.648594e-03
## 2015-12-31 -0.0019190855 -0.0389472098 -0.0235947998 -0.052156932 -1.743376e-02
## 2016-01-29 0.0123301147 -0.0516364737 -0.0567578828 -0.060306641 -5.106833e-02
## 2016-02-29 0.0088317698 -0.0082116139 -0.0339138341 0.020605212 -8.268926e-04
## 2016-03-31 0.0087089559 0.1218788821 0.0637457564 0.089910291 6.510062e-02
## 2016-04-29 0.0025462067 0.0040792413 0.0219751028 0.021044165 3.933256e-03
## 2016-05-31 0.0001351525 -0.0376284639 -0.0008560850 0.004396951 1.686859e-02
## 2016-06-30 0.0191669821 0.0445822210 -0.0244915251 0.008292475 3.469846e-03
## 2016-07-29 0.0054295073 0.0524424305 0.0390002385 0.049348352 3.582192e-02
## 2016-08-31 -0.0021561788 0.0087984061 0.0053268445 0.011260917 1.196974e-03
## 2016-09-30 0.0005162171 0.0248729185 0.0132790786 0.008614807 5.802546e-05
## 2016-10-31 -0.0082054532 -0.0083122645 -0.0224036661 -0.038134959 -1.748947e-02
## 2016-11-30 -0.0259897123 -0.0451616270 -0.0179745747 0.125246475 3.617641e-02
## 2016-12-30 0.0025385077 -0.0025300487 0.0267029898 0.031491742 2.006906e-02
## 2017-01-31 0.0021262162 0.0644314134 0.0323817472 -0.012143831 1.773656e-02
## 2017-02-28 0.0064380468 0.0172578354 0.0118366770 0.013428646 3.853903e-02
## 2017-03-31 -0.0005531278 0.0361888562 0.0318055897 -0.006533060 1.249122e-03
## 2017-04-28 0.0090288995 0.0168666395 0.0239524062 0.005108016 9.877524e-03
## 2017-05-31 0.0068472697 0.0280598963 0.0348100648 -0.022862609 1.401435e-02
## 2017-06-30 -0.0001824771 0.0092235488 0.0029559259 0.029151638 6.354361e-03
## 2017-07-31 0.0033344926 0.0565947442 0.0261878800 0.007481528 2.034601e-02
## 2017-08-31 0.0093692425 0.0232437116 -0.0004483592 -0.027564839 2.913568e-03
## 2017-09-29 -0.0057328752 -0.0004461573 0.0233428833 0.082321981 1.994872e-02
## 2017-10-31 0.0009785262 0.0322784943 0.0166536509 0.005915633 2.329089e-02
## 2017-11-30 -0.0014842345 -0.0038970180 0.0068697554 0.036913721 3.010802e-02
## 2017-12-29 0.0047406857 0.0369254159 0.0133985498 -0.003731303 1.205495e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398447e-05 0.0001042090 4.178117e-05 -7.811969e-05 -9.032221e-06
## EEM 1.042090e-04 0.0017547095 1.039016e-03 6.437735e-04 6.795437e-04
## EFA 4.178117e-05 0.0010390164 1.064237e-03 6.490293e-04 6.975418e-04
## IJS -7.811969e-05 0.0006437735 6.490293e-04 1.565450e-03 8.290254e-04
## SPY -9.032221e-06 0.0006795437 6.975418e-04 8.290254e-04 7.408301e-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.0234749
# 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.0003874079 0.009257142 0.005815631 0.005684469 0.002330252
rowSums(component_contribution)
## [1] 0.0234749
# 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.0062309983 -0.0029354856 0.0366058345 0.052133205 4.992285e-02
## 2013-02-28 0.0058915397 -0.0231053453 -0.0129693818 0.016175342 1.267856e-02
## 2013-03-28 0.0009844304 -0.0102352316 0.0129693818 0.040257885 3.726788e-02
## 2013-04-30 0.0096391959 0.0120851273 0.0489678087 0.001222538 1.903064e-02
## 2013-05-31 -0.0202137936 -0.0494835176 -0.0306557766 0.041976891 2.333455e-02
## 2013-06-28 -0.0157786224 -0.0547285338 -0.0271443468 -0.001403198 -1.343432e-02
## 2013-07-31 0.0026877041 0.0131598630 0.0518605797 0.063541230 5.038606e-02
## 2013-08-30 -0.0082981785 -0.0257056421 -0.0197463298 -0.034743287 -3.045111e-02
## 2013-09-30 0.0111447239 0.0695886549 0.0753385976 0.063873430 3.115581e-02
## 2013-10-31 0.0082913099 0.0408614795 0.0320813234 0.034234254 4.526727e-02
## 2013-11-29 -0.0025100110 -0.0025940761 0.0054499275 0.041661072 2.920647e-02
## 2013-12-31 -0.0055829453 -0.0040745915 0.0215280857 0.012891965 2.559595e-02
## 2014-01-31 0.0152916910 -0.0903222373 -0.0534135125 -0.035775655 -3.588435e-02
## 2014-02-28 0.0037576147 0.0332202932 0.0595050873 0.045257774 4.451015e-02
## 2014-03-31 -0.0014819410 0.0380217630 -0.0046026528 0.013315232 8.261201e-03
## 2014-04-30 0.0081826843 0.0077725550 0.0165295022 -0.023184439 6.927754e-03
## 2014-05-30 0.0117219356 0.0290914298 0.0158285209 0.006205615 2.294107e-02
## 2014-06-30 -0.0005752718 0.0237338294 0.0091651086 0.037718732 2.043464e-02
## 2014-07-31 -0.0025125341 0.0135558000 -0.0263796602 -0.052009582 -1.352879e-02
## 2014-08-29 0.0114304342 0.0279047127 0.0018003198 0.043657791 3.870524e-02
## 2014-09-30 -0.0061668821 -0.0808569957 -0.0395983828 -0.061260156 -1.389236e-02
## 2014-10-31 0.0105843615 0.0140965730 -0.0026547701 0.068874957 2.327789e-02
## 2014-11-28 0.0065483378 -0.0155413256 0.0006254113 0.004773494 2.710149e-02
## 2014-12-31 0.0014756214 -0.0404421179 -0.0407466407 0.025295678 -2.540269e-03
## 2015-01-30 0.0203151709 -0.0068953886 0.0062265346 -0.054627567 -3.007710e-02
## 2015-02-27 -0.0089882999 0.0431357164 0.0614503340 0.056914286 5.468205e-02
## 2015-03-31 0.0037408066 -0.0150864224 -0.0143887940 0.010156466 -1.583014e-02
## 2015-04-30 -0.0032338744 0.0662814793 0.0358165820 -0.018417570 9.785522e-03
## 2015-05-29 -0.0043830760 -0.0419108097 0.0019527893 0.007509707 1.277442e-02
## 2015-06-30 -0.0108254123 -0.0297468223 -0.0316788399 0.004171436 -2.052115e-02
## 2015-07-31 0.0085842200 -0.0651781505 0.0201144152 -0.027375424 2.233776e-02
## 2015-08-31 -0.0033639695 -0.0925121983 -0.0771522498 -0.047268843 -6.288654e-02
## 2015-09-30 0.0080820828 -0.0318250291 -0.0451951046 -0.038464347 -2.584744e-02
## 2015-10-30 0.0006846272 0.0618082789 0.0640260786 0.063590090 8.163508e-02
## 2015-11-30 -0.0038979846 -0.0255604049 -0.0075562313 0.024414797 3.648594e-03
## 2015-12-31 -0.0019190855 -0.0389472098 -0.0235947998 -0.052156932 -1.743376e-02
## 2016-01-29 0.0123301147 -0.0516364737 -0.0567578828 -0.060306641 -5.106833e-02
## 2016-02-29 0.0088317698 -0.0082116139 -0.0339138341 0.020605212 -8.268926e-04
## 2016-03-31 0.0087089559 0.1218788821 0.0637457564 0.089910291 6.510062e-02
## 2016-04-29 0.0025462067 0.0040792413 0.0219751028 0.021044165 3.933256e-03
## 2016-05-31 0.0001351525 -0.0376284639 -0.0008560850 0.004396951 1.686859e-02
## 2016-06-30 0.0191669821 0.0445822210 -0.0244915251 0.008292475 3.469846e-03
## 2016-07-29 0.0054295073 0.0524424305 0.0390002385 0.049348352 3.582192e-02
## 2016-08-31 -0.0021561788 0.0087984061 0.0053268445 0.011260917 1.196974e-03
## 2016-09-30 0.0005162171 0.0248729185 0.0132790786 0.008614807 5.802546e-05
## 2016-10-31 -0.0082054532 -0.0083122645 -0.0224036661 -0.038134959 -1.748947e-02
## 2016-11-30 -0.0259897123 -0.0451616270 -0.0179745747 0.125246475 3.617641e-02
## 2016-12-30 0.0025385077 -0.0025300487 0.0267029898 0.031491742 2.006906e-02
## 2017-01-31 0.0021262162 0.0644314134 0.0323817472 -0.012143831 1.773656e-02
## 2017-02-28 0.0064380468 0.0172578354 0.0118366770 0.013428646 3.853903e-02
## 2017-03-31 -0.0005531278 0.0361888562 0.0318055897 -0.006533060 1.249122e-03
## 2017-04-28 0.0090288995 0.0168666395 0.0239524062 0.005108016 9.877524e-03
## 2017-05-31 0.0068472697 0.0280598963 0.0348100648 -0.022862609 1.401435e-02
## 2017-06-30 -0.0001824771 0.0092235488 0.0029559259 0.029151638 6.354361e-03
## 2017-07-31 0.0033344926 0.0565947442 0.0261878800 0.007481528 2.034601e-02
## 2017-08-31 0.0093692425 0.0232437116 -0.0004483592 -0.027564839 2.913568e-03
## 2017-09-29 -0.0057328752 -0.0004461573 0.0233428833 0.082321981 1.994872e-02
## 2017-10-31 0.0009785262 0.0322784943 0.0166536509 0.005915633 2.329089e-02
## 2017-11-30 -0.0014842345 -0.0038970180 0.0068697554 0.036913721 3.010802e-02
## 2017-12-29 0.0047406857 0.0369254159 0.0133985498 -0.003731303 1.205495e-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 on 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)