# 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.0062314800 -0.0029358259  0.0366062193  0.052132993  4.992278e-02
## 2013-02-28  0.0058912506 -0.0231051182 -0.0129694754  0.016175455  1.267849e-02
## 2013-03-28  0.0009850083 -0.0102349968  0.0129694754  0.040258305  3.726764e-02
## 2013-04-30  0.0096387163  0.0120848926  0.0489677151  0.001222435  1.903059e-02
## 2013-05-31 -0.0202135056 -0.0494835786 -0.0306555928  0.041976295  2.333537e-02
## 2013-06-28 -0.0157785220 -0.0547281510 -0.0271445305 -0.001402903 -1.343419e-02
## 2013-07-31  0.0026877036  0.0131595412  0.0518604901  0.063541427  5.038548e-02
## 2013-08-30 -0.0082978786 -0.0257056421 -0.0197461487 -0.034743382 -3.045123e-02
## 2013-09-30  0.0111438336  0.0695888373  0.0753382517  0.063873616  3.115615e-02
## 2013-10-31  0.0082919956  0.0408611805  0.0320819063  0.034234425  4.526662e-02
## 2013-11-29 -0.0025092284 -0.0025940764  0.0054494356  0.041660895  2.920710e-02
## 2013-12-31 -0.0055836273 -0.0040741220  0.0215280092  0.012891717  2.559625e-02
## 2014-01-31  0.0152917818 -0.0903228470 -0.0534133570 -0.035774979 -3.588496e-02
## 2014-02-28  0.0037566486  0.0332204882  0.0595051716  0.045257506  4.451025e-02
## 2014-03-31 -0.0014813623  0.0380217055 -0.0046025730  0.013314827  8.261400e-03
## 2014-04-30  0.0081829709  0.0077729124  0.0165295008 -0.023183620  6.927654e-03
## 2014-05-30  0.0117217418  0.0290909613  0.0158282878  0.006204956  2.294126e-02
## 2014-06-30 -0.0005754612  0.0237342857  0.0091655695  0.037718496  2.043463e-02
## 2014-07-31 -0.0025130089  0.0135555746 -0.0263801237 -0.052009262 -1.352860e-02
## 2014-08-29  0.0114310968  0.0279047127  0.0018007125  0.043657866  3.870458e-02
## 2014-09-30 -0.0061671643 -0.0808569957 -0.0395986999 -0.061260484 -1.389227e-02
## 2014-10-31  0.0105844560  0.0140963418 -0.0026547703  0.068874890  2.327770e-02
## 2014-11-28  0.0065488950 -0.0155412118  0.0006253295  0.004773652  2.710132e-02
## 2014-12-31  0.0014746007 -0.0404421839 -0.0407468179  0.025295909 -2.539736e-03
## 2015-01-30  0.0203156345 -0.0068954515  0.0062267907 -0.054628205 -3.007710e-02
## 2015-02-27 -0.0089885750  0.0431361984  0.0614503390  0.056914924  5.468205e-02
## 2015-03-31  0.0037403510 -0.0150861196 -0.0143887144  0.010156086 -1.583050e-02
## 2015-04-30 -0.0032327772  0.0662810527  0.0358166599 -0.018417728  9.785962e-03
## 2015-05-29 -0.0043836266 -0.0419110386  0.0019526336  0.007509863  1.277460e-02
## 2015-06-30 -0.0108255073 -0.0297465852 -0.0316789227  0.004171589 -2.052158e-02
## 2015-07-31  0.0085847758 -0.0651783350  0.0201145758 -0.027375504  2.233776e-02
## 2015-08-31 -0.0033638760 -0.0925124158 -0.0771524198 -0.047268195 -6.288654e-02
## 2015-09-30  0.0080815289 -0.0318248201 -0.0451947567 -0.038464754 -2.584716e-02
## 2015-10-30  0.0006849943  0.0618086251  0.0640258172  0.063589685  8.163498e-02
## 2015-11-30 -0.0038978918 -0.0255606784 -0.0075559798  0.024415195  3.648681e-03
## 2015-12-31 -0.0019185313 -0.0389469911 -0.0235949679 -0.052156845 -1.743385e-02
## 2016-01-29  0.0123295576 -0.0516367692 -0.0567576095 -0.060306805 -5.106869e-02
## 2016-02-29  0.0088311349 -0.0082116146 -0.0339141074  0.020604955 -8.263364e-04
## 2016-03-31  0.0087090486  0.1218790281  0.0637457564  0.089910469  6.510035e-02
## 2016-04-29  0.0025468327  0.0040792410  0.0219750163  0.021044167  3.933516e-03
## 2016-05-31  0.0001350631 -0.0376284613 -0.0008559119  0.004397180  1.686842e-02
## 2016-06-30  0.0191668894  0.0445823535 -0.0244915230  0.008292171  3.470100e-03
## 2016-07-29  0.0054295064  0.0524422915  0.0389999791  0.049348359  3.582158e-02
## 2016-08-31 -0.0021561784  0.0087981505  0.0053271000  0.011261132  1.196892e-03
## 2016-09-30  0.0005156059  0.0248729854  0.0132789100  0.008614877  5.786201e-05
## 2016-10-31 -0.0082045767 -0.0083120146 -0.0224034967 -0.038135173 -1.748873e-02
## 2016-11-30 -0.0259899717 -0.0451618180 -0.0179745731  0.125246807  3.617559e-02
## 2016-12-30  0.0025382369 -0.0025301804  0.0267029026  0.031491609  2.006961e-02
## 2017-01-31  0.0021260365  0.0644313641  0.0323819116 -0.012143766  1.773625e-02
## 2017-02-28  0.0064376019  0.0172580214  0.0118363502  0.013428706  3.853911e-02
## 2017-03-31 -0.0005525919  0.0361889168  0.0318057522 -0.006532995  1.249493e-03
## 2017-04-28  0.0090293441  0.0168667545  0.0239522525  0.005107701  9.876786e-03
## 2017-05-31  0.0068469160  0.0280595578  0.0348100700 -0.022862801  1.401435e-02
## 2017-06-30 -0.0001822132  0.0092237724  0.0029560743  0.029151706  6.354795e-03
## 2017-07-31  0.0033343166  0.0565946395  0.0261879520  0.007481529  2.034580e-02
## 2017-08-31  0.0093692425  0.0232437140 -0.0004484313 -0.027564271  2.913709e-03
## 2017-09-29 -0.0057319140 -0.0004461573  0.0233428833  0.082321302  1.994893e-02
## 2017-10-31  0.0009776523  0.0322784976  0.0166535124  0.005916216  2.329088e-02
## 2017-11-30 -0.0014842344 -0.0038969189  0.0068701003  0.036913036  3.010795e-02
## 2017-12-29  0.0047404242  0.0369252242  0.0133982076 -0.003731079  1.205475e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398387e-05 0.0001042092 4.178166e-05 -7.812045e-05 -9.032560e-06
## EEM  1.042092e-04 0.0017547122 1.039017e-03  6.437735e-04  6.795442e-04
## EFA  4.178166e-05 0.0010390168 1.064237e-03  6.490286e-04  6.975406e-04
## IJS -7.812045e-05 0.0006437735 6.490286e-04  1.565448e-03  8.290229e-04
## SPY -9.032560e-06 0.0006795442 6.975406e-04  8.290229e-04  7.408277e-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.0003874059 0.009257152 0.005815631 0.005684463 0.002330248
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

4 Component Contribution with a Custom Function

# Transform data into wide form
calculate_component_contribution <- 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.0062314800 -0.0029358259  0.0366062193  0.052132993  4.992278e-02
## 2013-02-28  0.0058912506 -0.0231051182 -0.0129694754  0.016175455  1.267849e-02
## 2013-03-28  0.0009850083 -0.0102349968  0.0129694754  0.040258305  3.726764e-02
## 2013-04-30  0.0096387163  0.0120848926  0.0489677151  0.001222435  1.903059e-02
## 2013-05-31 -0.0202135056 -0.0494835786 -0.0306555928  0.041976295  2.333537e-02
## 2013-06-28 -0.0157785220 -0.0547281510 -0.0271445305 -0.001402903 -1.343419e-02
## 2013-07-31  0.0026877036  0.0131595412  0.0518604901  0.063541427  5.038548e-02
## 2013-08-30 -0.0082978786 -0.0257056421 -0.0197461487 -0.034743382 -3.045123e-02
## 2013-09-30  0.0111438336  0.0695888373  0.0753382517  0.063873616  3.115615e-02
## 2013-10-31  0.0082919956  0.0408611805  0.0320819063  0.034234425  4.526662e-02
## 2013-11-29 -0.0025092284 -0.0025940764  0.0054494356  0.041660895  2.920710e-02
## 2013-12-31 -0.0055836273 -0.0040741220  0.0215280092  0.012891717  2.559625e-02
## 2014-01-31  0.0152917818 -0.0903228470 -0.0534133570 -0.035774979 -3.588496e-02
## 2014-02-28  0.0037566486  0.0332204882  0.0595051716  0.045257506  4.451025e-02
## 2014-03-31 -0.0014813623  0.0380217055 -0.0046025730  0.013314827  8.261400e-03
## 2014-04-30  0.0081829709  0.0077729124  0.0165295008 -0.023183620  6.927654e-03
## 2014-05-30  0.0117217418  0.0290909613  0.0158282878  0.006204956  2.294126e-02
## 2014-06-30 -0.0005754612  0.0237342857  0.0091655695  0.037718496  2.043463e-02
## 2014-07-31 -0.0025130089  0.0135555746 -0.0263801237 -0.052009262 -1.352860e-02
## 2014-08-29  0.0114310968  0.0279047127  0.0018007125  0.043657866  3.870458e-02
## 2014-09-30 -0.0061671643 -0.0808569957 -0.0395986999 -0.061260484 -1.389227e-02
## 2014-10-31  0.0105844560  0.0140963418 -0.0026547703  0.068874890  2.327770e-02
## 2014-11-28  0.0065488950 -0.0155412118  0.0006253295  0.004773652  2.710132e-02
## 2014-12-31  0.0014746007 -0.0404421839 -0.0407468179  0.025295909 -2.539736e-03
## 2015-01-30  0.0203156345 -0.0068954515  0.0062267907 -0.054628205 -3.007710e-02
## 2015-02-27 -0.0089885750  0.0431361984  0.0614503390  0.056914924  5.468205e-02
## 2015-03-31  0.0037403510 -0.0150861196 -0.0143887144  0.010156086 -1.583050e-02
## 2015-04-30 -0.0032327772  0.0662810527  0.0358166599 -0.018417728  9.785962e-03
## 2015-05-29 -0.0043836266 -0.0419110386  0.0019526336  0.007509863  1.277460e-02
## 2015-06-30 -0.0108255073 -0.0297465852 -0.0316789227  0.004171589 -2.052158e-02
## 2015-07-31  0.0085847758 -0.0651783350  0.0201145758 -0.027375504  2.233776e-02
## 2015-08-31 -0.0033638760 -0.0925124158 -0.0771524198 -0.047268195 -6.288654e-02
## 2015-09-30  0.0080815289 -0.0318248201 -0.0451947567 -0.038464754 -2.584716e-02
## 2015-10-30  0.0006849943  0.0618086251  0.0640258172  0.063589685  8.163498e-02
## 2015-11-30 -0.0038978918 -0.0255606784 -0.0075559798  0.024415195  3.648681e-03
## 2015-12-31 -0.0019185313 -0.0389469911 -0.0235949679 -0.052156845 -1.743385e-02
## 2016-01-29  0.0123295576 -0.0516367692 -0.0567576095 -0.060306805 -5.106869e-02
## 2016-02-29  0.0088311349 -0.0082116146 -0.0339141074  0.020604955 -8.263364e-04
## 2016-03-31  0.0087090486  0.1218790281  0.0637457564  0.089910469  6.510035e-02
## 2016-04-29  0.0025468327  0.0040792410  0.0219750163  0.021044167  3.933516e-03
## 2016-05-31  0.0001350631 -0.0376284613 -0.0008559119  0.004397180  1.686842e-02
## 2016-06-30  0.0191668894  0.0445823535 -0.0244915230  0.008292171  3.470100e-03
## 2016-07-29  0.0054295064  0.0524422915  0.0389999791  0.049348359  3.582158e-02
## 2016-08-31 -0.0021561784  0.0087981505  0.0053271000  0.011261132  1.196892e-03
## 2016-09-30  0.0005156059  0.0248729854  0.0132789100  0.008614877  5.786201e-05
## 2016-10-31 -0.0082045767 -0.0083120146 -0.0224034967 -0.038135173 -1.748873e-02
## 2016-11-30 -0.0259899717 -0.0451618180 -0.0179745731  0.125246807  3.617559e-02
## 2016-12-30  0.0025382369 -0.0025301804  0.0267029026  0.031491609  2.006961e-02
## 2017-01-31  0.0021260365  0.0644313641  0.0323819116 -0.012143766  1.773625e-02
## 2017-02-28  0.0064376019  0.0172580214  0.0118363502  0.013428706  3.853911e-02
## 2017-03-31 -0.0005525919  0.0361889168  0.0318057522 -0.006532995  1.249493e-03
## 2017-04-28  0.0090293441  0.0168667545  0.0239522525  0.005107701  9.876786e-03
## 2017-05-31  0.0068469160  0.0280595578  0.0348100700 -0.022862801  1.401435e-02
## 2017-06-30 -0.0001822132  0.0092237724  0.0029560743  0.029151706  6.354795e-03
## 2017-07-31  0.0033343166  0.0565946395  0.0261879520  0.007481529  2.034580e-02
## 2017-08-31  0.0093692425  0.0232437140 -0.0004484313 -0.027564271  2.913709e-03
## 2017-09-29 -0.0057319140 -0.0004461573  0.0233428833  0.082321302  1.994893e-02
## 2017-10-31  0.0009776523  0.0322784976  0.0166535124  0.005916216  2.329088e-02
## 2017-11-30 -0.0014842344 -0.0038969189  0.0068701003  0.036913036  3.010795e-02
## 2017-12-29  0.0047404242  0.0369252242  0.0133982076 -0.003731079  1.205475e-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
    
    
    
    # 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 Volatility")

6 Rolling Component Contribution

Column Chart of Component Contribution and Weight

plot_data <- calculate_component_contribution(asset_returns_wide_tbl,
                                              w = c(.25, .25, .2, .2, .1)) %>% 
    as_tibble() %>%
    pivot_longer(cols = everything(),
                 names_to = "asset",
                 values_to = "contribution") %>%
    mutate(weight = c(.25, .25, .2, .2, .1)) %>%
    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)