# 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.0062310127 -0.0029353724  0.0366062159  0.052133405  4.992290e-02
## 2013-02-28  0.0058909236 -0.0231052291 -0.0129694742  0.016176080  1.267851e-02
## 2013-03-28  0.0009849862 -0.0102348782  0.0129694742  0.040258003  3.726799e-02
## 2013-04-30  0.0096396833  0.0120847738  0.0489677107  0.001222213  1.903000e-02
## 2013-05-31 -0.0202144652 -0.0494836945 -0.0306558657  0.041976202  2.333515e-02
## 2013-06-28 -0.0157782185 -0.0547283441 -0.0271442523 -0.001402900 -1.343399e-02
## 2013-07-31  0.0026883996  0.0131595438  0.0518603956  0.063541408  5.038606e-02
## 2013-08-30 -0.0082987915 -0.0257053864 -0.0197463316 -0.034743459 -3.045152e-02
## 2013-09-30  0.0111436918  0.0695884074  0.0753384346  0.063873668  3.115571e-02
## 2013-10-31  0.0082922975  0.0408613118  0.0320817420  0.034234090  4.526678e-02
## 2013-11-29 -0.0025095286 -0.0025937260  0.0054496815  0.041661299  2.920699e-02
## 2013-12-31 -0.0055833272 -0.0040742390  0.0215280874  0.012892026  2.559638e-02
## 2014-01-31  0.0152913653 -0.0903226541 -0.0534133482 -0.035775432 -3.588483e-02
## 2014-02-28  0.0037571025  0.0332206685  0.0595051619  0.045257371  4.451038e-02
## 2014-03-31 -0.0014819218  0.0380214520 -0.0046027319  0.013315312  8.261560e-03
## 2014-04-30  0.0081832244  0.0077729115  0.0165291083 -0.023183945  6.927474e-03
## 2014-05-30  0.0117220770  0.0290909579  0.0158287577  0.006205135  2.294092e-02
## 2014-06-30 -0.0005755922  0.0237339449  0.0091653391  0.037718491  2.043445e-02
## 2014-07-31 -0.0025122236  0.0135556888 -0.0263799706 -0.052008949 -1.352880e-02
## 2014-08-29  0.0114305827  0.0279043913  0.0018005555  0.043657775  3.870520e-02
## 2014-09-30 -0.0061674635 -0.0808564459 -0.0395983796 -0.061260845 -1.389257e-02
## 2014-10-31  0.0105847559  0.0140964557 -0.0026546880  0.068874824  2.327805e-02
## 2014-11-28  0.0065480418 -0.0155415604  0.0006250839  0.004774074  2.710125e-02
## 2014-12-31  0.0014752619 -0.0404416386 -0.0407466475  0.025295378 -2.539994e-03
## 2015-01-30  0.0203153773 -0.0068959409  0.0062266203 -0.054627802 -3.007659e-02
## 2015-02-27 -0.0089880230  0.0431360242  0.0614504983  0.056914685  5.468183e-02
## 2015-03-31  0.0037396872 -0.0150858838 -0.0143887929  0.010156426 -1.583008e-02
## 2015-04-30 -0.0032324714  0.0662810527  0.0358165791 -0.018417906  9.785472e-03
## 2015-05-29 -0.0043839521 -0.0419106881  0.0019525558  0.007509829  1.277430e-02
## 2015-06-30 -0.0108250275 -0.0297469958 -0.0316788449  0.004171822 -2.052116e-02
## 2015-07-31  0.0085842584 -0.0651779537  0.0201146545 -0.027375566  2.233782e-02
## 2015-08-31 -0.0033637438 -0.0925123143 -0.0771524985 -0.047268350 -6.288675e-02
## 2015-09-30  0.0080816793 -0.0318250973 -0.0451948457 -0.038465003 -2.584741e-02
## 2015-10-30  0.0006852747  0.0618083429  0.0640259896  0.063590070  8.163547e-02
## 2015-11-30 -0.0038980278 -0.0255606118 -0.0075558951  0.024414827  3.648361e-03
## 2015-12-31 -0.0019195684 -0.0389469938 -0.0235951360 -0.052156782 -1.743378e-02
## 2016-01-29  0.0123304925 -0.0516366195 -0.0567578828 -0.060306791 -5.106845e-02
## 2016-02-29  0.0088320094 -0.0082116139 -0.0339137399  0.020605075 -8.264552e-04
## 2016-03-31  0.0087085763  0.1218788136  0.0637456621  0.089910487  6.510026e-02
## 2016-04-29  0.0025461030  0.0040793099  0.0219751028  0.021044352  3.933364e-03
## 2016-05-31  0.0001356102 -0.0376283931 -0.0008560850  0.004397241  1.686843e-02
## 2016-06-30  0.0191669289  0.0445823535 -0.0244916138  0.008292014  3.470080e-03
## 2016-07-29  0.0054293623  0.0524422915  0.0390002419  0.049348198  3.582157e-02
## 2016-08-31 -0.0021561487  0.0087984693  0.0053269298  0.011261234  1.197172e-03
## 2016-09-30  0.0005155028  0.0248726666  0.0132792461  0.008614844  5.786503e-05
## 2016-10-31 -0.0082047487 -0.0083121401 -0.0224039192 -0.038135010 -1.748905e-02
## 2016-11-30 -0.0259897784 -0.0451617582 -0.0179744018  0.125246485  3.617583e-02
## 2016-12-30  0.0025385314 -0.0025299833  0.0267027328  0.031491994  2.006917e-02
## 2017-01-31  0.0021253449  0.0644314792  0.0323819993 -0.012144060  1.773648e-02
## 2017-02-28  0.0064381687  0.0172575929  0.0118364324  0.013428485  3.853953e-02
## 2017-03-31 -0.0005527062  0.0361892156  0.0318055948 -0.006532867  1.249149e-03
## 2017-04-28  0.0090285668  0.0168662927  0.0239523331  0.005107676  9.877098e-03
## 2017-05-31  0.0068480133  0.0280601262  0.0348103694 -0.022862902  1.401446e-02
## 2017-06-30 -0.0001826657  0.0092235488  0.0029559257  0.029152343  6.354586e-03
## 2017-07-31  0.0033341488  0.0565946395  0.0261876618  0.007481476  2.034563e-02
## 2017-08-31  0.0093690505  0.0232438163 -0.0004482151 -0.027564703  2.913383e-03
## 2017-09-29 -0.0057321222 -0.0004464642  0.0233428833  0.082321696  1.994934e-02
## 2017-10-31  0.0009780565  0.0322787022  0.0166535816  0.005915939  2.329066e-02
## 2017-11-30 -0.0014842909 -0.0038970184  0.0068699622  0.036913431  3.010804e-02
## 2017-12-29  0.0047407165  0.0369254195  0.0133982085 -0.003731504  1.205518e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398448e-05 0.0001042107 4.178318e-05 -7.811969e-05 -9.030962e-06
## EEM  1.042107e-04 0.0017547061 1.039016e-03  6.437722e-04  6.795434e-04
## EFA  4.178318e-05 0.0010390159 1.064238e-03  6.490306e-04  6.975416e-04
## IJS -7.811969e-05 0.0006437722 6.490306e-04  1.565451e-03  8.290260e-04
## SPY -9.030962e-06 0.0006795434 6.975416e-04  8.290260e-04  7.408304e-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.0003874178 0.009257131 0.005815637 0.00568447 0.002330253
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.0062310127 -0.0029353724  0.0366062159  0.052133405  4.992290e-02
## 2013-02-28  0.0058909236 -0.0231052291 -0.0129694742  0.016176080  1.267851e-02
## 2013-03-28  0.0009849862 -0.0102348782  0.0129694742  0.040258003  3.726799e-02
## 2013-04-30  0.0096396833  0.0120847738  0.0489677107  0.001222213  1.903000e-02
## 2013-05-31 -0.0202144652 -0.0494836945 -0.0306558657  0.041976202  2.333515e-02
## 2013-06-28 -0.0157782185 -0.0547283441 -0.0271442523 -0.001402900 -1.343399e-02
## 2013-07-31  0.0026883996  0.0131595438  0.0518603956  0.063541408  5.038606e-02
## 2013-08-30 -0.0082987915 -0.0257053864 -0.0197463316 -0.034743459 -3.045152e-02
## 2013-09-30  0.0111436918  0.0695884074  0.0753384346  0.063873668  3.115571e-02
## 2013-10-31  0.0082922975  0.0408613118  0.0320817420  0.034234090  4.526678e-02
## 2013-11-29 -0.0025095286 -0.0025937260  0.0054496815  0.041661299  2.920699e-02
## 2013-12-31 -0.0055833272 -0.0040742390  0.0215280874  0.012892026  2.559638e-02
## 2014-01-31  0.0152913653 -0.0903226541 -0.0534133482 -0.035775432 -3.588483e-02
## 2014-02-28  0.0037571025  0.0332206685  0.0595051619  0.045257371  4.451038e-02
## 2014-03-31 -0.0014819218  0.0380214520 -0.0046027319  0.013315312  8.261560e-03
## 2014-04-30  0.0081832244  0.0077729115  0.0165291083 -0.023183945  6.927474e-03
## 2014-05-30  0.0117220770  0.0290909579  0.0158287577  0.006205135  2.294092e-02
## 2014-06-30 -0.0005755922  0.0237339449  0.0091653391  0.037718491  2.043445e-02
## 2014-07-31 -0.0025122236  0.0135556888 -0.0263799706 -0.052008949 -1.352880e-02
## 2014-08-29  0.0114305827  0.0279043913  0.0018005555  0.043657775  3.870520e-02
## 2014-09-30 -0.0061674635 -0.0808564459 -0.0395983796 -0.061260845 -1.389257e-02
## 2014-10-31  0.0105847559  0.0140964557 -0.0026546880  0.068874824  2.327805e-02
## 2014-11-28  0.0065480418 -0.0155415604  0.0006250839  0.004774074  2.710125e-02
## 2014-12-31  0.0014752619 -0.0404416386 -0.0407466475  0.025295378 -2.539994e-03
## 2015-01-30  0.0203153773 -0.0068959409  0.0062266203 -0.054627802 -3.007659e-02
## 2015-02-27 -0.0089880230  0.0431360242  0.0614504983  0.056914685  5.468183e-02
## 2015-03-31  0.0037396872 -0.0150858838 -0.0143887929  0.010156426 -1.583008e-02
## 2015-04-30 -0.0032324714  0.0662810527  0.0358165791 -0.018417906  9.785472e-03
## 2015-05-29 -0.0043839521 -0.0419106881  0.0019525558  0.007509829  1.277430e-02
## 2015-06-30 -0.0108250275 -0.0297469958 -0.0316788449  0.004171822 -2.052116e-02
## 2015-07-31  0.0085842584 -0.0651779537  0.0201146545 -0.027375566  2.233782e-02
## 2015-08-31 -0.0033637438 -0.0925123143 -0.0771524985 -0.047268350 -6.288675e-02
## 2015-09-30  0.0080816793 -0.0318250973 -0.0451948457 -0.038465003 -2.584741e-02
## 2015-10-30  0.0006852747  0.0618083429  0.0640259896  0.063590070  8.163547e-02
## 2015-11-30 -0.0038980278 -0.0255606118 -0.0075558951  0.024414827  3.648361e-03
## 2015-12-31 -0.0019195684 -0.0389469938 -0.0235951360 -0.052156782 -1.743378e-02
## 2016-01-29  0.0123304925 -0.0516366195 -0.0567578828 -0.060306791 -5.106845e-02
## 2016-02-29  0.0088320094 -0.0082116139 -0.0339137399  0.020605075 -8.264552e-04
## 2016-03-31  0.0087085763  0.1218788136  0.0637456621  0.089910487  6.510026e-02
## 2016-04-29  0.0025461030  0.0040793099  0.0219751028  0.021044352  3.933364e-03
## 2016-05-31  0.0001356102 -0.0376283931 -0.0008560850  0.004397241  1.686843e-02
## 2016-06-30  0.0191669289  0.0445823535 -0.0244916138  0.008292014  3.470080e-03
## 2016-07-29  0.0054293623  0.0524422915  0.0390002419  0.049348198  3.582157e-02
## 2016-08-31 -0.0021561487  0.0087984693  0.0053269298  0.011261234  1.197172e-03
## 2016-09-30  0.0005155028  0.0248726666  0.0132792461  0.008614844  5.786503e-05
## 2016-10-31 -0.0082047487 -0.0083121401 -0.0224039192 -0.038135010 -1.748905e-02
## 2016-11-30 -0.0259897784 -0.0451617582 -0.0179744018  0.125246485  3.617583e-02
## 2016-12-30  0.0025385314 -0.0025299833  0.0267027328  0.031491994  2.006917e-02
## 2017-01-31  0.0021253449  0.0644314792  0.0323819993 -0.012144060  1.773648e-02
## 2017-02-28  0.0064381687  0.0172575929  0.0118364324  0.013428485  3.853953e-02
## 2017-03-31 -0.0005527062  0.0361892156  0.0318055948 -0.006532867  1.249149e-03
## 2017-04-28  0.0090285668  0.0168662927  0.0239523331  0.005107676  9.877098e-03
## 2017-05-31  0.0068480133  0.0280601262  0.0348103694 -0.022862902  1.401446e-02
## 2017-06-30 -0.0001826657  0.0092235488  0.0029559257  0.029152343  6.354586e-03
## 2017-07-31  0.0033341488  0.0565946395  0.0261876618  0.007481476  2.034563e-02
## 2017-08-31  0.0093690505  0.0232438163 -0.0004482151 -0.027564703  2.913383e-03
## 2017-09-29 -0.0057321222 -0.0004464642  0.0233428833  0.082321696  1.994934e-02
## 2017-10-31  0.0009780565  0.0322787022  0.0166535816  0.005915939  2.329066e-02
## 2017-11-30 -0.0014842909 -0.0038970184  0.0068699622  0.036913431  3.010804e-02
## 2017-12-29  0.0047407165  0.0369254195  0.0133982085 -0.003731504  1.205518e-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)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_fill_tq() +
    theme_tq() +
    
    
    labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
         x = NULL)