# 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.0062311656 -0.0029355736  0.0366061192  0.052133211  4.992323e-02
## 2013-02-28  0.0058908080 -0.0231052330 -0.0129693872  0.016175722  1.267796e-02
## 2013-03-28  0.0009853505 -0.0102353537  0.0129693872  0.040258125  3.726847e-02
## 2013-04-30  0.0096395686  0.0120850211  0.0489678953  0.001222481  1.903005e-02
## 2013-05-31 -0.0202145256 -0.0494832642 -0.0306555943  0.041976139  2.333527e-02
## 2013-06-28 -0.0157778200 -0.0547283223 -0.0271444699 -0.001402810 -1.343470e-02
## 2013-07-31  0.0026876429  0.0131593267  0.0518602560  0.063541572  5.038636e-02
## 2013-08-30 -0.0082980793 -0.0257051122 -0.0197462881 -0.034743607 -3.045120e-02
## 2013-09-30  0.0111440501  0.0695886503  0.0753384162  0.063873497  3.115588e-02
## 2013-10-31  0.0082915285  0.0408614106  0.0320817183  0.034234151  4.526635e-02
## 2013-11-29 -0.0025099208 -0.0025942551  0.0054497013  0.041661368  2.920691e-02
## 2013-12-31 -0.0055829683 -0.0040743618  0.0215282094  0.012891972  2.559600e-02
## 2014-01-31  0.0152919827 -0.0903224971 -0.0534132939 -0.035775468 -3.588444e-02
## 2014-02-28  0.0037564634  0.0332204154  0.0595050757  0.045257183  4.451081e-02
## 2014-03-31 -0.0014812664  0.0380215286 -0.0046029525  0.013315670  8.261035e-03
## 2014-04-30  0.0081826725  0.0077729759  0.0165294491 -0.023184118  6.927565e-03
## 2014-05-30  0.0117222318  0.0290911914  0.0158285760  0.006205059  2.294113e-02
## 2014-06-30 -0.0005761722  0.0237338338  0.0091653334  0.037718571  2.043459e-02
## 2014-07-31 -0.0025122201  0.0135556828 -0.0263797056 -0.052009271 -1.352872e-02
## 2014-08-29  0.0114310023  0.0279044730  0.0018002703  0.043657703  3.870481e-02
## 2014-09-30 -0.0061672707 -0.0808565855 -0.0395984966 -0.061260437 -1.389242e-02
## 2014-10-31  0.0105844841  0.0140964533 -0.0026547321  0.068874903  2.327797e-02
## 2014-11-28  0.0065487360 -0.0155411837  0.0006252305  0.004773801  2.710134e-02
## 2014-12-31  0.0014751869 -0.0404422264 -0.0407466515  0.025295591 -2.539611e-03
## 2015-01-30  0.0203153061 -0.0068954369  0.0062266693 -0.054627717 -3.007716e-02
## 2015-02-27 -0.0089884433  0.0431359538  0.0614504530  0.056914483  5.468184e-02
## 2015-03-31  0.0037405709 -0.0150864834 -0.0143887845  0.010156527 -1.583031e-02
## 2015-04-30 -0.0032335969  0.0662814223  0.0358166578 -0.018417750  9.786164e-03
## 2015-05-29 -0.0043836861 -0.0419107925  0.0019527233  0.007510029  1.277369e-02
## 2015-06-30 -0.0108249832 -0.0297468417 -0.0316790564  0.004171308 -2.052076e-02
## 2015-07-31  0.0085844222 -0.0651778834  0.0201146380 -0.027375425  2.233760e-02
## 2015-08-31 -0.0033636653 -0.0925124547 -0.0771525685 -0.047268502 -6.288692e-02
## 2015-09-30  0.0080812383 -0.0318250603 -0.0451946417 -0.038464548 -2.584699e-02
## 2015-10-30  0.0006855427  0.0618084371  0.0640258005  0.063589562  8.163502e-02
## 2015-11-30 -0.0038979773 -0.0255606045 -0.0075558800  0.024415388  3.648562e-03
## 2015-12-31 -0.0019192654 -0.0389470337 -0.0235949460 -0.052157193 -1.743373e-02
## 2016-01-29  0.0123302160 -0.0516366758 -0.0567577266 -0.060306697 -5.106853e-02
## 2016-02-29  0.0088310462 -0.0082115296 -0.0339141694  0.020605228 -8.263321e-04
## 2016-03-31  0.0087089597  0.1218789765  0.0637456938  0.089910422  6.510043e-02
## 2016-04-29  0.0025467174  0.0040792159  0.0219751003  0.021044144  3.932897e-03
## 2016-05-31  0.0001352651 -0.0376285078 -0.0008560461  0.004397174  1.686912e-02
## 2016-06-30  0.0191668870  0.0445822035 -0.0244915383  0.008292116  3.469733e-03
## 2016-07-29  0.0054291030  0.0524424134  0.0390002829  0.049348536  3.582173e-02
## 2016-08-31 -0.0021557650  0.0087984749  0.0053267805  0.011261145  1.196782e-03
## 2016-09-30  0.0005158996  0.0248729338  0.0132792408  0.008614374  5.803866e-05
## 2016-10-31 -0.0082051511 -0.0083123122 -0.0224037499 -0.038134756 -1.748916e-02
## 2016-11-30 -0.0259897217 -0.0451618992 -0.0179744835  0.125246426  3.617637e-02
## 2016-12-30  0.0025379425 -0.0025298823  0.0267029452  0.031491849  2.006904e-02
## 2017-01-31  0.0021265613  0.0644313953  0.0323820361 -0.012144093  1.773618e-02
## 2017-02-28  0.0064378794  0.0172578390  0.0118362329  0.013429101  3.853939e-02
## 2017-03-31 -0.0005530944  0.0361887584  0.0318057133 -0.006533260  1.249144e-03
## 2017-04-28  0.0090292053  0.0168665151  0.0239522160  0.005107603  9.877223e-03
## 2017-05-31  0.0068472158  0.0280602073  0.0348102595 -0.022862262  1.401429e-02
## 2017-06-30 -0.0001824719  0.0092235481  0.0029559265  0.029151394  6.354541e-03
## 2017-07-31  0.0033342260  0.0565944456  0.0261878679  0.007481745  2.034599e-02
## 2017-08-31  0.0093691094  0.0232439755 -0.0004482935 -0.027564842  2.913491e-03
## 2017-09-29 -0.0057320465 -0.0004460965  0.0233429117  0.082321928  1.994900e-02
## 2017-10-31  0.0009778658  0.0322784686  0.0166535938  0.005915814  2.329090e-02
## 2017-11-30 -0.0014840882 -0.0038971454  0.0068699046  0.036913020  3.010791e-02
## 2017-12-29  0.0047400482  0.0369253964  0.0133984028 -0.003731088  1.205519e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398400e-05 0.0001042085 4.178121e-05 -7.812025e-05 -9.033085e-06
## EEM  1.042085e-04 0.0017547085 1.039016e-03  6.437707e-04  6.795430e-04
## EFA  4.178121e-05 0.0010390164 1.064238e-03  6.490282e-04  6.975407e-04
## IJS -7.812025e-05 0.0006437707 6.490282e-04  1.565449e-03  8.290264e-04
## SPY -9.033085e-06 0.0006795430 6.975407e-04  8.290264e-04  7.408310e-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.0003874035 0.009257137 0.005815633 0.005684463 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

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.0062311656 -0.0029355736  0.0366061192  0.052133211  4.992323e-02
## 2013-02-28  0.0058908080 -0.0231052330 -0.0129693872  0.016175722  1.267796e-02
## 2013-03-28  0.0009853505 -0.0102353537  0.0129693872  0.040258125  3.726847e-02
## 2013-04-30  0.0096395686  0.0120850211  0.0489678953  0.001222481  1.903005e-02
## 2013-05-31 -0.0202145256 -0.0494832642 -0.0306555943  0.041976139  2.333527e-02
## 2013-06-28 -0.0157778200 -0.0547283223 -0.0271444699 -0.001402810 -1.343470e-02
## 2013-07-31  0.0026876429  0.0131593267  0.0518602560  0.063541572  5.038636e-02
## 2013-08-30 -0.0082980793 -0.0257051122 -0.0197462881 -0.034743607 -3.045120e-02
## 2013-09-30  0.0111440501  0.0695886503  0.0753384162  0.063873497  3.115588e-02
## 2013-10-31  0.0082915285  0.0408614106  0.0320817183  0.034234151  4.526635e-02
## 2013-11-29 -0.0025099208 -0.0025942551  0.0054497013  0.041661368  2.920691e-02
## 2013-12-31 -0.0055829683 -0.0040743618  0.0215282094  0.012891972  2.559600e-02
## 2014-01-31  0.0152919827 -0.0903224971 -0.0534132939 -0.035775468 -3.588444e-02
## 2014-02-28  0.0037564634  0.0332204154  0.0595050757  0.045257183  4.451081e-02
## 2014-03-31 -0.0014812664  0.0380215286 -0.0046029525  0.013315670  8.261035e-03
## 2014-04-30  0.0081826725  0.0077729759  0.0165294491 -0.023184118  6.927565e-03
## 2014-05-30  0.0117222318  0.0290911914  0.0158285760  0.006205059  2.294113e-02
## 2014-06-30 -0.0005761722  0.0237338338  0.0091653334  0.037718571  2.043459e-02
## 2014-07-31 -0.0025122201  0.0135556828 -0.0263797056 -0.052009271 -1.352872e-02
## 2014-08-29  0.0114310023  0.0279044730  0.0018002703  0.043657703  3.870481e-02
## 2014-09-30 -0.0061672707 -0.0808565855 -0.0395984966 -0.061260437 -1.389242e-02
## 2014-10-31  0.0105844841  0.0140964533 -0.0026547321  0.068874903  2.327797e-02
## 2014-11-28  0.0065487360 -0.0155411837  0.0006252305  0.004773801  2.710134e-02
## 2014-12-31  0.0014751869 -0.0404422264 -0.0407466515  0.025295591 -2.539611e-03
## 2015-01-30  0.0203153061 -0.0068954369  0.0062266693 -0.054627717 -3.007716e-02
## 2015-02-27 -0.0089884433  0.0431359538  0.0614504530  0.056914483  5.468184e-02
## 2015-03-31  0.0037405709 -0.0150864834 -0.0143887845  0.010156527 -1.583031e-02
## 2015-04-30 -0.0032335969  0.0662814223  0.0358166578 -0.018417750  9.786164e-03
## 2015-05-29 -0.0043836861 -0.0419107925  0.0019527233  0.007510029  1.277369e-02
## 2015-06-30 -0.0108249832 -0.0297468417 -0.0316790564  0.004171308 -2.052076e-02
## 2015-07-31  0.0085844222 -0.0651778834  0.0201146380 -0.027375425  2.233760e-02
## 2015-08-31 -0.0033636653 -0.0925124547 -0.0771525685 -0.047268502 -6.288692e-02
## 2015-09-30  0.0080812383 -0.0318250603 -0.0451946417 -0.038464548 -2.584699e-02
## 2015-10-30  0.0006855427  0.0618084371  0.0640258005  0.063589562  8.163502e-02
## 2015-11-30 -0.0038979773 -0.0255606045 -0.0075558800  0.024415388  3.648562e-03
## 2015-12-31 -0.0019192654 -0.0389470337 -0.0235949460 -0.052157193 -1.743373e-02
## 2016-01-29  0.0123302160 -0.0516366758 -0.0567577266 -0.060306697 -5.106853e-02
## 2016-02-29  0.0088310462 -0.0082115296 -0.0339141694  0.020605228 -8.263321e-04
## 2016-03-31  0.0087089597  0.1218789765  0.0637456938  0.089910422  6.510043e-02
## 2016-04-29  0.0025467174  0.0040792159  0.0219751003  0.021044144  3.932897e-03
## 2016-05-31  0.0001352651 -0.0376285078 -0.0008560461  0.004397174  1.686912e-02
## 2016-06-30  0.0191668870  0.0445822035 -0.0244915383  0.008292116  3.469733e-03
## 2016-07-29  0.0054291030  0.0524424134  0.0390002829  0.049348536  3.582173e-02
## 2016-08-31 -0.0021557650  0.0087984749  0.0053267805  0.011261145  1.196782e-03
## 2016-09-30  0.0005158996  0.0248729338  0.0132792408  0.008614374  5.803866e-05
## 2016-10-31 -0.0082051511 -0.0083123122 -0.0224037499 -0.038134756 -1.748916e-02
## 2016-11-30 -0.0259897217 -0.0451618992 -0.0179744835  0.125246426  3.617637e-02
## 2016-12-30  0.0025379425 -0.0025298823  0.0267029452  0.031491849  2.006904e-02
## 2017-01-31  0.0021265613  0.0644313953  0.0323820361 -0.012144093  1.773618e-02
## 2017-02-28  0.0064378794  0.0172578390  0.0118362329  0.013429101  3.853939e-02
## 2017-03-31 -0.0005530944  0.0361887584  0.0318057133 -0.006533260  1.249144e-03
## 2017-04-28  0.0090292053  0.0168665151  0.0239522160  0.005107603  9.877223e-03
## 2017-05-31  0.0068472158  0.0280602073  0.0348102595 -0.022862262  1.401429e-02
## 2017-06-30 -0.0001824719  0.0092235481  0.0029559265  0.029151394  6.354541e-03
## 2017-07-31  0.0033342260  0.0565944456  0.0261878679  0.007481745  2.034599e-02
## 2017-08-31  0.0093691094  0.0232439755 -0.0004482935 -0.027564842  2.913491e-03
## 2017-09-29 -0.0057320465 -0.0004460965  0.0233429117  0.082321928  1.994900e-02
## 2017-10-31  0.0009778658  0.0322784686  0.0166535938  0.005915814  2.329090e-02
## 2017-11-30 -0.0014840882 -0.0038971454  0.0068699046  0.036913020  3.010791e-02
## 2017-12-29  0.0047400482  0.0369253964  0.0133984028 -0.003731088  1.205519e-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 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 Weight
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