# 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.0062308973 -0.0029356242  0.0366063378  0.052133052  4.992321e-02
## 2013-02-28  0.0058917696 -0.0231052854 -0.0129693913  0.016175295  1.267813e-02
## 2013-03-28  0.0009842597 -0.0102353449  0.0129693913  0.040258641  3.726795e-02
## 2013-04-30  0.0096393114  0.0120849155  0.0489677137  0.001222502  1.903008e-02
## 2013-05-31 -0.0202143711 -0.0494833437 -0.0306555699  0.041976213  2.333518e-02
## 2013-06-28 -0.0157779683 -0.0547282638 -0.0271444801 -0.001402893 -1.343387e-02
## 2013-07-31  0.0026873488  0.0131596862  0.0518602840  0.063541668  5.038547e-02
## 2013-08-30 -0.0082977825 -0.0257056207 -0.0197462997 -0.034743927 -3.045145e-02
## 2013-09-30  0.0111435623  0.0695889134  0.0753385893  0.063873622  3.115649e-02
## 2013-10-31  0.0082923648  0.0408613952  0.0320817073  0.034234160  4.526635e-02
## 2013-11-29 -0.0025096918 -0.0025941018  0.0054497573  0.041661273  2.920686e-02
## 2013-12-31 -0.0055829008 -0.0040746191  0.0215279323  0.012892060  2.559601e-02
## 2014-01-31  0.0152916041 -0.0903225351 -0.0534133611 -0.035775378 -3.588423e-02
## 2014-02-28  0.0037569464  0.0332205572  0.0595051139  0.045257588  4.451039e-02
## 2014-03-31 -0.0014819403  0.0380218614 -0.0046025445  0.013315074  8.261115e-03
## 2014-04-30  0.0081830194  0.0077725603  0.0165292858 -0.023184383  6.927275e-03
## 2014-05-30  0.0117217348  0.0290911913  0.0158286525  0.006205221  2.294137e-02
## 2014-06-30 -0.0005754629  0.0237339556  0.0091652100  0.037718756  2.043491e-02
## 2014-07-31 -0.0025121613  0.0135555467 -0.0263797850 -0.052009244 -1.352887e-02
## 2014-08-29  0.0114306797  0.0279046023  0.0018005131  0.043657889  3.870463e-02
## 2014-09-30 -0.0061675020 -0.0808564664 -0.0395985939 -0.061260385 -1.389218e-02
## 2014-10-31  0.0105849340  0.0140961089 -0.0026548127  0.068875004  2.327806e-02
## 2014-11-28  0.0065488492 -0.0155412080  0.0006253148  0.004773682  2.710112e-02
## 2014-12-31  0.0014741052 -0.0404418564 -0.0407466701  0.025295606 -2.539767e-03
## 2015-01-30  0.0203156922 -0.0068956555  0.0062265031 -0.054627819 -3.007720e-02
## 2015-02-27 -0.0089878365  0.0431358049  0.0614506608  0.056914571  5.468203e-02
## 2015-03-31  0.0037399041 -0.0150860034 -0.0143888272  0.010156264 -1.583056e-02
## 2015-04-30 -0.0032331467  0.0662811397  0.0358165742 -0.018417754  9.786068e-03
## 2015-05-29 -0.0043832519 -0.0419108998  0.0019525408  0.007510009  1.277434e-02
## 2015-06-30 -0.0108257092 -0.0297465687 -0.0316786710  0.004171471 -2.052108e-02
## 2015-07-31  0.0085848903 -0.0651783550  0.0201143002 -0.027375528  2.233780e-02
## 2015-08-31 -0.0033640359 -0.0925121558 -0.0771523012 -0.047268394 -6.288684e-02
## 2015-09-30  0.0080814250 -0.0318252165 -0.0451949311 -0.038464677 -2.584720e-02
## 2015-10-30  0.0006855911  0.0618082470  0.0640260094  0.063589830  8.163496e-02
## 2015-11-30 -0.0038980358 -0.0255602272 -0.0075559415  0.024415326  3.648502e-03
## 2015-12-31 -0.0019191627 -0.0389471530 -0.0235952019 -0.052157269 -1.743371e-02
## 2016-01-29  0.0123298007 -0.0516366096 -0.0567576690 -0.060306770 -5.106844e-02
## 2016-02-29  0.0088317716 -0.0082115380 -0.0339139835  0.020605130 -8.262749e-04
## 2016-03-31  0.0087090469  0.1218789326  0.0637458542  0.089910375  6.510035e-02
## 2016-04-29  0.0025459487  0.0040792901  0.0219752038  0.021044346  3.933193e-03
## 2016-05-31  0.0001351970 -0.0376287047 -0.0008563104  0.004397173  1.686881e-02
## 2016-06-30  0.0191673824  0.0445825058 -0.0244913881  0.008292247  3.469641e-03
## 2016-07-29  0.0054294607  0.0524423744  0.0390001135  0.049348367  3.582187e-02
## 2016-08-31 -0.0021562743  0.0087984720  0.0053268421  0.011260870  1.197118e-03
## 2016-09-30  0.0005158448  0.0248729973  0.0132791871  0.008614747  5.773848e-05
## 2016-10-31 -0.0082050945 -0.0083123524 -0.0224037254 -0.038134761 -1.748925e-02
## 2016-11-30 -0.0259897303 -0.0451620402 -0.0179744481  0.125246339  3.617619e-02
## 2016-12-30  0.0025381882 -0.0025300922  0.0267029517  0.031491618  2.006909e-02
## 2017-01-31  0.0021261372  0.0644317961  0.0323818767 -0.012143973  1.773680e-02
## 2017-02-28  0.0064378338  0.0172577695  0.0118364598  0.013428833  3.853888e-02
## 2017-03-31 -0.0005524766  0.0361888793  0.0318056725 -0.006532724  1.249276e-03
## 2017-04-28  0.0090287971  0.0168664838  0.0239521597  0.005107708  9.877179e-03
## 2017-05-31  0.0068472455  0.0280597953  0.0348104153 -0.022862671  1.401426e-02
## 2017-06-30 -0.0001829763  0.0092239144  0.0029558502  0.029151640  6.354713e-03
## 2017-07-31  0.0033350288  0.0565943842  0.0261877742  0.007481668  2.034586e-02
## 2017-08-31  0.0093691413  0.0232438852 -0.0004481744 -0.027564741  2.913399e-03
## 2017-09-29 -0.0057320432 -0.0004463555  0.0233427189  0.082321842  1.994898e-02
## 2017-10-31  0.0009773248  0.0322786854  0.0166537732  0.005915767  2.329079e-02
## 2017-11-30 -0.0014836529 -0.0038972207  0.0068698021  0.036913524  3.010798e-02
## 2017-12-29  0.0047404514  0.0369257721  0.0133982532 -0.003731331  1.205515e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398446e-05 0.0001042121 4.178401e-05 -7.811947e-05 -9.030545e-06
## EEM  1.042121e-04 0.0017547099 1.039017e-03  6.437725e-04  6.795418e-04
## EFA  4.178401e-05 0.0010390169 1.064238e-03  6.490309e-04  6.975412e-04
## IJS -7.811947e-05 0.0006437725 6.490309e-04  1.565452e-03  8.290263e-04
## SPY -9.030545e-06 0.0006795418 6.975412e-04  8.290263e-04  7.408287e-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.02347492
# 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.000387424 0.009257141 0.005815637 0.00568447 0.002330249
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062308973 -0.0029356242  0.0366063378  0.052133052  4.992321e-02
## 2013-02-28  0.0058917696 -0.0231052854 -0.0129693913  0.016175295  1.267813e-02
## 2013-03-28  0.0009842597 -0.0102353449  0.0129693913  0.040258641  3.726795e-02
## 2013-04-30  0.0096393114  0.0120849155  0.0489677137  0.001222502  1.903008e-02
## 2013-05-31 -0.0202143711 -0.0494833437 -0.0306555699  0.041976213  2.333518e-02
## 2013-06-28 -0.0157779683 -0.0547282638 -0.0271444801 -0.001402893 -1.343387e-02
## 2013-07-31  0.0026873488  0.0131596862  0.0518602840  0.063541668  5.038547e-02
## 2013-08-30 -0.0082977825 -0.0257056207 -0.0197462997 -0.034743927 -3.045145e-02
## 2013-09-30  0.0111435623  0.0695889134  0.0753385893  0.063873622  3.115649e-02
## 2013-10-31  0.0082923648  0.0408613952  0.0320817073  0.034234160  4.526635e-02
## 2013-11-29 -0.0025096918 -0.0025941018  0.0054497573  0.041661273  2.920686e-02
## 2013-12-31 -0.0055829008 -0.0040746191  0.0215279323  0.012892060  2.559601e-02
## 2014-01-31  0.0152916041 -0.0903225351 -0.0534133611 -0.035775378 -3.588423e-02
## 2014-02-28  0.0037569464  0.0332205572  0.0595051139  0.045257588  4.451039e-02
## 2014-03-31 -0.0014819403  0.0380218614 -0.0046025445  0.013315074  8.261115e-03
## 2014-04-30  0.0081830194  0.0077725603  0.0165292858 -0.023184383  6.927275e-03
## 2014-05-30  0.0117217348  0.0290911913  0.0158286525  0.006205221  2.294137e-02
## 2014-06-30 -0.0005754629  0.0237339556  0.0091652100  0.037718756  2.043491e-02
## 2014-07-31 -0.0025121613  0.0135555467 -0.0263797850 -0.052009244 -1.352887e-02
## 2014-08-29  0.0114306797  0.0279046023  0.0018005131  0.043657889  3.870463e-02
## 2014-09-30 -0.0061675020 -0.0808564664 -0.0395985939 -0.061260385 -1.389218e-02
## 2014-10-31  0.0105849340  0.0140961089 -0.0026548127  0.068875004  2.327806e-02
## 2014-11-28  0.0065488492 -0.0155412080  0.0006253148  0.004773682  2.710112e-02
## 2014-12-31  0.0014741052 -0.0404418564 -0.0407466701  0.025295606 -2.539767e-03
## 2015-01-30  0.0203156922 -0.0068956555  0.0062265031 -0.054627819 -3.007720e-02
## 2015-02-27 -0.0089878365  0.0431358049  0.0614506608  0.056914571  5.468203e-02
## 2015-03-31  0.0037399041 -0.0150860034 -0.0143888272  0.010156264 -1.583056e-02
## 2015-04-30 -0.0032331467  0.0662811397  0.0358165742 -0.018417754  9.786068e-03
## 2015-05-29 -0.0043832519 -0.0419108998  0.0019525408  0.007510009  1.277434e-02
## 2015-06-30 -0.0108257092 -0.0297465687 -0.0316786710  0.004171471 -2.052108e-02
## 2015-07-31  0.0085848903 -0.0651783550  0.0201143002 -0.027375528  2.233780e-02
## 2015-08-31 -0.0033640359 -0.0925121558 -0.0771523012 -0.047268394 -6.288684e-02
## 2015-09-30  0.0080814250 -0.0318252165 -0.0451949311 -0.038464677 -2.584720e-02
## 2015-10-30  0.0006855911  0.0618082470  0.0640260094  0.063589830  8.163496e-02
## 2015-11-30 -0.0038980358 -0.0255602272 -0.0075559415  0.024415326  3.648502e-03
## 2015-12-31 -0.0019191627 -0.0389471530 -0.0235952019 -0.052157269 -1.743371e-02
## 2016-01-29  0.0123298007 -0.0516366096 -0.0567576690 -0.060306770 -5.106844e-02
## 2016-02-29  0.0088317716 -0.0082115380 -0.0339139835  0.020605130 -8.262749e-04
## 2016-03-31  0.0087090469  0.1218789326  0.0637458542  0.089910375  6.510035e-02
## 2016-04-29  0.0025459487  0.0040792901  0.0219752038  0.021044346  3.933193e-03
## 2016-05-31  0.0001351970 -0.0376287047 -0.0008563104  0.004397173  1.686881e-02
## 2016-06-30  0.0191673824  0.0445825058 -0.0244913881  0.008292247  3.469641e-03
## 2016-07-29  0.0054294607  0.0524423744  0.0390001135  0.049348367  3.582187e-02
## 2016-08-31 -0.0021562743  0.0087984720  0.0053268421  0.011260870  1.197118e-03
## 2016-09-30  0.0005158448  0.0248729973  0.0132791871  0.008614747  5.773848e-05
## 2016-10-31 -0.0082050945 -0.0083123524 -0.0224037254 -0.038134761 -1.748925e-02
## 2016-11-30 -0.0259897303 -0.0451620402 -0.0179744481  0.125246339  3.617619e-02
## 2016-12-30  0.0025381882 -0.0025300922  0.0267029517  0.031491618  2.006909e-02
## 2017-01-31  0.0021261372  0.0644317961  0.0323818767 -0.012143973  1.773680e-02
## 2017-02-28  0.0064378338  0.0172577695  0.0118364598  0.013428833  3.853888e-02
## 2017-03-31 -0.0005524766  0.0361888793  0.0318056725 -0.006532724  1.249276e-03
## 2017-04-28  0.0090287971  0.0168664838  0.0239521597  0.005107708  9.877179e-03
## 2017-05-31  0.0068472455  0.0280597953  0.0348104153 -0.022862671  1.401426e-02
## 2017-06-30 -0.0001829763  0.0092239144  0.0029558502  0.029151640  6.354713e-03
## 2017-07-31  0.0033350288  0.0565943842  0.0261877742  0.007481668  2.034586e-02
## 2017-08-31  0.0093691413  0.0232438852 -0.0004481744 -0.027564741  2.913399e-03
## 2017-09-29 -0.0057320432 -0.0004463555  0.0233427189  0.082321842  1.994898e-02
## 2017-10-31  0.0009773248  0.0322786854  0.0166537732  0.005915767  2.329079e-02
## 2017-11-30 -0.0014836529 -0.0038972207  0.0068698021  0.036913524  3.010798e-02
## 2017-12-29  0.0047404514  0.0369257721  0.0133982532 -0.003731331  1.205515e-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, 0.2, 0.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(0.25, 0.25, 0.2, 0.2, 0.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 <- asset_returns_wide_tbl %>% 
    
    calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>% 
    
    # Transform 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, 0.2, 0.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)