# 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.0062307093 -0.0029352588  0.0366063164  0.052133325  4.992283e-02
## 2013-02-28  0.0058909615 -0.0231053426 -0.0129693806  0.016175558  1.267792e-02
## 2013-03-28  0.0009851049 -0.0102349956  0.0129693806  0.040257880  3.726806e-02
## 2013-04-30  0.0096392903  0.0120846592  0.0489676260  0.001222538  1.903040e-02
## 2013-05-31 -0.0202140816 -0.0494834626 -0.0306555956  0.041976098  2.333467e-02
## 2013-06-28 -0.0157781297 -0.0547284085 -0.0271443442 -0.001402607 -1.343408e-02
## 2013-07-31  0.0026876045  0.0131598622  0.0518602163  0.063541329  5.038617e-02
## 2013-08-30 -0.0082980766 -0.0257056404 -0.0197460608 -0.034743670 -3.045110e-02
## 2013-09-30  0.0111432449  0.0695888937  0.0753383432  0.063874084  3.115569e-02
## 2013-10-31  0.0082922937  0.0408612923  0.0320818242  0.034233811  4.526662e-02
## 2013-11-29 -0.0025097180 -0.0025941928  0.0054495177  0.041661246  2.920721e-02
## 2013-12-31 -0.0055828460 -0.0040743565  0.0215279293  0.012892048  2.559625e-02
## 2014-01-31  0.0152912026 -0.0903225366 -0.0534131927 -0.035775396 -3.588506e-02
## 2014-02-28  0.0037573262  0.0332204819  0.0595050873  0.045257351  4.451046e-02
## 2014-03-31 -0.0014818452  0.0380219380 -0.0046026528  0.013315233  8.261201e-03
## 2014-04-30  0.0081838369  0.0077724933  0.0165293451 -0.023183947  6.927655e-03
## 2014-05-30  0.0117206960  0.0290911922  0.0158286006  0.006205040  2.294117e-02
## 2014-06-30 -0.0005748932  0.0237340549  0.0091653391  0.037718659  2.043435e-02
## 2014-07-31 -0.0025125343  0.0135553522 -0.0263798134 -0.052009345 -1.352822e-02
## 2014-08-29  0.0114306231  0.0279049351  0.0018005552  0.043657949  3.870440e-02
## 2014-09-30 -0.0061676372 -0.0808566440 -0.0395986182 -0.061260230 -1.389190e-02
## 2014-10-31  0.0105848358  0.0140961057 -0.0026546063  0.068874794  2.327798e-02
## 2014-11-28  0.0065490819 -0.0155408578  0.0006250839  0.004773494  2.710122e-02
## 2014-12-31  0.0014751570 -0.0404423479 -0.0407465623  0.025296062 -2.540092e-03
## 2015-01-30  0.0203149873 -0.0068957571  0.0062266198 -0.054628196 -3.007655e-02
## 2015-02-27 -0.0089878424  0.0431359626  0.0614504932  0.056914686  5.468115e-02
## 2015-03-31  0.0037398006 -0.0150862429 -0.0143888725  0.010156465 -1.583006e-02
## 2015-04-30 -0.0032327769  0.0662814118  0.0358166571 -0.018417877  9.785700e-03
## 2015-05-29 -0.0043834422 -0.0419109218  0.0019525556  0.007509862  1.277434e-02
## 2015-06-30 -0.0108258764 -0.0297469427 -0.0316788424  0.004171512 -2.052080e-02
## 2015-07-31  0.0085848688 -0.0651779659  0.0201144955 -0.027375424  2.233775e-02
## 2015-08-31 -0.0033641537 -0.0925122625 -0.0771524198 -0.047268433 -6.288680e-02
## 2015-09-30  0.0080820820 -0.0318250291 -0.0451949346 -0.038464757 -2.584716e-02
## 2015-10-30  0.0006848106  0.0618082789  0.0640259117  0.063589930  8.163490e-02
## 2015-11-30 -0.0038979835 -0.0255604049 -0.0075557282  0.024415269  3.648594e-03
## 2015-12-31 -0.0019187159 -0.0389470640 -0.0235953082 -0.052157408 -1.743368e-02
## 2016-01-29  0.0123297422 -0.0516367731 -0.0567574374 -0.060306477 -5.106860e-02
## 2016-02-29  0.0088316770 -0.0082114604 -0.0339142959  0.020605041 -8.264291e-04
## 2016-03-31  0.0087086856  0.1218788821  0.0637460333  0.089910306  6.510035e-02
## 2016-04-29  0.0025455816  0.0040792413  0.0219749278  0.021044169  3.933342e-03
## 2016-05-31  0.0001362244 -0.0376283222 -0.0008559985  0.004397257  1.686842e-02
## 2016-06-30  0.0191668877  0.0445823504 -0.0244916138  0.008292171  3.470270e-03
## 2016-07-29  0.0054294187  0.0524420950  0.0389999859  0.049348503  3.582150e-02
## 2016-08-31 -0.0021565278  0.0087987255  0.0053271858  0.011261130  1.197056e-03
## 2016-09-30  0.0005161299  0.0248725391  0.0132789949  0.008614594  5.786201e-05
## 2016-10-31 -0.0082052793 -0.0083122655 -0.0224035823 -0.038134666 -1.748890e-02
## 2016-11-30 -0.0259891726 -0.0451615672 -0.0179744875  0.125246117  3.617583e-02
## 2016-12-30  0.0025374253 -0.0025300489  0.0267028177  0.031491556  2.006922e-02
## 2017-01-31  0.0021266671  0.0644315409  0.0323820788 -0.012143517  1.773641e-02
## 2017-02-28  0.0064376907  0.0172577131  0.0118363492  0.013428770  3.853926e-02
## 2017-03-31 -0.0005527706  0.0361890338  0.0318056710 -0.006533058  1.249344e-03
## 2017-04-28  0.0090292555  0.0168664077  0.0239521757  0.005107702  9.877227e-03
## 2017-05-31  0.0068474445  0.0280597877  0.0348103694 -0.022862612  1.401427e-02
## 2017-06-30 -0.0001828290  0.0092239939  0.0029557777  0.029152077  6.354649e-03
## 2017-07-31  0.0033345803  0.0565943133  0.0261880981  0.007481341  2.034580e-02
## 2017-08-31  0.0093688942  0.0232439210 -0.0004486475 -0.027564708  2.913287e-03
## 2017-09-29 -0.0057322650 -0.0004463619  0.0233428866  0.082321610  1.994914e-02
## 2017-10-31  0.0009780894  0.0322784018  0.0166537225  0.005916215  2.329061e-02
## 2017-11-30 -0.0014841472 -0.0038967208  0.0068700998  0.036913256  3.010848e-02
## 2017-12-29  0.0047405986  0.0369250326  0.0133980709 -0.003731078  1.205455e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398274e-05 0.0001042096 4.178302e-05 -7.811891e-05 -9.029716e-06
## EEM  1.042096e-04 0.0017547081 1.039017e-03  6.437744e-04  6.795429e-04
## EFA  4.178302e-05 0.0010390169 1.064237e-03  6.490298e-04  6.975396e-04
## IJS -7.811891e-05 0.0006437744 6.490298e-04  1.565448e-03  8.290220e-04
## SPY -9.029716e-06 0.0006795429 6.975396e-04  8.290220e-04  7.408255e-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.0003874131 0.009257141 0.005815634 0.005684468 0.002330247
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.0062307093 -0.0029352588  0.0366063164  0.052133325  4.992283e-02
## 2013-02-28  0.0058909615 -0.0231053426 -0.0129693806  0.016175558  1.267792e-02
## 2013-03-28  0.0009851049 -0.0102349956  0.0129693806  0.040257880  3.726806e-02
## 2013-04-30  0.0096392903  0.0120846592  0.0489676260  0.001222538  1.903040e-02
## 2013-05-31 -0.0202140816 -0.0494834626 -0.0306555956  0.041976098  2.333467e-02
## 2013-06-28 -0.0157781297 -0.0547284085 -0.0271443442 -0.001402607 -1.343408e-02
## 2013-07-31  0.0026876045  0.0131598622  0.0518602163  0.063541329  5.038617e-02
## 2013-08-30 -0.0082980766 -0.0257056404 -0.0197460608 -0.034743670 -3.045110e-02
## 2013-09-30  0.0111432449  0.0695888937  0.0753383432  0.063874084  3.115569e-02
## 2013-10-31  0.0082922937  0.0408612923  0.0320818242  0.034233811  4.526662e-02
## 2013-11-29 -0.0025097180 -0.0025941928  0.0054495177  0.041661246  2.920721e-02
## 2013-12-31 -0.0055828460 -0.0040743565  0.0215279293  0.012892048  2.559625e-02
## 2014-01-31  0.0152912026 -0.0903225366 -0.0534131927 -0.035775396 -3.588506e-02
## 2014-02-28  0.0037573262  0.0332204819  0.0595050873  0.045257351  4.451046e-02
## 2014-03-31 -0.0014818452  0.0380219380 -0.0046026528  0.013315233  8.261201e-03
## 2014-04-30  0.0081838369  0.0077724933  0.0165293451 -0.023183947  6.927655e-03
## 2014-05-30  0.0117206960  0.0290911922  0.0158286006  0.006205040  2.294117e-02
## 2014-06-30 -0.0005748932  0.0237340549  0.0091653391  0.037718659  2.043435e-02
## 2014-07-31 -0.0025125343  0.0135553522 -0.0263798134 -0.052009345 -1.352822e-02
## 2014-08-29  0.0114306231  0.0279049351  0.0018005552  0.043657949  3.870440e-02
## 2014-09-30 -0.0061676372 -0.0808566440 -0.0395986182 -0.061260230 -1.389190e-02
## 2014-10-31  0.0105848358  0.0140961057 -0.0026546063  0.068874794  2.327798e-02
## 2014-11-28  0.0065490819 -0.0155408578  0.0006250839  0.004773494  2.710122e-02
## 2014-12-31  0.0014751570 -0.0404423479 -0.0407465623  0.025296062 -2.540092e-03
## 2015-01-30  0.0203149873 -0.0068957571  0.0062266198 -0.054628196 -3.007655e-02
## 2015-02-27 -0.0089878424  0.0431359626  0.0614504932  0.056914686  5.468115e-02
## 2015-03-31  0.0037398006 -0.0150862429 -0.0143888725  0.010156465 -1.583006e-02
## 2015-04-30 -0.0032327769  0.0662814118  0.0358166571 -0.018417877  9.785700e-03
## 2015-05-29 -0.0043834422 -0.0419109218  0.0019525556  0.007509862  1.277434e-02
## 2015-06-30 -0.0108258764 -0.0297469427 -0.0316788424  0.004171512 -2.052080e-02
## 2015-07-31  0.0085848688 -0.0651779659  0.0201144955 -0.027375424  2.233775e-02
## 2015-08-31 -0.0033641537 -0.0925122625 -0.0771524198 -0.047268433 -6.288680e-02
## 2015-09-30  0.0080820820 -0.0318250291 -0.0451949346 -0.038464757 -2.584716e-02
## 2015-10-30  0.0006848106  0.0618082789  0.0640259117  0.063589930  8.163490e-02
## 2015-11-30 -0.0038979835 -0.0255604049 -0.0075557282  0.024415269  3.648594e-03
## 2015-12-31 -0.0019187159 -0.0389470640 -0.0235953082 -0.052157408 -1.743368e-02
## 2016-01-29  0.0123297422 -0.0516367731 -0.0567574374 -0.060306477 -5.106860e-02
## 2016-02-29  0.0088316770 -0.0082114604 -0.0339142959  0.020605041 -8.264291e-04
## 2016-03-31  0.0087086856  0.1218788821  0.0637460333  0.089910306  6.510035e-02
## 2016-04-29  0.0025455816  0.0040792413  0.0219749278  0.021044169  3.933342e-03
## 2016-05-31  0.0001362244 -0.0376283222 -0.0008559985  0.004397257  1.686842e-02
## 2016-06-30  0.0191668877  0.0445823504 -0.0244916138  0.008292171  3.470270e-03
## 2016-07-29  0.0054294187  0.0524420950  0.0389999859  0.049348503  3.582150e-02
## 2016-08-31 -0.0021565278  0.0087987255  0.0053271858  0.011261130  1.197056e-03
## 2016-09-30  0.0005161299  0.0248725391  0.0132789949  0.008614594  5.786201e-05
## 2016-10-31 -0.0082052793 -0.0083122655 -0.0224035823 -0.038134666 -1.748890e-02
## 2016-11-30 -0.0259891726 -0.0451615672 -0.0179744875  0.125246117  3.617583e-02
## 2016-12-30  0.0025374253 -0.0025300489  0.0267028177  0.031491556  2.006922e-02
## 2017-01-31  0.0021266671  0.0644315409  0.0323820788 -0.012143517  1.773641e-02
## 2017-02-28  0.0064376907  0.0172577131  0.0118363492  0.013428770  3.853926e-02
## 2017-03-31 -0.0005527706  0.0361890338  0.0318056710 -0.006533058  1.249344e-03
## 2017-04-28  0.0090292555  0.0168664077  0.0239521757  0.005107702  9.877227e-03
## 2017-05-31  0.0068474445  0.0280597877  0.0348103694 -0.022862612  1.401427e-02
## 2017-06-30 -0.0001828290  0.0092239939  0.0029557777  0.029152077  6.354649e-03
## 2017-07-31  0.0033345803  0.0565943133  0.0261880981  0.007481341  2.034580e-02
## 2017-08-31  0.0093688942  0.0232439210 -0.0004486475 -0.027564708  2.913287e-03
## 2017-09-29 -0.0057322650 -0.0004463619  0.0233428866  0.082321610  1.994914e-02
## 2017-10-31  0.0009780894  0.0322784018  0.0166537225  0.005916215  2.329061e-02
## 2017-11-30 -0.0014841472 -0.0038967208  0.0068700998  0.036913256  3.010848e-02
## 2017-12-29  0.0047405986  0.0369250326  0.0133980709 -0.003731078  1.205455e-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 on 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