# 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.0062306319 -0.0029350924  0.0366061757  0.052133040  4.992303e-02
## 2013-02-28  0.0058911593 -0.0231053950 -0.0129691395  0.016175587  1.267838e-02
## 2013-03-28  0.0009845243 -0.0102351118  0.0129691395  0.040257950  3.726836e-02
## 2013-04-30  0.0096397453  0.0120847681  0.0489679329  0.001222476  1.902962e-02
## 2013-05-31 -0.0202148068 -0.0494834238 -0.0306558102  0.041976617  2.333530e-02
## 2013-06-28 -0.0157775348 -0.0547284477 -0.0271445512 -0.001403091 -1.343421e-02
## 2013-07-31  0.0026874539  0.0131598125  0.0518606179  0.063541133  5.038614e-02
## 2013-08-30 -0.0082982301 -0.0257058122 -0.0197463849 -0.034743104 -3.045133e-02
## 2013-09-30  0.0111436453  0.0695890421  0.0753385007  0.063873514  3.115605e-02
## 2013-10-31  0.0082922834  0.0408612799  0.0320817073  0.034233892  4.526603e-02
## 2013-11-29 -0.0025098786 -0.0025941888  0.0054497573  0.041661214  2.920708e-02
## 2013-12-31 -0.0055828202 -0.0040740977  0.0215280098  0.012892145  2.559670e-02
## 2014-01-31  0.0152910874 -0.0903227907 -0.0534134386 -0.035775144 -3.588462e-02
## 2014-02-28  0.0037572021  0.0332206166  0.0595051909  0.045256946  4.451019e-02
## 2014-03-31 -0.0014817567  0.0380218568 -0.0046027569  0.013315383  8.261304e-03
## 2014-04-30  0.0081832844  0.0077726767  0.0165295734 -0.023184057  6.927086e-03
## 2014-05-30  0.0117219155  0.0290910706  0.0158282756  0.006205138  2.294165e-02
## 2014-06-30 -0.0005759939  0.0237339251  0.0091652306  0.037718891  2.043435e-02
## 2014-07-31 -0.0025121621  0.0135553808 -0.0263795809 -0.052009399 -1.352859e-02
## 2014-08-29  0.0114310309  0.0279047915  0.0018002278  0.043657667  3.870472e-02
## 2014-09-30 -0.0061675020 -0.0808568916 -0.0395983087 -0.061260226 -1.389208e-02
## 2014-10-31  0.0105846887  0.0140967699 -0.0026548127  0.068874846  2.327744e-02
## 2014-11-28  0.0065484188 -0.0155414346  0.0006253148  0.004773623  2.710147e-02
## 2014-12-31  0.0014752012 -0.0404420932 -0.0407466701  0.025295689 -2.539942e-03
## 2015-01-30  0.0203154344 -0.0068956260  0.0062265031 -0.054628070 -3.007721e-02
## 2015-02-27 -0.0089880866  0.0431358088  0.0614506029  0.056914746  5.468230e-02
## 2015-03-31  0.0037400788 -0.0150862412 -0.0143887693  0.010156418 -1.583039e-02
## 2015-04-30 -0.0032328950  0.0662813542  0.0358164231 -0.018417774  9.786066e-03
## 2015-05-29 -0.0043837555 -0.0419107892  0.0019526919  0.007509952  1.277408e-02
## 2015-06-30 -0.0108252116 -0.0297466579 -0.0316786710  0.004171319 -2.052151e-02
## 2015-07-31  0.0085845574 -0.0651780755  0.0201145100 -0.027375320  2.233815e-02
## 2015-08-31 -0.0033636164 -0.0925124157 -0.0771525110 -0.047268233 -6.288659e-02
## 2015-09-30  0.0080810055 -0.0318248596 -0.0451948664 -0.038464844 -2.584702e-02
## 2015-10-30  0.0006853394  0.0618080610  0.0640258639  0.063589755  8.163478e-02
## 2015-11-30 -0.0038978720 -0.0255602592 -0.0075557791  0.024415330  3.648755e-03
## 2015-12-31 -0.0019189096 -0.0389471503 -0.0235951165 -0.052157030 -1.743371e-02
## 2016-01-29  0.0123297225 -0.0516366816 -0.0567577476 -0.060306852 -5.106870e-02
## 2016-02-29  0.0088318463 -0.0082115380 -0.0339138891  0.020605130 -8.264513e-04
## 2016-03-31  0.0087088852  0.1218792034  0.0637455001  0.089910297  6.510026e-02
## 2016-04-29  0.0025461086  0.0040788845  0.0219753752  0.021044214  3.933534e-03
## 2016-05-31  0.0001353674 -0.0376283948 -0.0008560587  0.004397021  1.686847e-02
## 2016-06-30  0.0191665713  0.0445823977 -0.0244917258  0.008292459  3.469808e-03
## 2016-07-29  0.0054298583  0.0524421804  0.0390001995  0.049348374  3.582195e-02
## 2016-08-31 -0.0021561182  0.0087984416  0.0053270066  0.011261139  1.196796e-03
## 2016-09-30  0.0005157718  0.0248730626  0.0132790225  0.008614622  5.813242e-05
## 2016-10-31 -0.0082056509 -0.0083123531 -0.0224037254 -0.038135055 -1.748925e-02
## 2016-11-30 -0.0259890984 -0.0451620121 -0.0179743636  0.125246568  3.617611e-02
## 2016-12-30  0.0025383600 -0.0025297675  0.0267029495  0.031491998  2.006901e-02
## 2017-01-31  0.0021262975  0.0644314144  0.0323817944 -0.012144399  1.773628e-02
## 2017-02-28  0.0064372557  0.0172578913  0.0118363811  0.013429306  3.853948e-02
## 2017-03-31 -0.0005531165  0.0361887638  0.0318056750 -0.006532896  1.249205e-03
## 2017-04-28  0.0090296920  0.0168665141  0.0239523104  0.005107519  9.877324e-03
## 2017-05-31  0.0068473289  0.0280599633  0.0348101251 -0.022862494  1.401398e-02
## 2017-06-30 -0.0001825774  0.0092238315  0.0029558687  0.029151651  6.354851e-03
## 2017-07-31  0.0033343782  0.0565943842  0.0261880413  0.007481124  2.034599e-02
## 2017-08-31  0.0093692250  0.0232438852 -0.0004484364 -0.027564384  2.913193e-03
## 2017-09-29 -0.0057324393 -0.0004460776  0.0233427746  0.082321842  1.994919e-02
## 2017-10-31  0.0009780437  0.0322784075  0.0166538426  0.005915767  2.329072e-02
## 2017-11-30 -0.0014841321 -0.0038969506  0.0068700026  0.036913299  3.010792e-02
## 2017-12-29  0.0047405248  0.0369251471  0.0133982350 -0.003730781  1.205490e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398342e-05 0.0001042115 0.0000417841 -7.811823e-05 -9.030912e-06
## EEM  1.042115e-04 0.0017547117 0.0010390168  6.437728e-04  6.795428e-04
## EFA  4.178410e-05 0.0010390168 0.0010642377  6.490277e-04  6.975406e-04
## IJS -7.811823e-05 0.0006437728 0.0006490277  1.565448e-03  8.290245e-04
## SPY -9.030912e-06 0.0006795428 0.0006975406  8.290245e-04  7.408298e-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.0003874223 0.009257148 0.005815633 0.005684461 0.002330249
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.0062306319 -0.0029350924  0.0366061757  0.052133040  4.992303e-02
## 2013-02-28  0.0058911593 -0.0231053950 -0.0129691395  0.016175587  1.267838e-02
## 2013-03-28  0.0009845243 -0.0102351118  0.0129691395  0.040257950  3.726836e-02
## 2013-04-30  0.0096397453  0.0120847681  0.0489679329  0.001222476  1.902962e-02
## 2013-05-31 -0.0202148068 -0.0494834238 -0.0306558102  0.041976617  2.333530e-02
## 2013-06-28 -0.0157775348 -0.0547284477 -0.0271445512 -0.001403091 -1.343421e-02
## 2013-07-31  0.0026874539  0.0131598125  0.0518606179  0.063541133  5.038614e-02
## 2013-08-30 -0.0082982301 -0.0257058122 -0.0197463849 -0.034743104 -3.045133e-02
## 2013-09-30  0.0111436453  0.0695890421  0.0753385007  0.063873514  3.115605e-02
## 2013-10-31  0.0082922834  0.0408612799  0.0320817073  0.034233892  4.526603e-02
## 2013-11-29 -0.0025098786 -0.0025941888  0.0054497573  0.041661214  2.920708e-02
## 2013-12-31 -0.0055828202 -0.0040740977  0.0215280098  0.012892145  2.559670e-02
## 2014-01-31  0.0152910874 -0.0903227907 -0.0534134386 -0.035775144 -3.588462e-02
## 2014-02-28  0.0037572021  0.0332206166  0.0595051909  0.045256946  4.451019e-02
## 2014-03-31 -0.0014817567  0.0380218568 -0.0046027569  0.013315383  8.261304e-03
## 2014-04-30  0.0081832844  0.0077726767  0.0165295734 -0.023184057  6.927086e-03
## 2014-05-30  0.0117219155  0.0290910706  0.0158282756  0.006205138  2.294165e-02
## 2014-06-30 -0.0005759939  0.0237339251  0.0091652306  0.037718891  2.043435e-02
## 2014-07-31 -0.0025121621  0.0135553808 -0.0263795809 -0.052009399 -1.352859e-02
## 2014-08-29  0.0114310309  0.0279047915  0.0018002278  0.043657667  3.870472e-02
## 2014-09-30 -0.0061675020 -0.0808568916 -0.0395983087 -0.061260226 -1.389208e-02
## 2014-10-31  0.0105846887  0.0140967699 -0.0026548127  0.068874846  2.327744e-02
## 2014-11-28  0.0065484188 -0.0155414346  0.0006253148  0.004773623  2.710147e-02
## 2014-12-31  0.0014752012 -0.0404420932 -0.0407466701  0.025295689 -2.539942e-03
## 2015-01-30  0.0203154344 -0.0068956260  0.0062265031 -0.054628070 -3.007721e-02
## 2015-02-27 -0.0089880866  0.0431358088  0.0614506029  0.056914746  5.468230e-02
## 2015-03-31  0.0037400788 -0.0150862412 -0.0143887693  0.010156418 -1.583039e-02
## 2015-04-30 -0.0032328950  0.0662813542  0.0358164231 -0.018417774  9.786066e-03
## 2015-05-29 -0.0043837555 -0.0419107892  0.0019526919  0.007509952  1.277408e-02
## 2015-06-30 -0.0108252116 -0.0297466579 -0.0316786710  0.004171319 -2.052151e-02
## 2015-07-31  0.0085845574 -0.0651780755  0.0201145100 -0.027375320  2.233815e-02
## 2015-08-31 -0.0033636164 -0.0925124157 -0.0771525110 -0.047268233 -6.288659e-02
## 2015-09-30  0.0080810055 -0.0318248596 -0.0451948664 -0.038464844 -2.584702e-02
## 2015-10-30  0.0006853394  0.0618080610  0.0640258639  0.063589755  8.163478e-02
## 2015-11-30 -0.0038978720 -0.0255602592 -0.0075557791  0.024415330  3.648755e-03
## 2015-12-31 -0.0019189096 -0.0389471503 -0.0235951165 -0.052157030 -1.743371e-02
## 2016-01-29  0.0123297225 -0.0516366816 -0.0567577476 -0.060306852 -5.106870e-02
## 2016-02-29  0.0088318463 -0.0082115380 -0.0339138891  0.020605130 -8.264513e-04
## 2016-03-31  0.0087088852  0.1218792034  0.0637455001  0.089910297  6.510026e-02
## 2016-04-29  0.0025461086  0.0040788845  0.0219753752  0.021044214  3.933534e-03
## 2016-05-31  0.0001353674 -0.0376283948 -0.0008560587  0.004397021  1.686847e-02
## 2016-06-30  0.0191665713  0.0445823977 -0.0244917258  0.008292459  3.469808e-03
## 2016-07-29  0.0054298583  0.0524421804  0.0390001995  0.049348374  3.582195e-02
## 2016-08-31 -0.0021561182  0.0087984416  0.0053270066  0.011261139  1.196796e-03
## 2016-09-30  0.0005157718  0.0248730626  0.0132790225  0.008614622  5.813242e-05
## 2016-10-31 -0.0082056509 -0.0083123531 -0.0224037254 -0.038135055 -1.748925e-02
## 2016-11-30 -0.0259890984 -0.0451620121 -0.0179743636  0.125246568  3.617611e-02
## 2016-12-30  0.0025383600 -0.0025297675  0.0267029495  0.031491998  2.006901e-02
## 2017-01-31  0.0021262975  0.0644314144  0.0323817944 -0.012144399  1.773628e-02
## 2017-02-28  0.0064372557  0.0172578913  0.0118363811  0.013429306  3.853948e-02
## 2017-03-31 -0.0005531165  0.0361887638  0.0318056750 -0.006532896  1.249205e-03
## 2017-04-28  0.0090296920  0.0168665141  0.0239523104  0.005107519  9.877324e-03
## 2017-05-31  0.0068473289  0.0280599633  0.0348101251 -0.022862494  1.401398e-02
## 2017-06-30 -0.0001825774  0.0092238315  0.0029558687  0.029151651  6.354851e-03
## 2017-07-31  0.0033343782  0.0565943842  0.0261880413  0.007481124  2.034599e-02
## 2017-08-31  0.0093692250  0.0232438852 -0.0004484364 -0.027564384  2.913193e-03
## 2017-09-29 -0.0057324393 -0.0004460776  0.0233427746  0.082321842  1.994919e-02
## 2017-10-31  0.0009780437  0.0322784075  0.0166538426  0.005915767  2.329072e-02
## 2017-11-30 -0.0014841321 -0.0038969506  0.0068700026  0.036913299  3.010792e-02
## 2017-12-29  0.0047405248  0.0369251471  0.0133982350 -0.003730781  1.205490e-02
calculate_component_contribution <- function(.data, w) {
    
    covariance_matrix <- cov(asset_returns_wide_tbl)



# 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_return_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   NaN   NaN   NaN   NaN   NaN

5 Visualizing Component Contribution

Column Chart of Component contribution

plot_data <- asset_return_wide_tbl %>% 
    
    calculate_component_contribution(w = c(.25, .25, .2,.2, .1)) %>%
    #Transform to long from
    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 from
     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 Potfolio Volatility and Weight",
         y = "percent",
         x = NULL)

6 Rolling Component Contribution