# 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.0062316770 -0.0029352585 0.0366063129 0.052133610 4.992301e-02
## 2013-02-28 0.0058913990 -0.0231055723 -0.0129692845 0.016175327 1.267769e-02
## 2013-03-28 0.0009851750 -0.0102351142 0.0129692845 0.040258016 3.726849e-02
## 2013-04-30 0.0096389334 0.0120847780 0.0489675324 0.001222623 1.902976e-02
## 2013-05-31 -0.0202138087 -0.0494833466 -0.0306556875 0.041976197 2.333527e-02
## 2013-06-28 -0.0157784109 -0.0547282154 -0.0271442523 -0.001402900 -1.343423e-02
## 2013-07-31 0.0026871430 0.0131597961 0.0518601267 0.063541402 5.038618e-02
## 2013-08-30 -0.0082969506 -0.0257057675 -0.0197461540 -0.034743647 -3.045141e-02
## 2013-09-30 0.0111434935 0.0695888937 0.0753387805 0.063873943 3.115672e-02
## 2013-10-31 0.0082913390 0.0408610589 0.0320814055 0.034233911 4.526600e-02
## 2013-11-29 -0.0025091469 -0.0025941934 0.0054497637 0.041661050 2.920668e-02
## 2013-12-31 -0.0055828469 -0.0040743575 0.0215280874 0.012892029 2.559629e-02
## 2014-01-31 0.0152909826 -0.0903222373 -0.0534135169 -0.035775271 -3.588463e-02
## 2014-02-28 0.0037571032 0.0332204176 0.0595052511 0.045257537 4.451058e-02
## 2014-03-31 -0.0014812591 0.0380213991 -0.0046026525 0.013315071 8.261361e-03
## 2014-04-30 0.0081827508 0.0077730322 0.0165295008 -0.023183867 6.927376e-03
## 2014-05-30 0.0117214271 0.0290910768 0.0158282878 0.006205217 2.294102e-02
## 2014-06-30 -0.0005754068 0.0237341703 0.0091657226 0.037718726 2.043454e-02
## 2014-07-31 -0.0025122248 0.0135553522 -0.0263801196 -0.052009515 -1.352822e-02
## 2014-08-29 0.0114312325 0.0279047188 0.0018006337 0.043657710 3.870454e-02
## 2014-09-30 -0.0061674624 -0.0808568967 -0.0395986967 -0.061260281 -1.389248e-02
## 2014-10-31 0.0105842956 0.0140965746 -0.0026546882 0.068874891 2.327777e-02
## 2014-11-28 0.0065484990 -0.0155412100 0.0006252476 0.004773526 2.710134e-02
## 2014-12-31 0.0014751707 -0.0404420568 -0.0407468997 0.025295995 -2.539817e-03
## 2015-01-30 0.0203156428 -0.0068956344 0.0062265367 -0.054627945 -3.007669e-02
## 2015-02-27 -0.0089885593 0.0431361369 0.0614507523 0.056914601 5.468192e-02
## 2015-03-31 0.0037404940 -0.0150866582 -0.0143887121 0.010156500 -1.583061e-02
## 2015-04-30 -0.0032330085 0.0662814793 0.0358165763 -0.018417597 9.786175e-03
## 2015-05-29 -0.0043840420 -0.0419110434 0.0019526334 0.007509901 1.277396e-02
## 2015-06-30 -0.0108254837 -0.0297466489 -0.0316790005 0.004171290 -2.052116e-02
## 2015-07-31 0.0085851669 -0.0651780903 0.0201145758 -0.027375722 2.233800e-02
## 2015-08-31 -0.0033640146 -0.0925121983 -0.0771525048 -0.047268195 -6.288702e-02
## 2015-09-30 0.0080814978 -0.0318250291 -0.0451948496 -0.038464748 -2.584694e-02
## 2015-10-30 0.0006851847 0.0618082105 0.0640260786 0.063589736 8.163485e-02
## 2015-11-30 -0.0038981185 -0.0255604067 -0.0075559791 0.024414906 3.648621e-03
## 2015-12-31 -0.0019186638 -0.0389471397 -0.0235949659 -0.052156537 -1.743361e-02
## 2016-01-29 0.0123295897 -0.0516366273 -0.0567578778 -0.060306862 -5.106891e-02
## 2016-02-29 0.0088318338 -0.0082114604 -0.0339140194 0.020605071 -8.259932e-04
## 2016-03-31 0.0087089307 0.1218789507 0.0637458506 0.089910239 6.510026e-02
## 2016-04-29 0.0025459278 0.0040793093 0.0219749298 0.021044354 3.933364e-03
## 2016-05-31 0.0001356103 -0.0376284587 -0.0008559986 0.004397014 1.686860e-02
## 2016-06-30 0.0191667604 0.0445822826 -0.0244916160 0.008292317 3.469826e-03
## 2016-07-29 0.0054297915 0.0524420342 0.0390006719 0.049348413 3.582173e-02
## 2016-08-31 -0.0021564055 0.0087985991 0.0053265037 0.011261020 1.197090e-03
## 2016-09-30 0.0005161021 0.0248731673 0.0132792472 0.008614633 5.753904e-05
## 2016-10-31 -0.0082055220 -0.0083124505 -0.0224037498 -0.038134872 -1.748872e-02
## 2016-11-30 -0.0259899647 -0.0451617553 -0.0179743131 0.125246300 3.617615e-02
## 2016-12-30 0.0025387095 -0.0025300489 0.0267026433 0.031492189 2.006908e-02
## 2017-01-31 0.0021261392 0.0644316642 0.0323819144 -0.012143997 1.773640e-02
## 2017-02-28 0.0064375526 0.0172576504 0.0118364324 0.013428485 3.853915e-02
## 2017-03-31 -0.0005527940 0.0361888562 0.0318056735 -0.006532741 1.249371e-03
## 2017-04-28 0.0090291772 0.0168664096 0.0239524080 0.005107800 9.876951e-03
## 2017-05-31 0.0068475795 0.0280600145 0.0348099190 -0.022862705 1.401439e-02
## 2017-06-30 -0.0001824931 0.0092237714 0.0029561485 0.029151773 6.354874e-03
## 2017-07-31 0.0033342345 0.0565943194 0.0261879520 0.007481354 2.034570e-02
## 2017-08-31 0.0093693045 0.0232439234 -0.0004483592 -0.027564836 2.913453e-03
## 2017-09-29 -0.0057321197 -0.0004462597 0.0233428816 0.082321843 1.994900e-02
## 2017-10-31 0.0009775424 0.0322787980 0.0166537190 0.005916172 2.329059e-02
## 2017-11-30 -0.0014839481 -0.0038973159 0.0068698920 0.036913096 3.010843e-02
## 2017-12-29 0.0047403740 0.0369256148 0.0133982067 -0.003731169 1.205499e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398355e-05 0.0001042095 4.178129e-05 -7.812104e-05 -9.032695e-06
## EEM 1.042095e-04 0.0017547092 1.039019e-03 6.437730e-04 6.795437e-04
## EFA 4.178129e-05 0.0010390185 1.064240e-03 6.490324e-04 6.975429e-04
## IJS -7.812104e-05 0.0006437730 6.490324e-04 1.565449e-03 8.290246e-04
## SPY -9.032695e-06 0.0006795437 6.975429e-04 8.290246e-04 7.408293e-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.0003874035 0.009257143 0.005815646 0.005684466 0.002330251
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.0062316770 -0.0029352585 0.0366063129 0.052133610 4.992301e-02
## 2013-02-28 0.0058913990 -0.0231055723 -0.0129692845 0.016175327 1.267769e-02
## 2013-03-28 0.0009851750 -0.0102351142 0.0129692845 0.040258016 3.726849e-02
## 2013-04-30 0.0096389334 0.0120847780 0.0489675324 0.001222623 1.902976e-02
## 2013-05-31 -0.0202138087 -0.0494833466 -0.0306556875 0.041976197 2.333527e-02
## 2013-06-28 -0.0157784109 -0.0547282154 -0.0271442523 -0.001402900 -1.343423e-02
## 2013-07-31 0.0026871430 0.0131597961 0.0518601267 0.063541402 5.038618e-02
## 2013-08-30 -0.0082969506 -0.0257057675 -0.0197461540 -0.034743647 -3.045141e-02
## 2013-09-30 0.0111434935 0.0695888937 0.0753387805 0.063873943 3.115672e-02
## 2013-10-31 0.0082913390 0.0408610589 0.0320814055 0.034233911 4.526600e-02
## 2013-11-29 -0.0025091469 -0.0025941934 0.0054497637 0.041661050 2.920668e-02
## 2013-12-31 -0.0055828469 -0.0040743575 0.0215280874 0.012892029 2.559629e-02
## 2014-01-31 0.0152909826 -0.0903222373 -0.0534135169 -0.035775271 -3.588463e-02
## 2014-02-28 0.0037571032 0.0332204176 0.0595052511 0.045257537 4.451058e-02
## 2014-03-31 -0.0014812591 0.0380213991 -0.0046026525 0.013315071 8.261361e-03
## 2014-04-30 0.0081827508 0.0077730322 0.0165295008 -0.023183867 6.927376e-03
## 2014-05-30 0.0117214271 0.0290910768 0.0158282878 0.006205217 2.294102e-02
## 2014-06-30 -0.0005754068 0.0237341703 0.0091657226 0.037718726 2.043454e-02
## 2014-07-31 -0.0025122248 0.0135553522 -0.0263801196 -0.052009515 -1.352822e-02
## 2014-08-29 0.0114312325 0.0279047188 0.0018006337 0.043657710 3.870454e-02
## 2014-09-30 -0.0061674624 -0.0808568967 -0.0395986967 -0.061260281 -1.389248e-02
## 2014-10-31 0.0105842956 0.0140965746 -0.0026546882 0.068874891 2.327777e-02
## 2014-11-28 0.0065484990 -0.0155412100 0.0006252476 0.004773526 2.710134e-02
## 2014-12-31 0.0014751707 -0.0404420568 -0.0407468997 0.025295995 -2.539817e-03
## 2015-01-30 0.0203156428 -0.0068956344 0.0062265367 -0.054627945 -3.007669e-02
## 2015-02-27 -0.0089885593 0.0431361369 0.0614507523 0.056914601 5.468192e-02
## 2015-03-31 0.0037404940 -0.0150866582 -0.0143887121 0.010156500 -1.583061e-02
## 2015-04-30 -0.0032330085 0.0662814793 0.0358165763 -0.018417597 9.786175e-03
## 2015-05-29 -0.0043840420 -0.0419110434 0.0019526334 0.007509901 1.277396e-02
## 2015-06-30 -0.0108254837 -0.0297466489 -0.0316790005 0.004171290 -2.052116e-02
## 2015-07-31 0.0085851669 -0.0651780903 0.0201145758 -0.027375722 2.233800e-02
## 2015-08-31 -0.0033640146 -0.0925121983 -0.0771525048 -0.047268195 -6.288702e-02
## 2015-09-30 0.0080814978 -0.0318250291 -0.0451948496 -0.038464748 -2.584694e-02
## 2015-10-30 0.0006851847 0.0618082105 0.0640260786 0.063589736 8.163485e-02
## 2015-11-30 -0.0038981185 -0.0255604067 -0.0075559791 0.024414906 3.648621e-03
## 2015-12-31 -0.0019186638 -0.0389471397 -0.0235949659 -0.052156537 -1.743361e-02
## 2016-01-29 0.0123295897 -0.0516366273 -0.0567578778 -0.060306862 -5.106891e-02
## 2016-02-29 0.0088318338 -0.0082114604 -0.0339140194 0.020605071 -8.259932e-04
## 2016-03-31 0.0087089307 0.1218789507 0.0637458506 0.089910239 6.510026e-02
## 2016-04-29 0.0025459278 0.0040793093 0.0219749298 0.021044354 3.933364e-03
## 2016-05-31 0.0001356103 -0.0376284587 -0.0008559986 0.004397014 1.686860e-02
## 2016-06-30 0.0191667604 0.0445822826 -0.0244916160 0.008292317 3.469826e-03
## 2016-07-29 0.0054297915 0.0524420342 0.0390006719 0.049348413 3.582173e-02
## 2016-08-31 -0.0021564055 0.0087985991 0.0053265037 0.011261020 1.197090e-03
## 2016-09-30 0.0005161021 0.0248731673 0.0132792472 0.008614633 5.753904e-05
## 2016-10-31 -0.0082055220 -0.0083124505 -0.0224037498 -0.038134872 -1.748872e-02
## 2016-11-30 -0.0259899647 -0.0451617553 -0.0179743131 0.125246300 3.617615e-02
## 2016-12-30 0.0025387095 -0.0025300489 0.0267026433 0.031492189 2.006908e-02
## 2017-01-31 0.0021261392 0.0644316642 0.0323819144 -0.012143997 1.773640e-02
## 2017-02-28 0.0064375526 0.0172576504 0.0118364324 0.013428485 3.853915e-02
## 2017-03-31 -0.0005527940 0.0361888562 0.0318056735 -0.006532741 1.249371e-03
## 2017-04-28 0.0090291772 0.0168664096 0.0239524080 0.005107800 9.876951e-03
## 2017-05-31 0.0068475795 0.0280600145 0.0348099190 -0.022862705 1.401439e-02
## 2017-06-30 -0.0001824931 0.0092237714 0.0029561485 0.029151773 6.354874e-03
## 2017-07-31 0.0033342345 0.0565943194 0.0261879520 0.007481354 2.034570e-02
## 2017-08-31 0.0093693045 0.0232439234 -0.0004483592 -0.027564836 2.913453e-03
## 2017-09-29 -0.0057321197 -0.0004462597 0.0233428816 0.082321843 1.994900e-02
## 2017-10-31 0.0009775424 0.0322787980 0.0166537190 0.005916172 2.329059e-02
## 2017-11-30 -0.0014839481 -0.0038973159 0.0068698920 0.036913096 3.010843e-02
## 2017-12-29 0.0047403740 0.0369256148 0.0133982067 -0.003731169 1.205499e-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(0.25, 0.25, 0.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(0.25, 0.25, 0.2, .2, .1)) %>%
# Transfer 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(0.25, 0.25, 0.2, .2, .1)) %>%
# Transfer to Long Form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add Weights
add_column(weight = c(0.25, 0.25, 0.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)