# 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.0062309977 -0.0029357127  0.0366062228  0.052133319  4.992297e-02
## 2013-02-28  0.0058909609 -0.0231050020 -0.0129690026  0.016175342  1.267830e-02
## 2013-03-28  0.0009851048 -0.0102351130  0.0129690026  0.040258296  3.726812e-02
## 2013-04-30  0.0096391940  0.0120848926  0.0489678978  0.001222538  1.903004e-02
## 2013-05-31 -0.0202139843 -0.0494836395 -0.0306556819  0.041976381  2.333583e-02
## 2013-06-28 -0.0157786224 -0.0547282188 -0.0271444361 -0.001403395 -1.343501e-02
## 2013-07-31  0.0026879998  0.0131597970  0.0518603956  0.063541526  5.038594e-02
## 2013-08-30 -0.0082978778 -0.0257058995 -0.0197465144 -0.034743382 -3.045088e-02
## 2013-09-30  0.0111439308  0.0695889676  0.0753387871  0.063873526  3.115580e-02
## 2013-10-31  0.0082916040  0.0408611805  0.0320817367  0.034234167  4.526650e-02
## 2013-11-29 -0.0025097175 -0.0025938424  0.0054494356  0.041660909  2.920679e-02
## 2013-12-31 -0.0055832380 -0.0040743561  0.0215280892  0.012892298  2.559636e-02
## 2014-01-31  0.0152917863 -0.0903226541 -0.0534133526 -0.035775311 -3.588465e-02
## 2014-02-28  0.0037570354  0.0332204819  0.0595050079  0.045257347  4.451025e-02
## 2014-03-31 -0.0014819416  0.0380215189 -0.0046024936  0.013315313  8.261499e-03
## 2014-04-30  0.0081837411  0.0077730312  0.0165292653 -0.023184520  6.927455e-03
## 2014-05-30  0.0117215491  0.0290911888  0.0158287552  0.006205779  2.294097e-02
## 2014-06-30 -0.0005760294  0.0237337140  0.0091650314  0.037718411  2.043492e-02
## 2014-07-31 -0.0025124403  0.0135556888 -0.0263794244 -0.052009590 -1.352888e-02
## 2014-08-29  0.0114303447  0.0279048239  0.0018002409  0.043658274  3.870486e-02
## 2014-09-30 -0.0061661293 -0.0808571130 -0.0395984581 -0.061260389 -1.389246e-02
## 2014-10-31  0.0105836108  0.0140965746 -0.0026548517  0.068874558  2.327771e-02
## 2014-11-28  0.0065495480 -0.0155412100  0.0006253294  0.004773809  2.710185e-02
## 2014-12-31  0.0014750640 -0.0404424236 -0.0407466441  0.025296060 -2.540002e-03
## 2015-01-30  0.0203153489 -0.0068955139  0.0062263657 -0.054628515 -3.007700e-02
## 2015-02-27 -0.0089887550  0.0431362652  0.0614506677  0.056914852  5.468152e-02
## 2015-03-31  0.0037401676 -0.0150863009 -0.0143887121  0.010156390 -1.583015e-02
## 2015-04-30 -0.0032326856  0.0662812399  0.0358163424 -0.018417649  9.785875e-03
## 2015-05-29 -0.0043839023 -0.0419109266  0.0019527895  0.007509709  1.277442e-02
## 2015-06-30 -0.0108251372 -0.0297468259 -0.0316786818  0.004171665 -2.052141e-02
## 2015-07-31  0.0085844053 -0.0651780301  0.0201143349 -0.027375343  2.233793e-02
## 2015-08-31 -0.0033639692 -0.0925122687 -0.0771525898 -0.047268426 -6.288663e-02
## 2015-09-30  0.0080818984 -0.0318248859 -0.0451948535 -0.038464921 -2.584744e-02
## 2015-10-30  0.0006852694  0.0618082062  0.0640261675  0.063589691  8.163527e-02
## 2015-11-30 -0.0038985351 -0.0255604049 -0.0075559791  0.024415509  3.648421e-03
## 2015-12-31 -0.0019189009 -0.0389469911 -0.0235949659 -0.052157079 -1.743350e-02
## 2016-01-29  0.0123298390 -0.0516366924 -0.0567580600 -0.060306805 -5.106859e-02
## 2016-02-29  0.0088320416 -0.0082116913 -0.0339139315  0.020604699 -8.262435e-04
## 2016-03-31  0.0087090439  0.1218790281  0.0637458564  0.089910492  6.509989e-02
## 2016-04-29  0.0025462953  0.0040791728  0.0219751047  0.021044325  3.933689e-03
## 2016-05-31  0.0001352417 -0.0376286057 -0.0008560851  0.004397180  1.686850e-02
## 2016-06-30  0.0191667107  0.0445825661 -0.0244917047  0.008292399  3.469760e-03
## 2016-07-29  0.0054294192  0.0524424201  0.0390004193  0.049348136  3.582208e-02
## 2016-08-31 -0.0021560913  0.0087982769  0.0053270996  0.011261275  1.196647e-03
## 2016-09-30  0.0005160424  0.0248727925  0.0132789926  0.008614665  5.818891e-05
## 2016-10-31 -0.0082056292 -0.0083118887 -0.0224038355 -0.038134959 -1.748939e-02
## 2016-11-30 -0.0259896266 -0.0451623342 -0.0179744890  0.125246604  3.617608e-02
## 2016-12-30  0.0025381475 -0.0025297208  0.0267029049  0.031491863  2.006930e-02
## 2017-01-31  0.0021262171  0.0644312942  0.0323819966 -0.012144208  1.773640e-02
## 2017-02-28  0.0064384964  0.0172579598  0.0118364314  0.013428710  3.853925e-02
## 2017-03-31 -0.0005533960  0.0361889168  0.0318055923 -0.006532745  1.249122e-03
## 2017-04-28  0.0090292563  0.0168664096  0.0239523312  0.005107451  9.877155e-03
## 2017-05-31  0.0068474451  0.0280600145  0.0348102184 -0.022862424  1.401428e-02
## 2017-06-30 -0.0001823011  0.0092235499  0.0029559259  0.029152015  6.354939e-03
## 2017-07-31  0.0033338773  0.0565947503  0.0261878079  0.007481403  2.034558e-02
## 2017-08-31  0.0093695048  0.0232440208 -0.0004482150 -0.027564835  2.913498e-03
## 2017-09-29 -0.0057323504 -0.0004464641  0.0233428112  0.082321971  1.994907e-02
## 2017-10-31  0.0009780019  0.0322783985  0.0166536509  0.005915981  2.329095e-02
## 2017-11-30 -0.0014844967 -0.0038968199  0.0068700306  0.036913144  3.010808e-02
## 2017-12-29  0.0047405995  0.0369254159  0.0133980709 -0.003731191  1.205462e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398422e-05 0.0001042085 4.178121e-05 -7.812348e-05 -9.032554e-06
## EEM  1.042085e-04 0.0017547131 1.039019e-03  6.437722e-04  6.795420e-04
## EFA  4.178121e-05 0.0010390190 1.064239e-03  6.490312e-04  6.975414e-04
## IJS -7.812348e-05 0.0006437722 6.490312e-04  1.565453e-03  8.290265e-04
## SPY -9.032554e-06 0.0006795420 6.975414e-04  8.290265e-04  7.408297e-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.0003873973 0.009257149 0.005815643 0.005684468 0.00233025
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.0062309977 -0.0029357127  0.0366062228  0.052133319  4.992297e-02
## 2013-02-28  0.0058909609 -0.0231050020 -0.0129690026  0.016175342  1.267830e-02
## 2013-03-28  0.0009851048 -0.0102351130  0.0129690026  0.040258296  3.726812e-02
## 2013-04-30  0.0096391940  0.0120848926  0.0489678978  0.001222538  1.903004e-02
## 2013-05-31 -0.0202139843 -0.0494836395 -0.0306556819  0.041976381  2.333583e-02
## 2013-06-28 -0.0157786224 -0.0547282188 -0.0271444361 -0.001403395 -1.343501e-02
## 2013-07-31  0.0026879998  0.0131597970  0.0518603956  0.063541526  5.038594e-02
## 2013-08-30 -0.0082978778 -0.0257058995 -0.0197465144 -0.034743382 -3.045088e-02
## 2013-09-30  0.0111439308  0.0695889676  0.0753387871  0.063873526  3.115580e-02
## 2013-10-31  0.0082916040  0.0408611805  0.0320817367  0.034234167  4.526650e-02
## 2013-11-29 -0.0025097175 -0.0025938424  0.0054494356  0.041660909  2.920679e-02
## 2013-12-31 -0.0055832380 -0.0040743561  0.0215280892  0.012892298  2.559636e-02
## 2014-01-31  0.0152917863 -0.0903226541 -0.0534133526 -0.035775311 -3.588465e-02
## 2014-02-28  0.0037570354  0.0332204819  0.0595050079  0.045257347  4.451025e-02
## 2014-03-31 -0.0014819416  0.0380215189 -0.0046024936  0.013315313  8.261499e-03
## 2014-04-30  0.0081837411  0.0077730312  0.0165292653 -0.023184520  6.927455e-03
## 2014-05-30  0.0117215491  0.0290911888  0.0158287552  0.006205779  2.294097e-02
## 2014-06-30 -0.0005760294  0.0237337140  0.0091650314  0.037718411  2.043492e-02
## 2014-07-31 -0.0025124403  0.0135556888 -0.0263794244 -0.052009590 -1.352888e-02
## 2014-08-29  0.0114303447  0.0279048239  0.0018002409  0.043658274  3.870486e-02
## 2014-09-30 -0.0061661293 -0.0808571130 -0.0395984581 -0.061260389 -1.389246e-02
## 2014-10-31  0.0105836108  0.0140965746 -0.0026548517  0.068874558  2.327771e-02
## 2014-11-28  0.0065495480 -0.0155412100  0.0006253294  0.004773809  2.710185e-02
## 2014-12-31  0.0014750640 -0.0404424236 -0.0407466441  0.025296060 -2.540002e-03
## 2015-01-30  0.0203153489 -0.0068955139  0.0062263657 -0.054628515 -3.007700e-02
## 2015-02-27 -0.0089887550  0.0431362652  0.0614506677  0.056914852  5.468152e-02
## 2015-03-31  0.0037401676 -0.0150863009 -0.0143887121  0.010156390 -1.583015e-02
## 2015-04-30 -0.0032326856  0.0662812399  0.0358163424 -0.018417649  9.785875e-03
## 2015-05-29 -0.0043839023 -0.0419109266  0.0019527895  0.007509709  1.277442e-02
## 2015-06-30 -0.0108251372 -0.0297468259 -0.0316786818  0.004171665 -2.052141e-02
## 2015-07-31  0.0085844053 -0.0651780301  0.0201143349 -0.027375343  2.233793e-02
## 2015-08-31 -0.0033639692 -0.0925122687 -0.0771525898 -0.047268426 -6.288663e-02
## 2015-09-30  0.0080818984 -0.0318248859 -0.0451948535 -0.038464921 -2.584744e-02
## 2015-10-30  0.0006852694  0.0618082062  0.0640261675  0.063589691  8.163527e-02
## 2015-11-30 -0.0038985351 -0.0255604049 -0.0075559791  0.024415509  3.648421e-03
## 2015-12-31 -0.0019189009 -0.0389469911 -0.0235949659 -0.052157079 -1.743350e-02
## 2016-01-29  0.0123298390 -0.0516366924 -0.0567580600 -0.060306805 -5.106859e-02
## 2016-02-29  0.0088320416 -0.0082116913 -0.0339139315  0.020604699 -8.262435e-04
## 2016-03-31  0.0087090439  0.1218790281  0.0637458564  0.089910492  6.509989e-02
## 2016-04-29  0.0025462953  0.0040791728  0.0219751047  0.021044325  3.933689e-03
## 2016-05-31  0.0001352417 -0.0376286057 -0.0008560851  0.004397180  1.686850e-02
## 2016-06-30  0.0191667107  0.0445825661 -0.0244917047  0.008292399  3.469760e-03
## 2016-07-29  0.0054294192  0.0524424201  0.0390004193  0.049348136  3.582208e-02
## 2016-08-31 -0.0021560913  0.0087982769  0.0053270996  0.011261275  1.196647e-03
## 2016-09-30  0.0005160424  0.0248727925  0.0132789926  0.008614665  5.818891e-05
## 2016-10-31 -0.0082056292 -0.0083118887 -0.0224038355 -0.038134959 -1.748939e-02
## 2016-11-30 -0.0259896266 -0.0451623342 -0.0179744890  0.125246604  3.617608e-02
## 2016-12-30  0.0025381475 -0.0025297208  0.0267029049  0.031491863  2.006930e-02
## 2017-01-31  0.0021262171  0.0644312942  0.0323819966 -0.012144208  1.773640e-02
## 2017-02-28  0.0064384964  0.0172579598  0.0118364314  0.013428710  3.853925e-02
## 2017-03-31 -0.0005533960  0.0361889168  0.0318055923 -0.006532745  1.249122e-03
## 2017-04-28  0.0090292563  0.0168664096  0.0239523312  0.005107451  9.877155e-03
## 2017-05-31  0.0068474451  0.0280600145  0.0348102184 -0.022862424  1.401428e-02
## 2017-06-30 -0.0001823011  0.0092235499  0.0029559259  0.029152015  6.354939e-03
## 2017-07-31  0.0033338773  0.0565947503  0.0261878079  0.007481403  2.034558e-02
## 2017-08-31  0.0093695048  0.0232440208 -0.0004482150 -0.027564835  2.913498e-03
## 2017-09-29 -0.0057323504 -0.0004464641  0.0233428112  0.082321971  1.994907e-02
## 2017-10-31  0.0009780019  0.0322783985  0.0166536509  0.005915981  2.329095e-02
## 2017-11-30 -0.0014844967 -0.0038968199  0.0068700306  0.036913144  3.010808e-02
## 2017-12-29  0.0047405995  0.0369254159  0.0133980709 -0.003731191  1.205462e-02
calculate_component_contributions <- 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
    w <- c(0.25, 0.25, 0.2, 0.2, 0.1)
    
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
    sd_portfolio
    
    
    # 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]
    
    rowSums(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_contributions(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_contributions(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")

Column Chart of Component Contribution and weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contributions(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
## # A tibble: 10 × 3
##    Asset type         value
##    <chr> <chr>        <dbl>
##  1 AGG   Contribution 0.017
##  2 AGG   weight       0.25 
##  3 EEM   Contribution 0.394
##  4 EEM   weight       0.25 
##  5 EFA   Contribution 0.248
##  6 EFA   weight       0.2  
##  7 IJS   Contribution 0.242
##  8 IJS   weight       0.2  
##  9 SPY   Contribution 0.099
## 10 SPY   weight       0.1
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)

## 6 Rolling Component Contribution