# 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.0062315751 -0.0029358358  0.0366062715  0.052132976  4.992314e-02
## 2013-02-28  0.0058910156 -0.0231048248 -0.0129695485  0.016175510  1.267823e-02
## 2013-03-28  0.0009851287 -0.0102352068  0.0129695485  0.040258487  3.726816e-02
## 2013-04-30  0.0096392326  0.0120846787  0.0489678048  0.001221836  1.903004e-02
## 2013-05-31 -0.0202145329 -0.0494836781 -0.0306558682  0.041976625  2.333506e-02
## 2013-06-28 -0.0157780043 -0.0547281253 -0.0271443950 -0.001403072 -1.343387e-02
## 2013-07-31  0.0026878088  0.0131597634  0.0518604449  0.063541331  5.038584e-02
## 2013-08-30 -0.0082974233 -0.0257054798 -0.0197462369 -0.034743251 -3.045161e-02
## 2013-09-30  0.0111432792  0.0695887098  0.0753385205  0.063873298  3.115592e-02
## 2013-10-31  0.0082923092  0.0408611609  0.0320815816  0.034234385  4.526649e-02
## 2013-11-29 -0.0025105908 -0.0025940684  0.0054496390  0.041660918  2.920743e-02
## 2013-12-31 -0.0055825599 -0.0040740475  0.0215280242  0.012892141  2.559595e-02
## 2014-01-31  0.0152916706 -0.0903228294 -0.0534133267 -0.035775251 -3.588439e-02
## 2014-02-28  0.0037569264  0.0332206312  0.0595050652  0.045257450  4.450989e-02
## 2014-03-31 -0.0014819323  0.0380215360 -0.0046025007  0.013315323  8.261375e-03
## 2014-04-30  0.0081836581  0.0077728543  0.0165292306 -0.023184432  6.927409e-03
## 2014-05-30  0.0117216272  0.0290913105  0.0158285335  0.006205406  2.294156e-02
## 2014-06-30 -0.0005759444  0.0237336330  0.0091655922  0.037718692  2.043469e-02
## 2014-07-31 -0.0025120688  0.0135557354 -0.0263798039 -0.052009416 -1.352885e-02
## 2014-08-29  0.0114308227  0.0279045126  0.0018003560  0.043657874  3.870467e-02
## 2014-09-30 -0.0061670131 -0.0808566992 -0.0395984011 -0.061260293 -1.389230e-02
## 2014-10-31  0.0105843370  0.0140965366 -0.0026548854  0.068874686  2.327790e-02
## 2014-11-28  0.0065487484 -0.0155410573  0.0006252274  0.004773575  2.710153e-02
## 2014-12-31  0.0014751092 -0.0404422589 -0.0407468319  0.025295976 -2.539900e-03
## 2015-01-30  0.0203149499 -0.0068959393  0.0062265907 -0.054628094 -3.007729e-02
## 2015-02-27 -0.0089880331  0.0431363863  0.0614505902  0.056914600  5.468210e-02
## 2015-03-31  0.0037403909 -0.0150865962 -0.0143887692  0.010156405 -1.583036e-02
## 2015-04-30 -0.0032333051  0.0662814080  0.0358165951 -0.018417602  9.786065e-03
## 2015-05-29 -0.0043835034 -0.0419107223  0.0019527689  0.007509937  1.277406e-02
## 2015-06-30 -0.0108257547 -0.0297470158 -0.0316788440  0.004171341 -2.052141e-02
## 2015-07-31  0.0085845634 -0.0651780132  0.0201144055 -0.027375403  2.233803e-02
## 2015-08-31 -0.0033636784 -0.0925122172 -0.0771524349 -0.047268298 -6.288667e-02
## 2015-09-30  0.0080816002 -0.0318249466 -0.0451949070 -0.038464836 -2.584700e-02
## 2015-10-30  0.0006852839  0.0618081097  0.0640260725  0.063589964  8.163477e-02
## 2015-11-30 -0.0038976709 -0.0255603548 -0.0075559842  0.024414968  3.648548e-03
## 2015-12-31 -0.0019189832 -0.0389469630 -0.0235950377 -0.052156987 -1.743346e-02
## 2016-01-29  0.0123295421 -0.0516367885 -0.0567578510 -0.060306912 -5.106896e-02
## 2016-02-29  0.0088318957 -0.0082114300 -0.0339139922  0.020605179 -8.262217e-04
## 2016-03-31  0.0087083361  0.1218788614  0.0637457118  0.089910342  6.510035e-02
## 2016-04-29  0.0025466504  0.0040792678  0.0219752947  0.021044254  3.933541e-03
## 2016-05-31  0.0001352850 -0.0376284126 -0.0008561489  0.004397117  1.686837e-02
## 2016-06-30  0.0191671403  0.0445823102 -0.0244915330  0.008292351  3.469921e-03
## 2016-07-29  0.0054292773  0.0524420211  0.0390002318  0.049348372  3.582186e-02
## 2016-08-31 -0.0021562179  0.0087985285  0.0053268061  0.011261141  1.196687e-03
## 2016-09-30  0.0005156823  0.0248730372  0.0132792447  0.008614539  5.835603e-05
## 2016-10-31 -0.0082050611 -0.0083122664 -0.0224038490 -0.038134852 -1.748925e-02
## 2016-11-30 -0.0259897417 -0.0451616456 -0.0179744407  0.125246484  3.617591e-02
## 2016-12-30  0.0025383656 -0.0025300701  0.0267028708  0.031491784  2.006928e-02
## 2017-01-31  0.0021260283  0.0644312746  0.0323819229 -0.012144001  1.773644e-02
## 2017-02-28  0.0064378729  0.0172580527  0.0118365242  0.013429051  3.853916e-02
## 2017-03-31 -0.0005527346  0.0361888122  0.0318056796 -0.006533345  1.249315e-03
## 2017-04-28  0.0090288611  0.0168666049  0.0239522284  0.005108001  9.877013e-03
## 2017-05-31  0.0068474606  0.0280597471  0.0348102134 -0.022862587  1.401434e-02
## 2017-06-30 -0.0001826990  0.0092237842  0.0029559197  0.029151621  6.354720e-03
## 2017-07-31  0.0033343277  0.0565946333  0.0261878539  0.007481462  2.034598e-02
## 2017-08-31  0.0093692406  0.0232437438 -0.0004482983 -0.027564548  2.913198e-03
## 2017-09-29 -0.0057319258 -0.0004462966  0.0233428581  0.082321800  1.994928e-02
## 2017-10-31  0.0009778856  0.0322785406  0.0166536403  0.005915994  2.329059e-02
## 2017-11-30 -0.0014839332 -0.0038971054  0.0068697606  0.036913188  3.010813e-02
## 2017-12-29  0.0047399550  0.0369256124  0.0133985337 -0.003731148  1.205489e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398349e-05 0.0001042066 4.178069e-05 -7.812247e-05 -9.033003e-06
## EEM  1.042066e-04 0.0017547084 1.039017e-03  6.437720e-04  6.795431e-04
## EFA  4.178069e-05 0.0010390174 1.064239e-03  6.490287e-04  6.975411e-04
## IJS -7.812247e-05 0.0006437720 6.490287e-04  1.565449e-03  8.290259e-04
## SPY -9.033003e-06 0.0006795431 6.975411e-04  8.290259e-04  7.408295e-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.02347488
# 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.0003873914 0.009257138 0.005815638 0.005684464 0.002330252
rowSums(component_contribution)
## [1] 0.02347488
# 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.0062315751 -0.0029358358  0.0366062715  0.052132976  4.992314e-02
## 2013-02-28  0.0058910156 -0.0231048248 -0.0129695485  0.016175510  1.267823e-02
## 2013-03-28  0.0009851287 -0.0102352068  0.0129695485  0.040258487  3.726816e-02
## 2013-04-30  0.0096392326  0.0120846787  0.0489678048  0.001221836  1.903004e-02
## 2013-05-31 -0.0202145329 -0.0494836781 -0.0306558682  0.041976625  2.333506e-02
## 2013-06-28 -0.0157780043 -0.0547281253 -0.0271443950 -0.001403072 -1.343387e-02
## 2013-07-31  0.0026878088  0.0131597634  0.0518604449  0.063541331  5.038584e-02
## 2013-08-30 -0.0082974233 -0.0257054798 -0.0197462369 -0.034743251 -3.045161e-02
## 2013-09-30  0.0111432792  0.0695887098  0.0753385205  0.063873298  3.115592e-02
## 2013-10-31  0.0082923092  0.0408611609  0.0320815816  0.034234385  4.526649e-02
## 2013-11-29 -0.0025105908 -0.0025940684  0.0054496390  0.041660918  2.920743e-02
## 2013-12-31 -0.0055825599 -0.0040740475  0.0215280242  0.012892141  2.559595e-02
## 2014-01-31  0.0152916706 -0.0903228294 -0.0534133267 -0.035775251 -3.588439e-02
## 2014-02-28  0.0037569264  0.0332206312  0.0595050652  0.045257450  4.450989e-02
## 2014-03-31 -0.0014819323  0.0380215360 -0.0046025007  0.013315323  8.261375e-03
## 2014-04-30  0.0081836581  0.0077728543  0.0165292306 -0.023184432  6.927409e-03
## 2014-05-30  0.0117216272  0.0290913105  0.0158285335  0.006205406  2.294156e-02
## 2014-06-30 -0.0005759444  0.0237336330  0.0091655922  0.037718692  2.043469e-02
## 2014-07-31 -0.0025120688  0.0135557354 -0.0263798039 -0.052009416 -1.352885e-02
## 2014-08-29  0.0114308227  0.0279045126  0.0018003560  0.043657874  3.870467e-02
## 2014-09-30 -0.0061670131 -0.0808566992 -0.0395984011 -0.061260293 -1.389230e-02
## 2014-10-31  0.0105843370  0.0140965366 -0.0026548854  0.068874686  2.327790e-02
## 2014-11-28  0.0065487484 -0.0155410573  0.0006252274  0.004773575  2.710153e-02
## 2014-12-31  0.0014751092 -0.0404422589 -0.0407468319  0.025295976 -2.539900e-03
## 2015-01-30  0.0203149499 -0.0068959393  0.0062265907 -0.054628094 -3.007729e-02
## 2015-02-27 -0.0089880331  0.0431363863  0.0614505902  0.056914600  5.468210e-02
## 2015-03-31  0.0037403909 -0.0150865962 -0.0143887692  0.010156405 -1.583036e-02
## 2015-04-30 -0.0032333051  0.0662814080  0.0358165951 -0.018417602  9.786065e-03
## 2015-05-29 -0.0043835034 -0.0419107223  0.0019527689  0.007509937  1.277406e-02
## 2015-06-30 -0.0108257547 -0.0297470158 -0.0316788440  0.004171341 -2.052141e-02
## 2015-07-31  0.0085845634 -0.0651780132  0.0201144055 -0.027375403  2.233803e-02
## 2015-08-31 -0.0033636784 -0.0925122172 -0.0771524349 -0.047268298 -6.288667e-02
## 2015-09-30  0.0080816002 -0.0318249466 -0.0451949070 -0.038464836 -2.584700e-02
## 2015-10-30  0.0006852839  0.0618081097  0.0640260725  0.063589964  8.163477e-02
## 2015-11-30 -0.0038976709 -0.0255603548 -0.0075559842  0.024414968  3.648548e-03
## 2015-12-31 -0.0019189832 -0.0389469630 -0.0235950377 -0.052156987 -1.743346e-02
## 2016-01-29  0.0123295421 -0.0516367885 -0.0567578510 -0.060306912 -5.106896e-02
## 2016-02-29  0.0088318957 -0.0082114300 -0.0339139922  0.020605179 -8.262217e-04
## 2016-03-31  0.0087083361  0.1218788614  0.0637457118  0.089910342  6.510035e-02
## 2016-04-29  0.0025466504  0.0040792678  0.0219752947  0.021044254  3.933541e-03
## 2016-05-31  0.0001352850 -0.0376284126 -0.0008561489  0.004397117  1.686837e-02
## 2016-06-30  0.0191671403  0.0445823102 -0.0244915330  0.008292351  3.469921e-03
## 2016-07-29  0.0054292773  0.0524420211  0.0390002318  0.049348372  3.582186e-02
## 2016-08-31 -0.0021562179  0.0087985285  0.0053268061  0.011261141  1.196687e-03
## 2016-09-30  0.0005156823  0.0248730372  0.0132792447  0.008614539  5.835603e-05
## 2016-10-31 -0.0082050611 -0.0083122664 -0.0224038490 -0.038134852 -1.748925e-02
## 2016-11-30 -0.0259897417 -0.0451616456 -0.0179744407  0.125246484  3.617591e-02
## 2016-12-30  0.0025383656 -0.0025300701  0.0267028708  0.031491784  2.006928e-02
## 2017-01-31  0.0021260283  0.0644312746  0.0323819229 -0.012144001  1.773644e-02
## 2017-02-28  0.0064378729  0.0172580527  0.0118365242  0.013429051  3.853916e-02
## 2017-03-31 -0.0005527346  0.0361888122  0.0318056796 -0.006533345  1.249315e-03
## 2017-04-28  0.0090288611  0.0168666049  0.0239522284  0.005108001  9.877013e-03
## 2017-05-31  0.0068474606  0.0280597471  0.0348102134 -0.022862587  1.401434e-02
## 2017-06-30 -0.0001826990  0.0092237842  0.0029559197  0.029151621  6.354720e-03
## 2017-07-31  0.0033343277  0.0565946333  0.0261878539  0.007481462  2.034598e-02
## 2017-08-31  0.0093692406  0.0232437438 -0.0004482983 -0.027564548  2.913198e-03
## 2017-09-29 -0.0057319258 -0.0004462966  0.0233428581  0.082321800  1.994928e-02
## 2017-10-31  0.0009778856  0.0322785406  0.0166536403  0.005915994  2.329059e-02
## 2017-11-30 -0.0014839332 -0.0038971054  0.0068697606  0.036913188  3.010813e-02
## 2017-12-29  0.0047399550  0.0369256124  0.0133985337 -0.003731148  1.205489e-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 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")

Column Chart of Contribution and Weight

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") %>%

    # 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 %>% 
    
    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