# 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.0062314812 -0.0029353724  0.0366061223  0.052133234  4.992252e-02
## 2013-02-28  0.0058912517 -0.0231053453 -0.0129694754  0.016175672  1.267837e-02
## 2013-03-28  0.0009849122 -0.0102348794  0.0129694754  0.040258407  3.726794e-02
## 2013-04-30  0.0096391959  0.0120845432  0.0489677151  0.001222538  1.903017e-02
## 2013-05-31 -0.0202139882 -0.0494829810 -0.0306554090  0.041976188  2.333526e-02
## 2013-06-28 -0.0157775381 -0.0547287740 -0.0271446199 -0.001403198 -1.343396e-02
## 2013-07-31  0.0026871102  0.0131597987  0.0518603956  0.063541532  5.038526e-02
## 2013-08-30 -0.0082982754 -0.0257056421 -0.0197466059 -0.034743386 -3.045089e-02
## 2013-09-30  0.0111442302  0.0695885941  0.0753389633  0.063873802  3.115603e-02
## 2013-10-31  0.0082919948  0.0408614236  0.0320814055  0.034233814  4.526662e-02
## 2013-11-29 -0.0025098145 -0.0025940764  0.0054497637  0.041661250  2.920721e-02
## 2013-12-31 -0.0055833352 -0.0040741220  0.0215280075  0.012892049  2.559595e-02
## 2014-01-31  0.0152921720 -0.0903229756 -0.0534133526 -0.035775570 -3.588445e-02
## 2014-02-28  0.0037566479  0.0332206168  0.0595050079  0.045257770  4.451054e-02
## 2014-03-31 -0.0014816517  0.0380218851 -0.0046025734  0.013315231  8.260700e-03
## 2014-04-30  0.0081827801  0.0077724951  0.0165295022 -0.023184355  6.927952e-03
## 2014-05-30  0.0117218398  0.0290914298  0.0158284436  0.006205450  2.294078e-02
## 2014-06-30 -0.0005758402  0.0237339422  0.0091654156  0.037718653  2.043493e-02
## 2014-07-31 -0.0025117759  0.0135556873 -0.0263798899 -0.052009420 -1.352898e-02
## 2014-08-29  0.0114309015  0.0279047127  0.0018005552  0.043657787  3.870487e-02
## 2014-09-30 -0.0061682005 -0.0808571130 -0.0395985366 -0.061260404 -1.389265e-02
## 2014-10-31  0.0105850227  0.0140963434 -0.0026547698  0.068874811  2.327799e-02
## 2014-11-28  0.0065485235 -0.0155408614  0.0006254112  0.004773887  2.710159e-02
## 2014-12-31  0.0014750648 -0.0404422354 -0.0407468077  0.025295599 -2.539647e-03
## 2015-01-30  0.0203157235 -0.0068955117  0.0062263657 -0.054627728 -3.007710e-02
## 2015-02-27 -0.0089886650  0.0431357216  0.0614507473  0.056914372  5.468161e-02
## 2015-03-31  0.0037403506 -0.0150860651 -0.0143888725  0.010156618 -1.582997e-02
## 2015-04-30 -0.0032331434  0.0662812399  0.0358165791 -0.018418031  9.785698e-03
## 2015-05-29 -0.0043835358 -0.0419110434  0.0019527114  0.007510092  1.277425e-02
## 2015-06-30 -0.0108254163 -0.0297463480 -0.0316790808  0.004171207 -2.052106e-02
## 2015-07-31  0.0085848688 -0.0651783269  0.0201146561 -0.027375117  2.233767e-02
## 2015-08-31 -0.0033640611 -0.0925123329 -0.0771523348 -0.047268594 -6.288636e-02
## 2015-09-30  0.0080817140 -0.0318248859 -0.0451949307 -0.038464334 -2.584725e-02
## 2015-10-30  0.0006855447  0.0618084113  0.0640259062  0.063589590  8.163489e-02
## 2015-11-30 -0.0038985343 -0.0255606100 -0.0075558116  0.024414957  3.648421e-03
## 2015-12-31 -0.0019194542 -0.0389472098 -0.0235951360 -0.052156850 -1.743359e-02
## 2016-01-29  0.0123305727 -0.0516365505 -0.0567578828 -0.060306898 -5.106869e-02
## 2016-02-29  0.0088313156 -0.0082116146 -0.0339138341  0.020605386 -8.261509e-04
## 2016-03-31  0.0087088679  0.1218788225  0.0637455795  0.089910369  6.510033e-02
## 2016-04-29  0.0025466540  0.0040793101  0.0219751066  0.021044011  3.933255e-03
## 2016-05-31  0.0001353311 -0.0376283957 -0.0008559120  0.004397028  1.686851e-02
## 2016-06-30  0.0191667124  0.0445824922 -0.0244917913  0.008292324  3.469761e-03
## 2016-07-29  0.0054299426  0.0524419664  0.0390005046  0.049348359  3.582175e-02
## 2016-08-31 -0.0021564397  0.0087985353  0.0053266748  0.011261345  1.196811e-03
## 2016-09-30  0.0005160423  0.0248731688  0.0132793321  0.008614593  5.843409e-05
## 2016-10-31 -0.0082050124 -0.0083122629 -0.0224037498 -0.038135103 -1.748922e-02
## 2016-11-30 -0.0259899694 -0.0451618807 -0.0179744003  0.125246871  3.617616e-02
## 2016-12-30  0.0025377862 -0.0025301146  0.0267029852  0.031491544  2.006882e-02
## 2017-01-31  0.0021266667  0.0644316066  0.0323816596 -0.012143830  1.773649e-02
## 2017-02-28  0.0064376895  0.0172575919  0.0118363511  0.013428394  3.853956e-02
## 2017-03-31 -0.0005529492  0.0361891550  0.0318059121 -0.006532872  1.249047e-03
## 2017-04-28  0.0090293441  0.0168661777  0.0239522507  0.005107765  9.877155e-03
## 2017-05-31  0.0068470040  0.0280602412  0.0348102158 -0.022862293  1.401427e-02
## 2017-06-30 -0.0001820372  0.0092237704  0.0029557777  0.029151696  6.354506e-03
## 2017-07-31  0.0033338773  0.0565944180  0.0261879539  0.007481465  2.034587e-02
## 2017-08-31  0.0093685491  0.0232438163 -0.0004482871 -0.027564835  2.913569e-03
## 2017-09-29 -0.0057312200 -0.0004463619  0.0233427407  0.082321737  1.994934e-02
## 2017-10-31  0.0009773033  0.0322785008  0.0166535828  0.005915750  2.329054e-02
## 2017-11-30 -0.0014837981 -0.0038971182  0.0068700315  0.036913497  3.010802e-02
## 2017-12-29  0.0047405116  0.0369256184  0.0133983443 -0.003731303  1.205475e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398489e-05 0.0001042096 4.178133e-05 -7.811913e-05 -9.033150e-06
## EEM  1.042096e-04 0.0017547121 1.039017e-03  6.437725e-04  6.795413e-04
## EFA  4.178133e-05 0.0010390169 1.064240e-03  6.490315e-04  6.975398e-04
## IJS -7.811913e-05 0.0006437725 6.490315e-04  1.565451e-03  8.290251e-04
## SPY -9.033150e-06 0.0006795413 6.975398e-04  8.290251e-04  7.408260e-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.0003874109 0.009257144 0.005815637 0.005684472 0.002330244
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

calculate_component_contribution <- function(.data, w) {
    
    covariance_matrix <- cov(.data)
    
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
    
    component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
    
    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 = .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 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 = .5)) +
    theme_tq()+
    labs(title = "Percent Contribution to Portfolio Volatility and Weight",
         y = "Percent", x = NULL)