# 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.0062307930 -0.0029355222 0.0366063155 0.052132992 4.992313e-02
## 2013-02-28 0.0058906774 -0.0231052093 -0.0129694881 0.016175328 1.267796e-02
## 2013-03-28 0.0009853151 -0.0102350252 0.0129694881 0.040258120 3.726849e-02
## 2013-04-30 0.0096388933 0.0120848115 0.0489674191 0.001222724 1.902991e-02
## 2013-05-31 -0.0202135619 -0.0494836372 -0.0306550239 0.041976434 2.333502e-02
## 2013-06-28 -0.0157788269 -0.0547283650 -0.0271445972 -0.001403378 -1.343373e-02
## 2013-07-31 0.0026881636 0.0131595625 0.0518603128 0.063541394 5.038554e-02
## 2013-08-30 -0.0082985299 -0.0257055242 -0.0197462328 -0.034743462 -3.045167e-02
## 2013-09-30 0.0111438845 0.0695888170 0.0753383508 0.063873951 3.115605e-02
## 2013-10-31 0.0082922148 0.0408612933 0.0320816500 0.034234078 4.526724e-02
## 2013-11-29 -0.0025093537 -0.0025937601 0.0054497174 0.041660888 2.920668e-02
## 2013-12-31 -0.0055835931 -0.0040746445 0.0215281391 0.012892199 2.559626e-02
## 2014-01-31 0.0152916216 -0.0903225733 -0.0534133439 -0.035775253 -3.588443e-02
## 2014-02-28 0.0037566757 0.0332204245 0.0595049984 0.045257392 4.450995e-02
## 2014-03-31 -0.0014812684 0.0380219646 -0.0046025706 0.013315263 8.261406e-03
## 2014-04-30 0.0081834899 0.0077726091 0.0165292844 -0.023184185 6.927568e-03
## 2014-05-30 0.0117212398 0.0290909289 0.0158286973 0.006205092 2.294137e-02
## 2014-06-30 -0.0005754330 0.0237341499 0.0091651538 0.037718653 2.043454e-02
## 2014-07-31 -0.0025124716 0.0135557649 -0.0263797134 -0.052009258 -1.352895e-02
## 2014-08-29 0.0114307576 0.0279045871 0.0018002890 0.043657956 3.870463e-02
## 2014-09-30 -0.0061675632 -0.0808566913 -0.0395984426 -0.061260142 -1.389227e-02
## 2014-10-31 0.0105849400 0.0140965322 -0.0026549209 0.068874705 2.327784e-02
## 2014-11-28 0.0065485524 -0.0155414654 0.0006254909 0.004773640 2.710175e-02
## 2014-12-31 0.0014752277 -0.0404421469 -0.0407465854 0.025295591 -2.540051e-03
## 2015-01-30 0.0203148396 -0.0068955831 0.0062262723 -0.054628024 -3.007700e-02
## 2015-02-27 -0.0089881209 0.0431360712 0.0614508773 0.056914650 5.468194e-02
## 2015-03-31 0.0037406476 -0.0150864350 -0.0143888974 0.010156557 -1.583010e-02
## 2015-04-30 -0.0032327530 0.0662814616 0.0358168139 -0.018417746 9.785600e-03
## 2015-05-29 -0.0043835148 -0.0419110858 0.0019523045 0.007509638 1.277428e-02
## 2015-06-30 -0.0108260018 -0.0297465700 -0.0316787766 0.004171446 -2.052103e-02
## 2015-07-31 0.0085842794 -0.0651781650 0.0201146254 -0.027375349 2.233787e-02
## 2015-08-31 -0.0033636987 -0.0925120724 -0.0771526018 -0.047268059 -6.288693e-02
## 2015-09-30 0.0080819958 -0.0318252402 -0.0451948145 -0.038465019 -2.584725e-02
## 2015-10-30 0.0006848655 0.0618083585 0.0640258586 0.063589726 8.163532e-02
## 2015-11-30 -0.0038979576 -0.0255605259 -0.0075555741 0.024415418 3.648265e-03
## 2015-12-31 -0.0019188519 -0.0389470134 -0.0235951772 -0.052157033 -1.743340e-02
## 2016-01-29 0.0123299726 -0.0516366931 -0.0567578412 -0.060306846 -5.106880e-02
## 2016-02-29 0.0088316860 -0.0082117352 -0.0339140726 0.020605082 -8.265373e-04
## 2016-03-31 0.0087086756 0.1218792206 0.0637456515 0.089910369 6.510068e-02
## 2016-04-29 0.0025465375 0.0040792049 0.0219753006 0.021044150 3.933088e-03
## 2016-05-31 0.0001350530 -0.0376284762 -0.0008560353 0.004397312 1.686875e-02
## 2016-06-30 0.0191671418 0.0445824038 -0.0244915284 0.008292162 3.469925e-03
## 2016-07-29 0.0054288743 0.0524421429 0.0390001101 0.049348292 3.582166e-02
## 2016-08-31 -0.0021560827 0.0087985234 0.0053267463 0.011261332 1.197076e-03
## 2016-09-30 0.0005158697 0.0248725778 0.0132793585 0.008614707 5.766018e-05
## 2016-10-31 -0.0082051887 -0.0083119088 -0.0224037957 -0.038134992 -1.748892e-02
## 2016-11-30 -0.0259893440 -0.0451616191 -0.0179744060 0.125246506 3.617599e-02
## 2016-12-30 0.0025383726 -0.0025300970 0.0267028928 0.031491600 2.006918e-02
## 2017-01-31 0.0021258156 0.0644313279 0.0323820065 -0.012143867 1.773652e-02
## 2017-02-28 0.0064378139 0.0172579235 0.0118362119 0.013428510 3.853932e-02
## 2017-03-31 -0.0005532389 0.0361890238 0.0318056523 -0.006532664 1.249160e-03
## 2017-04-28 0.0090291210 0.0168663031 0.0239524566 0.005107505 9.877408e-03
## 2017-05-31 0.0068482294 0.0280598037 0.0348099128 -0.022862787 1.401403e-02
## 2017-06-30 -0.0001833156 0.0092237874 0.0029561971 0.029152233 6.354861e-03
## 2017-07-31 0.0033348208 0.0565945375 0.0261878258 0.007481284 2.034562e-02
## 2017-08-31 0.0093693956 0.0232438540 -0.0004483498 -0.027564636 2.913452e-03
## 2017-09-29 -0.0057325344 -0.0004464472 0.0233426662 0.082322210 1.994905e-02
## 2017-10-31 0.0009782347 0.0322787833 0.0166538153 0.005915634 2.329081e-02
## 2017-11-30 -0.0014841419 -0.0038972359 0.0068701598 0.036913010 3.010802e-02
## 2017-12-29 0.0047399630 0.0369254424 0.0133982889 -0.003730720 1.205494e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398391e-05 0.0001042097 4.178097e-05 -7.811861e-05 -9.031048e-06
## EEM 1.042097e-04 0.0017547111 1.039016e-03 6.437726e-04 6.795432e-04
## EFA 4.178097e-05 0.0010390159 1.064237e-03 6.490305e-04 6.975419e-04
## IJS -7.811861e-05 0.0006437726 6.490305e-04 1.565450e-03 8.290249e-04
## SPY -9.031048e-06 0.0006795432 6.975419e-04 8.290249e-04 7.408313e-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.0003874114 0.009257143 0.00581563 0.00568447 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.0062307930 -0.0029355222 0.0366063155 0.052132992 4.992313e-02
## 2013-02-28 0.0058906774 -0.0231052093 -0.0129694881 0.016175328 1.267796e-02
## 2013-03-28 0.0009853151 -0.0102350252 0.0129694881 0.040258120 3.726849e-02
## 2013-04-30 0.0096388933 0.0120848115 0.0489674191 0.001222724 1.902991e-02
## 2013-05-31 -0.0202135619 -0.0494836372 -0.0306550239 0.041976434 2.333502e-02
## 2013-06-28 -0.0157788269 -0.0547283650 -0.0271445972 -0.001403378 -1.343373e-02
## 2013-07-31 0.0026881636 0.0131595625 0.0518603128 0.063541394 5.038554e-02
## 2013-08-30 -0.0082985299 -0.0257055242 -0.0197462328 -0.034743462 -3.045167e-02
## 2013-09-30 0.0111438845 0.0695888170 0.0753383508 0.063873951 3.115605e-02
## 2013-10-31 0.0082922148 0.0408612933 0.0320816500 0.034234078 4.526724e-02
## 2013-11-29 -0.0025093537 -0.0025937601 0.0054497174 0.041660888 2.920668e-02
## 2013-12-31 -0.0055835931 -0.0040746445 0.0215281391 0.012892199 2.559626e-02
## 2014-01-31 0.0152916216 -0.0903225733 -0.0534133439 -0.035775253 -3.588443e-02
## 2014-02-28 0.0037566757 0.0332204245 0.0595049984 0.045257392 4.450995e-02
## 2014-03-31 -0.0014812684 0.0380219646 -0.0046025706 0.013315263 8.261406e-03
## 2014-04-30 0.0081834899 0.0077726091 0.0165292844 -0.023184185 6.927568e-03
## 2014-05-30 0.0117212398 0.0290909289 0.0158286973 0.006205092 2.294137e-02
## 2014-06-30 -0.0005754330 0.0237341499 0.0091651538 0.037718653 2.043454e-02
## 2014-07-31 -0.0025124716 0.0135557649 -0.0263797134 -0.052009258 -1.352895e-02
## 2014-08-29 0.0114307576 0.0279045871 0.0018002890 0.043657956 3.870463e-02
## 2014-09-30 -0.0061675632 -0.0808566913 -0.0395984426 -0.061260142 -1.389227e-02
## 2014-10-31 0.0105849400 0.0140965322 -0.0026549209 0.068874705 2.327784e-02
## 2014-11-28 0.0065485524 -0.0155414654 0.0006254909 0.004773640 2.710175e-02
## 2014-12-31 0.0014752277 -0.0404421469 -0.0407465854 0.025295591 -2.540051e-03
## 2015-01-30 0.0203148396 -0.0068955831 0.0062262723 -0.054628024 -3.007700e-02
## 2015-02-27 -0.0089881209 0.0431360712 0.0614508773 0.056914650 5.468194e-02
## 2015-03-31 0.0037406476 -0.0150864350 -0.0143888974 0.010156557 -1.583010e-02
## 2015-04-30 -0.0032327530 0.0662814616 0.0358168139 -0.018417746 9.785600e-03
## 2015-05-29 -0.0043835148 -0.0419110858 0.0019523045 0.007509638 1.277428e-02
## 2015-06-30 -0.0108260018 -0.0297465700 -0.0316787766 0.004171446 -2.052103e-02
## 2015-07-31 0.0085842794 -0.0651781650 0.0201146254 -0.027375349 2.233787e-02
## 2015-08-31 -0.0033636987 -0.0925120724 -0.0771526018 -0.047268059 -6.288693e-02
## 2015-09-30 0.0080819958 -0.0318252402 -0.0451948145 -0.038465019 -2.584725e-02
## 2015-10-30 0.0006848655 0.0618083585 0.0640258586 0.063589726 8.163532e-02
## 2015-11-30 -0.0038979576 -0.0255605259 -0.0075555741 0.024415418 3.648265e-03
## 2015-12-31 -0.0019188519 -0.0389470134 -0.0235951772 -0.052157033 -1.743340e-02
## 2016-01-29 0.0123299726 -0.0516366931 -0.0567578412 -0.060306846 -5.106880e-02
## 2016-02-29 0.0088316860 -0.0082117352 -0.0339140726 0.020605082 -8.265373e-04
## 2016-03-31 0.0087086756 0.1218792206 0.0637456515 0.089910369 6.510068e-02
## 2016-04-29 0.0025465375 0.0040792049 0.0219753006 0.021044150 3.933088e-03
## 2016-05-31 0.0001350530 -0.0376284762 -0.0008560353 0.004397312 1.686875e-02
## 2016-06-30 0.0191671418 0.0445824038 -0.0244915284 0.008292162 3.469925e-03
## 2016-07-29 0.0054288743 0.0524421429 0.0390001101 0.049348292 3.582166e-02
## 2016-08-31 -0.0021560827 0.0087985234 0.0053267463 0.011261332 1.197076e-03
## 2016-09-30 0.0005158697 0.0248725778 0.0132793585 0.008614707 5.766018e-05
## 2016-10-31 -0.0082051887 -0.0083119088 -0.0224037957 -0.038134992 -1.748892e-02
## 2016-11-30 -0.0259893440 -0.0451616191 -0.0179744060 0.125246506 3.617599e-02
## 2016-12-30 0.0025383726 -0.0025300970 0.0267028928 0.031491600 2.006918e-02
## 2017-01-31 0.0021258156 0.0644313279 0.0323820065 -0.012143867 1.773652e-02
## 2017-02-28 0.0064378139 0.0172579235 0.0118362119 0.013428510 3.853932e-02
## 2017-03-31 -0.0005532389 0.0361890238 0.0318056523 -0.006532664 1.249160e-03
## 2017-04-28 0.0090291210 0.0168663031 0.0239524566 0.005107505 9.877408e-03
## 2017-05-31 0.0068482294 0.0280598037 0.0348099128 -0.022862787 1.401403e-02
## 2017-06-30 -0.0001833156 0.0092237874 0.0029561971 0.029152233 6.354861e-03
## 2017-07-31 0.0033348208 0.0565945375 0.0261878258 0.007481284 2.034562e-02
## 2017-08-31 0.0093693956 0.0232438540 -0.0004483498 -0.027564636 2.913452e-03
## 2017-09-29 -0.0057325344 -0.0004464472 0.0233426662 0.082322210 1.994905e-02
## 2017-10-31 0.0009782347 0.0322787833 0.0166538153 0.005915634 2.329081e-02
## 2017-11-30 -0.0014841419 -0.0038972359 0.0068701598 0.036913010 3.010802e-02
## 2017-12-29 0.0047399630 0.0369254424 0.0133982889 -0.003730720 1.205494e-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
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")
plot_data_weights <- 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_weights %>%
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_tq() +
theme(plot.title = element_text(hjust = 0.5)) +
labs(
title = "Percent Contribution to Portfolio Volatility and Weight",
y = "Percent",
x = NULL
)