# 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.0062315646 -0.0029356841  0.0366066739  0.052133642  4.992290e-02
## 2013-02-28  0.0058908422 -0.0231051224 -0.0129697514  0.016175193  1.267827e-02
## 2013-03-28  0.0009851843 -0.0102350107  0.0129697514  0.040258133  3.726775e-02
## 2013-04-30  0.0096392076  0.0120849040  0.0489675366  0.001222682  1.903024e-02
## 2013-05-31 -0.0202139874 -0.0494834901 -0.0306556861  0.041976043  2.333539e-02
## 2013-06-28 -0.0157787936 -0.0547283223 -0.0271442917 -0.001402810 -1.343401e-02
## 2013-07-31  0.0026882309  0.0131596979  0.0518602560  0.063541216  5.038568e-02
## 2013-08-30 -0.0082976307 -0.0257054199 -0.0197461995 -0.034743342 -3.045155e-02
## 2013-09-30  0.0111433289  0.0695888236  0.0753385742  0.063873860  3.115612e-02
## 2013-10-31  0.0082919457  0.0408610601  0.0320815512  0.034233976  4.526647e-02
## 2013-11-29 -0.0025097366 -0.0025940275  0.0054497009  0.041661287  2.920742e-02
## 2013-12-31 -0.0055832250 -0.0040743613  0.0215278976  0.012892054  2.559580e-02
## 2014-01-31  0.0152916379 -0.0903227368 -0.0534132249 -0.035775134 -3.588403e-02
## 2014-02-28  0.0037568319  0.0332206618  0.0595049311  0.045257168  4.450970e-02
## 2014-03-31 -0.0014809240  0.0380217573 -0.0046024895  0.013315194  8.261529e-03
## 2014-04-30  0.0081829692  0.0077726260  0.0165294466 -0.023184041  6.927565e-03
## 2014-05-30  0.0117220476  0.0290913038  0.0158284238  0.006204980  2.294123e-02
## 2014-06-30 -0.0005761783  0.0237339410  0.0091654077  0.037718885  2.043468e-02
## 2014-07-31 -0.0025125411  0.0135555715 -0.0263797798 -0.052009344 -1.352872e-02
## 2014-08-29  0.0114309435  0.0279046807  0.0018004986  0.043658011  3.870508e-02
## 2014-09-30 -0.0061667580 -0.0808567872 -0.0395986457 -0.061260743 -1.389297e-02
## 2014-10-31  0.0105842622  0.0140964517 -0.0026547319  0.068874821  2.327798e-02
## 2014-11-28  0.0065487608 -0.0155412963  0.0006253892  0.004774032  2.710170e-02
## 2014-12-31  0.0014752336 -0.0404422264 -0.0407468895  0.025295810 -2.540050e-03
## 2015-01-30  0.0203148160 -0.0068956168  0.0062263408 -0.054628245 -3.007699e-02
## 2015-02-27 -0.0089882361  0.0431359040  0.0614508588  0.056914637  5.468201e-02
## 2015-03-31  0.0037408140 -0.0150861372 -0.0143889400  0.010156379 -1.583048e-02
## 2015-04-30 -0.0032332525  0.0662814149  0.0358166605 -0.018417601  9.786079e-03
## 2015-05-29 -0.0043838122 -0.0419111291  0.0019527235  0.007509804  1.277420e-02
## 2015-06-30 -0.0108252441 -0.0297462626 -0.0316789031  0.004171457 -2.052118e-02
## 2015-07-31  0.0085842190 -0.0651784226  0.0201146365 -0.027375578  2.233777e-02
## 2015-08-31 -0.0033635547 -0.0925124728 -0.0771525624 -0.047268108 -6.288700e-02
## 2015-09-30  0.0080816911 -0.0318249253 -0.0451949829 -0.038464955 -2.584690e-02
## 2015-10-30  0.0006853482  0.0618085079  0.0640258975  0.063589885  8.163484e-02
## 2015-11-30 -0.0038985034 -0.0255605362 -0.0075555551  0.024415308  3.648562e-03
## 2015-12-31 -0.0019191984 -0.0389471020 -0.0235951925 -0.052157028 -1.743339e-02
## 2016-01-29  0.0123301913 -0.0516366010 -0.0567577314 -0.060307023 -5.106879e-02
## 2016-02-29  0.0088316634 -0.0082117552 -0.0339140811  0.020605230 -8.264234e-04
## 2016-03-31  0.0087091057  0.1218791941  0.0637456938  0.089910353  6.510018e-02
## 2016-04-29  0.0025462449  0.0040791492  0.0219751842  0.021044297  3.933751e-03
## 2016-05-31  0.0001353738 -0.0376285768 -0.0008560460  0.004396951  1.686836e-02
## 2016-06-30  0.0191667117  0.0445824706 -0.0244914502  0.008292413  3.469734e-03
## 2016-07-29  0.0054299839  0.0524421527  0.0390000281  0.049348321  3.582182e-02
## 2016-08-31 -0.0021565153  0.0087984755  0.0053269456  0.011261077  1.196862e-03
## 2016-09-30  0.0005156207  0.0248728142  0.0132791585  0.008614789  5.819965e-05
## 2016-10-31 -0.0082047991 -0.0083120694 -0.0224036669 -0.038135178 -1.748908e-02
## 2016-11-30 -0.0259898089 -0.0451617686 -0.0179745666  0.125246893  3.617620e-02
## 2016-12-30  0.0025378974 -0.0025300740  0.0267028628  0.031491719  2.006896e-02
## 2017-01-31  0.0021266027  0.0644315155  0.0323819590 -0.012143844  1.773633e-02
## 2017-02-28  0.0064373304  0.0172578369  0.0118363924  0.013428546  3.853939e-02
## 2017-03-31 -0.0005525665  0.0361888681  0.0318057133 -0.006533076  1.249217e-03
## 2017-04-28  0.0090290606  0.0168662874  0.0239523650  0.005107970  9.877293e-03
## 2017-05-31  0.0068471886  0.0280600984  0.0348101105 -0.022862755  1.401400e-02
## 2017-06-30 -0.0001826005  0.0092236570  0.0029559265  0.029152067  6.354754e-03
## 2017-07-31  0.0033346330  0.0565946495  0.0261877281  0.007481197  2.034585e-02
## 2017-08-31  0.0093695214  0.0232435724 -0.0004482236 -0.027564532  2.913422e-03
## 2017-09-29 -0.0057323988 -0.0004460965  0.0233429133  0.082321617  1.994914e-02
## 2017-10-31  0.0009777967  0.0322784749  0.0166537293  0.005916156  2.329076e-02
## 2017-11-30 -0.0014840998 -0.0038970493  0.0068697707  0.036913336  3.010785e-02
## 2017-12-29  0.0047402862  0.0369256799  0.0133984037 -0.003731415  1.205500e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398378e-05 0.0001042101 4.178048e-05 -7.812162e-05 -9.032562e-06
## EEM  1.042101e-04 0.0017547132 1.039018e-03  6.437737e-04  6.795432e-04
## EFA  4.178048e-05 0.0010390175 1.064239e-03  6.490303e-04  6.975407e-04
## IJS -7.812162e-05 0.0006437737 6.490303e-04  1.565453e-03  8.290258e-04
## SPY -9.032562e-06 0.0006795432 6.975407e-04  8.290258e-04  7.408282e-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.0003874027 0.009257153 0.005815633 0.005684472 0.002330249
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

# 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.0062315646 -0.0029356841  0.0366066739  0.052133642  4.992290e-02
## 2013-02-28  0.0058908422 -0.0231051224 -0.0129697514  0.016175193  1.267827e-02
## 2013-03-28  0.0009851843 -0.0102350107  0.0129697514  0.040258133  3.726775e-02
## 2013-04-30  0.0096392076  0.0120849040  0.0489675366  0.001222682  1.903024e-02
## 2013-05-31 -0.0202139874 -0.0494834901 -0.0306556861  0.041976043  2.333539e-02
## 2013-06-28 -0.0157787936 -0.0547283223 -0.0271442917 -0.001402810 -1.343401e-02
## 2013-07-31  0.0026882309  0.0131596979  0.0518602560  0.063541216  5.038568e-02
## 2013-08-30 -0.0082976307 -0.0257054199 -0.0197461995 -0.034743342 -3.045155e-02
## 2013-09-30  0.0111433289  0.0695888236  0.0753385742  0.063873860  3.115612e-02
## 2013-10-31  0.0082919457  0.0408610601  0.0320815512  0.034233976  4.526647e-02
## 2013-11-29 -0.0025097366 -0.0025940275  0.0054497009  0.041661287  2.920742e-02
## 2013-12-31 -0.0055832250 -0.0040743613  0.0215278976  0.012892054  2.559580e-02
## 2014-01-31  0.0152916379 -0.0903227368 -0.0534132249 -0.035775134 -3.588403e-02
## 2014-02-28  0.0037568319  0.0332206618  0.0595049311  0.045257168  4.450970e-02
## 2014-03-31 -0.0014809240  0.0380217573 -0.0046024895  0.013315194  8.261529e-03
## 2014-04-30  0.0081829692  0.0077726260  0.0165294466 -0.023184041  6.927565e-03
## 2014-05-30  0.0117220476  0.0290913038  0.0158284238  0.006204980  2.294123e-02
## 2014-06-30 -0.0005761783  0.0237339410  0.0091654077  0.037718885  2.043468e-02
## 2014-07-31 -0.0025125411  0.0135555715 -0.0263797798 -0.052009344 -1.352872e-02
## 2014-08-29  0.0114309435  0.0279046807  0.0018004986  0.043658011  3.870508e-02
## 2014-09-30 -0.0061667580 -0.0808567872 -0.0395986457 -0.061260743 -1.389297e-02
## 2014-10-31  0.0105842622  0.0140964517 -0.0026547319  0.068874821  2.327798e-02
## 2014-11-28  0.0065487608 -0.0155412963  0.0006253892  0.004774032  2.710170e-02
## 2014-12-31  0.0014752336 -0.0404422264 -0.0407468895  0.025295810 -2.540050e-03
## 2015-01-30  0.0203148160 -0.0068956168  0.0062263408 -0.054628245 -3.007699e-02
## 2015-02-27 -0.0089882361  0.0431359040  0.0614508588  0.056914637  5.468201e-02
## 2015-03-31  0.0037408140 -0.0150861372 -0.0143889400  0.010156379 -1.583048e-02
## 2015-04-30 -0.0032332525  0.0662814149  0.0358166605 -0.018417601  9.786079e-03
## 2015-05-29 -0.0043838122 -0.0419111291  0.0019527235  0.007509804  1.277420e-02
## 2015-06-30 -0.0108252441 -0.0297462626 -0.0316789031  0.004171457 -2.052118e-02
## 2015-07-31  0.0085842190 -0.0651784226  0.0201146365 -0.027375578  2.233777e-02
## 2015-08-31 -0.0033635547 -0.0925124728 -0.0771525624 -0.047268108 -6.288700e-02
## 2015-09-30  0.0080816911 -0.0318249253 -0.0451949829 -0.038464955 -2.584690e-02
## 2015-10-30  0.0006853482  0.0618085079  0.0640258975  0.063589885  8.163484e-02
## 2015-11-30 -0.0038985034 -0.0255605362 -0.0075555551  0.024415308  3.648562e-03
## 2015-12-31 -0.0019191984 -0.0389471020 -0.0235951925 -0.052157028 -1.743339e-02
## 2016-01-29  0.0123301913 -0.0516366010 -0.0567577314 -0.060307023 -5.106879e-02
## 2016-02-29  0.0088316634 -0.0082117552 -0.0339140811  0.020605230 -8.264234e-04
## 2016-03-31  0.0087091057  0.1218791941  0.0637456938  0.089910353  6.510018e-02
## 2016-04-29  0.0025462449  0.0040791492  0.0219751842  0.021044297  3.933751e-03
## 2016-05-31  0.0001353738 -0.0376285768 -0.0008560460  0.004396951  1.686836e-02
## 2016-06-30  0.0191667117  0.0445824706 -0.0244914502  0.008292413  3.469734e-03
## 2016-07-29  0.0054299839  0.0524421527  0.0390000281  0.049348321  3.582182e-02
## 2016-08-31 -0.0021565153  0.0087984755  0.0053269456  0.011261077  1.196862e-03
## 2016-09-30  0.0005156207  0.0248728142  0.0132791585  0.008614789  5.819965e-05
## 2016-10-31 -0.0082047991 -0.0083120694 -0.0224036669 -0.038135178 -1.748908e-02
## 2016-11-30 -0.0259898089 -0.0451617686 -0.0179745666  0.125246893  3.617620e-02
## 2016-12-30  0.0025378974 -0.0025300740  0.0267028628  0.031491719  2.006896e-02
## 2017-01-31  0.0021266027  0.0644315155  0.0323819590 -0.012143844  1.773633e-02
## 2017-02-28  0.0064373304  0.0172578369  0.0118363924  0.013428546  3.853939e-02
## 2017-03-31 -0.0005525665  0.0361888681  0.0318057133 -0.006533076  1.249217e-03
## 2017-04-28  0.0090290606  0.0168662874  0.0239523650  0.005107970  9.877293e-03
## 2017-05-31  0.0068471886  0.0280600984  0.0348101105 -0.022862755  1.401400e-02
## 2017-06-30 -0.0001826005  0.0092236570  0.0029559265  0.029152067  6.354754e-03
## 2017-07-31  0.0033346330  0.0565946495  0.0261877281  0.007481197  2.034585e-02
## 2017-08-31  0.0093695214  0.0232435724 -0.0004482236 -0.027564532  2.913422e-03
## 2017-09-29 -0.0057323988 -0.0004460965  0.0233429133  0.082321617  1.994914e-02
## 2017-10-31  0.0009777967  0.0322784749  0.0166537293  0.005916156  2.329076e-02
## 2017-11-30 -0.0014840998 -0.0038970493  0.0068697707  0.036913336  3.010785e-02
## 2017-12-29  0.0047402862  0.0369256799  0.0133984037 -0.003731415  1.205500e-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
    
    # 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 = "Precent 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 weight
    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 = "Precent Contribution to Portfolio Volatility and Weight",
         x = "Precent",
         y = NULL)

6 Rolling Component Contribution