# 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.0062313750 -0.0029354630  0.0366063075  0.052133543  4.992272e-02
## 2013-02-28  0.0058912661 -0.0231051173 -0.0129694791  0.016175455  1.267848e-02
## 2013-03-28  0.0009850712 -0.0102351227  0.0129694791  0.040258194  3.726772e-02
## 2013-04-30  0.0096393383  0.0120849026  0.0489678953  0.001222163  1.903030e-02
## 2013-05-31 -0.0202136301 -0.0494837218 -0.0306558616  0.041976486  2.333515e-02
## 2013-06-28 -0.0157790017 -0.0547282663 -0.0271442942 -0.001403043 -1.343435e-02
## 2013-07-31  0.0026873392  0.0131597606  0.0518602606  0.063541452  5.038578e-02
## 2013-08-30 -0.0082978167 -0.0257054834 -0.0197462899 -0.034743279 -3.045103e-02
## 2013-09-30  0.0111446506  0.0695885319  0.0753386693  0.063873556  3.115622e-02
## 2013-10-31  0.0082914658  0.0408614153  0.0320816335  0.034234195  4.526667e-02
## 2013-11-29 -0.0025100362 -0.0025941415  0.0054495424  0.041660925  2.920651e-02
## 2013-12-31 -0.0055829044 -0.0040741329  0.0215281336  0.012892370  2.559638e-02
## 2014-01-31  0.0152914329 -0.0903229138 -0.0534133024 -0.035775319 -3.588453e-02
## 2014-02-28  0.0037566444  0.0332207850  0.0595051622  0.045257568  4.451019e-02
## 2014-03-31 -0.0014808095  0.0380215801 -0.0046026433  0.013314955  8.261218e-03
## 2014-04-30  0.0081829052  0.0077728584  0.0165291408 -0.023184279  6.927471e-03
## 2014-05-30  0.0117218179  0.0290911880  0.0158286522  0.006205384  2.294109e-02
## 2014-06-30 -0.0005758743  0.0237337214  0.0091654820  0.037718567  2.043479e-02
## 2014-07-31 -0.0025122026  0.0135557911 -0.0263800066 -0.052009454 -1.352855e-02
## 2014-08-29  0.0114307715  0.0279046807  0.0018006511  0.043658288  3.870465e-02
## 2014-09-30 -0.0061673311 -0.0808569014 -0.0395984874 -0.061260692 -1.389219e-02
## 2014-10-31  0.0105844686  0.0140965659 -0.0026548903  0.068874818  2.327788e-02
## 2014-11-28  0.0065489703 -0.0155414106  0.0006251511  0.004773794  2.710122e-02
## 2014-12-31  0.0014750949 -0.0404421120 -0.0407465688  0.025295977 -2.539838e-03
## 2015-01-30  0.0203152692 -0.0068958566  0.0062263403 -0.054628351 -3.007708e-02
## 2015-02-27 -0.0089879221  0.0431362586  0.0614508539  0.056914568  5.468224e-02
## 2015-03-31  0.0037398053 -0.0150863686 -0.0143889389  0.010156389 -1.583045e-02
## 2015-04-30 -0.0032334700  0.0662814223  0.0358165822 -0.018417519  9.785951e-03
## 2015-05-29 -0.0043833847 -0.0419110200  0.0019525726  0.007509649  1.277379e-02
## 2015-06-30 -0.0108250055 -0.0297464970 -0.0316784407  0.004171671 -2.052093e-02
## 2015-07-31  0.0085842148 -0.0651781257  0.0201141723 -0.027375517  2.233772e-02
## 2015-08-31 -0.0033638194 -0.0925124668 -0.0771523273 -0.047268345 -6.288669e-02
## 2015-09-30  0.0080818217 -0.0318248523 -0.0451948066 -0.038464799 -2.584705e-02
## 2015-10-30  0.0006853845  0.0618082331  0.0640258814  0.063589878  8.163505e-02
## 2015-11-30 -0.0038981617 -0.0255604030 -0.0075559609  0.024414954  3.648704e-03
## 2015-12-31 -0.0019191601 -0.0389471020 -0.0235949460 -0.052156758 -1.743365e-02
## 2016-01-29  0.0123294789 -0.0516368254 -0.0567578149 -0.060306689 -5.106865e-02
## 2016-02-29  0.0088319300 -0.0082114554 -0.0339139897  0.020604949 -8.265769e-04
## 2016-03-31  0.0087088708  0.1218789852  0.0637457739  0.089910260  6.510007e-02
## 2016-04-29  0.0025461146  0.0040793491  0.0219749289  0.021044238  3.933736e-03
## 2016-05-31  0.0001357437 -0.0376286433 -0.0008558782  0.004397116  1.686829e-02
## 2016-06-30  0.0191666728  0.0445823386 -0.0244916202  0.008292550  3.470052e-03
## 2016-07-29  0.0054297811  0.0524419089  0.0390001141  0.049348371  3.582166e-02
## 2016-08-31 -0.0021562627  0.0087989755  0.0053269456  0.011260829  1.197301e-03
## 2016-09-30  0.0005158048  0.0248726900  0.0132791585  0.008614876  5.757072e-05
## 2016-10-31 -0.0082052308 -0.0083120694 -0.0224038330 -0.038134929 -1.748905e-02
## 2016-11-30 -0.0259894448 -0.0451619603 -0.0179741467  0.125246366  3.617599e-02
## 2016-12-30  0.0025378099 -0.0025299463  0.0267025268  0.031492233  2.006940e-02
## 2017-01-31  0.0021263343  0.0644315195  0.0323819616 -0.012144276  1.773623e-02
## 2017-02-28  0.0064377606  0.0172576608  0.0118364721  0.013428623  3.853934e-02
## 2017-03-31 -0.0005527881  0.0361891043  0.0318057896 -0.006532958  1.249307e-03
## 2017-04-28  0.0090290824  0.0168665113  0.0239521397  0.005107774  9.877017e-03
## 2017-05-31  0.0068477752  0.0280599834  0.0348102595 -0.022862689  1.401423e-02
## 2017-06-30 -0.0001826963  0.0092236560  0.0029559982  0.029151874  6.354832e-03
## 2017-07-31  0.0033344687  0.0565944397  0.0261877263  0.007481738  2.034579e-02
## 2017-08-31  0.0093692439  0.0232437740 -0.0004482935 -0.027564791  2.913448e-03
## 2017-09-29 -0.0057325044 -0.0004461961  0.0233429133  0.082321138  1.994915e-02
## 2017-10-31  0.0009780905  0.0322784749  0.0166536621  0.005916547  2.329061e-02
## 2017-11-30 -0.0014841430 -0.0038969524  0.0068698379  0.036913286  3.010826e-02
## 2017-12-29  0.0047403701  0.0369254897  0.0133982062 -0.003731136  1.205487e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398345e-05 0.0001042114 4.178228e-05 -7.811897e-05 -9.031071e-06
## EEM  1.042114e-04 0.0017547123 1.039018e-03  6.437735e-04  6.795431e-04
## EFA  4.178228e-05 0.0010390178 1.064237e-03  6.490308e-04  6.975407e-04
## IJS -7.811897e-05 0.0006437735 6.490308e-04  1.565450e-03  8.290248e-04
## SPY -9.031071e-06 0.0006795431 6.975407e-04  8.290248e-04  7.408271e-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.02347492
# 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.0003874164 0.009257151 0.005815634 0.005684469 0.002330248
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062313750 -0.0029354630  0.0366063075  0.052133543  4.992272e-02
## 2013-02-28  0.0058912661 -0.0231051173 -0.0129694791  0.016175455  1.267848e-02
## 2013-03-28  0.0009850712 -0.0102351227  0.0129694791  0.040258194  3.726772e-02
## 2013-04-30  0.0096393383  0.0120849026  0.0489678953  0.001222163  1.903030e-02
## 2013-05-31 -0.0202136301 -0.0494837218 -0.0306558616  0.041976486  2.333515e-02
## 2013-06-28 -0.0157790017 -0.0547282663 -0.0271442942 -0.001403043 -1.343435e-02
## 2013-07-31  0.0026873392  0.0131597606  0.0518602606  0.063541452  5.038578e-02
## 2013-08-30 -0.0082978167 -0.0257054834 -0.0197462899 -0.034743279 -3.045103e-02
## 2013-09-30  0.0111446506  0.0695885319  0.0753386693  0.063873556  3.115622e-02
## 2013-10-31  0.0082914658  0.0408614153  0.0320816335  0.034234195  4.526667e-02
## 2013-11-29 -0.0025100362 -0.0025941415  0.0054495424  0.041660925  2.920651e-02
## 2013-12-31 -0.0055829044 -0.0040741329  0.0215281336  0.012892370  2.559638e-02
## 2014-01-31  0.0152914329 -0.0903229138 -0.0534133024 -0.035775319 -3.588453e-02
## 2014-02-28  0.0037566444  0.0332207850  0.0595051622  0.045257568  4.451019e-02
## 2014-03-31 -0.0014808095  0.0380215801 -0.0046026433  0.013314955  8.261218e-03
## 2014-04-30  0.0081829052  0.0077728584  0.0165291408 -0.023184279  6.927471e-03
## 2014-05-30  0.0117218179  0.0290911880  0.0158286522  0.006205384  2.294109e-02
## 2014-06-30 -0.0005758743  0.0237337214  0.0091654820  0.037718567  2.043479e-02
## 2014-07-31 -0.0025122026  0.0135557911 -0.0263800066 -0.052009454 -1.352855e-02
## 2014-08-29  0.0114307715  0.0279046807  0.0018006511  0.043658288  3.870465e-02
## 2014-09-30 -0.0061673311 -0.0808569014 -0.0395984874 -0.061260692 -1.389219e-02
## 2014-10-31  0.0105844686  0.0140965659 -0.0026548903  0.068874818  2.327788e-02
## 2014-11-28  0.0065489703 -0.0155414106  0.0006251511  0.004773794  2.710122e-02
## 2014-12-31  0.0014750949 -0.0404421120 -0.0407465688  0.025295977 -2.539838e-03
## 2015-01-30  0.0203152692 -0.0068958566  0.0062263403 -0.054628351 -3.007708e-02
## 2015-02-27 -0.0089879221  0.0431362586  0.0614508539  0.056914568  5.468224e-02
## 2015-03-31  0.0037398053 -0.0150863686 -0.0143889389  0.010156389 -1.583045e-02
## 2015-04-30 -0.0032334700  0.0662814223  0.0358165822 -0.018417519  9.785951e-03
## 2015-05-29 -0.0043833847 -0.0419110200  0.0019525726  0.007509649  1.277379e-02
## 2015-06-30 -0.0108250055 -0.0297464970 -0.0316784407  0.004171671 -2.052093e-02
## 2015-07-31  0.0085842148 -0.0651781257  0.0201141723 -0.027375517  2.233772e-02
## 2015-08-31 -0.0033638194 -0.0925124668 -0.0771523273 -0.047268345 -6.288669e-02
## 2015-09-30  0.0080818217 -0.0318248523 -0.0451948066 -0.038464799 -2.584705e-02
## 2015-10-30  0.0006853845  0.0618082331  0.0640258814  0.063589878  8.163505e-02
## 2015-11-30 -0.0038981617 -0.0255604030 -0.0075559609  0.024414954  3.648704e-03
## 2015-12-31 -0.0019191601 -0.0389471020 -0.0235949460 -0.052156758 -1.743365e-02
## 2016-01-29  0.0123294789 -0.0516368254 -0.0567578149 -0.060306689 -5.106865e-02
## 2016-02-29  0.0088319300 -0.0082114554 -0.0339139897  0.020604949 -8.265769e-04
## 2016-03-31  0.0087088708  0.1218789852  0.0637457739  0.089910260  6.510007e-02
## 2016-04-29  0.0025461146  0.0040793491  0.0219749289  0.021044238  3.933736e-03
## 2016-05-31  0.0001357437 -0.0376286433 -0.0008558782  0.004397116  1.686829e-02
## 2016-06-30  0.0191666728  0.0445823386 -0.0244916202  0.008292550  3.470052e-03
## 2016-07-29  0.0054297811  0.0524419089  0.0390001141  0.049348371  3.582166e-02
## 2016-08-31 -0.0021562627  0.0087989755  0.0053269456  0.011260829  1.197301e-03
## 2016-09-30  0.0005158048  0.0248726900  0.0132791585  0.008614876  5.757072e-05
## 2016-10-31 -0.0082052308 -0.0083120694 -0.0224038330 -0.038134929 -1.748905e-02
## 2016-11-30 -0.0259894448 -0.0451619603 -0.0179741467  0.125246366  3.617599e-02
## 2016-12-30  0.0025378099 -0.0025299463  0.0267025268  0.031492233  2.006940e-02
## 2017-01-31  0.0021263343  0.0644315195  0.0323819616 -0.012144276  1.773623e-02
## 2017-02-28  0.0064377606  0.0172576608  0.0118364721  0.013428623  3.853934e-02
## 2017-03-31 -0.0005527881  0.0361891043  0.0318057896 -0.006532958  1.249307e-03
## 2017-04-28  0.0090290824  0.0168665113  0.0239521397  0.005107774  9.877017e-03
## 2017-05-31  0.0068477752  0.0280599834  0.0348102595 -0.022862689  1.401423e-02
## 2017-06-30 -0.0001826963  0.0092236560  0.0029559982  0.029151874  6.354832e-03
## 2017-07-31  0.0033344687  0.0565944397  0.0261877263  0.007481738  2.034579e-02
## 2017-08-31  0.0093692439  0.0232437740 -0.0004482935 -0.027564791  2.913448e-03
## 2017-09-29 -0.0057325044 -0.0004461961  0.0233429133  0.082321138  1.994915e-02
## 2017-10-31  0.0009780905  0.0322784749  0.0166536621  0.005916547  2.329061e-02
## 2017-11-30 -0.0014841430 -0.0038969524  0.0068698379  0.036913286  3.010826e-02
## 2017-12-29  0.0047403701  0.0369254897  0.0133982062 -0.003731136  1.205487e-02
# Custom function
calculate_component_contribution <- function(.data, w) {

    # Covariance of asset returns
    covariance_matrix <- cov(.data)
    
    # 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 and Weight

asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution")%>%
    
    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")

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") +
    
    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