# 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.0062311955 -0.0029354067  0.0366062115  0.052133445  4.992312e-02
## 2013-02-28  0.0058907789 -0.0231054462 -0.0129694869  0.016175216  1.267828e-02
## 2013-03-28  0.0009854155 -0.0102351473  0.0129694869  0.040258429  3.726811e-02
## 2013-04-30  0.0096390919  0.0120852888  0.0489677831  0.001222202  1.902973e-02
## 2013-05-31 -0.0202144697 -0.0494834449 -0.0306554848  0.041976038  2.333555e-02
## 2013-06-28 -0.0157783236 -0.0547288068 -0.0271445972 -0.001402678 -1.343456e-02
## 2013-07-31  0.0026881641  0.0131599527  0.0518603128  0.063541475  5.038601e-02
## 2013-08-30 -0.0082987387 -0.0257057168 -0.0197462328 -0.034743355 -3.045155e-02
## 2013-09-30  0.0111450150  0.0695887506  0.0753382631  0.063873379  3.115661e-02
## 2013-10-31  0.0082915998  0.0408611744  0.0320819076  0.034234084  4.526602e-02
## 2013-11-29 -0.0025101676 -0.0025938797  0.0054496320  0.041661319  2.920722e-02
## 2013-12-31 -0.0055825737 -0.0040744059  0.0215280547  0.012892029  2.559605e-02
## 2014-01-31  0.0152907099 -0.0903223767 -0.0534134311 -0.035775423 -3.588444e-02
## 2014-02-28  0.0037571800  0.0332204814  0.0595049213  0.045257396  4.451057e-02
## 2014-03-31 -0.0014815702  0.0380215278 -0.0046024063  0.013315591  8.260800e-03
## 2014-04-30  0.0081831940  0.0077726711  0.0165293656 -0.023184430  6.927470e-03
## 2014-05-30  0.0117220365  0.0290914031  0.0158286161  0.006205507  2.294128e-02
## 2014-06-30 -0.0005759261  0.0237337969  0.0091653122  0.037718238  2.043474e-02
## 2014-07-31 -0.0025115827  0.0135555382 -0.0263797091 -0.052008921 -1.352838e-02
## 2014-08-29  0.0114306543  0.0279049241  0.0018004510  0.043657538  3.870461e-02
## 2014-09-30 -0.0061678558 -0.0808568015 -0.0395984295 -0.061260061 -1.389235e-02
## 2014-10-31  0.0105846470  0.0140964143 -0.0026548354  0.068874465  2.327783e-02
## 2014-11-28  0.0065488438 -0.0155412278  0.0006251522  0.004773720  2.710129e-02
## 2014-12-31  0.0014748414 -0.0404423289 -0.0407466700  0.025295672 -2.539691e-03
## 2015-01-30  0.0203160752 -0.0068956463  0.0062266226 -0.054627701 -3.007700e-02
## 2015-02-27 -0.0089886851  0.0431360164  0.0614503622  0.056914404  5.468158e-02
## 2015-03-31  0.0037402658 -0.0150861327 -0.0143887327  0.010156711 -1.583010e-02
## 2015-04-30 -0.0032329438  0.0662815680  0.0358165720 -0.018417665  9.785779e-03
## 2015-05-29 -0.0043834198 -0.0419110760  0.0019525463  0.007509791  1.277454e-02
## 2015-06-30 -0.0108260997 -0.0297467468 -0.0316788597  0.004171212 -2.052165e-02
## 2015-07-31  0.0085845692 -0.0651780954  0.0201144642 -0.027375345  2.233797e-02
## 2015-08-31 -0.0033635056 -0.0925122752 -0.0771524455 -0.047268467 -6.288649e-02
## 2015-09-30  0.0080816112 -0.0318252425 -0.0451946346 -0.038464596 -2.584715e-02
## 2015-10-30  0.0006853432  0.0618085023  0.0640259392  0.063589716  8.163469e-02
## 2015-11-30 -0.0038984356 -0.0255603810 -0.0075559206  0.024415098  3.648704e-03
## 2015-12-31 -0.0019187560 -0.0389470793 -0.0235950923 -0.052156791 -1.743402e-02
## 2016-01-29  0.0123296879 -0.0516368418 -0.0567576579 -0.060306929 -5.106826e-02
## 2016-02-29  0.0088317825 -0.0082115773 -0.0339140694  0.020605169 -8.264435e-04
## 2016-03-31  0.0087090503  0.1218790627  0.0637455540  0.089910124  6.510015e-02
## 2016-04-29  0.0025460719  0.0040790657  0.0219753006  0.021044619  3.933614e-03
## 2016-05-31  0.0001357041 -0.0376284815 -0.0008562144  0.004397156  1.686866e-02
## 2016-06-30  0.0191668611  0.0445823410 -0.0244914410  0.008291931  3.469753e-03
## 2016-07-29  0.0054292368  0.0524422847  0.0390002901  0.049348369  3.582208e-02
## 2016-08-31 -0.0021562636  0.0087985240  0.0053268336  0.011261115  1.196497e-03
## 2016-09-30  0.0005160514  0.0248727063  0.0132790963  0.008614923  5.832105e-05
## 2016-10-31 -0.0082050933 -0.0083122279 -0.0224037090 -0.038134843 -1.748950e-02
## 2016-11-30 -0.0259897998 -0.0451616310 -0.0179744060  0.125246095  3.617632e-02
## 2016-12-30  0.0025379033 -0.0025299636  0.0267028928  0.031492116  2.006902e-02
## 2017-01-31  0.0021264717  0.0644312734  0.0323819215 -0.012144314  1.773652e-02
## 2017-02-28  0.0064378121  0.0172579268  0.0118364650  0.013429021  3.853932e-02
## 2017-03-31 -0.0005534249  0.0361890900  0.0318055656 -0.006532918  1.248935e-03
## 2017-04-28  0.0090293047  0.0168666568  0.0239522163  0.005107759  9.877485e-03
## 2017-05-31  0.0068481359  0.0280593413  0.0348101484 -0.022862780  1.401425e-02
## 2017-06-30 -0.0001831323  0.0092240153  0.0029558909  0.029151908  6.354643e-03
## 2017-07-31  0.0033347283  0.0565944308  0.0261877572  0.007481284  2.034570e-02
## 2017-08-31  0.0093688503  0.0232438564 -0.0004480516 -0.027564700  2.913453e-03
## 2017-09-29 -0.0057318991 -0.0004459257  0.0233426662  0.082321918  1.994906e-02
## 2017-10-31  0.0009776890  0.0322782651  0.0166536003  0.005915872  2.329068e-02
## 2017-11-30 -0.0014840511 -0.0038969322  0.0068702324  0.036913355  3.010829e-02
## 2017-12-29  0.0047403258  0.0369254350  0.0133982908 -0.003731404  1.205474e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398543e-05 0.0001042145 4.178607e-05 -0.0000781198 -9.030059e-06
## EEM  1.042145e-04 0.0017547114 1.039018e-03  0.0006437732  6.795423e-04
## EFA  4.178607e-05 0.0010390176 1.064235e-03  0.0006490279  6.975398e-04
## IJS -7.811980e-05 0.0006437732 6.490279e-04  0.0015654445  8.290248e-04
## SPY -9.030059e-06 0.0006795423 6.975398e-04  0.0008290248  7.408285e-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.02347492
# 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.0003874372 0.009257154 0.005815632 0.005684452 0.002330248
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062311955 -0.0029354067  0.0366062115  0.052133445  4.992312e-02
## 2013-02-28  0.0058907789 -0.0231054462 -0.0129694869  0.016175216  1.267828e-02
## 2013-03-28  0.0009854155 -0.0102351473  0.0129694869  0.040258429  3.726811e-02
## 2013-04-30  0.0096390919  0.0120852888  0.0489677831  0.001222202  1.902973e-02
## 2013-05-31 -0.0202144697 -0.0494834449 -0.0306554848  0.041976038  2.333555e-02
## 2013-06-28 -0.0157783236 -0.0547288068 -0.0271445972 -0.001402678 -1.343456e-02
## 2013-07-31  0.0026881641  0.0131599527  0.0518603128  0.063541475  5.038601e-02
## 2013-08-30 -0.0082987387 -0.0257057168 -0.0197462328 -0.034743355 -3.045155e-02
## 2013-09-30  0.0111450150  0.0695887506  0.0753382631  0.063873379  3.115661e-02
## 2013-10-31  0.0082915998  0.0408611744  0.0320819076  0.034234084  4.526602e-02
## 2013-11-29 -0.0025101676 -0.0025938797  0.0054496320  0.041661319  2.920722e-02
## 2013-12-31 -0.0055825737 -0.0040744059  0.0215280547  0.012892029  2.559605e-02
## 2014-01-31  0.0152907099 -0.0903223767 -0.0534134311 -0.035775423 -3.588444e-02
## 2014-02-28  0.0037571800  0.0332204814  0.0595049213  0.045257396  4.451057e-02
## 2014-03-31 -0.0014815702  0.0380215278 -0.0046024063  0.013315591  8.260800e-03
## 2014-04-30  0.0081831940  0.0077726711  0.0165293656 -0.023184430  6.927470e-03
## 2014-05-30  0.0117220365  0.0290914031  0.0158286161  0.006205507  2.294128e-02
## 2014-06-30 -0.0005759261  0.0237337969  0.0091653122  0.037718238  2.043474e-02
## 2014-07-31 -0.0025115827  0.0135555382 -0.0263797091 -0.052008921 -1.352838e-02
## 2014-08-29  0.0114306543  0.0279049241  0.0018004510  0.043657538  3.870461e-02
## 2014-09-30 -0.0061678558 -0.0808568015 -0.0395984295 -0.061260061 -1.389235e-02
## 2014-10-31  0.0105846470  0.0140964143 -0.0026548354  0.068874465  2.327783e-02
## 2014-11-28  0.0065488438 -0.0155412278  0.0006251522  0.004773720  2.710129e-02
## 2014-12-31  0.0014748414 -0.0404423289 -0.0407466700  0.025295672 -2.539691e-03
## 2015-01-30  0.0203160752 -0.0068956463  0.0062266226 -0.054627701 -3.007700e-02
## 2015-02-27 -0.0089886851  0.0431360164  0.0614503622  0.056914404  5.468158e-02
## 2015-03-31  0.0037402658 -0.0150861327 -0.0143887327  0.010156711 -1.583010e-02
## 2015-04-30 -0.0032329438  0.0662815680  0.0358165720 -0.018417665  9.785779e-03
## 2015-05-29 -0.0043834198 -0.0419110760  0.0019525463  0.007509791  1.277454e-02
## 2015-06-30 -0.0108260997 -0.0297467468 -0.0316788597  0.004171212 -2.052165e-02
## 2015-07-31  0.0085845692 -0.0651780954  0.0201144642 -0.027375345  2.233797e-02
## 2015-08-31 -0.0033635056 -0.0925122752 -0.0771524455 -0.047268467 -6.288649e-02
## 2015-09-30  0.0080816112 -0.0318252425 -0.0451946346 -0.038464596 -2.584715e-02
## 2015-10-30  0.0006853432  0.0618085023  0.0640259392  0.063589716  8.163469e-02
## 2015-11-30 -0.0038984356 -0.0255603810 -0.0075559206  0.024415098  3.648704e-03
## 2015-12-31 -0.0019187560 -0.0389470793 -0.0235950923 -0.052156791 -1.743402e-02
## 2016-01-29  0.0123296879 -0.0516368418 -0.0567576579 -0.060306929 -5.106826e-02
## 2016-02-29  0.0088317825 -0.0082115773 -0.0339140694  0.020605169 -8.264435e-04
## 2016-03-31  0.0087090503  0.1218790627  0.0637455540  0.089910124  6.510015e-02
## 2016-04-29  0.0025460719  0.0040790657  0.0219753006  0.021044619  3.933614e-03
## 2016-05-31  0.0001357041 -0.0376284815 -0.0008562144  0.004397156  1.686866e-02
## 2016-06-30  0.0191668611  0.0445823410 -0.0244914410  0.008291931  3.469753e-03
## 2016-07-29  0.0054292368  0.0524422847  0.0390002901  0.049348369  3.582208e-02
## 2016-08-31 -0.0021562636  0.0087985240  0.0053268336  0.011261115  1.196497e-03
## 2016-09-30  0.0005160514  0.0248727063  0.0132790963  0.008614923  5.832105e-05
## 2016-10-31 -0.0082050933 -0.0083122279 -0.0224037090 -0.038134843 -1.748950e-02
## 2016-11-30 -0.0259897998 -0.0451616310 -0.0179744060  0.125246095  3.617632e-02
## 2016-12-30  0.0025379033 -0.0025299636  0.0267028928  0.031492116  2.006902e-02
## 2017-01-31  0.0021264717  0.0644312734  0.0323819215 -0.012144314  1.773652e-02
## 2017-02-28  0.0064378121  0.0172579268  0.0118364650  0.013429021  3.853932e-02
## 2017-03-31 -0.0005534249  0.0361890900  0.0318055656 -0.006532918  1.248935e-03
## 2017-04-28  0.0090293047  0.0168666568  0.0239522163  0.005107759  9.877485e-03
## 2017-05-31  0.0068481359  0.0280593413  0.0348101484 -0.022862780  1.401425e-02
## 2017-06-30 -0.0001831323  0.0092240153  0.0029558909  0.029151908  6.354643e-03
## 2017-07-31  0.0033347283  0.0565944308  0.0261877572  0.007481284  2.034570e-02
## 2017-08-31  0.0093688503  0.0232438564 -0.0004480516 -0.027564700  2.913453e-03
## 2017-09-29 -0.0057318991 -0.0004459257  0.0233426662  0.082321918  1.994906e-02
## 2017-10-31  0.0009776890  0.0322782651  0.0166536003  0.005915872  2.329068e-02
## 2017-11-30 -0.0014840511 -0.0038969322  0.0068702324  0.036913355  3.010829e-02
## 2017-12-29  0.0047403258  0.0369254350  0.0133982908 -0.003731404  1.205474e-02
calculate_componet_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_componet_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_componet_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 = "cornflower blue") +
        
        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_componet_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 Portfolio volatility and weight",
             y = "Percent",
             x = NULL)

6 Rolling Component Contribution