# 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
w <- c(0.25, 0.25, 0.2, 0.2, 0.1)
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.0062299911 -0.0029355225 0.0366062187 0.052133568 4.992286e-02
## 2013-02-28 0.0058909790 -0.0231054489 -0.0129692933 0.016175326 1.267828e-02
## 2013-03-28 0.0009843125 -0.0102348493 0.0129692933 0.040257907 3.726818e-02
## 2013-04-30 0.0096394967 0.0120848727 0.0489677002 0.001222933 1.902967e-02
## 2013-05-31 -0.0202136673 -0.0494835190 -0.0306553983 0.041975929 2.333573e-02
## 2013-06-28 -0.0157787288 -0.0547284306 -0.0271446024 -0.001402578 -1.343432e-02
## 2013-07-31 0.0026884721 0.0131598871 0.0518605081 0.063541275 5.038612e-02
## 2013-08-30 -0.0082988396 -0.0257056503 -0.0197461382 -0.034743355 -3.045201e-02
## 2013-09-30 0.0111443986 0.0695886222 0.0753383440 0.063873288 3.115684e-02
## 2013-10-31 0.0082917047 0.0408613553 0.0320815623 0.034234263 4.526602e-02
## 2013-11-29 -0.0025096595 -0.0025939391 0.0054497174 0.041660892 2.920701e-02
## 2013-12-31 -0.0055835959 -0.0040742859 0.0215280565 0.012892367 2.559636e-02
## 2014-01-31 0.0152921333 -0.0903226875 -0.0534133484 -0.035775163 -3.588433e-02
## 2014-02-28 0.0037562741 0.0332204857 0.0595051678 0.045257302 4.450975e-02
## 2014-03-31 -0.0014809674 0.0380217767 -0.0046026528 0.013315262 8.261307e-03
## 2014-04-30 0.0081829920 0.0077726702 0.0165293656 -0.023184350 6.927570e-03
## 2014-05-30 0.0117217396 0.0290911643 0.0158285362 0.006205093 2.294177e-02
## 2014-06-30 -0.0005757290 0.0237339145 0.0091652337 0.037719296 2.043444e-02
## 2014-07-31 -0.0025119782 0.0135556515 -0.0263797134 -0.052009818 -1.352866e-02
## 2014-08-29 0.0114313430 0.0279045902 0.0018004513 0.043657794 3.870452e-02
## 2014-09-30 -0.0061676573 -0.0808567603 -0.0395984360 -0.061260238 -1.389245e-02
## 2014-10-31 0.0105842525 0.0140963579 -0.0026549205 0.068874722 2.327765e-02
## 2014-11-28 0.0065491332 -0.0155409921 0.0006254062 0.004773481 2.710175e-02
## 2014-12-31 0.0014745512 -0.0404421420 -0.0407469344 0.025296066 -2.539781e-03
## 2015-01-30 0.0203149380 -0.0068957078 0.0062266243 -0.054627938 -3.007737e-02
## 2015-02-27 -0.0089881218 0.0431361914 0.0614507897 0.056914486 5.468213e-02
## 2015-03-31 0.0037405529 -0.0150865552 -0.0143889810 0.010156634 -1.583037e-02
## 2015-04-30 -0.0032328490 0.0662816900 0.0358168168 -0.018417979 9.785957e-03
## 2015-05-29 -0.0043839952 -0.0419113143 0.0019524656 0.007510182 1.277428e-02
## 2015-06-30 -0.0108253321 -0.0297465700 -0.0316786910 0.004171212 -2.052138e-02
## 2015-07-31 0.0085850486 -0.0651782959 0.0201142965 -0.027375583 2.233805e-02
## 2015-08-31 -0.0033636964 -0.0925120851 -0.0771522632 -0.047268395 -6.288694e-02
## 2015-09-30 0.0080815125 -0.0318250965 -0.0451950823 -0.038464343 -2.584687e-02
## 2015-10-30 0.0006847699 0.0618083585 0.0640260369 0.063589548 8.163460e-02
## 2015-11-30 -0.0038978613 -0.0255603113 -0.0075560082 0.024415179 3.648704e-03
## 2015-12-31 -0.0019193319 -0.0389472280 -0.0235950075 -0.052156958 -1.743358e-02
## 2016-01-29 0.0123302609 -0.0516368496 -0.0567575689 -0.060306763 -5.106871e-02
## 2016-02-29 0.0088312156 -0.0082113418 -0.0339140694 0.020605342 -8.261624e-04
## 2016-03-31 0.0087087730 0.1218787042 0.0637457369 0.089910267 6.510032e-02
## 2016-04-29 0.0025467244 0.0040794844 0.0219748493 0.021044224 3.933350e-03
## 2016-05-31 0.0001350530 -0.0376284039 -0.0008557669 0.004396924 1.686849e-02
## 2016-06-30 0.0191670541 0.0445821242 -0.0244916201 0.008292547 3.469839e-03
## 2016-07-29 0.0054296017 0.0524422191 0.0390002019 0.049348062 3.582166e-02
## 2016-08-31 -0.0021566273 0.0087985246 0.0053270974 0.011261188 1.196828e-03
## 2016-09-30 0.0005161425 0.0248733420 0.0132788342 0.008614565 5.823845e-05
## 2016-10-31 -0.0082054621 -0.0083124789 -0.0224037110 -0.038134631 -1.748900e-02
## 2016-11-30 -0.0259889702 -0.0451622182 -0.0179743174 0.125246235 3.617606e-02
## 2016-12-30 0.0025378090 -0.0025297631 0.0267029806 0.031491797 2.006902e-02
## 2017-01-31 0.0021259098 0.0644316549 0.0323817487 -0.012143932 1.773644e-02
## 2017-02-28 0.0064382802 0.0172577360 0.0118362139 0.013428702 3.853925e-02
## 2017-03-31 -0.0005529595 0.0361892048 0.0318060644 -0.006532664 1.249161e-03
## 2017-04-28 0.0090289315 0.0168659495 0.0239518946 0.005107631 9.877484e-03
## 2017-05-31 0.0068475856 0.0280596962 0.0348102306 -0.022862847 1.401403e-02
## 2017-06-30 -0.0001824910 0.0092240164 0.0029558911 0.029151722 6.354643e-03
## 2017-07-31 0.0033339971 0.0565946505 0.0261879828 0.007481599 2.034591e-02
## 2017-08-31 0.0093693981 0.0232438540 -0.0004482007 -0.027564765 2.913595e-03
## 2017-09-29 -0.0057323540 -0.0004463429 0.0233428848 0.082321864 1.994898e-02
## 2017-10-31 0.0009780530 0.0322785780 0.0166535251 0.005916226 2.329060e-02
## 2017-11-30 -0.0014836872 -0.0038970336 0.0068700179 0.036913347 3.010809e-02
## 2017-12-29 0.0047403245 0.0369255364 0.0133983620 -0.003731404 1.205507e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398405e-05 0.0001042097 4.178224e-05 -0.0000781173 -9.030386e-06
## EEM 1.042097e-04 0.0017547124 1.039017e-03 0.0006437709 6.795429e-04
## EFA 4.178224e-05 0.0010390174 1.064237e-03 0.0006490278 6.975405e-04
## IJS -7.811730e-05 0.0006437709 6.490278e-04 0.0015654464 8.290239e-04
## SPY -9.030386e-06 0.0006795429 6.975405e-04 0.0008290239 7.408295e-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.0003874178 0.009257146 0.005815632 0.005684459 0.00233025
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.0062299911 -0.0029355225 0.0366062187 0.052133568 4.992286e-02
## 2013-02-28 0.0058909790 -0.0231054489 -0.0129692933 0.016175326 1.267828e-02
## 2013-03-28 0.0009843125 -0.0102348493 0.0129692933 0.040257907 3.726818e-02
## 2013-04-30 0.0096394967 0.0120848727 0.0489677002 0.001222933 1.902967e-02
## 2013-05-31 -0.0202136673 -0.0494835190 -0.0306553983 0.041975929 2.333573e-02
## 2013-06-28 -0.0157787288 -0.0547284306 -0.0271446024 -0.001402578 -1.343432e-02
## 2013-07-31 0.0026884721 0.0131598871 0.0518605081 0.063541275 5.038612e-02
## 2013-08-30 -0.0082988396 -0.0257056503 -0.0197461382 -0.034743355 -3.045201e-02
## 2013-09-30 0.0111443986 0.0695886222 0.0753383440 0.063873288 3.115684e-02
## 2013-10-31 0.0082917047 0.0408613553 0.0320815623 0.034234263 4.526602e-02
## 2013-11-29 -0.0025096595 -0.0025939391 0.0054497174 0.041660892 2.920701e-02
## 2013-12-31 -0.0055835959 -0.0040742859 0.0215280565 0.012892367 2.559636e-02
## 2014-01-31 0.0152921333 -0.0903226875 -0.0534133484 -0.035775163 -3.588433e-02
## 2014-02-28 0.0037562741 0.0332204857 0.0595051678 0.045257302 4.450975e-02
## 2014-03-31 -0.0014809674 0.0380217767 -0.0046026528 0.013315262 8.261307e-03
## 2014-04-30 0.0081829920 0.0077726702 0.0165293656 -0.023184350 6.927570e-03
## 2014-05-30 0.0117217396 0.0290911643 0.0158285362 0.006205093 2.294177e-02
## 2014-06-30 -0.0005757290 0.0237339145 0.0091652337 0.037719296 2.043444e-02
## 2014-07-31 -0.0025119782 0.0135556515 -0.0263797134 -0.052009818 -1.352866e-02
## 2014-08-29 0.0114313430 0.0279045902 0.0018004513 0.043657794 3.870452e-02
## 2014-09-30 -0.0061676573 -0.0808567603 -0.0395984360 -0.061260238 -1.389245e-02
## 2014-10-31 0.0105842525 0.0140963579 -0.0026549205 0.068874722 2.327765e-02
## 2014-11-28 0.0065491332 -0.0155409921 0.0006254062 0.004773481 2.710175e-02
## 2014-12-31 0.0014745512 -0.0404421420 -0.0407469344 0.025296066 -2.539781e-03
## 2015-01-30 0.0203149380 -0.0068957078 0.0062266243 -0.054627938 -3.007737e-02
## 2015-02-27 -0.0089881218 0.0431361914 0.0614507897 0.056914486 5.468213e-02
## 2015-03-31 0.0037405529 -0.0150865552 -0.0143889810 0.010156634 -1.583037e-02
## 2015-04-30 -0.0032328490 0.0662816900 0.0358168168 -0.018417979 9.785957e-03
## 2015-05-29 -0.0043839952 -0.0419113143 0.0019524656 0.007510182 1.277428e-02
## 2015-06-30 -0.0108253321 -0.0297465700 -0.0316786910 0.004171212 -2.052138e-02
## 2015-07-31 0.0085850486 -0.0651782959 0.0201142965 -0.027375583 2.233805e-02
## 2015-08-31 -0.0033636964 -0.0925120851 -0.0771522632 -0.047268395 -6.288694e-02
## 2015-09-30 0.0080815125 -0.0318250965 -0.0451950823 -0.038464343 -2.584687e-02
## 2015-10-30 0.0006847699 0.0618083585 0.0640260369 0.063589548 8.163460e-02
## 2015-11-30 -0.0038978613 -0.0255603113 -0.0075560082 0.024415179 3.648704e-03
## 2015-12-31 -0.0019193319 -0.0389472280 -0.0235950075 -0.052156958 -1.743358e-02
## 2016-01-29 0.0123302609 -0.0516368496 -0.0567575689 -0.060306763 -5.106871e-02
## 2016-02-29 0.0088312156 -0.0082113418 -0.0339140694 0.020605342 -8.261624e-04
## 2016-03-31 0.0087087730 0.1218787042 0.0637457369 0.089910267 6.510032e-02
## 2016-04-29 0.0025467244 0.0040794844 0.0219748493 0.021044224 3.933350e-03
## 2016-05-31 0.0001350530 -0.0376284039 -0.0008557669 0.004396924 1.686849e-02
## 2016-06-30 0.0191670541 0.0445821242 -0.0244916201 0.008292547 3.469839e-03
## 2016-07-29 0.0054296017 0.0524422191 0.0390002019 0.049348062 3.582166e-02
## 2016-08-31 -0.0021566273 0.0087985246 0.0053270974 0.011261188 1.196828e-03
## 2016-09-30 0.0005161425 0.0248733420 0.0132788342 0.008614565 5.823845e-05
## 2016-10-31 -0.0082054621 -0.0083124789 -0.0224037110 -0.038134631 -1.748900e-02
## 2016-11-30 -0.0259889702 -0.0451622182 -0.0179743174 0.125246235 3.617606e-02
## 2016-12-30 0.0025378090 -0.0025297631 0.0267029806 0.031491797 2.006902e-02
## 2017-01-31 0.0021259098 0.0644316549 0.0323817487 -0.012143932 1.773644e-02
## 2017-02-28 0.0064382802 0.0172577360 0.0118362139 0.013428702 3.853925e-02
## 2017-03-31 -0.0005529595 0.0361892048 0.0318060644 -0.006532664 1.249161e-03
## 2017-04-28 0.0090289315 0.0168659495 0.0239518946 0.005107631 9.877484e-03
## 2017-05-31 0.0068475856 0.0280596962 0.0348102306 -0.022862847 1.401403e-02
## 2017-06-30 -0.0001824910 0.0092240164 0.0029558911 0.029151722 6.354643e-03
## 2017-07-31 0.0033339971 0.0565946505 0.0261879828 0.007481599 2.034591e-02
## 2017-08-31 0.0093693981 0.0232438540 -0.0004482007 -0.027564765 2.913595e-03
## 2017-09-29 -0.0057323540 -0.0004463429 0.0233428848 0.082321864 1.994898e-02
## 2017-10-31 0.0009780530 0.0322785780 0.0166535251 0.005916226 2.329060e-02
## 2017-11-30 -0.0014836872 -0.0038970336 0.0068700179 0.036913347 3.010809e-02
## 2017-12-29 0.0047403245 0.0369255364 0.0133983620 -0.003731404 1.205507e-02
calculate_componet_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_componet_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
Colunn Chart of Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_componet_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_componet_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)