# 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.0062302927 -0.0029352913  0.0366062115  0.052133457  4.992305e-02
## 2013-02-28  0.0058905782 -0.0231050908 -0.0129693888  0.016175437  1.267809e-02
## 2013-03-28  0.0009854157 -0.0102352633  0.0129693888  0.040258325  3.726781e-02
## 2013-04-30  0.0096394909  0.0120846946  0.0489677831  0.001221994  1.903106e-02
## 2013-05-31 -0.0202143643 -0.0494834629 -0.0306555798  0.041976751  2.333430e-02
## 2013-06-28 -0.0157784217 -0.0547279748 -0.0271445021 -0.001402677 -1.343397e-02
## 2013-07-31  0.0026879583  0.0131594934  0.0518601274  0.063540699  5.038589e-02
## 2013-08-30 -0.0082980141 -0.0257058497 -0.0197463310 -0.034743368 -3.045108e-02
## 2013-09-30  0.0111439834  0.0695891314  0.0753388099  0.063873586  3.115569e-02
## 2013-10-31  0.0082913993  0.0408612834  0.0320814746  0.034234531  4.526625e-02
## 2013-11-29 -0.0025095583 -0.0025943560  0.0054495485  0.041660969  2.920743e-02
## 2013-12-31 -0.0055829825 -0.0040742267  0.0215281427  0.012892113  2.559615e-02
## 2014-01-31  0.0152920247 -0.0903224366 -0.0534132658 -0.035775250 -3.588454e-02
## 2014-02-28  0.0037563730  0.0332206083  0.0595052499  0.045257554  4.451036e-02
## 2014-03-31 -0.0014809666  0.0380216451 -0.0046028175  0.013315178  8.261404e-03
## 2014-04-30  0.0081825890  0.0077725481  0.0165293669 -0.023184348  6.927267e-03
## 2014-05-30  0.0117215413  0.0290912819  0.0158284575  0.006205258  2.294137e-02
## 2014-06-30 -0.0005755318  0.0237336819  0.0091651560  0.037718647  2.043435e-02
## 2014-07-31 -0.0025120768  0.0135558798 -0.0263794758 -0.052009418 -1.352876e-02
## 2014-08-29  0.0114305610  0.0279044768  0.0018005325  0.043658036  3.870509e-02
## 2014-09-30 -0.0061671704 -0.0808568798 -0.0395985172 -0.061260394 -1.389235e-02
## 2014-10-31  0.0105849369  0.0140965953 -0.0026550052  0.068874476  2.327783e-02
## 2014-11-28  0.0065478737 -0.0155410501  0.0006255755  0.004774119  2.710120e-02
## 2014-12-31  0.0014760972 -0.0404422641 -0.0407468428  0.025295744 -2.539781e-03
## 2015-01-30  0.0203146406 -0.0068958337  0.0062265356 -0.054628257 -3.007728e-02
## 2015-02-27 -0.0089890729  0.0431362594  0.0614505374  0.056914883  5.468195e-02
## 2015-03-31  0.0037409354 -0.0150867401 -0.0143888162  0.010156248 -1.583055e-02
## 2015-04-30 -0.0032332313  0.0662818809  0.0358165749 -0.018417591  9.786048e-03
## 2015-05-29 -0.0043833269 -0.0419110809  0.0019526270  0.007509870  1.277463e-02
## 2015-06-30 -0.0108253310 -0.0297466891 -0.0316786936  0.004171290 -2.052129e-02
## 2015-07-31  0.0085849517 -0.0651779685  0.0201145423 -0.027375265  2.233761e-02
## 2015-08-31 -0.0033646601 -0.0925126998 -0.0771526018 -0.047268546 -6.288631e-02
## 2015-09-30  0.0080822849 -0.0318247351 -0.0451946305 -0.038464596 -2.584771e-02
## 2015-10-30  0.0006847700  0.0618081449  0.0640255883  0.063589472  8.163551e-02
## 2015-11-30 -0.0038979579 -0.0255604580 -0.0075556617  0.024415420  3.648178e-03
## 2015-12-31 -0.0019188521 -0.0389468676 -0.0235952704 -0.052156787 -1.743367e-02
## 2016-01-29  0.0123301636 -0.0516366891 -0.0567577625 -0.060307190 -5.106844e-02
## 2016-02-29  0.0088314029 -0.0082116556 -0.0339139816  0.020605346 -8.262561e-04
## 2016-03-31  0.0087089570  0.1218791326  0.0637459318  0.089910361  6.510023e-02
## 2016-04-29  0.0025457931  0.0040789262  0.0219750282  0.021044070  3.933526e-03
## 2016-05-31  0.0001356111 -0.0376283396 -0.0008559458  0.004397080  1.686823e-02
## 2016-06-30  0.0191673261  0.0445822687 -0.0244915284  0.008292548  3.470097e-03
## 2016-07-29  0.0054295998  0.0524423503  0.0390000219  0.049347993  3.582182e-02
## 2016-08-31 -0.0021566266  0.0087983934  0.0053270101  0.011261406  1.196993e-03
## 2016-09-30  0.0005162332  0.0248730249  0.0132790097  0.008614493  5.774279e-05
## 2016-10-31 -0.0082053667 -0.0083122258 -0.0224035338 -0.038134780 -1.748892e-02
## 2016-11-30 -0.0259895200 -0.0451616860 -0.0179745848  0.125246514  3.617590e-02
## 2016-12-30  0.0025377152 -0.0025300301  0.0267030708  0.031491793  2.006902e-02
## 2017-01-31  0.0021262845  0.0644315795  0.0323818337 -0.012143994  1.773637e-02
## 2017-02-28  0.0064380924  0.0172575484  0.0118363809  0.013428955  3.853956e-02
## 2017-03-31 -0.0005532387  0.0361890282  0.0318056496 -0.006533109  1.249236e-03
## 2017-04-28  0.0090293021  0.0168665395  0.0239523752  0.005107441  9.876888e-03
## 2017-05-31  0.0068479508  0.0280595725  0.0348101430 -0.022862464  1.401455e-02
## 2017-06-30 -0.0001831323  0.0092241272  0.0029558904  0.029151720  6.354352e-03
## 2017-07-31  0.0033343627  0.0565943117  0.0261879023  0.007481850  2.034620e-02
## 2017-08-31  0.0093693956  0.0232439582 -0.0004482752 -0.027565014  2.913381e-03
## 2017-09-29 -0.0057328074 -0.0004463429  0.0233427373  0.082322037  1.994892e-02
## 2017-10-31  0.0009781441  0.0322786757  0.0166536696  0.005915989  2.329068e-02
## 2017-11-30 -0.0014832321 -0.0038972355  0.0068699463  0.036913233  3.010829e-02
## 2017-12-29  0.0047395980  0.0369254387  0.0133984322 -0.003731062  1.205494e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398433e-05 0.0001042075 4.177982e-05 -7.812093e-05 -9.032063e-06
## EEM  1.042075e-04 0.0017547125 1.039018e-03  6.437724e-04  6.795448e-04
## EFA  4.177982e-05 0.0010390177 1.064237e-03  6.490277e-04  6.975412e-04
## IJS -7.812093e-05 0.0006437724 6.490277e-04  1.565450e-03  8.290246e-04
## SPY -9.032063e-06 0.0006795448 6.975412e-04  8.290246e-04  7.408294e-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.02347489
# 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.0003873981 0.00925715 0.00581563 0.005684464 0.002330252
rowSums(component_contribution)
## [1] 0.02347489
# 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 worm
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.0062302927 -0.0029352913  0.0366062115  0.052133457  4.992305e-02
## 2013-02-28  0.0058905782 -0.0231050908 -0.0129693888  0.016175437  1.267809e-02
## 2013-03-28  0.0009854157 -0.0102352633  0.0129693888  0.040258325  3.726781e-02
## 2013-04-30  0.0096394909  0.0120846946  0.0489677831  0.001221994  1.903106e-02
## 2013-05-31 -0.0202143643 -0.0494834629 -0.0306555798  0.041976751  2.333430e-02
## 2013-06-28 -0.0157784217 -0.0547279748 -0.0271445021 -0.001402677 -1.343397e-02
## 2013-07-31  0.0026879583  0.0131594934  0.0518601274  0.063540699  5.038589e-02
## 2013-08-30 -0.0082980141 -0.0257058497 -0.0197463310 -0.034743368 -3.045108e-02
## 2013-09-30  0.0111439834  0.0695891314  0.0753388099  0.063873586  3.115569e-02
## 2013-10-31  0.0082913993  0.0408612834  0.0320814746  0.034234531  4.526625e-02
## 2013-11-29 -0.0025095583 -0.0025943560  0.0054495485  0.041660969  2.920743e-02
## 2013-12-31 -0.0055829825 -0.0040742267  0.0215281427  0.012892113  2.559615e-02
## 2014-01-31  0.0152920247 -0.0903224366 -0.0534132658 -0.035775250 -3.588454e-02
## 2014-02-28  0.0037563730  0.0332206083  0.0595052499  0.045257554  4.451036e-02
## 2014-03-31 -0.0014809666  0.0380216451 -0.0046028175  0.013315178  8.261404e-03
## 2014-04-30  0.0081825890  0.0077725481  0.0165293669 -0.023184348  6.927267e-03
## 2014-05-30  0.0117215413  0.0290912819  0.0158284575  0.006205258  2.294137e-02
## 2014-06-30 -0.0005755318  0.0237336819  0.0091651560  0.037718647  2.043435e-02
## 2014-07-31 -0.0025120768  0.0135558798 -0.0263794758 -0.052009418 -1.352876e-02
## 2014-08-29  0.0114305610  0.0279044768  0.0018005325  0.043658036  3.870509e-02
## 2014-09-30 -0.0061671704 -0.0808568798 -0.0395985172 -0.061260394 -1.389235e-02
## 2014-10-31  0.0105849369  0.0140965953 -0.0026550052  0.068874476  2.327783e-02
## 2014-11-28  0.0065478737 -0.0155410501  0.0006255755  0.004774119  2.710120e-02
## 2014-12-31  0.0014760972 -0.0404422641 -0.0407468428  0.025295744 -2.539781e-03
## 2015-01-30  0.0203146406 -0.0068958337  0.0062265356 -0.054628257 -3.007728e-02
## 2015-02-27 -0.0089890729  0.0431362594  0.0614505374  0.056914883  5.468195e-02
## 2015-03-31  0.0037409354 -0.0150867401 -0.0143888162  0.010156248 -1.583055e-02
## 2015-04-30 -0.0032332313  0.0662818809  0.0358165749 -0.018417591  9.786048e-03
## 2015-05-29 -0.0043833269 -0.0419110809  0.0019526270  0.007509870  1.277463e-02
## 2015-06-30 -0.0108253310 -0.0297466891 -0.0316786936  0.004171290 -2.052129e-02
## 2015-07-31  0.0085849517 -0.0651779685  0.0201145423 -0.027375265  2.233761e-02
## 2015-08-31 -0.0033646601 -0.0925126998 -0.0771526018 -0.047268546 -6.288631e-02
## 2015-09-30  0.0080822849 -0.0318247351 -0.0451946305 -0.038464596 -2.584771e-02
## 2015-10-30  0.0006847700  0.0618081449  0.0640255883  0.063589472  8.163551e-02
## 2015-11-30 -0.0038979579 -0.0255604580 -0.0075556617  0.024415420  3.648178e-03
## 2015-12-31 -0.0019188521 -0.0389468676 -0.0235952704 -0.052156787 -1.743367e-02
## 2016-01-29  0.0123301636 -0.0516366891 -0.0567577625 -0.060307190 -5.106844e-02
## 2016-02-29  0.0088314029 -0.0082116556 -0.0339139816  0.020605346 -8.262561e-04
## 2016-03-31  0.0087089570  0.1218791326  0.0637459318  0.089910361  6.510023e-02
## 2016-04-29  0.0025457931  0.0040789262  0.0219750282  0.021044070  3.933526e-03
## 2016-05-31  0.0001356111 -0.0376283396 -0.0008559458  0.004397080  1.686823e-02
## 2016-06-30  0.0191673261  0.0445822687 -0.0244915284  0.008292548  3.470097e-03
## 2016-07-29  0.0054295998  0.0524423503  0.0390000219  0.049347993  3.582182e-02
## 2016-08-31 -0.0021566266  0.0087983934  0.0053270101  0.011261406  1.196993e-03
## 2016-09-30  0.0005162332  0.0248730249  0.0132790097  0.008614493  5.774279e-05
## 2016-10-31 -0.0082053667 -0.0083122258 -0.0224035338 -0.038134780 -1.748892e-02
## 2016-11-30 -0.0259895200 -0.0451616860 -0.0179745848  0.125246514  3.617590e-02
## 2016-12-30  0.0025377152 -0.0025300301  0.0267030708  0.031491793  2.006902e-02
## 2017-01-31  0.0021262845  0.0644315795  0.0323818337 -0.012143994  1.773637e-02
## 2017-02-28  0.0064380924  0.0172575484  0.0118363809  0.013428955  3.853956e-02
## 2017-03-31 -0.0005532387  0.0361890282  0.0318056496 -0.006533109  1.249236e-03
## 2017-04-28  0.0090293021  0.0168665395  0.0239523752  0.005107441  9.876888e-03
## 2017-05-31  0.0068479508  0.0280595725  0.0348101430 -0.022862464  1.401455e-02
## 2017-06-30 -0.0001831323  0.0092241272  0.0029558904  0.029151720  6.354352e-03
## 2017-07-31  0.0033343627  0.0565943117  0.0261879023  0.007481850  2.034620e-02
## 2017-08-31  0.0093693956  0.0232439582 -0.0004482752 -0.027565014  2.913381e-03
## 2017-09-29 -0.0057328074 -0.0004463429  0.0233427373  0.082322037  1.994892e-02
## 2017-10-31  0.0009781441  0.0322786757  0.0166536696  0.005915989  2.329068e-02
## 2017-11-30 -0.0014832321 -0.0038972355  0.0068699463  0.036913233  3.010829e-02
## 2017-12-29  0.0047395980  0.0369254387  0.0133984322 -0.003731062  1.205494e-02
calculate_component_contribution <- function(.data, w) {

    # Covariance of asset returns
    covariance_matrix <- cov(.data)

    # Standard deviation of portfolio
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

    # Component contribution
    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",
         x = "Asset",
         y = "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 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")