# 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.0062310066 -0.0029355919  0.0366065391  0.052133365  4.992274e-02
## 2013-02-28  0.0058914079 -0.0231051917 -0.0129695801  0.016175112  1.267862e-02
## 2013-03-28  0.0009847864 -0.0102350357  0.0129695801  0.040258245  3.726805e-02
## 2013-04-30  0.0096392494  0.0120847586  0.0489674814  0.001222713  1.902994e-02
## 2013-05-31 -0.0202137658 -0.0494835884 -0.0306554071  0.041976321  2.333506e-02
## 2013-06-28 -0.0157782713 -0.0547280673 -0.0271443834 -0.001403367 -1.343405e-02
## 2013-07-31  0.0026872774  0.0131597274  0.0518601944  0.063541651  5.038595e-02
## 2013-08-30 -0.0082976492 -0.0257058261 -0.0197464148 -0.034743434 -3.045173e-02
## 2013-09-30  0.0111435835  0.0695888717  0.0753387630  0.063873636  3.115644e-02
## 2013-10-31  0.0082917960  0.0408612724  0.0320816681  0.034233873  4.526626e-02
## 2013-11-29 -0.0025097063 -0.0025939877  0.0054496043  0.041661179  2.920704e-02
## 2013-12-31 -0.0055832593 -0.0040743904  0.0215282511  0.012892094  2.559628e-02
## 2014-01-31  0.0152918974 -0.0903226999 -0.0534134627 -0.035775204 -3.588472e-02
## 2014-02-28  0.0037568204  0.0332208075  0.0595051200  0.045257510  4.451045e-02
## 2014-03-31 -0.0014809894  0.0380215077 -0.0046025830  0.013315247  8.261184e-03
## 2014-04-30  0.0081825312  0.0077729812  0.0165292256 -0.023184427  6.927589e-03
## 2014-05-30  0.0117217509  0.0290909644  0.0158285962  0.006205334  2.294107e-02
## 2014-06-30 -0.0005756273  0.0237341871  0.0091653057  0.037718742  2.043482e-02
## 2014-07-31 -0.0025119900  0.0135554832 -0.0263799505 -0.052009494 -1.352884e-02
## 2014-08-29  0.0114303672  0.0279043981  0.0018006320  0.043658175  3.870483e-02
## 2014-09-30 -0.0061668988 -0.0808566610 -0.0395985379 -0.061260595 -1.389231e-02
## 2014-10-31  0.0105845606  0.0140964731 -0.0026548297  0.068874966  2.327794e-02
## 2014-11-28  0.0065489055 -0.0155409751  0.0006253047  0.004773505  2.710140e-02
## 2014-12-31  0.0014749044 -0.0404422937 -0.0407466536  0.025295708 -2.539621e-03
## 2015-01-30  0.0203152625 -0.0068958558  0.0062264483 -0.054627804 -3.007737e-02
## 2015-02-27 -0.0089884002  0.0431361472  0.0614504891  0.056914546  5.468176e-02
## 2015-03-31  0.0037407093 -0.0150861191 -0.0143885538  0.010156422 -1.583031e-02
## 2015-04-30 -0.0032332181  0.0662810885  0.0358164484 -0.018417540  9.786267e-03
## 2015-05-29 -0.0043839599 -0.0419111025  0.0019526299  0.007509945  1.277413e-02
## 2015-06-30 -0.0108257055 -0.0297463837 -0.0316788193  0.004171106 -2.052176e-02
## 2015-07-31  0.0085844074 -0.0651782385  0.0201146009 -0.027375336  2.233796e-02
## 2015-08-31 -0.0033631538 -0.0925122383 -0.0771524992 -0.047268435 -6.288640e-02
## 2015-09-30  0.0080812234 -0.0318250341 -0.0451949241 -0.038464928 -2.584685e-02
## 2015-10-30  0.0006852881  0.0618082734  0.0640259413  0.063590242  8.163470e-02
## 2015-11-30 -0.0038978663 -0.0255604808 -0.0075557106  0.024414927  3.648443e-03
## 2015-12-31 -0.0019191268 -0.0389469649 -0.0235951094 -0.052157134 -1.743350e-02
## 2016-01-29  0.0123296563 -0.0516366464 -0.0567578901 -0.060306866 -5.106874e-02
## 2016-02-29  0.0088318452 -0.0082115852 -0.0339141720  0.020605154 -8.263023e-04
## 2016-03-31  0.0087091726  0.1218790025  0.0637458787  0.089910766  6.510038e-02
## 2016-04-29  0.0025458317  0.0040792259  0.0219751523  0.021044039  3.933412e-03
## 2016-05-31  0.0001356256 -0.0376284898 -0.0008561906  0.004397029  1.686829e-02
## 2016-06-30  0.0191667025  0.0445822218 -0.0244914120  0.008292355  3.469967e-03
## 2016-07-29  0.0054299514  0.0524423663  0.0390001722  0.049348568  3.582192e-02
## 2016-08-31 -0.0021567302  0.0087985836  0.0053269137  0.011260935  1.196872e-03
## 2016-09-30  0.0005161191  0.0248726852  0.0132790421  0.008614676  5.807728e-05
## 2016-10-31 -0.0082052245 -0.0083122594 -0.0224037521 -0.038134825 -1.748925e-02
## 2016-11-30 -0.0259897097 -0.0451617381 -0.0179742941  0.125246164  3.617609e-02
## 2016-12-30  0.0025381837 -0.0025298212  0.0267029060  0.031492009  2.006916e-02
## 2017-01-31  0.0021263097  0.0644312868  0.0323818273 -0.012144035  1.773658e-02
## 2017-02-28  0.0064378609  0.0172578478  0.0118364539  0.013428807  3.853905e-02
## 2017-03-31 -0.0005526352  0.0361890651  0.0318056779 -0.006532991  1.249218e-03
## 2017-04-28  0.0090291308  0.0168664392  0.0239523250  0.005107782  9.877348e-03
## 2017-05-31  0.0068472478  0.0280597349  0.0348101849 -0.022862416  1.401399e-02
## 2017-06-30 -0.0001826281  0.0092236201  0.0029559203  0.029151578  6.354902e-03
## 2017-07-31  0.0033341099  0.0565948315  0.0261879325  0.007481695  2.034574e-02
## 2017-08-31  0.0093692617  0.0232437087 -0.0004485126 -0.027565053  2.913519e-03
## 2017-09-29 -0.0057323803 -0.0004460779  0.0233429045  0.082321696  1.994915e-02
## 2017-10-31  0.0009781253  0.0322783209  0.0166536351  0.005916359  2.329071e-02
## 2017-11-30 -0.0014841287 -0.0038968548  0.0068699976  0.036913058  3.010825e-02
## 2017-12-29  0.0047403979  0.0369253694  0.0133984867 -0.003731043  1.205481e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398328e-05 0.0001042081 4.177969e-05 -7.812066e-05 -9.032868e-06
## EEM  1.042081e-04 0.0017547090 1.039018e-03  6.437778e-04  6.795437e-04
## EFA  4.177969e-05 0.0010390177 1.064239e-03  6.490347e-04  6.975419e-04
## IJS -7.812066e-05 0.0006437778 6.490347e-04  1.565452e-03  8.290279e-04
## SPY -9.032868e-06 0.0006795437 6.975419e-04  8.290279e-04  7.408289e-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.0003873961 0.009257144 0.005815639 0.005684489 0.002330251
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.0062310066 -0.0029355919  0.0366065391  0.052133365  4.992274e-02
## 2013-02-28  0.0058914079 -0.0231051917 -0.0129695801  0.016175112  1.267862e-02
## 2013-03-28  0.0009847864 -0.0102350357  0.0129695801  0.040258245  3.726805e-02
## 2013-04-30  0.0096392494  0.0120847586  0.0489674814  0.001222713  1.902994e-02
## 2013-05-31 -0.0202137658 -0.0494835884 -0.0306554071  0.041976321  2.333506e-02
## 2013-06-28 -0.0157782713 -0.0547280673 -0.0271443834 -0.001403367 -1.343405e-02
## 2013-07-31  0.0026872774  0.0131597274  0.0518601944  0.063541651  5.038595e-02
## 2013-08-30 -0.0082976492 -0.0257058261 -0.0197464148 -0.034743434 -3.045173e-02
## 2013-09-30  0.0111435835  0.0695888717  0.0753387630  0.063873636  3.115644e-02
## 2013-10-31  0.0082917960  0.0408612724  0.0320816681  0.034233873  4.526626e-02
## 2013-11-29 -0.0025097063 -0.0025939877  0.0054496043  0.041661179  2.920704e-02
## 2013-12-31 -0.0055832593 -0.0040743904  0.0215282511  0.012892094  2.559628e-02
## 2014-01-31  0.0152918974 -0.0903226999 -0.0534134627 -0.035775204 -3.588472e-02
## 2014-02-28  0.0037568204  0.0332208075  0.0595051200  0.045257510  4.451045e-02
## 2014-03-31 -0.0014809894  0.0380215077 -0.0046025830  0.013315247  8.261184e-03
## 2014-04-30  0.0081825312  0.0077729812  0.0165292256 -0.023184427  6.927589e-03
## 2014-05-30  0.0117217509  0.0290909644  0.0158285962  0.006205334  2.294107e-02
## 2014-06-30 -0.0005756273  0.0237341871  0.0091653057  0.037718742  2.043482e-02
## 2014-07-31 -0.0025119900  0.0135554832 -0.0263799505 -0.052009494 -1.352884e-02
## 2014-08-29  0.0114303672  0.0279043981  0.0018006320  0.043658175  3.870483e-02
## 2014-09-30 -0.0061668988 -0.0808566610 -0.0395985379 -0.061260595 -1.389231e-02
## 2014-10-31  0.0105845606  0.0140964731 -0.0026548297  0.068874966  2.327794e-02
## 2014-11-28  0.0065489055 -0.0155409751  0.0006253047  0.004773505  2.710140e-02
## 2014-12-31  0.0014749044 -0.0404422937 -0.0407466536  0.025295708 -2.539621e-03
## 2015-01-30  0.0203152625 -0.0068958558  0.0062264483 -0.054627804 -3.007737e-02
## 2015-02-27 -0.0089884002  0.0431361472  0.0614504891  0.056914546  5.468176e-02
## 2015-03-31  0.0037407093 -0.0150861191 -0.0143885538  0.010156422 -1.583031e-02
## 2015-04-30 -0.0032332181  0.0662810885  0.0358164484 -0.018417540  9.786267e-03
## 2015-05-29 -0.0043839599 -0.0419111025  0.0019526299  0.007509945  1.277413e-02
## 2015-06-30 -0.0108257055 -0.0297463837 -0.0316788193  0.004171106 -2.052176e-02
## 2015-07-31  0.0085844074 -0.0651782385  0.0201146009 -0.027375336  2.233796e-02
## 2015-08-31 -0.0033631538 -0.0925122383 -0.0771524992 -0.047268435 -6.288640e-02
## 2015-09-30  0.0080812234 -0.0318250341 -0.0451949241 -0.038464928 -2.584685e-02
## 2015-10-30  0.0006852881  0.0618082734  0.0640259413  0.063590242  8.163470e-02
## 2015-11-30 -0.0038978663 -0.0255604808 -0.0075557106  0.024414927  3.648443e-03
## 2015-12-31 -0.0019191268 -0.0389469649 -0.0235951094 -0.052157134 -1.743350e-02
## 2016-01-29  0.0123296563 -0.0516366464 -0.0567578901 -0.060306866 -5.106874e-02
## 2016-02-29  0.0088318452 -0.0082115852 -0.0339141720  0.020605154 -8.263023e-04
## 2016-03-31  0.0087091726  0.1218790025  0.0637458787  0.089910766  6.510038e-02
## 2016-04-29  0.0025458317  0.0040792259  0.0219751523  0.021044039  3.933412e-03
## 2016-05-31  0.0001356256 -0.0376284898 -0.0008561906  0.004397029  1.686829e-02
## 2016-06-30  0.0191667025  0.0445822218 -0.0244914120  0.008292355  3.469967e-03
## 2016-07-29  0.0054299514  0.0524423663  0.0390001722  0.049348568  3.582192e-02
## 2016-08-31 -0.0021567302  0.0087985836  0.0053269137  0.011260935  1.196872e-03
## 2016-09-30  0.0005161191  0.0248726852  0.0132790421  0.008614676  5.807728e-05
## 2016-10-31 -0.0082052245 -0.0083122594 -0.0224037521 -0.038134825 -1.748925e-02
## 2016-11-30 -0.0259897097 -0.0451617381 -0.0179742941  0.125246164  3.617609e-02
## 2016-12-30  0.0025381837 -0.0025298212  0.0267029060  0.031492009  2.006916e-02
## 2017-01-31  0.0021263097  0.0644312868  0.0323818273 -0.012144035  1.773658e-02
## 2017-02-28  0.0064378609  0.0172578478  0.0118364539  0.013428807  3.853905e-02
## 2017-03-31 -0.0005526352  0.0361890651  0.0318056779 -0.006532991  1.249218e-03
## 2017-04-28  0.0090291308  0.0168664392  0.0239523250  0.005107782  9.877348e-03
## 2017-05-31  0.0068472478  0.0280597349  0.0348101849 -0.022862416  1.401399e-02
## 2017-06-30 -0.0001826281  0.0092236201  0.0029559203  0.029151578  6.354902e-03
## 2017-07-31  0.0033341099  0.0565948315  0.0261879325  0.007481695  2.034574e-02
## 2017-08-31  0.0093692617  0.0232437087 -0.0004485126 -0.027565053  2.913519e-03
## 2017-09-29 -0.0057323803 -0.0004460779  0.0233429045  0.082321696  1.994915e-02
## 2017-10-31  0.0009781253  0.0322783209  0.0166536351  0.005916359  2.329071e-02
## 2017-11-30 -0.0014841287 -0.0038968548  0.0068699976  0.036913058  3.010825e-02
## 2017-12-29  0.0047403979  0.0369253694  0.0133984867 -0.003731043  1.205481e-02
calculate_component_contribution <- function(.data, w) {

    # Covariance of asset returns
    covariance_matrix <- cov(asset_returns_wide_tbl)

    # 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 from
    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(.25, .25, .2, .2, .1)) %>%

    # Transform to long from
    pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%

    # Add weights
    add_column(weight = c(.25, .25, .2, .2, .1)) %>%

    # Transfrom 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)