# 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.0062305625 -0.0029356841 0.0366064017 0.052133087 4.992299e-02
## 2013-02-28 0.0058904111 -0.0231050093 -0.0129693872 0.016174927 1.267861e-02
## 2013-03-28 0.0009846251 -0.0102350096 0.0129693872 0.040258110 3.726760e-02
## 2013-04-30 0.0096394882 0.0120845638 0.0489677225 0.001222568 1.903001e-02
## 2013-05-31 -0.0202136587 -0.0494830269 -0.0306554215 0.041976878 2.333573e-02
## 2013-06-28 -0.0157782721 -0.0547286224 -0.0271444699 -0.001402849 -1.343434e-02
## 2013-07-31 0.0026874686 0.0131595131 0.0518602560 0.063540876 5.038576e-02
## 2013-08-30 -0.0082974388 -0.0257052994 -0.0197464655 -0.034743000 -3.045160e-02
## 2013-09-30 0.0111434058 0.0695887138 0.0753385935 0.063873280 3.115578e-02
## 2013-10-31 0.0082922156 0.0408610696 0.0320818776 0.034234198 4.526713e-02
## 2013-11-29 -0.0025101740 -0.0025938002 0.0054493836 0.041661174 2.920693e-02
## 2013-12-31 -0.0055835584 -0.0040744757 0.0215282903 0.012891802 2.559607e-02
## 2014-01-31 0.0152922737 -0.0903224345 -0.0534133799 -0.035775083 -3.588433e-02
## 2014-02-28 0.0037569429 0.0332202922 0.0595050852 0.045257415 4.451019e-02
## 2014-03-31 -0.0014815363 0.0380218224 -0.0046024888 0.013315354 8.261513e-03
## 2014-04-30 0.0081831074 0.0077725112 0.0165291395 -0.023184358 6.927176e-03
## 2014-05-30 0.0117215956 0.0290913104 0.0158286510 0.006205544 2.294128e-02
## 2014-06-30 -0.0005756678 0.0237339463 0.0091652585 0.037718328 2.043450e-02
## 2014-07-31 -0.0025120175 0.0135555745 -0.0263797056 -0.052009458 -1.352855e-02
## 2014-08-29 0.0114305854 0.0279044760 0.0018004225 0.043658057 3.870483e-02
## 2014-09-30 -0.0061676573 -0.0808563659 -0.0395985696 -0.061260214 -1.389237e-02
## 2014-10-31 0.0105848003 0.0140962266 -0.0026548113 0.068874574 2.327779e-02
## 2014-11-28 0.0065488072 -0.0155412998 0.0006253892 0.004773795 2.710122e-02
## 2014-12-31 0.0014749701 -0.0404421167 -0.0407468928 0.025295828 -2.539574e-03
## 2015-01-30 0.0203154034 -0.0068951980 0.0062269162 -0.054628126 -3.007717e-02
## 2015-02-27 -0.0089881593 0.0431357191 0.0614504432 0.056915018 5.468207e-02
## 2015-03-31 0.0037404818 -0.0150860189 -0.0143889389 0.010156013 -1.583045e-02
## 2015-04-30 -0.0032333567 0.0662808544 0.0358166578 -0.018417442 9.785953e-03
## 2015-05-29 -0.0043839103 -0.0419108018 0.0019527233 0.007509723 1.277414e-02
## 2015-06-30 -0.0108252523 -0.0297464970 -0.0316787449 0.004171221 -2.052119e-02
## 2015-07-31 0.0085850003 -0.0651782508 0.0201142502 -0.027375369 2.233780e-02
## 2015-08-31 -0.0033640124 -0.0925122045 -0.0771524098 -0.047268429 -6.288651e-02
## 2015-09-30 0.0080813414 -0.0318249895 -0.0451946379 -0.038464471 -2.584723e-02
## 2015-10-30 0.0006858486 0.0618082997 0.0640257952 0.063589474 8.163488e-02
## 2015-11-30 -0.0038983967 -0.0255604013 -0.0075558793 0.024415727 3.648705e-03
## 2015-12-31 -0.0019188551 -0.0389473124 -0.0235951109 -0.052157295 -1.743365e-02
## 2016-01-29 0.0123293338 -0.0516365338 -0.0567578198 -0.060307033 -5.106893e-02
## 2016-02-29 0.0088320962 -0.0082114542 -0.0339140842 0.020605629 -8.259359e-04
## 2016-03-31 0.0087089773 0.1218787676 0.0637458709 0.089910231 6.510015e-02
## 2016-04-29 0.0025460615 0.0040792829 0.0219750985 0.021044231 3.933308e-03
## 2016-05-31 0.0001352208 -0.0376283723 -0.0008558781 0.004397115 1.686847e-02
## 2016-06-30 0.0191666114 0.0445821345 -0.0244917902 0.008291953 3.469801e-03
## 2016-07-29 0.0054298399 0.0524423508 0.0390002829 0.049348456 3.582191e-02
## 2016-08-31 -0.0021559449 0.0087985997 0.0053266982 0.011261250 1.196978e-03
## 2016-09-30 0.0005159036 0.0248727505 0.0132791607 0.008614528 5.813593e-05
## 2016-10-31 -0.0082052164 -0.0083120689 -0.0224035044 -0.038134718 -1.748905e-02
## 2016-11-30 -0.0259898863 -0.0451619574 -0.0179744820 0.125246484 3.617606e-02
## 2016-12-30 0.0025381850 -0.0025298821 0.0267027783 0.031491673 2.006909e-02
## 2017-01-31 0.0021258727 0.0644316316 0.0323819590 -0.012144032 1.773638e-02
## 2017-02-28 0.0064380351 0.0172575387 0.0118364711 0.013428625 3.853926e-02
## 2017-03-31 -0.0005526225 0.0361889862 0.0318056345 -0.006532649 1.249013e-03
## 2017-04-28 0.0090290275 0.0168663993 0.0239523650 0.005107588 9.877382e-03
## 2017-05-31 0.0068476720 0.0280600953 0.0348101105 -0.022862437 1.401416e-02
## 2017-06-30 -0.0001830138 0.0092236560 0.0029558548 0.029151683 6.354832e-03
## 2017-07-31 0.0033344876 0.0565945416 0.0261880794 0.007481251 2.034551e-02
## 2017-08-31 0.0093691971 0.0232436720 -0.0004485032 -0.027564366 2.913587e-03
## 2017-09-29 -0.0057323849 -0.0004460965 0.0233428450 0.082321713 1.994921e-02
## 2017-10-31 0.0009784572 0.0322783753 0.0166535960 0.005915744 2.329075e-02
## 2017-11-30 -0.0014842394 -0.0038969524 0.0068700389 0.036913515 3.010794e-02
## 2017-12-29 0.0047399854 0.0369253964 0.0133982053 -0.003731578 1.205506e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398392e-05 0.0001042101 4.178286e-05 -7.812083e-05 -9.031188e-06
## EEM 1.042101e-04 0.0017547037 1.039015e-03 6.437691e-04 6.795399e-04
## EFA 4.178286e-05 0.0010390148 1.064238e-03 6.490280e-04 6.975393e-04
## IJS -7.812083e-05 0.0006437691 6.490280e-04 1.565447e-03 8.290247e-04
## SPY -9.031188e-06 0.0006795399 6.975393e-04 8.290247e-04 7.408286e-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.02347487
# 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.0003874121 0.009257123 0.005815635 0.005684456 0.002330248
rowSums(component_contribution)
## [1] 0.02347487
# 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.0062305625 -0.0029356841 0.0366064017 0.052133087 4.992299e-02
## 2013-02-28 0.0058904111 -0.0231050093 -0.0129693872 0.016174927 1.267861e-02
## 2013-03-28 0.0009846251 -0.0102350096 0.0129693872 0.040258110 3.726760e-02
## 2013-04-30 0.0096394882 0.0120845638 0.0489677225 0.001222568 1.903001e-02
## 2013-05-31 -0.0202136587 -0.0494830269 -0.0306554215 0.041976878 2.333573e-02
## 2013-06-28 -0.0157782721 -0.0547286224 -0.0271444699 -0.001402849 -1.343434e-02
## 2013-07-31 0.0026874686 0.0131595131 0.0518602560 0.063540876 5.038576e-02
## 2013-08-30 -0.0082974388 -0.0257052994 -0.0197464655 -0.034743000 -3.045160e-02
## 2013-09-30 0.0111434058 0.0695887138 0.0753385935 0.063873280 3.115578e-02
## 2013-10-31 0.0082922156 0.0408610696 0.0320818776 0.034234198 4.526713e-02
## 2013-11-29 -0.0025101740 -0.0025938002 0.0054493836 0.041661174 2.920693e-02
## 2013-12-31 -0.0055835584 -0.0040744757 0.0215282903 0.012891802 2.559607e-02
## 2014-01-31 0.0152922737 -0.0903224345 -0.0534133799 -0.035775083 -3.588433e-02
## 2014-02-28 0.0037569429 0.0332202922 0.0595050852 0.045257415 4.451019e-02
## 2014-03-31 -0.0014815363 0.0380218224 -0.0046024888 0.013315354 8.261513e-03
## 2014-04-30 0.0081831074 0.0077725112 0.0165291395 -0.023184358 6.927176e-03
## 2014-05-30 0.0117215956 0.0290913104 0.0158286510 0.006205544 2.294128e-02
## 2014-06-30 -0.0005756678 0.0237339463 0.0091652585 0.037718328 2.043450e-02
## 2014-07-31 -0.0025120175 0.0135555745 -0.0263797056 -0.052009458 -1.352855e-02
## 2014-08-29 0.0114305854 0.0279044760 0.0018004225 0.043658057 3.870483e-02
## 2014-09-30 -0.0061676573 -0.0808563659 -0.0395985696 -0.061260214 -1.389237e-02
## 2014-10-31 0.0105848003 0.0140962266 -0.0026548113 0.068874574 2.327779e-02
## 2014-11-28 0.0065488072 -0.0155412998 0.0006253892 0.004773795 2.710122e-02
## 2014-12-31 0.0014749701 -0.0404421167 -0.0407468928 0.025295828 -2.539574e-03
## 2015-01-30 0.0203154034 -0.0068951980 0.0062269162 -0.054628126 -3.007717e-02
## 2015-02-27 -0.0089881593 0.0431357191 0.0614504432 0.056915018 5.468207e-02
## 2015-03-31 0.0037404818 -0.0150860189 -0.0143889389 0.010156013 -1.583045e-02
## 2015-04-30 -0.0032333567 0.0662808544 0.0358166578 -0.018417442 9.785953e-03
## 2015-05-29 -0.0043839103 -0.0419108018 0.0019527233 0.007509723 1.277414e-02
## 2015-06-30 -0.0108252523 -0.0297464970 -0.0316787449 0.004171221 -2.052119e-02
## 2015-07-31 0.0085850003 -0.0651782508 0.0201142502 -0.027375369 2.233780e-02
## 2015-08-31 -0.0033640124 -0.0925122045 -0.0771524098 -0.047268429 -6.288651e-02
## 2015-09-30 0.0080813414 -0.0318249895 -0.0451946379 -0.038464471 -2.584723e-02
## 2015-10-30 0.0006858486 0.0618082997 0.0640257952 0.063589474 8.163488e-02
## 2015-11-30 -0.0038983967 -0.0255604013 -0.0075558793 0.024415727 3.648705e-03
## 2015-12-31 -0.0019188551 -0.0389473124 -0.0235951109 -0.052157295 -1.743365e-02
## 2016-01-29 0.0123293338 -0.0516365338 -0.0567578198 -0.060307033 -5.106893e-02
## 2016-02-29 0.0088320962 -0.0082114542 -0.0339140842 0.020605629 -8.259359e-04
## 2016-03-31 0.0087089773 0.1218787676 0.0637458709 0.089910231 6.510015e-02
## 2016-04-29 0.0025460615 0.0040792829 0.0219750985 0.021044231 3.933308e-03
## 2016-05-31 0.0001352208 -0.0376283723 -0.0008558781 0.004397115 1.686847e-02
## 2016-06-30 0.0191666114 0.0445821345 -0.0244917902 0.008291953 3.469801e-03
## 2016-07-29 0.0054298399 0.0524423508 0.0390002829 0.049348456 3.582191e-02
## 2016-08-31 -0.0021559449 0.0087985997 0.0053266982 0.011261250 1.196978e-03
## 2016-09-30 0.0005159036 0.0248727505 0.0132791607 0.008614528 5.813593e-05
## 2016-10-31 -0.0082052164 -0.0083120689 -0.0224035044 -0.038134718 -1.748905e-02
## 2016-11-30 -0.0259898863 -0.0451619574 -0.0179744820 0.125246484 3.617606e-02
## 2016-12-30 0.0025381850 -0.0025298821 0.0267027783 0.031491673 2.006909e-02
## 2017-01-31 0.0021258727 0.0644316316 0.0323819590 -0.012144032 1.773638e-02
## 2017-02-28 0.0064380351 0.0172575387 0.0118364711 0.013428625 3.853926e-02
## 2017-03-31 -0.0005526225 0.0361889862 0.0318056345 -0.006532649 1.249013e-03
## 2017-04-28 0.0090290275 0.0168663993 0.0239523650 0.005107588 9.877382e-03
## 2017-05-31 0.0068476720 0.0280600953 0.0348101105 -0.022862437 1.401416e-02
## 2017-06-30 -0.0001830138 0.0092236560 0.0029558548 0.029151683 6.354832e-03
## 2017-07-31 0.0033344876 0.0565945416 0.0261880794 0.007481251 2.034551e-02
## 2017-08-31 0.0093691971 0.0232436720 -0.0004485032 -0.027564366 2.913587e-03
## 2017-09-29 -0.0057323849 -0.0004460965 0.0233428450 0.082321713 1.994921e-02
## 2017-10-31 0.0009784572 0.0322783753 0.0166535960 0.005915744 2.329075e-02
## 2017-11-30 -0.0014842394 -0.0038969524 0.0068700389 0.036913515 3.010794e-02
## 2017-12-29 0.0047399854 0.0369253964 0.0133982053 -0.003731578 1.205506e-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()
component_percentages
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 )) %>%
# Tranform 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 = .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 )) %>%
# Tranform 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 = .5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
x = NULL)