# 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.0062307930 -0.0029354071  0.0366062151  0.052134021  4.992273e-02
## 2013-02-28  0.0058906774 -0.0231055674 -0.0129692920  0.016175432  1.267847e-02
## 2013-03-28  0.0009852149 -0.0102349103  0.0129692920  0.040257999  3.726786e-02
## 2013-04-30  0.0096393907  0.0120850523  0.0489676956  0.001222306  1.903034e-02
## 2013-05-31 -0.0202146683 -0.0494836432 -0.0306553004  0.041976138  2.333513e-02
## 2013-06-28 -0.0157775002 -0.0547281751 -0.0271445972 -0.001402578 -1.343445e-02
## 2013-07-31  0.0026876487  0.0131598206  0.0518602201  0.063541463  5.038635e-02
## 2013-08-30 -0.0082982186 -0.0257058480 -0.0197464238 -0.034743932 -3.045131e-02
## 2013-09-30  0.0111437776  0.0695891269  0.0753386345  0.063873951  3.115535e-02
## 2013-10-31  0.0082922122  0.0408611024  0.0320816500  0.034233989  4.526724e-02
## 2013-11-29 -0.0025103706 -0.0025941177  0.0054497174  0.041661484  2.920658e-02
## 2013-12-31 -0.0055827784 -0.0040744059  0.0215279738  0.012891358  2.559616e-02
## 2014-01-31  0.0152917208 -0.0903225733 -0.0534132658 -0.035775265 -3.588423e-02
## 2014-02-28  0.0037573778  0.0332206781  0.0595050034  0.045257655  4.451016e-02
## 2014-03-31 -0.0014820715  0.0380215888 -0.0046023234  0.013315509  8.261405e-03
## 2014-04-30  0.0081829904  0.0077727918  0.0165292005 -0.023184348  6.927467e-03
## 2014-05-30  0.0117217373  0.0290912214  0.0158286161  0.006205092  2.294098e-02
## 2014-06-30 -0.0005761234  0.0237336819  0.0091653914  0.037718973  2.043493e-02
## 2014-07-31 -0.0025121764  0.0135557665 -0.0263799509 -0.052009662 -1.352876e-02
## 2014-08-29  0.0114312498  0.0279048107  0.0018004513  0.043658201  3.870434e-02
## 2014-09-30 -0.0061669718 -0.0808569210 -0.0395983516 -0.061260646 -1.389208e-02
## 2014-10-31  0.0105834718  0.0140964160 -0.0026547509  0.068874887  2.327802e-02
## 2014-11-28  0.0065493304 -0.0155409902  0.0006250676  0.004773561  2.710111e-02
## 2014-12-31  0.0014754206 -0.0404425110 -0.0407466735  0.025295829 -2.539871e-03
## 2015-01-30  0.0203148337 -0.0068953330  0.0062266232 -0.054627778 -3.007673e-02
## 2015-02-27 -0.0089884047  0.0431356451  0.0614506145  0.056914559  5.468167e-02
## 2015-03-31  0.0037405525 -0.0150861345 -0.0143890645  0.010156402 -1.582992e-02
## 2015-04-30 -0.0032328487  0.0662818042  0.0358165779 -0.018417980  9.785511e-03
## 2015-05-29 -0.0043837073 -0.0419114285  0.0019527076  0.007509950  1.277436e-02
## 2015-06-30 -0.0108251342 -0.0297465700 -0.0316788597  0.004171522 -2.052121e-02
## 2015-07-31  0.0085842752 -0.0651780995  0.0201145456 -0.027375107  2.233770e-02
## 2015-08-31 -0.0033643716 -0.0925122815 -0.0771522632 -0.047268705 -6.288649e-02
## 2015-09-30  0.0080821886 -0.0318250965 -0.0451950823 -0.038464942 -2.584734e-02
## 2015-10-30  0.0006854387  0.0618083585  0.0640262095  0.063590224  8.163514e-02
## 2015-11-30 -0.0038984348 -0.0255603828 -0.0075560938  0.024414698  3.648441e-03
## 2015-12-31 -0.0019188517 -0.0389471565 -0.0235950944 -0.052156804 -1.743349e-02
## 2016-01-29  0.0123297816 -0.0516366148 -0.0567576631 -0.060306945 -5.106899e-02
## 2016-02-29  0.0088318749 -0.0082115767 -0.0339141701  0.020605261 -8.260687e-04
## 2016-03-31  0.0087078355  0.1218791235  0.0637458404  0.089910614  6.510032e-02
## 2016-04-29  0.0025470044  0.0040791347  0.0219752091  0.021044069  3.933438e-03
## 2016-05-31  0.0001352390 -0.0376286903 -0.0008562144  0.004397234  1.686849e-02
## 2016-06-30  0.0191668699  0.0445822718 -0.0244912575  0.008292316  3.469582e-03
## 2016-07-29  0.0054296930  0.0524421571  0.0390000184  0.049347993  3.582233e-02
## 2016-08-31 -0.0021563545  0.0087988508  0.0053270097  0.011261261  1.196745e-03
## 2016-09-30  0.0005160514  0.0248727031  0.0132790086  0.008614780  5.766018e-05
## 2016-10-31 -0.0082053689 -0.0083121629 -0.0224037090 -0.038134849 -1.748875e-02
## 2016-11-30 -0.0259892451 -0.0451617559 -0.0179744060  0.125246243  3.617590e-02
## 2016-12-30  0.0025378090 -0.0025298963  0.0267029806  0.031491735  2.006886e-02
## 2017-01-31  0.0021260035  0.0644313908  0.0323817487 -0.012143676  1.773668e-02
## 2017-02-28  0.0064380936  0.0172576753  0.0118364660  0.013428382  3.853910e-02
## 2017-03-31 -0.0005530526  0.0361892092  0.0318057310 -0.006532793  1.249386e-03
## 2017-04-28  0.0090295788  0.0168663031  0.0239522143  0.005107887  9.877186e-03
## 2017-05-31  0.0068471245  0.0280598037  0.0348101457 -0.022862718  1.401418e-02
## 2017-06-30 -0.0001824910  0.0092236744  0.0029557376  0.029151657  6.354716e-03
## 2017-07-31  0.0033342711  0.0565945437  0.0261879082  0.007481411  2.034591e-02
## 2017-08-31  0.0093693956  0.0232439607 -0.0004482007 -0.027564448  2.913452e-03
## 2017-09-29 -0.0057327164 -0.0004461343  0.0233429593  0.082321854  1.994912e-02
## 2017-10-31  0.0009782349  0.0322781674  0.0166535967  0.005915872  2.329060e-02
## 2017-11-30 -0.0014837780 -0.0038965274  0.0068699463  0.036913469  3.010815e-02
## 2017-12-29  0.0047399621  0.0369250368  0.0133981514 -0.003731404  1.205488e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398340e-05 0.0001042076 4.178126e-05 -7.812131e-05 -9.031748e-06
## EEM  1.042076e-04 0.0017547125 1.039018e-03  6.437751e-04  6.795420e-04
## EFA  4.178126e-05 0.0010390182 1.064237e-03  6.490324e-04  6.975404e-04
## IJS -7.812131e-05 0.0006437751 6.490324e-04  1.565454e-03  8.290257e-04
## SPY -9.031748e-06 0.0006795420 6.975404e-04  8.290257e-04  7.408274e-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.0003873984 0.009257148 0.005815637 0.00568448 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.0062307930 -0.0029354071  0.0366062151  0.052134021  4.992273e-02
## 2013-02-28  0.0058906774 -0.0231055674 -0.0129692920  0.016175432  1.267847e-02
## 2013-03-28  0.0009852149 -0.0102349103  0.0129692920  0.040257999  3.726786e-02
## 2013-04-30  0.0096393907  0.0120850523  0.0489676956  0.001222306  1.903034e-02
## 2013-05-31 -0.0202146683 -0.0494836432 -0.0306553004  0.041976138  2.333513e-02
## 2013-06-28 -0.0157775002 -0.0547281751 -0.0271445972 -0.001402578 -1.343445e-02
## 2013-07-31  0.0026876487  0.0131598206  0.0518602201  0.063541463  5.038635e-02
## 2013-08-30 -0.0082982186 -0.0257058480 -0.0197464238 -0.034743932 -3.045131e-02
## 2013-09-30  0.0111437776  0.0695891269  0.0753386345  0.063873951  3.115535e-02
## 2013-10-31  0.0082922122  0.0408611024  0.0320816500  0.034233989  4.526724e-02
## 2013-11-29 -0.0025103706 -0.0025941177  0.0054497174  0.041661484  2.920658e-02
## 2013-12-31 -0.0055827784 -0.0040744059  0.0215279738  0.012891358  2.559616e-02
## 2014-01-31  0.0152917208 -0.0903225733 -0.0534132658 -0.035775265 -3.588423e-02
## 2014-02-28  0.0037573778  0.0332206781  0.0595050034  0.045257655  4.451016e-02
## 2014-03-31 -0.0014820715  0.0380215888 -0.0046023234  0.013315509  8.261405e-03
## 2014-04-30  0.0081829904  0.0077727918  0.0165292005 -0.023184348  6.927467e-03
## 2014-05-30  0.0117217373  0.0290912214  0.0158286161  0.006205092  2.294098e-02
## 2014-06-30 -0.0005761234  0.0237336819  0.0091653914  0.037718973  2.043493e-02
## 2014-07-31 -0.0025121764  0.0135557665 -0.0263799509 -0.052009662 -1.352876e-02
## 2014-08-29  0.0114312498  0.0279048107  0.0018004513  0.043658201  3.870434e-02
## 2014-09-30 -0.0061669718 -0.0808569210 -0.0395983516 -0.061260646 -1.389208e-02
## 2014-10-31  0.0105834718  0.0140964160 -0.0026547509  0.068874887  2.327802e-02
## 2014-11-28  0.0065493304 -0.0155409902  0.0006250676  0.004773561  2.710111e-02
## 2014-12-31  0.0014754206 -0.0404425110 -0.0407466735  0.025295829 -2.539871e-03
## 2015-01-30  0.0203148337 -0.0068953330  0.0062266232 -0.054627778 -3.007673e-02
## 2015-02-27 -0.0089884047  0.0431356451  0.0614506145  0.056914559  5.468167e-02
## 2015-03-31  0.0037405525 -0.0150861345 -0.0143890645  0.010156402 -1.582992e-02
## 2015-04-30 -0.0032328487  0.0662818042  0.0358165779 -0.018417980  9.785511e-03
## 2015-05-29 -0.0043837073 -0.0419114285  0.0019527076  0.007509950  1.277436e-02
## 2015-06-30 -0.0108251342 -0.0297465700 -0.0316788597  0.004171522 -2.052121e-02
## 2015-07-31  0.0085842752 -0.0651780995  0.0201145456 -0.027375107  2.233770e-02
## 2015-08-31 -0.0033643716 -0.0925122815 -0.0771522632 -0.047268705 -6.288649e-02
## 2015-09-30  0.0080821886 -0.0318250965 -0.0451950823 -0.038464942 -2.584734e-02
## 2015-10-30  0.0006854387  0.0618083585  0.0640262095  0.063590224  8.163514e-02
## 2015-11-30 -0.0038984348 -0.0255603828 -0.0075560938  0.024414698  3.648441e-03
## 2015-12-31 -0.0019188517 -0.0389471565 -0.0235950944 -0.052156804 -1.743349e-02
## 2016-01-29  0.0123297816 -0.0516366148 -0.0567576631 -0.060306945 -5.106899e-02
## 2016-02-29  0.0088318749 -0.0082115767 -0.0339141701  0.020605261 -8.260687e-04
## 2016-03-31  0.0087078355  0.1218791235  0.0637458404  0.089910614  6.510032e-02
## 2016-04-29  0.0025470044  0.0040791347  0.0219752091  0.021044069  3.933438e-03
## 2016-05-31  0.0001352390 -0.0376286903 -0.0008562144  0.004397234  1.686849e-02
## 2016-06-30  0.0191668699  0.0445822718 -0.0244912575  0.008292316  3.469582e-03
## 2016-07-29  0.0054296930  0.0524421571  0.0390000184  0.049347993  3.582233e-02
## 2016-08-31 -0.0021563545  0.0087988508  0.0053270097  0.011261261  1.196745e-03
## 2016-09-30  0.0005160514  0.0248727031  0.0132790086  0.008614780  5.766018e-05
## 2016-10-31 -0.0082053689 -0.0083121629 -0.0224037090 -0.038134849 -1.748875e-02
## 2016-11-30 -0.0259892451 -0.0451617559 -0.0179744060  0.125246243  3.617590e-02
## 2016-12-30  0.0025378090 -0.0025298963  0.0267029806  0.031491735  2.006886e-02
## 2017-01-31  0.0021260035  0.0644313908  0.0323817487 -0.012143676  1.773668e-02
## 2017-02-28  0.0064380936  0.0172576753  0.0118364660  0.013428382  3.853910e-02
## 2017-03-31 -0.0005530526  0.0361892092  0.0318057310 -0.006532793  1.249386e-03
## 2017-04-28  0.0090295788  0.0168663031  0.0239522143  0.005107887  9.877186e-03
## 2017-05-31  0.0068471245  0.0280598037  0.0348101457 -0.022862718  1.401418e-02
## 2017-06-30 -0.0001824910  0.0092236744  0.0029557376  0.029151657  6.354716e-03
## 2017-07-31  0.0033342711  0.0565945437  0.0261879082  0.007481411  2.034591e-02
## 2017-08-31  0.0093693956  0.0232439607 -0.0004482007 -0.027564448  2.913452e-03
## 2017-09-29 -0.0057327164 -0.0004461343  0.0233429593  0.082321854  1.994912e-02
## 2017-10-31  0.0009782349  0.0322781674  0.0166535967  0.005915872  2.329060e-02
## 2017-11-30 -0.0014837780 -0.0038965274  0.0068699463  0.036913469  3.010815e-02
## 2017-12-29  0.0047399621  0.0369250368  0.0133981514 -0.003731404  1.205488e-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(accuracy = 1)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    
    labs(title = "Percent Contribution to Portfolio Volitility")

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(accuracy = 1)) +
    scale_fill_tq()+
    theme(plot.title = element_text(hjust = 0.5)) +
    theme_tq()+
    
    labs(title = "Percent Contribution to Portfolio Volitility and Weight",
         y = "Percent",
         x = NULL)

6 Rolling Component Contribution