# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(readr)

# Time series
library(lubridate)
library(tibbletime)

# modeling
library(broom)

Goal

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

1 Import Stock Prices

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",    
                 from = "2012-12-31",
                 to   = "2017-12-31")

2 Convert Prices to Returns

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"))

3 Component Contribution Step-by-Step

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.0062306946 -0.0029352910  0.0366060179  0.052132887  4.992267e-02
## 2013-02-28  0.0058914819 -0.0231053251 -0.0129694894  0.016175766  1.267866e-02
## 2013-03-28  0.0009847132 -0.0102350850  0.0129694894  0.040258112  3.726768e-02
## 2013-04-30  0.0096389935  0.0120847531  0.0489679767  0.001222515  1.903028e-02
## 2013-05-31 -0.0202139672 -0.0494836432 -0.0306556748  0.041975934  2.333572e-02
## 2013-06-28 -0.0157782158 -0.0547284376 -0.0271445048 -0.001402778 -1.343480e-02
## 2013-07-31  0.0026872392  0.0131600831  0.0518605032  0.063541576  5.038623e-02
## 2013-08-30 -0.0082976044 -0.0257058480 -0.0197464201 -0.034743550 -3.045119e-02
## 2013-09-30  0.0111441893  0.0695888790  0.0753385331  0.063873756  3.115546e-02
## 2013-10-31  0.0082921090  0.0408613504  0.0320815623  0.034233989  4.526669e-02
## 2013-11-29 -0.0025101668 -0.0025939984  0.0054499708  0.041661146  2.920732e-02
## 2013-12-31 -0.0055831860 -0.0040745851  0.0215278031  0.012891946  2.559584e-02
## 2014-01-31  0.0152917224 -0.0903224479 -0.0534132612 -0.035775429 -3.588465e-02
## 2014-02-28  0.0037566754  0.0332206126  0.0595049162  0.045257652  4.451037e-02
## 2014-03-31 -0.0014816705  0.0380216499 -0.0046024885  0.013315345  8.261204e-03
## 2014-04-30  0.0081834924  0.0077726096  0.0165292032 -0.023184517  6.927669e-03
## 2014-05-30  0.0117216376  0.0290913425  0.0158285387  0.006205592  2.294118e-02
## 2014-06-30 -0.0005752357  0.0237341416  0.0091655519  0.037718644  2.043474e-02
## 2014-07-31 -0.0025127675  0.0135549667 -0.0263798718 -0.052009414 -1.352895e-02
## 2014-08-29  0.0114312464  0.0279048201  0.0018003702  0.043658033  3.870491e-02
## 2014-09-30 -0.0061673635 -0.0808565305 -0.0395982704 -0.061260474 -1.389236e-02
## 2014-10-31  0.0105847381  0.0140963562 -0.0026550049  0.068874716  2.327774e-02
## 2014-11-28  0.0065480658 -0.0155410501  0.0006253216  0.004773720  2.710175e-02
## 2014-12-31  0.0014747450 -0.0404421395 -0.0407465854  0.025295515 -2.539870e-03
## 2015-01-30  0.0203159845 -0.0068957074  0.0062263599 -0.054627869 -3.007709e-02
## 2015-02-27 -0.0089885922  0.0431357079  0.0614507073  0.056914883  5.468175e-02
## 2015-03-31  0.0037407420 -0.0150861345 -0.0143887315  0.010156248 -1.583046e-02
## 2015-04-30 -0.0032336108  0.0662815758  0.0358164078 -0.018417591  9.785957e-03
## 2015-05-29 -0.0043832298 -0.0419112000  0.0019528683  0.007509715  1.277436e-02
## 2015-06-30 -0.0108259081 -0.0297467540 -0.0316790180  0.004171599 -2.052121e-02
## 2015-07-31  0.0085846652 -0.0651778500  0.0201146254 -0.027375578  2.233779e-02
## 2015-08-31 -0.0033639871 -0.0925124188 -0.0771526897 -0.047268471 -6.288657e-02
## 2015-09-30  0.0080816143 -0.0318250989 -0.0451946346 -0.038464513 -2.584696e-02
## 2015-10-30  0.0006857256  0.0618085023  0.0640259392  0.063589716  8.163485e-02
## 2015-11-30 -0.0038986274 -0.0255605241 -0.0075558337  0.024415335  3.648265e-03
## 2015-12-31 -0.0019191407 -0.0389471593 -0.0235950903 -0.052157279 -1.743376e-02
## 2016-01-29  0.0123303593 -0.0516366187 -0.0567581238 -0.060306856 -5.106835e-02
## 2016-02-29  0.0088317817 -0.0082114984 -0.0339136925  0.020605433 -8.266309e-04
## 2016-03-31  0.0087088630  0.1218789139  0.0637455540  0.089910354  6.510051e-02
## 2016-04-29  0.0025462582  0.0040789964  0.0219752111  0.021044224  3.933351e-03
## 2016-05-31  0.0001357041 -0.0376282700 -0.0008561249  0.004396847  1.686884e-02
## 2016-06-30  0.0191664943  0.0445824760 -0.0244914410  0.008292394  3.469495e-03
## 2016-07-29  0.0054297827  0.0524421429  0.0390002019  0.049348584  3.582183e-02
## 2016-08-31 -0.0021565358  0.0087984584  0.0053268341  0.011261040  1.196828e-03
## 2016-09-30  0.0005157787  0.0248730233  0.0132793574  0.008614421  5.807323e-05
## 2016-10-31 -0.0082048206 -0.0083124171 -0.0224038823 -0.038134780 -1.748892e-02
## 2016-11-30 -0.0259898938 -0.0451616250 -0.0179745864  0.125246580  3.617615e-02
## 2016-12-30  0.0025387479 -0.0025300303  0.0267030732  0.031491727  2.006902e-02
## 2017-01-31  0.0021251595  0.0644312693  0.0323820065 -0.012143930  1.773660e-02
## 2017-02-28  0.0064380017  0.0172578639  0.0118362959  0.013428763  3.853902e-02
## 2017-03-31 -0.0005532389  0.0361887920  0.0318054869 -0.006533046  1.248936e-03
## 2017-04-28  0.0090303209  0.0168668376  0.0239524585  0.005107759  9.877709e-03
## 2017-05-31  0.0068468478  0.0280598005  0.0348099922 -0.022862653  1.401403e-02
## 2017-06-30 -0.0001826743  0.0092240122  0.0029561971  0.029152034  6.354862e-03
## 2017-07-31  0.0033346369  0.0565943055  0.0261877512  0.007481283  2.034569e-02
## 2017-08-31  0.0093690320  0.0232436430 -0.0004484244 -0.027564439  2.913666e-03
## 2017-09-29 -0.0057321715 -0.0004459257  0.0233428882  0.082321651  1.994884e-02
## 2017-10-31  0.0009774165  0.0322781641  0.0166538141  0.005915871  2.329060e-02
## 2017-11-30 -0.0014836877 -0.0038969326  0.0068699458  0.036913124  3.010848e-02
## 2017-12-29  0.0047401450  0.0369253410  0.0133982908 -0.003731291  1.205481e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398528e-05 0.0001042081 4.178195e-05 -0.0000781193 -9.032075e-06
## EEM  1.042081e-04 0.0017547089 1.039017e-03  0.0006437769  6.795429e-04
## EFA  4.178195e-05 0.0010390174 1.064238e-03  0.0006490309  6.975404e-04
## IJS -7.811930e-05 0.0006437769 6.490309e-04  0.0015654510  8.290248e-04
## SPY -9.032075e-06 0.0006795429 6.975404e-04  0.0008290248  7.408288e-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.0003874101 0.009257142 0.005815635 0.005684479 0.002330248
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

4 Component Contribution with a Custom Function

# 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.0062306946 -0.0029352910  0.0366060179  0.052132887  4.992267e-02
## 2013-02-28  0.0058914819 -0.0231053251 -0.0129694894  0.016175766  1.267866e-02
## 2013-03-28  0.0009847132 -0.0102350850  0.0129694894  0.040258112  3.726768e-02
## 2013-04-30  0.0096389935  0.0120847531  0.0489679767  0.001222515  1.903028e-02
## 2013-05-31 -0.0202139672 -0.0494836432 -0.0306556748  0.041975934  2.333572e-02
## 2013-06-28 -0.0157782158 -0.0547284376 -0.0271445048 -0.001402778 -1.343480e-02
## 2013-07-31  0.0026872392  0.0131600831  0.0518605032  0.063541576  5.038623e-02
## 2013-08-30 -0.0082976044 -0.0257058480 -0.0197464201 -0.034743550 -3.045119e-02
## 2013-09-30  0.0111441893  0.0695888790  0.0753385331  0.063873756  3.115546e-02
## 2013-10-31  0.0082921090  0.0408613504  0.0320815623  0.034233989  4.526669e-02
## 2013-11-29 -0.0025101668 -0.0025939984  0.0054499708  0.041661146  2.920732e-02
## 2013-12-31 -0.0055831860 -0.0040745851  0.0215278031  0.012891946  2.559584e-02
## 2014-01-31  0.0152917224 -0.0903224479 -0.0534132612 -0.035775429 -3.588465e-02
## 2014-02-28  0.0037566754  0.0332206126  0.0595049162  0.045257652  4.451037e-02
## 2014-03-31 -0.0014816705  0.0380216499 -0.0046024885  0.013315345  8.261204e-03
## 2014-04-30  0.0081834924  0.0077726096  0.0165292032 -0.023184517  6.927669e-03
## 2014-05-30  0.0117216376  0.0290913425  0.0158285387  0.006205592  2.294118e-02
## 2014-06-30 -0.0005752357  0.0237341416  0.0091655519  0.037718644  2.043474e-02
## 2014-07-31 -0.0025127675  0.0135549667 -0.0263798718 -0.052009414 -1.352895e-02
## 2014-08-29  0.0114312464  0.0279048201  0.0018003702  0.043658033  3.870491e-02
## 2014-09-30 -0.0061673635 -0.0808565305 -0.0395982704 -0.061260474 -1.389236e-02
## 2014-10-31  0.0105847381  0.0140963562 -0.0026550049  0.068874716  2.327774e-02
## 2014-11-28  0.0065480658 -0.0155410501  0.0006253216  0.004773720  2.710175e-02
## 2014-12-31  0.0014747450 -0.0404421395 -0.0407465854  0.025295515 -2.539870e-03
## 2015-01-30  0.0203159845 -0.0068957074  0.0062263599 -0.054627869 -3.007709e-02
## 2015-02-27 -0.0089885922  0.0431357079  0.0614507073  0.056914883  5.468175e-02
## 2015-03-31  0.0037407420 -0.0150861345 -0.0143887315  0.010156248 -1.583046e-02
## 2015-04-30 -0.0032336108  0.0662815758  0.0358164078 -0.018417591  9.785957e-03
## 2015-05-29 -0.0043832298 -0.0419112000  0.0019528683  0.007509715  1.277436e-02
## 2015-06-30 -0.0108259081 -0.0297467540 -0.0316790180  0.004171599 -2.052121e-02
## 2015-07-31  0.0085846652 -0.0651778500  0.0201146254 -0.027375578  2.233779e-02
## 2015-08-31 -0.0033639871 -0.0925124188 -0.0771526897 -0.047268471 -6.288657e-02
## 2015-09-30  0.0080816143 -0.0318250989 -0.0451946346 -0.038464513 -2.584696e-02
## 2015-10-30  0.0006857256  0.0618085023  0.0640259392  0.063589716  8.163485e-02
## 2015-11-30 -0.0038986274 -0.0255605241 -0.0075558337  0.024415335  3.648265e-03
## 2015-12-31 -0.0019191407 -0.0389471593 -0.0235950903 -0.052157279 -1.743376e-02
## 2016-01-29  0.0123303593 -0.0516366187 -0.0567581238 -0.060306856 -5.106835e-02
## 2016-02-29  0.0088317817 -0.0082114984 -0.0339136925  0.020605433 -8.266309e-04
## 2016-03-31  0.0087088630  0.1218789139  0.0637455540  0.089910354  6.510051e-02
## 2016-04-29  0.0025462582  0.0040789964  0.0219752111  0.021044224  3.933351e-03
## 2016-05-31  0.0001357041 -0.0376282700 -0.0008561249  0.004396847  1.686884e-02
## 2016-06-30  0.0191664943  0.0445824760 -0.0244914410  0.008292394  3.469495e-03
## 2016-07-29  0.0054297827  0.0524421429  0.0390002019  0.049348584  3.582183e-02
## 2016-08-31 -0.0021565358  0.0087984584  0.0053268341  0.011261040  1.196828e-03
## 2016-09-30  0.0005157787  0.0248730233  0.0132793574  0.008614421  5.807323e-05
## 2016-10-31 -0.0082048206 -0.0083124171 -0.0224038823 -0.038134780 -1.748892e-02
## 2016-11-30 -0.0259898938 -0.0451616250 -0.0179745864  0.125246580  3.617615e-02
## 2016-12-30  0.0025387479 -0.0025300303  0.0267030732  0.031491727  2.006902e-02
## 2017-01-31  0.0021251595  0.0644312693  0.0323820065 -0.012143930  1.773660e-02
## 2017-02-28  0.0064380017  0.0172578639  0.0118362959  0.013428763  3.853902e-02
## 2017-03-31 -0.0005532389  0.0361887920  0.0318054869 -0.006533046  1.248936e-03
## 2017-04-28  0.0090303209  0.0168668376  0.0239524585  0.005107759  9.877709e-03
## 2017-05-31  0.0068468478  0.0280598005  0.0348099922 -0.022862653  1.401403e-02
## 2017-06-30 -0.0001826743  0.0092240122  0.0029561971  0.029152034  6.354862e-03
## 2017-07-31  0.0033346369  0.0565943055  0.0261877512  0.007481283  2.034569e-02
## 2017-08-31  0.0093690320  0.0232436430 -0.0004484244 -0.027564439  2.913666e-03
## 2017-09-29 -0.0057321715 -0.0004459257  0.0233428882  0.082321651  1.994884e-02
## 2017-10-31  0.0009774165  0.0322781641  0.0166538141  0.005915871  2.329060e-02
## 2017-11-30 -0.0014836877 -0.0038969326  0.0068699458  0.036913124  3.010848e-02
## 2017-12-29  0.0047401450  0.0369253410  0.0133982908 -0.003731291  1.205481e-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

5 Visualizing Component Contribution

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(accurace = 1)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    
    labs(title = "Percent Contribution to Portfolio Volatility")

6 Rolling Component Contribution

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(accurace = 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)