# 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.0062313993 -0.0029355228  0.0366064087  0.052133114  4.992267e-02
## 2013-02-28  0.0058911832 -0.0231050963 -0.0129695837  0.016175439  1.267828e-02
## 2013-03-28  0.0009848141 -0.0102349067  0.0129695837  0.040258642  3.726824e-02
## 2013-04-30  0.0096395950  0.0120845750  0.0489676863  0.001222098  1.902949e-02
## 2013-05-31 -0.0202139672 -0.0494834007 -0.0306557699  0.041976438  2.333597e-02
## 2013-06-28 -0.0157783188 -0.0547281681 -0.0271443121 -0.001402777 -1.343492e-02
## 2013-07-31  0.0026876501  0.0131594951  0.0518603128  0.063541175  5.038671e-02
## 2013-08-30 -0.0082984299 -0.0257055208 -0.0197462328 -0.034743744 -3.045166e-02
## 2013-09-30  0.0111446044  0.0695887461  0.0753382631  0.063873951  3.115593e-02
## 2013-10-31  0.0082913993  0.0408613504  0.0320818226  0.034233637  4.526669e-02
## 2013-11-29 -0.0025092530 -0.0025940580  0.0054498014  0.041661837  2.920721e-02
## 2013-12-31 -0.0055831854 -0.0040745854  0.0215280529  0.012891775  2.559574e-02
## 2014-01-31  0.0152916200 -0.0903225191 -0.0534133394 -0.035775596 -3.588434e-02
## 2014-02-28  0.0037567758  0.0332208705  0.0595047468  0.045257403  4.451006e-02
## 2014-03-31 -0.0014819720  0.0380214009 -0.0046024063  0.013315593  8.261508e-03
## 2014-04-30  0.0081840924  0.0077729134  0.0165293656 -0.023184433  6.927768e-03
## 2014-05-30  0.0117214358  0.0290909254  0.0158285362  0.006205674  2.294098e-02
## 2014-06-30 -0.0005761232  0.0237339173  0.0091653129  0.037718238  2.043435e-02
## 2014-07-31 -0.0025115827  0.0135557665 -0.0263798739 -0.052009005 -1.352828e-02
## 2014-08-29  0.0114303610  0.0279047005  0.0018006950  0.043657945  3.870443e-02
## 2014-09-30 -0.0061672675 -0.0808567510 -0.0395985139 -0.061260641 -1.389236e-02
## 2014-10-31  0.0105847412  0.0140965920 -0.0026547509  0.068874722  2.327793e-02
## 2014-11-28  0.0065478744 -0.0155415252  0.0006249830  0.004773720  2.710148e-02
## 2014-12-31  0.0014759042 -0.0404422117 -0.0407466770  0.025295905 -2.539511e-03
## 2015-01-30  0.0203148357 -0.0068955213  0.0062267113 -0.054628098 -3.007755e-02
## 2015-02-27 -0.0089880238  0.0431360137  0.0614503674  0.056915033  5.468204e-02
## 2015-03-31  0.0037401711 -0.0150864979 -0.0143885668  0.010156093 -1.583019e-02
## 2015-04-30 -0.0032333261  0.0662816446  0.0358164884 -0.018417746  9.785956e-03
## 2015-05-29 -0.0043833265 -0.0419110858  0.0019526268  0.007510103  1.277445e-02
## 2015-06-30 -0.0108255237 -0.0297463859 -0.0316789402  0.004170980 -2.052174e-02
## 2015-07-31  0.0085849525 -0.0651782836  0.0201144642 -0.027375270  2.233814e-02
## 2015-08-31 -0.0033635040 -0.0925124252 -0.0771522697 -0.047268304 -6.288713e-02
## 2015-09-30  0.0080811293 -0.0318248046 -0.0451947184 -0.038464679 -2.584678e-02
## 2015-10-30  0.0006851522  0.0618082102  0.0640258472  0.063589635  8.163505e-02
## 2015-11-30 -0.0038984363 -0.0255604543 -0.0075560075  0.024415258  3.648266e-03
## 2015-12-31 -0.0019184681 -0.0389470850 -0.0235949164 -0.052157120 -1.743358e-02
## 2016-01-29  0.0123296867 -0.0516366148 -0.0567579354 -0.060306591 -5.106853e-02
## 2016-02-29  0.0088314054 -0.0082115767 -0.0339136860  0.020605080 -8.264435e-04
## 2016-03-31  0.0087085865  0.1218789139  0.0637456334  0.089910203  6.510050e-02
## 2016-04-29  0.0025470040  0.0040791356  0.0219749368  0.021044540  3.933350e-03
## 2016-05-31  0.0001349600 -0.0376284815 -0.0008558563  0.004396847  1.686832e-02
## 2016-06-30  0.0191669629  0.0445826174 -0.0244917096  0.008292394  3.470183e-03
## 2016-07-29  0.0054296022  0.0524420738  0.0390002019  0.049348434  3.582141e-02
## 2016-08-31 -0.0021559000  0.0087986535  0.0053270097  0.011260752  1.197159e-03
## 2016-09-30  0.0005156877  0.0248728917  0.0132790952  0.008614996  5.799062e-05
## 2016-10-31 -0.0082050940 -0.0083124805 -0.0224037957 -0.038134846 -1.748925e-02
## 2016-11-30 -0.0259895200 -0.0451617588 -0.0179745864  0.125246694  3.617632e-02
## 2016-12-30  0.0025379028 -0.0025298965  0.0267028975  0.031491338  2.006918e-02
## 2017-01-31  0.0021261905  0.0644315208  0.0323820121 -0.012143739  1.773628e-02
## 2017-02-28  0.0064373476  0.0172577978  0.0118365500  0.013428826  3.853925e-02
## 2017-03-31 -0.0005522153  0.0361887854  0.0318056470 -0.006533173  1.249236e-03
## 2017-04-28  0.0090290221  0.0168665415  0.0239521349  0.005107505  9.877260e-03
## 2017-05-31  0.0068474005  0.0280598037  0.0348103786 -0.022862139  1.401432e-02
## 2017-06-30 -0.0001829491  0.0092239003  0.0029557372  0.029151521  6.354497e-03
## 2017-07-31  0.0033347289  0.0565945313  0.0261879788  0.007481598  2.034598e-02
## 2017-08-31  0.0093690329  0.0232437473 -0.0004482006 -0.027564630  2.913168e-03
## 2017-09-29 -0.0057319901 -0.0004461343  0.0233425171  0.082321898  1.994934e-02
## 2017-10-31  0.0009778709  0.0322782684  0.0166538869  0.005915516  2.329074e-02
## 2017-11-30 -0.0014843241 -0.0038969326  0.0068698746  0.036913587  3.010788e-02
## 2017-12-29  0.0047400549  0.0369255364  0.0133984322 -0.003731518  1.205514e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398318e-05 0.0001042086 0.0000417825 -7.812077e-05 -9.032944e-06
## EEM  1.042086e-04 0.0017547097 0.0010390156  6.437732e-04  6.795426e-04
## EFA  4.178250e-05 0.0010390156 0.0010642353  6.490280e-04  6.975399e-04
## IJS -7.812077e-05 0.0006437732 0.0006490280  1.565451e-03  8.290275e-04
## SPY -9.032944e-06 0.0006795426 0.0006975399  8.290275e-04  7.408318e-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.0003874031 0.009257141 0.005815627 0.005684471 0.002330251
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")

asset_returns_wide_tbl
##                      AGG           EEM           EFA          IJS           SPY
## 2013-01-31 -0.0062313993 -0.0029355228  0.0366064087  0.052133114  4.992267e-02
## 2013-02-28  0.0058911832 -0.0231050963 -0.0129695837  0.016175439  1.267828e-02
## 2013-03-28  0.0009848141 -0.0102349067  0.0129695837  0.040258642  3.726824e-02
## 2013-04-30  0.0096395950  0.0120845750  0.0489676863  0.001222098  1.902949e-02
## 2013-05-31 -0.0202139672 -0.0494834007 -0.0306557699  0.041976438  2.333597e-02
## 2013-06-28 -0.0157783188 -0.0547281681 -0.0271443121 -0.001402777 -1.343492e-02
## 2013-07-31  0.0026876501  0.0131594951  0.0518603128  0.063541175  5.038671e-02
## 2013-08-30 -0.0082984299 -0.0257055208 -0.0197462328 -0.034743744 -3.045166e-02
## 2013-09-30  0.0111446044  0.0695887461  0.0753382631  0.063873951  3.115593e-02
## 2013-10-31  0.0082913993  0.0408613504  0.0320818226  0.034233637  4.526669e-02
## 2013-11-29 -0.0025092530 -0.0025940580  0.0054498014  0.041661837  2.920721e-02
## 2013-12-31 -0.0055831854 -0.0040745854  0.0215280529  0.012891775  2.559574e-02
## 2014-01-31  0.0152916200 -0.0903225191 -0.0534133394 -0.035775596 -3.588434e-02
## 2014-02-28  0.0037567758  0.0332208705  0.0595047468  0.045257403  4.451006e-02
## 2014-03-31 -0.0014819720  0.0380214009 -0.0046024063  0.013315593  8.261508e-03
## 2014-04-30  0.0081840924  0.0077729134  0.0165293656 -0.023184433  6.927768e-03
## 2014-05-30  0.0117214358  0.0290909254  0.0158285362  0.006205674  2.294098e-02
## 2014-06-30 -0.0005761232  0.0237339173  0.0091653129  0.037718238  2.043435e-02
## 2014-07-31 -0.0025115827  0.0135557665 -0.0263798739 -0.052009005 -1.352828e-02
## 2014-08-29  0.0114303610  0.0279047005  0.0018006950  0.043657945  3.870443e-02
## 2014-09-30 -0.0061672675 -0.0808567510 -0.0395985139 -0.061260641 -1.389236e-02
## 2014-10-31  0.0105847412  0.0140965920 -0.0026547509  0.068874722  2.327793e-02
## 2014-11-28  0.0065478744 -0.0155415252  0.0006249830  0.004773720  2.710148e-02
## 2014-12-31  0.0014759042 -0.0404422117 -0.0407466770  0.025295905 -2.539511e-03
## 2015-01-30  0.0203148357 -0.0068955213  0.0062267113 -0.054628098 -3.007755e-02
## 2015-02-27 -0.0089880238  0.0431360137  0.0614503674  0.056915033  5.468204e-02
## 2015-03-31  0.0037401711 -0.0150864979 -0.0143885668  0.010156093 -1.583019e-02
## 2015-04-30 -0.0032333261  0.0662816446  0.0358164884 -0.018417746  9.785956e-03
## 2015-05-29 -0.0043833265 -0.0419110858  0.0019526268  0.007510103  1.277445e-02
## 2015-06-30 -0.0108255237 -0.0297463859 -0.0316789402  0.004170980 -2.052174e-02
## 2015-07-31  0.0085849525 -0.0651782836  0.0201144642 -0.027375270  2.233814e-02
## 2015-08-31 -0.0033635040 -0.0925124252 -0.0771522697 -0.047268304 -6.288713e-02
## 2015-09-30  0.0080811293 -0.0318248046 -0.0451947184 -0.038464679 -2.584678e-02
## 2015-10-30  0.0006851522  0.0618082102  0.0640258472  0.063589635  8.163505e-02
## 2015-11-30 -0.0038984363 -0.0255604543 -0.0075560075  0.024415258  3.648266e-03
## 2015-12-31 -0.0019184681 -0.0389470850 -0.0235949164 -0.052157120 -1.743358e-02
## 2016-01-29  0.0123296867 -0.0516366148 -0.0567579354 -0.060306591 -5.106853e-02
## 2016-02-29  0.0088314054 -0.0082115767 -0.0339136860  0.020605080 -8.264435e-04
## 2016-03-31  0.0087085865  0.1218789139  0.0637456334  0.089910203  6.510050e-02
## 2016-04-29  0.0025470040  0.0040791356  0.0219749368  0.021044540  3.933350e-03
## 2016-05-31  0.0001349600 -0.0376284815 -0.0008558563  0.004396847  1.686832e-02
## 2016-06-30  0.0191669629  0.0445826174 -0.0244917096  0.008292394  3.470183e-03
## 2016-07-29  0.0054296022  0.0524420738  0.0390002019  0.049348434  3.582141e-02
## 2016-08-31 -0.0021559000  0.0087986535  0.0053270097  0.011260752  1.197159e-03
## 2016-09-30  0.0005156877  0.0248728917  0.0132790952  0.008614996  5.799062e-05
## 2016-10-31 -0.0082050940 -0.0083124805 -0.0224037957 -0.038134846 -1.748925e-02
## 2016-11-30 -0.0259895200 -0.0451617588 -0.0179745864  0.125246694  3.617632e-02
## 2016-12-30  0.0025379028 -0.0025298965  0.0267028975  0.031491338  2.006918e-02
## 2017-01-31  0.0021261905  0.0644315208  0.0323820121 -0.012143739  1.773628e-02
## 2017-02-28  0.0064373476  0.0172577978  0.0118365500  0.013428826  3.853925e-02
## 2017-03-31 -0.0005522153  0.0361887854  0.0318056470 -0.006533173  1.249236e-03
## 2017-04-28  0.0090290221  0.0168665415  0.0239521349  0.005107505  9.877260e-03
## 2017-05-31  0.0068474005  0.0280598037  0.0348103786 -0.022862139  1.401432e-02
## 2017-06-30 -0.0001829491  0.0092239003  0.0029557372  0.029151521  6.354497e-03
## 2017-07-31  0.0033347289  0.0565945313  0.0261879788  0.007481598  2.034598e-02
## 2017-08-31  0.0093690329  0.0232437473 -0.0004482006 -0.027564630  2.913168e-03
## 2017-09-29 -0.0057319901 -0.0004461343  0.0233425171  0.082321898  1.994934e-02
## 2017-10-31  0.0009778709  0.0322782684  0.0166538869  0.005915516  2.329074e-02
## 2017-11-30 -0.0014843241 -0.0038969326  0.0068698746  0.036913587  3.010788e-02
## 2017-12-29  0.0047400549  0.0369255364  0.0133984322 -0.003731518  1.205514e-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 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(.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 = 0.5)) +
  theme_tq() +
  labs(title = "Percent Contribution to Portfolio Volatility and Weight", 
       y = "Percent",
       x = NULL)

## 6 Rolling Component Contribution