# 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.0062308063 -0.0029355988  0.0366065001  0.052133440  4.992251e-02
## 2013-02-28  0.0058913475 -0.0231052291 -0.0129695665  0.016175451  1.267837e-02
## 2013-03-28  0.0009841418 -0.0102348782  0.0129695665  0.040258193  3.726824e-02
## 2013-04-30  0.0096398689  0.0120844258  0.0489673452  0.001222538  1.902986e-02
## 2013-05-31 -0.0202141809 -0.0494834075 -0.0306555956  0.041976385  2.333560e-02
## 2013-06-28 -0.0157779351 -0.0547282832 -0.0271444386 -0.001403198 -1.343443e-02
## 2013-07-31  0.0026882945  0.0131597343  0.0518604901  0.063541150  5.038571e-02
## 2013-08-30 -0.0082985679 -0.0257055117 -0.0197463316 -0.034743680 -3.045191e-02
## 2013-09-30  0.0111433410  0.0695888285  0.0753384346  0.063874011  3.115683e-02
## 2013-10-31  0.0082919988  0.0408610589  0.0320817420  0.034234344  4.526629e-02
## 2013-11-29 -0.0025097180 -0.0025939594  0.0054495182  0.041660989  2.920732e-02
## 2013-12-31 -0.0055829442 -0.0040744740  0.0215282508  0.012891884  2.559625e-02
## 2014-01-31  0.0152918816 -0.0903227406 -0.0534133482 -0.035775149 -3.588464e-02
## 2014-02-28  0.0037564562  0.0332208034  0.0595051619  0.045257432  4.451014e-02
## 2014-03-31 -0.0014810730  0.0380215189 -0.0046027319  0.013315232  8.261200e-03
## 2014-04-30  0.0081834498  0.0077726748  0.0165294223 -0.023184275  6.927457e-03
## 2014-05-30  0.0117206949  0.0290915452  0.0158282891  0.006205532  2.294127e-02
## 2014-06-30 -0.0005753668  0.0237337140  0.0091655702  0.037718650  2.043483e-02
## 2014-07-31 -0.0025121555  0.0135560224 -0.0263799686 -0.052009499 -1.352860e-02
## 2014-08-29  0.0114308109  0.0279041659  0.0018005554  0.043658025  3.870449e-02
## 2014-09-30 -0.0061675416 -0.0808565541 -0.0395985398 -0.061260643 -1.389227e-02
## 2014-10-31  0.0105844589  0.0140965713 -0.0026546882  0.068874811  2.327762e-02
## 2014-11-28  0.0065497325 -0.0155414412  0.0006251658  0.004773887  2.710159e-02
## 2014-12-31  0.0014745998 -0.0404421791 -0.0407467327  0.025295599 -2.540003e-03
## 2015-01-30  0.0203148056 -0.0068957584  0.0062264515 -0.054627728 -3.007683e-02
## 2015-02-27 -0.0089876607  0.0431361473  0.0614508320  0.056914066  5.468196e-02
## 2015-03-31  0.0037400746 -0.0150860633 -0.0143886301  0.010156923 -1.583041e-02
## 2015-04-30 -0.0032334174  0.0662812322  0.0358164147 -0.018417800  9.785786e-03
## 2015-05-29 -0.0043835358 -0.0419108050  0.0019525556  0.007509785  1.277425e-02
## 2015-06-30 -0.0108255093 -0.0297470595 -0.0316789227  0.004171360 -2.052133e-02
## 2015-07-31  0.0085845929 -0.0651777732  0.0201146545 -0.027375506  2.233819e-02
## 2015-08-31 -0.0033628592 -0.0925125256 -0.0771524135 -0.047268035 -6.288680e-02
## 2015-09-30  0.0080810647 -0.0318251041 -0.0451947528 -0.038465006 -2.584716e-02
## 2015-10-30  0.0006852694  0.0618085611  0.0640258117  0.063589776  8.163490e-02
## 2015-11-30 -0.0038983505 -0.0255607521 -0.0075559791  0.024415353  3.648681e-03
## 2015-12-31 -0.0019187159 -0.0389468536 -0.0235951381 -0.052157088 -1.743385e-02
## 2016-01-29  0.0123296510 -0.0516367731 -0.0567577056 -0.060306728 -5.106860e-02
## 2016-02-29  0.0088314068 -0.0082114604 -0.0339140194  0.020605214 -8.263364e-04
## 2016-03-31  0.0087088679  0.1218791563  0.0637458506  0.089910220  6.510035e-02
## 2016-04-29  0.0025464754  0.0040791037  0.0219750163  0.021044169  3.933516e-03
## 2016-05-31  0.0001350632 -0.0376284587 -0.0008562582  0.004397257  1.686833e-02
## 2016-06-30  0.0191667209  0.0445822149 -0.0244912654  0.008292096  3.469846e-03
## 2016-07-29  0.0054301193  0.0524423593  0.0390002385  0.049348507  3.582208e-02
## 2016-08-31 -0.0021566150  0.0087984693  0.0053267596  0.011261131  1.196811e-03
## 2016-09-30  0.0005160426  0.0248727288  0.0132791635  0.008614665  5.794373e-05
## 2016-10-31 -0.0082045759 -0.0083122023 -0.0224038374 -0.038134959 -1.748914e-02
## 2016-11-30 -0.0259895177 -0.0451618894 -0.0179742290  0.125246798  3.617592e-02
## 2016-12-30  0.0025370642 -0.0025297863  0.0267028154  0.031491356  2.006922e-02
## 2017-01-31  0.0021265773  0.0644313517  0.0323818294 -0.012143895  1.773656e-02
## 2017-02-28  0.0064384065  0.0172581395  0.0118365136  0.013428835  3.853925e-02
## 2017-03-31 -0.0005537535  0.0361886138  0.0318056710 -0.006532996  1.249047e-03
## 2017-04-28  0.0090297910  0.0168666395  0.0239520989  0.005107702  9.877230e-03
## 2017-05-31  0.0068471800  0.0280596727  0.0348102979 -0.022862613  1.401442e-02
## 2017-06-30 -0.0001828290  0.0092238832  0.0029561481  0.029151643  6.354650e-03
## 2017-07-31  0.0033345809  0.0565945288  0.0261878041  0.007481838  2.034580e-02
## 2017-08-31  0.0093690696  0.0232439185 -0.0004485755 -0.027564454  2.913217e-03
## 2017-09-29 -0.0057320903 -0.0004463619  0.0233429570  0.082321356  1.994928e-02
## 2017-10-31  0.0009778273  0.0322783985  0.0166538599  0.005915982  2.329088e-02
## 2017-11-30 -0.0014839725 -0.0038968199  0.0068696856  0.036913377  3.010795e-02
## 2017-12-29  0.0047400762  0.0369253201  0.0133986167 -0.003730966  1.205482e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398292e-05 0.0001042088 4.178176e-05 -7.811915e-05 -9.030653e-06
## EEM  1.042088e-04 0.0017547101 1.039018e-03  6.437734e-04  6.795449e-04
## EFA  4.178176e-05 0.0010390178 1.064238e-03  6.490311e-04  6.975428e-04
## IJS -7.811915e-05 0.0006437734 6.490311e-04  1.565449e-03  8.290255e-04
## SPY -9.030653e-06 0.0006795449 6.975428e-04  8.290255e-04  7.408303e-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.0003874069 0.009257143 0.005815638 0.00568447 0.002330255
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

sset_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.0062308063 -0.0029355988  0.0366065001  0.052133440  4.992251e-02
## 2013-02-28  0.0058913475 -0.0231052291 -0.0129695665  0.016175451  1.267837e-02
## 2013-03-28  0.0009841418 -0.0102348782  0.0129695665  0.040258193  3.726824e-02
## 2013-04-30  0.0096398689  0.0120844258  0.0489673452  0.001222538  1.902986e-02
## 2013-05-31 -0.0202141809 -0.0494834075 -0.0306555956  0.041976385  2.333560e-02
## 2013-06-28 -0.0157779351 -0.0547282832 -0.0271444386 -0.001403198 -1.343443e-02
## 2013-07-31  0.0026882945  0.0131597343  0.0518604901  0.063541150  5.038571e-02
## 2013-08-30 -0.0082985679 -0.0257055117 -0.0197463316 -0.034743680 -3.045191e-02
## 2013-09-30  0.0111433410  0.0695888285  0.0753384346  0.063874011  3.115683e-02
## 2013-10-31  0.0082919988  0.0408610589  0.0320817420  0.034234344  4.526629e-02
## 2013-11-29 -0.0025097180 -0.0025939594  0.0054495182  0.041660989  2.920732e-02
## 2013-12-31 -0.0055829442 -0.0040744740  0.0215282508  0.012891884  2.559625e-02
## 2014-01-31  0.0152918816 -0.0903227406 -0.0534133482 -0.035775149 -3.588464e-02
## 2014-02-28  0.0037564562  0.0332208034  0.0595051619  0.045257432  4.451014e-02
## 2014-03-31 -0.0014810730  0.0380215189 -0.0046027319  0.013315232  8.261200e-03
## 2014-04-30  0.0081834498  0.0077726748  0.0165294223 -0.023184275  6.927457e-03
## 2014-05-30  0.0117206949  0.0290915452  0.0158282891  0.006205532  2.294127e-02
## 2014-06-30 -0.0005753668  0.0237337140  0.0091655702  0.037718650  2.043483e-02
## 2014-07-31 -0.0025121555  0.0135560224 -0.0263799686 -0.052009499 -1.352860e-02
## 2014-08-29  0.0114308109  0.0279041659  0.0018005554  0.043658025  3.870449e-02
## 2014-09-30 -0.0061675416 -0.0808565541 -0.0395985398 -0.061260643 -1.389227e-02
## 2014-10-31  0.0105844589  0.0140965713 -0.0026546882  0.068874811  2.327762e-02
## 2014-11-28  0.0065497325 -0.0155414412  0.0006251658  0.004773887  2.710159e-02
## 2014-12-31  0.0014745998 -0.0404421791 -0.0407467327  0.025295599 -2.540003e-03
## 2015-01-30  0.0203148056 -0.0068957584  0.0062264515 -0.054627728 -3.007683e-02
## 2015-02-27 -0.0089876607  0.0431361473  0.0614508320  0.056914066  5.468196e-02
## 2015-03-31  0.0037400746 -0.0150860633 -0.0143886301  0.010156923 -1.583041e-02
## 2015-04-30 -0.0032334174  0.0662812322  0.0358164147 -0.018417800  9.785786e-03
## 2015-05-29 -0.0043835358 -0.0419108050  0.0019525556  0.007509785  1.277425e-02
## 2015-06-30 -0.0108255093 -0.0297470595 -0.0316789227  0.004171360 -2.052133e-02
## 2015-07-31  0.0085845929 -0.0651777732  0.0201146545 -0.027375506  2.233819e-02
## 2015-08-31 -0.0033628592 -0.0925125256 -0.0771524135 -0.047268035 -6.288680e-02
## 2015-09-30  0.0080810647 -0.0318251041 -0.0451947528 -0.038465006 -2.584716e-02
## 2015-10-30  0.0006852694  0.0618085611  0.0640258117  0.063589776  8.163490e-02
## 2015-11-30 -0.0038983505 -0.0255607521 -0.0075559791  0.024415353  3.648681e-03
## 2015-12-31 -0.0019187159 -0.0389468536 -0.0235951381 -0.052157088 -1.743385e-02
## 2016-01-29  0.0123296510 -0.0516367731 -0.0567577056 -0.060306728 -5.106860e-02
## 2016-02-29  0.0088314068 -0.0082114604 -0.0339140194  0.020605214 -8.263364e-04
## 2016-03-31  0.0087088679  0.1218791563  0.0637458506  0.089910220  6.510035e-02
## 2016-04-29  0.0025464754  0.0040791037  0.0219750163  0.021044169  3.933516e-03
## 2016-05-31  0.0001350632 -0.0376284587 -0.0008562582  0.004397257  1.686833e-02
## 2016-06-30  0.0191667209  0.0445822149 -0.0244912654  0.008292096  3.469846e-03
## 2016-07-29  0.0054301193  0.0524423593  0.0390002385  0.049348507  3.582208e-02
## 2016-08-31 -0.0021566150  0.0087984693  0.0053267596  0.011261131  1.196811e-03
## 2016-09-30  0.0005160426  0.0248727288  0.0132791635  0.008614665  5.794373e-05
## 2016-10-31 -0.0082045759 -0.0083122023 -0.0224038374 -0.038134959 -1.748914e-02
## 2016-11-30 -0.0259895177 -0.0451618894 -0.0179742290  0.125246798  3.617592e-02
## 2016-12-30  0.0025370642 -0.0025297863  0.0267028154  0.031491356  2.006922e-02
## 2017-01-31  0.0021265773  0.0644313517  0.0323818294 -0.012143895  1.773656e-02
## 2017-02-28  0.0064384065  0.0172581395  0.0118365136  0.013428835  3.853925e-02
## 2017-03-31 -0.0005537535  0.0361886138  0.0318056710 -0.006532996  1.249047e-03
## 2017-04-28  0.0090297910  0.0168666395  0.0239520989  0.005107702  9.877230e-03
## 2017-05-31  0.0068471800  0.0280596727  0.0348102979 -0.022862613  1.401442e-02
## 2017-06-30 -0.0001828290  0.0092238832  0.0029561481  0.029151643  6.354650e-03
## 2017-07-31  0.0033345809  0.0565945288  0.0261878041  0.007481838  2.034580e-02
## 2017-08-31  0.0093690696  0.0232439185 -0.0004485755 -0.027564454  2.913217e-03
## 2017-09-29 -0.0057320903 -0.0004463619  0.0233429570  0.082321356  1.994928e-02
## 2017-10-31  0.0009778273  0.0322783985  0.0166538599  0.005915982  2.329088e-02
## 2017-11-30 -0.0014839725 -0.0038968199  0.0068696856  0.036913377  3.010795e-02
## 2017-12-29  0.0047400762  0.0369253201  0.0133986167 -0.003730966  1.205482e-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 componenet contribution

plot_data <- asset_returns_wide_tbl %>% 
    
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform
    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
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weights
    add_column(weight = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform
    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)