# 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.0062311910 -0.0029353724  0.0366059316  0.052133205  4.992336e-02
## 2013-02-28  0.0058914433 -0.0231052291 -0.0129692870  0.016175342  1.267786e-02
## 2013-03-28  0.0009847193 -0.0102352304  0.0129692870  0.040258193  3.726813e-02
## 2013-04-30  0.0096390033  0.0120851259  0.0489679870  0.001222230  1.903017e-02
## 2013-05-31 -0.0202137936 -0.0494836945 -0.0306558629  0.041976398  2.333549e-02
## 2013-06-28 -0.0157783258 -0.0547282154 -0.0271445331 -0.001402705 -1.343502e-02
## 2013-07-31  0.0026878019  0.0131595421  0.0518605845  0.063541045  5.038630e-02
## 2013-08-30 -0.0082983740 -0.0257055786 -0.0197465144 -0.034743293 -3.045088e-02
## 2013-09-30  0.0111441319  0.0695892020  0.0753388719  0.063873532  3.115546e-02
## 2013-10-31  0.0082920931  0.0408610491  0.0320814876  0.034234257  4.526674e-02
## 2013-11-29 -0.0025102055 -0.0025941928  0.0054498449  0.041661243  2.920711e-02
## 2013-12-31 -0.0055827477 -0.0040743565  0.0215279241  0.012891635  2.559626e-02
## 2014-01-31  0.0152911043 -0.0903224723 -0.0534133482 -0.035774811 -3.588455e-02
## 2014-02-28  0.0037577119  0.0332204176  0.0595050030  0.045257340  4.451004e-02
## 2014-03-31 -0.0014817480  0.0380215189 -0.0046027327  0.013315310  8.261400e-03
## 2014-04-30  0.0081831624  0.0077727936  0.0165295035 -0.023184763  6.927654e-03
## 2014-05-30  0.0117217396  0.0290913110  0.0158285221  0.006205615  2.294097e-02
## 2014-06-30 -0.0005753663  0.0237338294  0.0091653391  0.037718814  2.043492e-02
## 2014-07-31 -0.0025131032  0.0135558000 -0.0263800492 -0.052009333 -1.352879e-02
## 2014-08-29  0.0114310008  0.0279047127  0.0018008695  0.043657621  3.870468e-02
## 2014-09-30 -0.0061667859 -0.0808569957 -0.0395985334 -0.061260320 -1.389199e-02
## 2014-10-31  0.0105842641  0.0140964574 -0.0026548515  0.068874963  2.327770e-02
## 2014-11-28  0.0065486146 -0.0155413274  0.0006254112  0.004773651  2.710113e-02
## 2014-12-31  0.0014749715 -0.0404420005 -0.0407468077  0.025295599 -2.539825e-03
## 2015-01-30  0.0203152636 -0.0068955733  0.0062264504 -0.054627890 -3.007673e-02
## 2015-02-27 -0.0089881166  0.0431360190  0.0614505033  0.056914610  5.468151e-02
## 2015-03-31  0.0037401666 -0.0150860017 -0.0143888748  0.010156466 -1.583032e-02
## 2015-04-30 -0.0032333261  0.0662809406  0.0358167407 -0.018417724  9.785788e-03
## 2015-05-29 -0.0043836278 -0.0419109266  0.0019528670  0.007509938  1.277460e-02
## 2015-06-30 -0.0108254173 -0.0297465852 -0.0316789152  0.004171207 -2.052132e-02
## 2015-07-31  0.0085844084 -0.0651782066  0.0201143349 -0.027375272  2.233793e-02
## 2015-08-31 -0.0033638779 -0.0925121920 -0.0771524198 -0.047268269 -6.288672e-02
## 2015-09-30  0.0080819006 -0.0318250996 -0.0451949346 -0.038464836 -2.584725e-02
## 2015-10-30  0.0006851779  0.0618084156  0.0640261620  0.063589925  8.163508e-02
## 2015-11-30 -0.0038976155 -0.0255605417 -0.0075558944  0.024415111  3.648681e-03
## 2015-12-31 -0.0019187153 -0.0389472098 -0.0235952201 -0.052157166 -1.743403e-02
## 2016-01-29  0.0123298299 -0.0516363969 -0.0567577917 -0.060306990 -5.106842e-02
## 2016-02-29  0.0088314028 -0.0082117681 -0.0339140194  0.020605304 -8.265218e-04
## 2016-03-31  0.0087084161  0.1218789596  0.0637457622  0.089910313  6.510035e-02
## 2016-04-29  0.0025467434  0.0040794461  0.0219751912  0.021044400  3.933516e-03
## 2016-05-31  0.0001352418 -0.0376286687 -0.0008560850  0.004397027  1.686876e-02
## 2016-06-30  0.0191665372  0.0445824244 -0.0244916138  0.008292248  3.469675e-03
## 2016-07-29  0.0054296821  0.0524422271  0.0390002419  0.049348431  3.582199e-02
## 2016-08-31 -0.0021566153  0.0087984061  0.0053267601  0.011261131  1.196402e-03
## 2016-09-30  0.0005164791  0.0248729807  0.0132794159  0.008614383  5.827065e-05
## 2016-10-31 -0.0082053659 -0.0083120758 -0.0224039192 -0.038134530 -1.748889e-02
## 2016-11-30 -0.0259898026 -0.0451619435 -0.0179742274  0.125246458  3.617591e-02
## 2016-12-30  0.0025385079 -0.0025300489  0.0267027282  0.031491989  2.006898e-02
## 2017-01-31  0.0021256769  0.0644315409  0.0323819116 -0.012144270  1.773672e-02
## 2017-02-28  0.0064378722  0.0172575919  0.0118363502  0.013428459  3.853932e-02
## 2017-03-31 -0.0005527707  0.0361891550  0.0318055948 -0.006532369  1.248973e-03
## 2017-04-28  0.0090288150  0.0168661777  0.0239524099  0.005107638  9.877081e-03
## 2017-05-31  0.0068480651  0.0280602412  0.0348101442 -0.022863056  1.401428e-02
## 2017-06-30 -0.0001828290  0.0092235488  0.0029559261  0.029152084  6.354867e-03
## 2017-07-31  0.0033344929  0.0565945349  0.0261879539  0.007481281  2.034559e-02
## 2017-08-31  0.0093695040  0.0232437164 -0.0004482150 -0.027564463  2.913710e-03
## 2017-09-29 -0.0057327868 -0.0004459528  0.0233426686  0.082321556  1.994886e-02
## 2017-10-31  0.0009779149  0.0322783953  0.0166535135  0.005916216  2.329082e-02
## 2017-11-30 -0.0014838854 -0.0038968195  0.0068704448  0.036913260  3.010821e-02
## 2017-12-29  0.0047405116  0.0369252206  0.0133981361 -0.003730966  1.205501e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398379e-05 0.0001042098 4.178396e-05 -7.812013e-05 -9.030836e-06
## EEM  1.042098e-04 0.0017547102 1.039018e-03  6.437713e-04  6.795411e-04
## EFA  4.178396e-05 0.0010390185 1.064240e-03  6.490302e-04  6.975417e-04
## IJS -7.812013e-05 0.0006437713 6.490302e-04  1.565448e-03  8.290226e-04
## SPY -9.030836e-06 0.0006795411 6.975417e-04  8.290226e-04  7.408289e-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.0003874144 0.00925714 0.005815647 0.005684459 0.002330247
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

asset_returns_wide_tbl <- asset_returns_tbl %>%

    pivot_wider(names_from = asset, values_from = returns) %>%

    column_to_rownames(var = "date")

calculate_component_contribution <- function(asset_returns_wide_tbl, w) {

    covariance_matrix <- cov(asset_returns_wide_tbl)
    
    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(0.25,0.25,0.2,0.2,0.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

# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%

    ggplot(aes(asset, contribution)) +
    geom_col(fill = "cornflowerblue") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    
    labs(title = "Percent Contribution to Portfolio Standard Deviation",
         y = "Percent Contribution to Risk",
         x = NULL)

# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%
    add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
    pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%

    ggplot(aes(asset, value, fill = type)) +
    geom_col(position = "dodge") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    theme_tq() +
    scale_fill_tq() +

    labs(title = "Percent Contribution to Volatility",
         y = "percent",
         x = "asset") 

6 Rolling Component Contribution