# 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.0062314676 -0.0029355732  0.0366062100  0.052133194  4.992311e-02
## 2013-02-28  0.0058912661 -0.0231051173 -0.0129694779  0.016175241  1.267786e-02
## 2013-03-28  0.0009847934 -0.0102354657  0.0129694779  0.040258295  3.726870e-02
## 2013-04-30  0.0096394327  0.0120850197  0.0489676317  0.001222365  1.902953e-02
## 2013-05-31 -0.0202143825 -0.0494831398 -0.0306554215  0.041976570  2.333527e-02
## 2013-06-28 -0.0157776856 -0.0547286850 -0.0271444699 -0.001403236 -1.343377e-02
## 2013-07-31  0.0026874330  0.0131600088  0.0518601691  0.063541440  5.038597e-02
## 2013-08-30 -0.0082981952 -0.0257060498 -0.0197462899 -0.034743650 -3.045181e-02
## 2013-09-30  0.0111438932  0.0695889127  0.0753385049  0.063874010  3.115600e-02
## 2013-10-31  0.0082919401  0.0408614153  0.0320817183  0.034233845  4.526669e-02
## 2013-11-29 -0.0025092846 -0.0025941415  0.0054496221  0.041661256  2.920662e-02
## 2013-12-31 -0.0055832793 -0.0040744762  0.0215279785  0.012891558  2.559638e-02
## 2014-01-31  0.0152920816 -0.0903224453 -0.0534131473 -0.035774586 -3.588402e-02
## 2014-02-28  0.0037561775  0.0332207204  0.0595049311  0.045257160  4.451018e-02
## 2014-03-31 -0.0014809947  0.0380212863 -0.0046023347  0.013315195  8.261019e-03
## 2014-04-30  0.0081833643  0.0077729759  0.0165291395 -0.023184279  6.927470e-03
## 2014-05-30  0.0117211731  0.0290913038  0.0158285760  0.006205544  2.294109e-02
## 2014-06-30 -0.0005755099  0.0237340508  0.0091651849  0.037718406  2.043478e-02
## 2014-07-31 -0.0025124757  0.0135552451 -0.0263794808 -0.052009046 -1.352864e-02
## 2014-08-29  0.0114308608  0.0279046866  0.0018004224  0.043657646  3.870465e-02
## 2014-09-30 -0.0061668757 -0.0808564624 -0.0395985666 -0.061260458 -1.389228e-02
## 2014-10-31  0.0105838326  0.0140961124 -0.0026548905  0.068874740  2.327797e-02
## 2014-11-28  0.0065489703 -0.0155410711  0.0006253892  0.004774026  2.710122e-02
## 2014-12-31  0.0014748274 -0.0404419882 -0.0407467275  0.025295522 -2.540014e-03
## 2015-01-30  0.0203151872 -0.0068958549  0.0062264224 -0.054627733 -3.007699e-02
## 2015-02-27 -0.0089884543  0.0431357892  0.0614506945  0.056914550  5.468208e-02
## 2015-03-31  0.0037405991 -0.0150861389 -0.0143889400  0.010156238 -1.583036e-02
## 2015-04-30 -0.0032330296  0.0662816405  0.0358165849 -0.018417668  9.786126e-03
## 2015-05-29 -0.0043835601 -0.0419111244  0.0019526482  0.007509874  1.277413e-02
## 2015-06-30 -0.0108258984 -0.0297467280 -0.0316786743  0.004171296 -2.052127e-02
## 2015-07-31  0.0085848419 -0.0651780710  0.0201144059 -0.027375444  2.233780e-02
## 2015-08-31 -0.0033631076 -0.0925123356 -0.0771523273 -0.047268268 -6.288651e-02
## 2015-09-30  0.0080814635 -0.0318250626 -0.0451949791 -0.038464967 -2.584723e-02
## 2015-10-30  0.0006846784  0.0618082416  0.0640259730  0.063590124  8.163496e-02
## 2015-11-30 -0.0038978977 -0.0255603382 -0.0075558800  0.024415259  3.648448e-03
## 2015-12-31 -0.0019187167 -0.0389471757 -0.0235949460 -0.052157061 -1.743391e-02
## 2016-01-29  0.0123299140 -0.0516366086 -0.0567579916 -0.060307199 -5.106831e-02
## 2016-02-29  0.0088321845 -0.0082114548 -0.0339139044  0.020605630 -8.263022e-04
## 2016-03-31  0.0087080873  0.1218790433  0.0637459510  0.089910162  6.510015e-02
## 2016-04-29  0.0025462860  0.0040791492  0.0219749270  0.021044159  3.933223e-03
## 2016-05-31  0.0001353141 -0.0376285078 -0.0008559620  0.004397191  1.686880e-02
## 2016-06-30  0.0191670958  0.0445821375 -0.0244915341  0.008292177  3.469884e-03
## 2016-07-29  0.0054294440  0.0524423541  0.0390001936  0.049348382  3.582198e-02
## 2016-08-31 -0.0021558427  0.0087984760  0.0053268624  0.011261111  1.196736e-03
## 2016-09-30  0.0005155527  0.0248727551  0.0132789138  0.008614737  5.805518e-05
## 2016-10-31 -0.0082050601 -0.0083122537 -0.0224035875 -0.038134643 -1.748905e-02
## 2016-11-30 -0.0259899572 -0.0451615271 -0.0179743144  0.125246277  3.617598e-02
## 2016-12-30  0.0025382437 -0.0025298179  0.0267027760  0.031491490  2.006924e-02
## 2017-01-31  0.0021257285  0.0644311431  0.0323817970 -0.012143536  1.773646e-02
## 2017-02-28  0.0064386220  0.0172580171  0.0118366296  0.013428929  3.853904e-02
## 2017-03-31 -0.0005532176  0.0361889820  0.0318055557 -0.006533450  1.249307e-03
## 2017-04-28  0.0090294224  0.0168662855  0.0239521415  0.005107835  9.877090e-03
## 2017-05-31  0.0068469262  0.0280599865  0.0348103340 -0.022862374  1.401437e-02
## 2017-06-30 -0.0001822732  0.0092237649  0.0029559265  0.029151437  6.354618e-03
## 2017-07-31  0.0033339626  0.0565944397  0.0261878679  0.007481739  2.034579e-02
## 2017-08-31  0.0093693321  0.0232437740 -0.0004484333 -0.027564857  2.913517e-03
## 2017-09-29 -0.0057320866 -0.0004461961  0.0233428466  0.082322245  1.994901e-02
## 2017-10-31  0.0009779226  0.0322784749  0.0166536644  0.005915628  2.329075e-02
## 2017-11-30 -0.0014841433 -0.0038968556  0.0068701724  0.036913066  3.010813e-02
## 2017-12-29  0.0047402872  0.0369253929  0.0133982693 -0.003731136  1.205499e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398418e-05 0.0001042034 4.177674e-05 -7.812449e-05 -9.035750e-06
## EEM  1.042034e-04 0.0017547086 1.039017e-03  6.437743e-04  6.795426e-04
## EFA  4.177674e-05 0.0010390173 1.064236e-03  6.490302e-04  6.975403e-04
## IJS -7.812449e-05 0.0006437743 6.490302e-04  1.565448e-03  8.290244e-04
## SPY -9.035750e-06 0.0006795426 6.975403e-04  8.290244e-04  7.408279e-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.02347486
# 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.0003873695 0.009257143 0.005815632 0.005684469 0.002330248
rowSums(component_contribution)
## [1] 0.02347486
# 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.0062314676 -0.0029355732  0.0366062100  0.052133194  4.992311e-02
## 2013-02-28  0.0058912661 -0.0231051173 -0.0129694779  0.016175241  1.267786e-02
## 2013-03-28  0.0009847934 -0.0102354657  0.0129694779  0.040258295  3.726870e-02
## 2013-04-30  0.0096394327  0.0120850197  0.0489676317  0.001222365  1.902953e-02
## 2013-05-31 -0.0202143825 -0.0494831398 -0.0306554215  0.041976570  2.333527e-02
## 2013-06-28 -0.0157776856 -0.0547286850 -0.0271444699 -0.001403236 -1.343377e-02
## 2013-07-31  0.0026874330  0.0131600088  0.0518601691  0.063541440  5.038597e-02
## 2013-08-30 -0.0082981952 -0.0257060498 -0.0197462899 -0.034743650 -3.045181e-02
## 2013-09-30  0.0111438932  0.0695889127  0.0753385049  0.063874010  3.115600e-02
## 2013-10-31  0.0082919401  0.0408614153  0.0320817183  0.034233845  4.526669e-02
## 2013-11-29 -0.0025092846 -0.0025941415  0.0054496221  0.041661256  2.920662e-02
## 2013-12-31 -0.0055832793 -0.0040744762  0.0215279785  0.012891558  2.559638e-02
## 2014-01-31  0.0152920816 -0.0903224453 -0.0534131473 -0.035774586 -3.588402e-02
## 2014-02-28  0.0037561775  0.0332207204  0.0595049311  0.045257160  4.451018e-02
## 2014-03-31 -0.0014809947  0.0380212863 -0.0046023347  0.013315195  8.261019e-03
## 2014-04-30  0.0081833643  0.0077729759  0.0165291395 -0.023184279  6.927470e-03
## 2014-05-30  0.0117211731  0.0290913038  0.0158285760  0.006205544  2.294109e-02
## 2014-06-30 -0.0005755099  0.0237340508  0.0091651849  0.037718406  2.043478e-02
## 2014-07-31 -0.0025124757  0.0135552451 -0.0263794808 -0.052009046 -1.352864e-02
## 2014-08-29  0.0114308608  0.0279046866  0.0018004224  0.043657646  3.870465e-02
## 2014-09-30 -0.0061668757 -0.0808564624 -0.0395985666 -0.061260458 -1.389228e-02
## 2014-10-31  0.0105838326  0.0140961124 -0.0026548905  0.068874740  2.327797e-02
## 2014-11-28  0.0065489703 -0.0155410711  0.0006253892  0.004774026  2.710122e-02
## 2014-12-31  0.0014748274 -0.0404419882 -0.0407467275  0.025295522 -2.540014e-03
## 2015-01-30  0.0203151872 -0.0068958549  0.0062264224 -0.054627733 -3.007699e-02
## 2015-02-27 -0.0089884543  0.0431357892  0.0614506945  0.056914550  5.468208e-02
## 2015-03-31  0.0037405991 -0.0150861389 -0.0143889400  0.010156238 -1.583036e-02
## 2015-04-30 -0.0032330296  0.0662816405  0.0358165849 -0.018417668  9.786126e-03
## 2015-05-29 -0.0043835601 -0.0419111244  0.0019526482  0.007509874  1.277413e-02
## 2015-06-30 -0.0108258984 -0.0297467280 -0.0316786743  0.004171296 -2.052127e-02
## 2015-07-31  0.0085848419 -0.0651780710  0.0201144059 -0.027375444  2.233780e-02
## 2015-08-31 -0.0033631076 -0.0925123356 -0.0771523273 -0.047268268 -6.288651e-02
## 2015-09-30  0.0080814635 -0.0318250626 -0.0451949791 -0.038464967 -2.584723e-02
## 2015-10-30  0.0006846784  0.0618082416  0.0640259730  0.063590124  8.163496e-02
## 2015-11-30 -0.0038978977 -0.0255603382 -0.0075558800  0.024415259  3.648448e-03
## 2015-12-31 -0.0019187167 -0.0389471757 -0.0235949460 -0.052157061 -1.743391e-02
## 2016-01-29  0.0123299140 -0.0516366086 -0.0567579916 -0.060307199 -5.106831e-02
## 2016-02-29  0.0088321845 -0.0082114548 -0.0339139044  0.020605630 -8.263022e-04
## 2016-03-31  0.0087080873  0.1218790433  0.0637459510  0.089910162  6.510015e-02
## 2016-04-29  0.0025462860  0.0040791492  0.0219749270  0.021044159  3.933223e-03
## 2016-05-31  0.0001353141 -0.0376285078 -0.0008559620  0.004397191  1.686880e-02
## 2016-06-30  0.0191670958  0.0445821375 -0.0244915341  0.008292177  3.469884e-03
## 2016-07-29  0.0054294440  0.0524423541  0.0390001936  0.049348382  3.582198e-02
## 2016-08-31 -0.0021558427  0.0087984760  0.0053268624  0.011261111  1.196736e-03
## 2016-09-30  0.0005155527  0.0248727551  0.0132789138  0.008614737  5.805518e-05
## 2016-10-31 -0.0082050601 -0.0083122537 -0.0224035875 -0.038134643 -1.748905e-02
## 2016-11-30 -0.0259899572 -0.0451615271 -0.0179743144  0.125246277  3.617598e-02
## 2016-12-30  0.0025382437 -0.0025298179  0.0267027760  0.031491490  2.006924e-02
## 2017-01-31  0.0021257285  0.0644311431  0.0323817970 -0.012143536  1.773646e-02
## 2017-02-28  0.0064386220  0.0172580171  0.0118366296  0.013428929  3.853904e-02
## 2017-03-31 -0.0005532176  0.0361889820  0.0318055557 -0.006533450  1.249307e-03
## 2017-04-28  0.0090294224  0.0168662855  0.0239521415  0.005107835  9.877090e-03
## 2017-05-31  0.0068469262  0.0280599865  0.0348103340 -0.022862374  1.401437e-02
## 2017-06-30 -0.0001822732  0.0092237649  0.0029559265  0.029151437  6.354618e-03
## 2017-07-31  0.0033339626  0.0565944397  0.0261878679  0.007481739  2.034579e-02
## 2017-08-31  0.0093693321  0.0232437740 -0.0004484333 -0.027564857  2.913517e-03
## 2017-09-29 -0.0057320866 -0.0004461961  0.0233428466  0.082322245  1.994901e-02
## 2017-10-31  0.0009779226  0.0322784749  0.0166536644  0.005915628  2.329075e-02
## 2017-11-30 -0.0014841433 -0.0038968556  0.0068701724  0.036913066  3.010813e-02
## 2017-12-29  0.0047402872  0.0369253929  0.0133982693 -0.003731136  1.205499e-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 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")

6 Rolling Component 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 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)