# 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.0062319040 -0.0029354844  0.0366063564  0.052133154  4.992266e-02
## 2013-02-28  0.0058919479 -0.0231054123 -0.0129695801  0.016175322  1.267825e-02
## 2013-03-28  0.0009846972 -0.0102345922  0.0129695801  0.040258245  3.726770e-02
## 2013-04-30  0.0096396933  0.0120845357  0.0489676492  0.001222713  1.903041e-02
## 2013-05-31 -0.0202140311 -0.0494834727 -0.0306553154  0.041976321  2.333563e-02
## 2013-06-28 -0.0157786380 -0.0547284273 -0.0271447318 -0.001403271 -1.343439e-02
## 2013-07-31  0.0026880099  0.0131601527  0.0518603677  0.063541466  5.038594e-02
## 2013-08-30 -0.0082982915 -0.0257059451 -0.0197464131 -0.034743344 -3.045150e-02
## 2013-09-30  0.0111436777  0.0695890406  0.0753384375  0.063873462  3.115610e-02
## 2013-10-31  0.0082925208  0.0408611523  0.0320819076  0.034234215  4.526657e-02
## 2013-11-29 -0.0025100675 -0.0025942096  0.0054496043  0.041661011  2.920693e-02
## 2013-12-31 -0.0055833495 -0.0040741678  0.0215280253  0.012892014  2.559597e-02
## 2014-01-31  0.0152918063 -0.0903225062 -0.0534133163 -0.035775125 -3.588442e-02
## 2014-02-28  0.0037567310  0.0332200299  0.0595050498  0.045257274  4.451063e-02
## 2014-03-31 -0.0014814373  0.0380220938 -0.0046025085  0.013315406  8.260890e-03
## 2014-04-30  0.0081836905  0.0077727547  0.0165293746 -0.023184270  6.927396e-03
## 2014-05-30  0.0117213925  0.0290910772  0.0158284495  0.006205334  2.294145e-02
## 2014-06-30 -0.0005759786  0.0237338661  0.0091654505  0.037718587  2.043444e-02
## 2014-07-31 -0.0025119022  0.0135556986 -0.0263798746 -0.052009177 -1.352874e-02
## 2014-08-29  0.0114302802  0.0279047090  0.0018003362  0.043657703  3.870483e-02
## 2014-09-30 -0.0061672497 -0.0808566437 -0.0395983901 -0.061260365 -1.389231e-02
## 2014-10-31  0.0105850854  0.0140961407 -0.0026549068  0.068875042  2.327803e-02
## 2014-11-28  0.0065483020 -0.0155413112  0.0006253048  0.004773581  2.710113e-02
## 2014-12-31  0.0014749912 -0.0404418478 -0.0407466568  0.025295482 -2.539448e-03
## 2015-01-30  0.0203154397 -0.0068957389  0.0062264488 -0.054627652 -3.007719e-02
## 2015-02-27 -0.0089879773  0.0431360303  0.0614506437  0.056914542  5.468150e-02
## 2015-03-31  0.0037400309 -0.0150861191 -0.0143887809  0.010156495 -1.583014e-02
## 2015-04-30 -0.0032333898  0.0662811948  0.0358166005 -0.018417687  9.786096e-03
## 2015-05-29 -0.0043829386 -0.0419108762  0.0019526299  0.007509723  1.277396e-02
## 2015-06-30 -0.0108257018 -0.0297468307 -0.0316787437  0.004171475 -2.052108e-02
## 2015-07-31  0.0085844900 -0.0651780633  0.0201143031 -0.027375559  2.233803e-02
## 2015-08-31 -0.0033637532 -0.0925124330 -0.0771524370 -0.047268280 -6.288682e-02
## 2015-09-30  0.0080814802 -0.0318248313 -0.0451946803 -0.038464595 -2.584721e-02
## 2015-10-30  0.0006853732  0.0618081395  0.0640259361  0.063589443  8.163506e-02
## 2015-11-30 -0.0038982927 -0.0255602827 -0.0075558683  0.024415314  3.648358e-03
## 2015-12-31 -0.0019189562 -0.0389472365 -0.0235952734 -0.052156975 -1.743342e-02
## 2016-01-29  0.0123299110 -0.0516363622 -0.0567576470 -0.060306856 -5.106900e-02
## 2016-02-29  0.0088320113 -0.0082118780 -0.0339139945  0.020605482 -8.258504e-04
## 2016-03-31  0.0087088374  0.1218792145  0.0637458677  0.089910043  6.510019e-02
## 2016-04-29  0.0025458317  0.0040789664  0.0219750672  0.021044488  3.933158e-03
## 2016-05-31  0.0001357085 -0.0376282281 -0.0008562720  0.004396955  1.686887e-02
## 2016-06-30  0.0191669447  0.0445823475 -0.0244912450  0.008292207  3.469635e-03
## 2016-07-29  0.0054293838  0.0524419290  0.0390000052  0.049348429  3.582192e-02
## 2016-08-31 -0.0021562446  0.0087987068  0.0053269137  0.011261074  1.197031e-03
## 2016-09-30  0.0005161190  0.0248728062  0.0132790421  0.008614403  5.783827e-05
## 2016-10-31 -0.0082053041 -0.0083120212 -0.0224036715 -0.038134481 -1.748916e-02
## 2016-11-30 -0.0259899568 -0.0451621008 -0.0179743747  0.125246281  3.617617e-02
## 2016-12-30  0.0025379332 -0.0025297591  0.0267028261  0.031491640  2.006892e-02
## 2017-01-31  0.0021263938  0.0644313492  0.0323819846 -0.012143670  1.773636e-02
## 2017-02-28  0.0064378625  0.0172577327  0.0118363766  0.013428744  3.853935e-02
## 2017-03-31 -0.0005528011  0.0361890692  0.0318057520 -0.006533051  1.249435e-03
## 2017-04-28  0.0090292167  0.0168665502  0.0239521063  0.005107782  9.876988e-03
## 2017-05-31  0.0068474133  0.0280596287  0.0348103994 -0.022862910  1.401442e-02
## 2017-06-30 -0.0001827914  0.0092241468  0.0029559201  0.029151711  6.354549e-03
## 2017-07-31  0.0033342737  0.0565943115  0.0261876594  0.007481937  2.034588e-02
## 2017-08-31  0.0093694244  0.0232437110 -0.0004483091 -0.027564624  2.913656e-03
## 2017-09-29 -0.0057322182 -0.0004461751  0.0233428382  0.082321554  1.994868e-02
## 2017-10-31  0.0009779633  0.0322786091  0.0166537014  0.005916133  2.329072e-02
## 2017-11-30 -0.0014841287 -0.0038970433  0.0068700624  0.036913166  3.010826e-02
## 2017-12-29  0.0047402365  0.0369253728  0.0133982941 -0.003731478  1.205494e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398526e-05 0.0001042098 4.178168e-05 -7.812162e-05 -9.032054e-06
## EEM  1.042098e-04 0.0017547085 1.039017e-03  6.437724e-04  6.795422e-04
## EFA  4.178168e-05 0.0010390168 1.064237e-03  6.490289e-04  6.975408e-04
## IJS -7.812162e-05 0.0006437724 6.490289e-04  1.565444e-03  8.290231e-04
## SPY -9.032054e-06 0.0006795422 6.975408e-04  8.290231e-04  7.408294e-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.02347489
# 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.0003874095 0.009257143 0.005815634 0.005684454 0.002330248
rowSums(component_contribution)
## [1] 0.02347489
# 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")

# Custom function
calculate_component_contribution <- function(asset_returns_wide_tbl, w) {
  
  # Covariance of asset returns
  covariance_matrix <- cov(asset_returns_wide_tbl)
  
  # Standard deviation of portfolio
  sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
  
  # Component contribution 
  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(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

Column Chart of Component Contribution

plot_data <- asset_returns_wide_tbl %>%
  
  calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.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(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
  
  # Transform to long form
  pivot_longer(cols = everything(), names_to = "asset", values_to = "Contribution") %>%
  
  # Add weights
  add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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