# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(readr)

# Time series
library(lubridate)
library(tibble)

# 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.0062311300 -0.0029357532  0.0366064556  0.052133422  4.992270e-02
## 2013-02-28  0.0058906613 -0.0231050509 -0.0129697075  0.016175186  1.267860e-02
## 2013-03-28  0.0009852082 -0.0102349807  0.0129697075  0.040258312  3.726781e-02
## 2013-04-30  0.0096395915  0.0120844530  0.0489676284  0.001222118  1.903027e-02
## 2013-05-31 -0.0202147878 -0.0494829782 -0.0306556269  0.041976526  2.333572e-02
## 2013-06-28 -0.0157777450 -0.0547287247 -0.0271443680 -0.001402974 -1.343477e-02
## 2013-07-31  0.0026875520  0.0131600122  0.0518602626  0.063541331  5.038628e-02
## 2013-08-30 -0.0082979607 -0.0257057936 -0.0197463914 -0.034743514 -3.045172e-02
## 2013-09-30  0.0111436307  0.0695891366  0.0753385931  0.063873628  3.115591e-02
## 2013-10-31  0.0082923092  0.0408609243  0.0320815049  0.034234317  4.526669e-02
## 2013-11-29 -0.0025098074 -0.0025940684  0.0054499553  0.041660918  2.920673e-02
## 2013-12-31 -0.0055833433 -0.0040740475  0.0215280208  0.012891977  2.559616e-02
## 2014-01-31  0.0152915793 -0.0903227043 -0.0534133386 -0.035775491 -3.588450e-02
## 2014-02-28  0.0037572791  0.0332202943  0.0595049227  0.045257935  4.451019e-02
## 2014-03-31 -0.0014819319  0.0380218352 -0.0046026549  0.013315242  8.261281e-03
## 2014-04-30  0.0081831366  0.0077726513  0.0165294605 -0.023184349  6.927695e-03
## 2014-05-30  0.0117222328  0.0290912296  0.0158286069  0.006205324  2.294109e-02
## 2014-06-30 -0.0005766364  0.0237340214  0.0091653136  0.037718830  2.043497e-02
## 2014-07-31 -0.0025114652  0.0135554353 -0.0263799022 -0.052009636 -1.352876e-02
## 2014-08-29  0.0114305653  0.0279046207  0.0018005837  0.043658175  3.870484e-02
## 2014-09-30 -0.0061673583 -0.0808565852 -0.0395984799 -0.061260680 -1.389256e-02
## 2014-10-31  0.0105847703  0.0140964225 -0.0026549646  0.068874854  2.327825e-02
## 2014-11-28  0.0065484085 -0.0155411715  0.0006253065  0.004773712  2.710127e-02
## 2014-12-31  0.0014748582 -0.0404421446 -0.0407464444  0.025295629 -2.540074e-03
## 2015-01-30  0.0203156882 -0.0068956998  0.0062262822 -0.054627642 -3.007702e-02
## 2015-02-27 -0.0089886794  0.0431362328  0.0614507247  0.056914282  5.468176e-02
## 2015-03-31  0.0037405539 -0.0150864493 -0.0143888258  0.010156613 -1.583020e-02
## 2015-04-30 -0.0032328847  0.0662812840  0.0358165171 -0.018417945  9.785982e-03
## 2015-05-29 -0.0043840885 -0.0419109449  0.0019528440  0.007509996  1.277423e-02
## 2015-06-30 -0.0108250025 -0.0297465802 -0.0316789191  0.004171493 -2.052149e-02
## 2015-07-31  0.0085847997 -0.0651782102  0.0201143296 -0.027375403  2.233819e-02
## 2015-08-31 -0.0033640037 -0.0925124450 -0.0771524204 -0.047268523 -6.288692e-02
## 2015-09-30  0.0080812627 -0.0318248438 -0.0451948454 -0.038464462 -2.584718e-02
## 2015-10-30  0.0006858571  0.0618082427  0.0640260121  0.063589596  8.163513e-02
## 2015-11-30 -0.0038983204 -0.0255604196 -0.0075559238  0.024415031  3.648303e-03
## 2015-12-31 -0.0019192337 -0.0389471022 -0.0235951208 -0.052156750 -1.743355e-02
## 2016-01-29  0.0123298803 -0.0516367923 -0.0567578339 -0.060306994 -5.106853e-02
## 2016-02-29  0.0088318857 -0.0082114306 -0.0339138580  0.020605179 -8.263032e-04
## 2016-03-31  0.0087084952  0.1218790034  0.0637457289  0.089910186  6.510018e-02
## 2016-04-29  0.0025460183  0.0040794667  0.0219750423  0.021044486  3.933372e-03
## 2016-05-31  0.0001357589 -0.0376286093 -0.0008560654  0.004397249  1.686862e-02
## 2016-06-30  0.0191669147  0.0445823401 -0.0244913638  0.008292010  3.469756e-03
## 2016-07-29  0.0054294330  0.0524418596  0.0390001461  0.049348504  3.582203e-02
## 2016-08-31 -0.0021560638  0.0087987151  0.0053268880  0.011261265  1.196846e-03
## 2016-09-30  0.0005160013  0.0248727921  0.0132790819  0.008614415  5.780605e-05
## 2016-10-31 -0.0082051518 -0.0083121454 -0.0224036234 -0.038134925 -1.748878e-02
## 2016-11-30 -0.0259894056 -0.0451617733 -0.0179745854  0.125246799  3.617599e-02
## 2016-12-30  0.0025374826 -0.0025300704  0.0267028708  0.031491652  2.006898e-02
## 2017-01-31  0.0021264317  0.0644314025  0.0323820023 -0.012143984  1.773666e-02
## 2017-02-28  0.0064381145  0.0172579347  0.0118365036  0.013428565  3.853909e-02
## 2017-03-31 -0.0005532190  0.0361890439  0.0318055449 -0.006533033  1.249245e-03
## 2017-04-28  0.0090291758  0.0168663793  0.0239523044  0.005107689  9.877226e-03
## 2017-05-31  0.0068479365  0.0280597502  0.0348102134 -0.022862228  1.401420e-02
## 2017-06-30 -0.0001831760  0.0092238929  0.0029558483  0.029151745  6.354788e-03
## 2017-07-31  0.0033342548  0.0565945569  0.0261878558  0.007481461  2.034598e-02
## 2017-08-31  0.0093693832  0.0232437207 -0.0004482288 -0.027564497  2.913260e-03
## 2017-09-29 -0.0057321511 -0.0004461971  0.0233427221  0.082321407  1.994902e-02
## 2017-10-31  0.0009781223  0.0322784443  0.0166538934  0.005916111  2.329066e-02
## 2017-11-30 -0.0014842420 -0.0038969123  0.0068699092  0.036913499  3.010800e-02
## 2017-12-29  0.0047404887  0.0369254224  0.0133983336 -0.003731133  1.205502e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398466e-05 0.0001042104 4.178251e-05 -7.812259e-05 -9.031685e-06
## EEM  1.042104e-04 0.0017547100 1.039017e-03  6.437741e-04  6.795445e-04
## EFA  4.178251e-05 0.0010390171 1.064237e-03  6.490303e-04  6.975423e-04
## IJS -7.812259e-05 0.0006437741 6.490303e-04  1.565451e-03  8.290258e-04
## SPY -9.031685e-06 0.0006795445 6.975423e-04  8.290258e-04  7.408307e-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.0003874091 0.009257147 0.005815636 0.005684466 0.002330253
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.0062311300 -0.0029357532  0.0366064556  0.052133422  4.992270e-02
## 2013-02-28  0.0058906613 -0.0231050509 -0.0129697075  0.016175186  1.267860e-02
## 2013-03-28  0.0009852082 -0.0102349807  0.0129697075  0.040258312  3.726781e-02
## 2013-04-30  0.0096395915  0.0120844530  0.0489676284  0.001222118  1.903027e-02
## 2013-05-31 -0.0202147878 -0.0494829782 -0.0306556269  0.041976526  2.333572e-02
## 2013-06-28 -0.0157777450 -0.0547287247 -0.0271443680 -0.001402974 -1.343477e-02
## 2013-07-31  0.0026875520  0.0131600122  0.0518602626  0.063541331  5.038628e-02
## 2013-08-30 -0.0082979607 -0.0257057936 -0.0197463914 -0.034743514 -3.045172e-02
## 2013-09-30  0.0111436307  0.0695891366  0.0753385931  0.063873628  3.115591e-02
## 2013-10-31  0.0082923092  0.0408609243  0.0320815049  0.034234317  4.526669e-02
## 2013-11-29 -0.0025098074 -0.0025940684  0.0054499553  0.041660918  2.920673e-02
## 2013-12-31 -0.0055833433 -0.0040740475  0.0215280208  0.012891977  2.559616e-02
## 2014-01-31  0.0152915793 -0.0903227043 -0.0534133386 -0.035775491 -3.588450e-02
## 2014-02-28  0.0037572791  0.0332202943  0.0595049227  0.045257935  4.451019e-02
## 2014-03-31 -0.0014819319  0.0380218352 -0.0046026549  0.013315242  8.261281e-03
## 2014-04-30  0.0081831366  0.0077726513  0.0165294605 -0.023184349  6.927695e-03
## 2014-05-30  0.0117222328  0.0290912296  0.0158286069  0.006205324  2.294109e-02
## 2014-06-30 -0.0005766364  0.0237340214  0.0091653136  0.037718830  2.043497e-02
## 2014-07-31 -0.0025114652  0.0135554353 -0.0263799022 -0.052009636 -1.352876e-02
## 2014-08-29  0.0114305653  0.0279046207  0.0018005837  0.043658175  3.870484e-02
## 2014-09-30 -0.0061673583 -0.0808565852 -0.0395984799 -0.061260680 -1.389256e-02
## 2014-10-31  0.0105847703  0.0140964225 -0.0026549646  0.068874854  2.327825e-02
## 2014-11-28  0.0065484085 -0.0155411715  0.0006253065  0.004773712  2.710127e-02
## 2014-12-31  0.0014748582 -0.0404421446 -0.0407464444  0.025295629 -2.540074e-03
## 2015-01-30  0.0203156882 -0.0068956998  0.0062262822 -0.054627642 -3.007702e-02
## 2015-02-27 -0.0089886794  0.0431362328  0.0614507247  0.056914282  5.468176e-02
## 2015-03-31  0.0037405539 -0.0150864493 -0.0143888258  0.010156613 -1.583020e-02
## 2015-04-30 -0.0032328847  0.0662812840  0.0358165171 -0.018417945  9.785982e-03
## 2015-05-29 -0.0043840885 -0.0419109449  0.0019528440  0.007509996  1.277423e-02
## 2015-06-30 -0.0108250025 -0.0297465802 -0.0316789191  0.004171493 -2.052149e-02
## 2015-07-31  0.0085847997 -0.0651782102  0.0201143296 -0.027375403  2.233819e-02
## 2015-08-31 -0.0033640037 -0.0925124450 -0.0771524204 -0.047268523 -6.288692e-02
## 2015-09-30  0.0080812627 -0.0318248438 -0.0451948454 -0.038464462 -2.584718e-02
## 2015-10-30  0.0006858571  0.0618082427  0.0640260121  0.063589596  8.163513e-02
## 2015-11-30 -0.0038983204 -0.0255604196 -0.0075559238  0.024415031  3.648303e-03
## 2015-12-31 -0.0019192337 -0.0389471022 -0.0235951208 -0.052156750 -1.743355e-02
## 2016-01-29  0.0123298803 -0.0516367923 -0.0567578339 -0.060306994 -5.106853e-02
## 2016-02-29  0.0088318857 -0.0082114306 -0.0339138580  0.020605179 -8.263032e-04
## 2016-03-31  0.0087084952  0.1218790034  0.0637457289  0.089910186  6.510018e-02
## 2016-04-29  0.0025460183  0.0040794667  0.0219750423  0.021044486  3.933372e-03
## 2016-05-31  0.0001357589 -0.0376286093 -0.0008560654  0.004397249  1.686862e-02
## 2016-06-30  0.0191669147  0.0445823401 -0.0244913638  0.008292010  3.469756e-03
## 2016-07-29  0.0054294330  0.0524418596  0.0390001461  0.049348504  3.582203e-02
## 2016-08-31 -0.0021560638  0.0087987151  0.0053268880  0.011261265  1.196846e-03
## 2016-09-30  0.0005160013  0.0248727921  0.0132790819  0.008614415  5.780605e-05
## 2016-10-31 -0.0082051518 -0.0083121454 -0.0224036234 -0.038134925 -1.748878e-02
## 2016-11-30 -0.0259894056 -0.0451617733 -0.0179745854  0.125246799  3.617599e-02
## 2016-12-30  0.0025374826 -0.0025300704  0.0267028708  0.031491652  2.006898e-02
## 2017-01-31  0.0021264317  0.0644314025  0.0323820023 -0.012143984  1.773666e-02
## 2017-02-28  0.0064381145  0.0172579347  0.0118365036  0.013428565  3.853909e-02
## 2017-03-31 -0.0005532190  0.0361890439  0.0318055449 -0.006533033  1.249245e-03
## 2017-04-28  0.0090291758  0.0168663793  0.0239523044  0.005107689  9.877226e-03
## 2017-05-31  0.0068479365  0.0280597502  0.0348102134 -0.022862228  1.401420e-02
## 2017-06-30 -0.0001831760  0.0092238929  0.0029558483  0.029151745  6.354788e-03
## 2017-07-31  0.0033342548  0.0565945569  0.0261878558  0.007481461  2.034598e-02
## 2017-08-31  0.0093693832  0.0232437207 -0.0004482288 -0.027564497  2.913260e-03
## 2017-09-29 -0.0057321511 -0.0004461971  0.0233427221  0.082321407  1.994902e-02
## 2017-10-31  0.0009781223  0.0322784443  0.0166538934  0.005916111  2.329066e-02
## 2017-11-30 -0.0014842420 -0.0038969123  0.0068699092  0.036913499  3.010800e-02
## 2017-12-29  0.0047404887  0.0369254224  0.0133983336 -0.003731133  1.205502e-02
calculate_component_contribution <- function(.data, w) {
    
         # Covariance of asset returns
        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
        
        # 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 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)