# 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.0062307936 -0.0029359845  0.0366067099  0.052132992  0.0499234505
## 2013-02-28  0.0058908786 -0.0231047409 -0.0129694856  0.016175111  0.0126781470
## 2013-03-28  0.0009852148 -0.0102352022  0.0129694856  0.040258755  0.0372677375
## 2013-04-30  0.0096389926  0.0120846334  0.0489675941  0.001222098  0.0190301569
## 2013-05-31 -0.0202142691 -0.0494833947 -0.0306557727  0.041976442  0.0233357805
## 2013-06-28 -0.0157786307 -0.0547285548 -0.0271445100 -0.001402978 -0.0134347955
## 2013-07-31  0.0026885753  0.0131599519  0.0518604203  0.063541475  0.0503858948
## 2013-08-30 -0.0082984264 -0.0257055822 -0.0197459528 -0.034743161 -0.0304516658
## 2013-09-30  0.0111437810  0.0695887372  0.0753384317  0.063873276  0.0311566180
## 2013-10-31  0.0082924178  0.0408614644  0.0320815595  0.034234257  0.0452662435
## 2013-11-29 -0.0025101673 -0.0025941171  0.0054495480  0.041660969  0.0292071123
## 2013-12-31 -0.0055833918 -0.0040744050  0.0215282236  0.012892030  0.0255958501
## 2014-01-31  0.0152923333 -0.0903228785 -0.0534133439 -0.035775339 -0.0358844480
## 2014-02-28  0.0037568754  0.0332203632  0.0595048340  0.045257644  0.0445105797
## 2014-03-31 -0.0014818710  0.0380220914 -0.0046024888  0.013315098  0.0082612037
## 2014-04-30  0.0081839894  0.0077727303  0.0165295293 -0.023184018  0.0069276682
## 2014-05-30  0.0117206437  0.0290909254  0.0158282152  0.006205091  0.0229408808
## 2014-06-30 -0.0005757290  0.0237340322  0.0091657903  0.037718647  0.0204348367
## 2014-07-31 -0.0025120773  0.0135556515 -0.0263801114 -0.052009502 -0.0135286644
## 2014-08-29  0.0114312475  0.0279044800  0.0018004515  0.043657879  0.0387049914
## 2014-09-30 -0.0061674624 -0.0808567098 -0.0395983549 -0.061260238 -0.0138927304
## 2014-10-31  0.0105844482  0.0140967713 -0.0026548358  0.068874962  0.0232777406
## 2014-11-28  0.0065488425 -0.0155414654  0.0006254062  0.004773401  0.0271016612
## 2014-12-31  0.0014752273 -0.0404424585 -0.0407468428  0.025295985 -0.0025400512
## 2015-01-30  0.0203152121 -0.0068955225  0.0062267108 -0.054628098 -0.0300770969
## 2015-02-27 -0.0089887831  0.0431360217  0.0614503622  0.056914491  0.0546820383
## 2015-03-31  0.0037406476 -0.0150861345 -0.0143886491  0.010156788 -0.0158303673
## 2015-04-30 -0.0032336117  0.0662816900  0.0358164078 -0.018417977  0.0097859574
## 2015-05-29 -0.0043826561 -0.0419110760  0.0019527879  0.007509948  0.0127742761
## 2015-06-30 -0.0108259050 -0.0297466241 -0.0316789376  0.004171290 -0.0205211189
## 2015-07-31  0.0085841825 -0.0651779561  0.0201145440 -0.027375426  0.0223376979
## 2015-08-31 -0.0033635059 -0.0925127526 -0.0771523446 -0.047268308 -0.0628865798
## 2015-09-30  0.0080811340 -0.0318249529 -0.0451949903 -0.038464423 -0.0258471482
## 2015-10-30  0.0006858212  0.0618083585  0.0640259449  0.063589705  0.0816346944
## 2015-11-30 -0.0038984359 -0.0255605974 -0.0075558343  0.024415015  0.0036487044
## 2015-12-31 -0.0019185640 -0.0389470163 -0.0235950923 -0.052156953 -0.0174337556
## 2016-01-29  0.0123296867 -0.0516366187 -0.0567579406 -0.060307023 -0.0510683483
## 2016-02-29  0.0088316876 -0.0082115773 -0.0339137867  0.020605606 -0.0008264435
## 2016-03-31  0.0087086773  0.1218792024  0.0637454626  0.089909942  0.0651002339
## 2016-04-29  0.0025467240  0.0040788564  0.0219753920  0.021044151  0.0039333511
## 2016-05-31  0.0001355180 -0.0376284842 -0.0008560353  0.004397389  0.0168684929
## 2016-06-30  0.0191663118  0.0445826206 -0.0244915284  0.008291932  0.0034700969
## 2016-07-29  0.0054297837  0.0524421429  0.0390001984  0.049348668  0.0358219074
## 2016-08-31 -0.0021558087  0.0087985234  0.0053267459  0.011260969  0.0011967450
## 2016-09-30  0.0005154148  0.0248728949  0.0132792707  0.008614637  0.0000578254
## 2016-10-31 -0.0082052773 -0.0083124176 -0.0224037957 -0.038134706 -0.0174890044
## 2016-11-30 -0.0259900892 -0.0451616280 -0.0179744962  0.125246243  0.0361759898
## 2016-12-30  0.0025381862 -0.0025301646  0.0267029830  0.031492116  0.0200693430
## 2017-01-31  0.0021263787  0.0644317220  0.0323819215 -0.012143864  0.0177365193
## 2017-02-28  0.0064382796  0.0172578596  0.0118362129  0.013428761  0.0385389434
## 2017-03-31 -0.0005529595  0.0361888428  0.0318057363 -0.006533364  0.0012493108
## 2017-04-28  0.0090293919  0.0168665395  0.0239521387  0.005108014  0.0098772611
## 2017-05-31  0.0068470322  0.0280598005  0.0348105376 -0.022862520  0.0140140295
## 2017-06-30 -0.0001825827  0.0092237863  0.0029558137  0.029151332  0.0063550085
## 2017-07-31  0.0033343627  0.0565943179  0.0261878278  0.007481537  0.0203456230
## 2017-08-31  0.0093692147  0.0232439607 -0.0004481261 -0.027564509  0.0029136662
## 2017-09-29 -0.0057319896 -0.0004462386  0.0233426628  0.082321790  0.0199489139
## 2017-10-31  0.0009778708  0.0322785747  0.0166537412  0.005915990  0.0232908783
## 2017-11-30 -0.0014841419 -0.0038969318  0.0068698035  0.036913237  0.0301076847
## 2017-12-29  0.0047399630  0.0369252359  0.0133985033 -0.003731404  0.0120555322
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398481e-05 0.0001042084 4.178284e-05 -7.811933e-05 -9.030503e-06
## EEM  1.042084e-04 0.0017547144 1.039016e-03  6.437712e-04  6.795450e-04
## EFA  4.178284e-05 0.0010390164 1.064237e-03  6.490272e-04  6.975409e-04
## IJS -7.811933e-05 0.0006437712 6.490272e-04  1.565447e-03  8.290250e-04
## SPY -9.030503e-06 0.0006795450 6.975409e-04  8.290250e-04  7.408291e-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.0234749
# 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.0003874132 0.009257149 0.00581563 0.005684457 0.002330253
rowSums(component_contribution)
## [1] 0.0234749
# 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.0062307936 -0.0029359845  0.0366067099  0.052132992  0.0499234505
## 2013-02-28  0.0058908786 -0.0231047409 -0.0129694856  0.016175111  0.0126781470
## 2013-03-28  0.0009852148 -0.0102352022  0.0129694856  0.040258755  0.0372677375
## 2013-04-30  0.0096389926  0.0120846334  0.0489675941  0.001222098  0.0190301569
## 2013-05-31 -0.0202142691 -0.0494833947 -0.0306557727  0.041976442  0.0233357805
## 2013-06-28 -0.0157786307 -0.0547285548 -0.0271445100 -0.001402978 -0.0134347955
## 2013-07-31  0.0026885753  0.0131599519  0.0518604203  0.063541475  0.0503858948
## 2013-08-30 -0.0082984264 -0.0257055822 -0.0197459528 -0.034743161 -0.0304516658
## 2013-09-30  0.0111437810  0.0695887372  0.0753384317  0.063873276  0.0311566180
## 2013-10-31  0.0082924178  0.0408614644  0.0320815595  0.034234257  0.0452662435
## 2013-11-29 -0.0025101673 -0.0025941171  0.0054495480  0.041660969  0.0292071123
## 2013-12-31 -0.0055833918 -0.0040744050  0.0215282236  0.012892030  0.0255958501
## 2014-01-31  0.0152923333 -0.0903228785 -0.0534133439 -0.035775339 -0.0358844480
## 2014-02-28  0.0037568754  0.0332203632  0.0595048340  0.045257644  0.0445105797
## 2014-03-31 -0.0014818710  0.0380220914 -0.0046024888  0.013315098  0.0082612037
## 2014-04-30  0.0081839894  0.0077727303  0.0165295293 -0.023184018  0.0069276682
## 2014-05-30  0.0117206437  0.0290909254  0.0158282152  0.006205091  0.0229408808
## 2014-06-30 -0.0005757290  0.0237340322  0.0091657903  0.037718647  0.0204348367
## 2014-07-31 -0.0025120773  0.0135556515 -0.0263801114 -0.052009502 -0.0135286644
## 2014-08-29  0.0114312475  0.0279044800  0.0018004515  0.043657879  0.0387049914
## 2014-09-30 -0.0061674624 -0.0808567098 -0.0395983549 -0.061260238 -0.0138927304
## 2014-10-31  0.0105844482  0.0140967713 -0.0026548358  0.068874962  0.0232777406
## 2014-11-28  0.0065488425 -0.0155414654  0.0006254062  0.004773401  0.0271016612
## 2014-12-31  0.0014752273 -0.0404424585 -0.0407468428  0.025295985 -0.0025400512
## 2015-01-30  0.0203152121 -0.0068955225  0.0062267108 -0.054628098 -0.0300770969
## 2015-02-27 -0.0089887831  0.0431360217  0.0614503622  0.056914491  0.0546820383
## 2015-03-31  0.0037406476 -0.0150861345 -0.0143886491  0.010156788 -0.0158303673
## 2015-04-30 -0.0032336117  0.0662816900  0.0358164078 -0.018417977  0.0097859574
## 2015-05-29 -0.0043826561 -0.0419110760  0.0019527879  0.007509948  0.0127742761
## 2015-06-30 -0.0108259050 -0.0297466241 -0.0316789376  0.004171290 -0.0205211189
## 2015-07-31  0.0085841825 -0.0651779561  0.0201145440 -0.027375426  0.0223376979
## 2015-08-31 -0.0033635059 -0.0925127526 -0.0771523446 -0.047268308 -0.0628865798
## 2015-09-30  0.0080811340 -0.0318249529 -0.0451949903 -0.038464423 -0.0258471482
## 2015-10-30  0.0006858212  0.0618083585  0.0640259449  0.063589705  0.0816346944
## 2015-11-30 -0.0038984359 -0.0255605974 -0.0075558343  0.024415015  0.0036487044
## 2015-12-31 -0.0019185640 -0.0389470163 -0.0235950923 -0.052156953 -0.0174337556
## 2016-01-29  0.0123296867 -0.0516366187 -0.0567579406 -0.060307023 -0.0510683483
## 2016-02-29  0.0088316876 -0.0082115773 -0.0339137867  0.020605606 -0.0008264435
## 2016-03-31  0.0087086773  0.1218792024  0.0637454626  0.089909942  0.0651002339
## 2016-04-29  0.0025467240  0.0040788564  0.0219753920  0.021044151  0.0039333511
## 2016-05-31  0.0001355180 -0.0376284842 -0.0008560353  0.004397389  0.0168684929
## 2016-06-30  0.0191663118  0.0445826206 -0.0244915284  0.008291932  0.0034700969
## 2016-07-29  0.0054297837  0.0524421429  0.0390001984  0.049348668  0.0358219074
## 2016-08-31 -0.0021558087  0.0087985234  0.0053267459  0.011260969  0.0011967450
## 2016-09-30  0.0005154148  0.0248728949  0.0132792707  0.008614637  0.0000578254
## 2016-10-31 -0.0082052773 -0.0083124176 -0.0224037957 -0.038134706 -0.0174890044
## 2016-11-30 -0.0259900892 -0.0451616280 -0.0179744962  0.125246243  0.0361759898
## 2016-12-30  0.0025381862 -0.0025301646  0.0267029830  0.031492116  0.0200693430
## 2017-01-31  0.0021263787  0.0644317220  0.0323819215 -0.012143864  0.0177365193
## 2017-02-28  0.0064382796  0.0172578596  0.0118362129  0.013428761  0.0385389434
## 2017-03-31 -0.0005529595  0.0361888428  0.0318057363 -0.006533364  0.0012493108
## 2017-04-28  0.0090293919  0.0168665395  0.0239521387  0.005108014  0.0098772611
## 2017-05-31  0.0068470322  0.0280598005  0.0348105376 -0.022862520  0.0140140295
## 2017-06-30 -0.0001825827  0.0092237863  0.0029558137  0.029151332  0.0063550085
## 2017-07-31  0.0033343627  0.0565943179  0.0261878278  0.007481537  0.0203456230
## 2017-08-31  0.0093692147  0.0232439607 -0.0004481261 -0.027564509  0.0029136662
## 2017-09-29 -0.0057319896 -0.0004462386  0.0233426628  0.082321790  0.0199489139
## 2017-10-31  0.0009778708  0.0322785747  0.0166537412  0.005915990  0.0232908783
## 2017-11-30 -0.0014841419 -0.0038969318  0.0068698035  0.036913237  0.0301076847
## 2017-12-29  0.0047399630  0.0369252359  0.0133985033 -0.003731404  0.0120555322
calculate_componet_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_componet_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_componet_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_componet_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