# 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.0062309613 -0.0029356428  0.0366062037  0.052132988  4.992264e-02
## 2013-02-28  0.0058913558 -0.0231051614 -0.0129692061  0.016175620  1.267842e-02
## 2013-03-28  0.0009846970 -0.0102350949  0.0129692061  0.040258004  3.726781e-02
## 2013-04-30  0.0096393225  0.0120847929  0.0489677005  0.001222323  1.903039e-02
## 2013-05-31 -0.0202144393 -0.0494835595 -0.0306556961  0.041976899  2.333540e-02
## 2013-06-28 -0.0157779198 -0.0547281186 -0.0271443950 -0.001403539 -1.343444e-02
## 2013-07-31  0.0026878081  0.0131596382  0.0518605098  0.063541626  5.038585e-02
## 2013-08-30 -0.0082980421 -0.0257057968 -0.0197463901 -0.034743418 -3.045118e-02
## 2013-09-30  0.0111438972  0.0695890268  0.0753385269  0.063873533  3.115559e-02
## 2013-10-31  0.0082916864  0.0408612744  0.0320817229  0.034234079  4.526690e-02
## 2013-11-29 -0.0025097162 -0.0025939543  0.0054495796  0.041661156  2.920723e-02
## 2013-12-31 -0.0055829045 -0.0040747038  0.0215281785  0.012892059  2.559576e-02
## 2014-01-31  0.0152916680 -0.0903223382 -0.0534133996 -0.035775318 -3.588450e-02
## 2014-02-28  0.0037570167  0.0332203569  0.0595051947  0.045257436  4.451048e-02
## 2014-03-31 -0.0014819319  0.0380217478 -0.0046026346  0.013315406  8.260990e-03
## 2014-04-30  0.0081835657  0.0077728543  0.0165293051 -0.023184105  6.927502e-03
## 2014-05-30  0.0117213798  0.0290910017  0.0158284565  0.006205078  2.294147e-02
## 2014-06-30 -0.0005754420  0.0237341337  0.0091654436  0.037718774  2.043469e-02
## 2014-07-31 -0.0025126610  0.0135553272 -0.0263799576 -0.052009416 -1.352885e-02
## 2014-08-29  0.0114308247  0.0279048341  0.0018006595  0.043657715  3.870432e-02
## 2014-09-30 -0.0061673593 -0.0808568044 -0.0395986345 -0.061260388 -1.389195e-02
## 2014-10-31  0.0105850255  0.0140965366 -0.0026547278  0.068874860  2.327790e-02
## 2014-11-28  0.0065483204 -0.0155412572  0.0006253064  0.004773654  2.710144e-02
## 2014-12-31  0.0014753610 -0.0404419698 -0.0407468286  0.025295919 -2.540158e-03
## 2015-01-30  0.0203152712 -0.0068957890  0.0062267332 -0.054628037 -3.007694e-02
## 2015-02-27 -0.0089886786  0.0431360321  0.0614503653  0.056914676  5.468202e-02
## 2015-03-31  0.0037402198 -0.0150861321 -0.0143886327  0.010156329 -1.583028e-02
## 2015-04-30 -0.0032327235  0.0662810586  0.0358163834 -0.018417813  9.785894e-03
## 2015-05-29 -0.0043832535 -0.0419108359  0.0019528441  0.007509996  1.277440e-02
## 2015-06-30 -0.0108260910 -0.0297467851 -0.0316789216  0.004171569 -2.052157e-02
## 2015-07-31  0.0085848134 -0.0651781303  0.0201144071 -0.027375713  2.233811e-02
## 2015-08-31 -0.0033638412 -0.0925124571 -0.0771524204 -0.047268289 -6.288694e-02
## 2015-09-30  0.0080815129 -0.0318247067 -0.0451948454 -0.038464611 -2.584709e-02
## 2015-10-30  0.0006856949  0.0618085753  0.0640260725  0.063589904  8.163529e-02
## 2015-11-30 -0.0038984075 -0.0255608204 -0.0075559234  0.024414950  3.648471e-03
## 2015-12-31 -0.0019189838 -0.0389470340 -0.0235951816 -0.052156828 -1.743388e-02
## 2016-01-29  0.0123298685 -0.0516367176 -0.0567576800 -0.060307169 -5.106853e-02
## 2016-02-29  0.0088315655 -0.0082116560 -0.0339139210  0.020605438 -8.263966e-04
## 2016-03-31  0.0087089830  0.1218790873  0.0637456380  0.089910413  6.510018e-02
## 2016-04-29  0.0025460072  0.0040791682  0.0219750423  0.021044231  3.933542e-03
## 2016-05-31  0.0001355272 -0.0376283820 -0.0008560654  0.004397040  1.686862e-02
## 2016-06-30  0.0191669870  0.0445821813 -0.0244914495  0.008292162  3.469841e-03
## 2016-07-29  0.0054292888  0.0524424066  0.0390003965  0.049348504  3.582202e-02
## 2016-08-31 -0.0021559095  0.0087987130  0.0053266413  0.011261070  1.196687e-03
## 2016-09-30  0.0005156823  0.0248726650  0.0132791638  0.008614803  5.796024e-05
## 2016-10-31 -0.0082049151 -0.0083121444 -0.0224036234 -0.038134900 -1.748925e-02
## 2016-11-30 -0.0259898102 -0.0451617676 -0.0179745854  0.125246386  3.617623e-02
## 2016-12-30  0.0025382059 -0.0025301341  0.0267030347  0.031491736  2.006905e-02
## 2017-01-31  0.0021257104  0.0644314586  0.0323817590 -0.012144064  1.773659e-02
## 2017-02-28  0.0064380329  0.0172576968  0.0118364457  0.013428755  3.853916e-02
## 2017-03-31 -0.0005528189  0.0361891619  0.0318056821 -0.006532923  1.249175e-03
## 2017-04-28  0.0090291870  0.0168662675  0.0239522302  0.005107939  9.877296e-03
## 2017-05-31  0.0068476155  0.0280599708  0.0348101443 -0.022862651  1.401413e-02
## 2017-06-30 -0.0001827819  0.0092238919  0.0029560630  0.029151917  6.354928e-03
## 2017-07-31  0.0033342548  0.0565943473  0.0261877148  0.007481460  2.034584e-02
## 2017-08-31  0.0093696188  0.0232439220 -0.0004481592 -0.027564351  2.913331e-03
## 2017-09-29 -0.0057326956 -0.0004461971  0.0233427901  0.082321270  1.994902e-02
## 2017-10-31  0.0009780402  0.0322784411  0.0166535746  0.005915878  2.329080e-02
## 2017-11-30 -0.0014836139 -0.0038971054  0.0068702098  0.036913503  3.010775e-02
## 2017-12-29  0.0047397901  0.0369256124  0.0133981527 -0.003731344  1.205521e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398472e-05 0.0001042095 4.178302e-05 -7.812059e-05 -9.030964e-06
## EEM  1.042095e-04 0.0017547102 1.039017e-03  6.437742e-04  6.795430e-04
## EFA  4.178302e-05 0.0010390169 1.064238e-03  6.490297e-04  6.975417e-04
## IJS -7.812059e-05 0.0006437742 6.490297e-04  1.565450e-03  8.290249e-04
## SPY -9.030964e-06 0.0006795430 6.975417e-04  8.290249e-04  7.408301e-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.0003874131 0.009257143 0.005815635 0.005684468 0.002330251
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.0062309613 -0.0029356428  0.0366062037  0.052132988  4.992264e-02
## 2013-02-28  0.0058913558 -0.0231051614 -0.0129692061  0.016175620  1.267842e-02
## 2013-03-28  0.0009846970 -0.0102350949  0.0129692061  0.040258004  3.726781e-02
## 2013-04-30  0.0096393225  0.0120847929  0.0489677005  0.001222323  1.903039e-02
## 2013-05-31 -0.0202144393 -0.0494835595 -0.0306556961  0.041976899  2.333540e-02
## 2013-06-28 -0.0157779198 -0.0547281186 -0.0271443950 -0.001403539 -1.343444e-02
## 2013-07-31  0.0026878081  0.0131596382  0.0518605098  0.063541626  5.038585e-02
## 2013-08-30 -0.0082980421 -0.0257057968 -0.0197463901 -0.034743418 -3.045118e-02
## 2013-09-30  0.0111438972  0.0695890268  0.0753385269  0.063873533  3.115559e-02
## 2013-10-31  0.0082916864  0.0408612744  0.0320817229  0.034234079  4.526690e-02
## 2013-11-29 -0.0025097162 -0.0025939543  0.0054495796  0.041661156  2.920723e-02
## 2013-12-31 -0.0055829045 -0.0040747038  0.0215281785  0.012892059  2.559576e-02
## 2014-01-31  0.0152916680 -0.0903223382 -0.0534133996 -0.035775318 -3.588450e-02
## 2014-02-28  0.0037570167  0.0332203569  0.0595051947  0.045257436  4.451048e-02
## 2014-03-31 -0.0014819319  0.0380217478 -0.0046026346  0.013315406  8.260990e-03
## 2014-04-30  0.0081835657  0.0077728543  0.0165293051 -0.023184105  6.927502e-03
## 2014-05-30  0.0117213798  0.0290910017  0.0158284565  0.006205078  2.294147e-02
## 2014-06-30 -0.0005754420  0.0237341337  0.0091654436  0.037718774  2.043469e-02
## 2014-07-31 -0.0025126610  0.0135553272 -0.0263799576 -0.052009416 -1.352885e-02
## 2014-08-29  0.0114308247  0.0279048341  0.0018006595  0.043657715  3.870432e-02
## 2014-09-30 -0.0061673593 -0.0808568044 -0.0395986345 -0.061260388 -1.389195e-02
## 2014-10-31  0.0105850255  0.0140965366 -0.0026547278  0.068874860  2.327790e-02
## 2014-11-28  0.0065483204 -0.0155412572  0.0006253064  0.004773654  2.710144e-02
## 2014-12-31  0.0014753610 -0.0404419698 -0.0407468286  0.025295919 -2.540158e-03
## 2015-01-30  0.0203152712 -0.0068957890  0.0062267332 -0.054628037 -3.007694e-02
## 2015-02-27 -0.0089886786  0.0431360321  0.0614503653  0.056914676  5.468202e-02
## 2015-03-31  0.0037402198 -0.0150861321 -0.0143886327  0.010156329 -1.583028e-02
## 2015-04-30 -0.0032327235  0.0662810586  0.0358163834 -0.018417813  9.785894e-03
## 2015-05-29 -0.0043832535 -0.0419108359  0.0019528441  0.007509996  1.277440e-02
## 2015-06-30 -0.0108260910 -0.0297467851 -0.0316789216  0.004171569 -2.052157e-02
## 2015-07-31  0.0085848134 -0.0651781303  0.0201144071 -0.027375713  2.233811e-02
## 2015-08-31 -0.0033638412 -0.0925124571 -0.0771524204 -0.047268289 -6.288694e-02
## 2015-09-30  0.0080815129 -0.0318247067 -0.0451948454 -0.038464611 -2.584709e-02
## 2015-10-30  0.0006856949  0.0618085753  0.0640260725  0.063589904  8.163529e-02
## 2015-11-30 -0.0038984075 -0.0255608204 -0.0075559234  0.024414950  3.648471e-03
## 2015-12-31 -0.0019189838 -0.0389470340 -0.0235951816 -0.052156828 -1.743388e-02
## 2016-01-29  0.0123298685 -0.0516367176 -0.0567576800 -0.060307169 -5.106853e-02
## 2016-02-29  0.0088315655 -0.0082116560 -0.0339139210  0.020605438 -8.263966e-04
## 2016-03-31  0.0087089830  0.1218790873  0.0637456380  0.089910413  6.510018e-02
## 2016-04-29  0.0025460072  0.0040791682  0.0219750423  0.021044231  3.933542e-03
## 2016-05-31  0.0001355272 -0.0376283820 -0.0008560654  0.004397040  1.686862e-02
## 2016-06-30  0.0191669870  0.0445821813 -0.0244914495  0.008292162  3.469841e-03
## 2016-07-29  0.0054292888  0.0524424066  0.0390003965  0.049348504  3.582202e-02
## 2016-08-31 -0.0021559095  0.0087987130  0.0053266413  0.011261070  1.196687e-03
## 2016-09-30  0.0005156823  0.0248726650  0.0132791638  0.008614803  5.796024e-05
## 2016-10-31 -0.0082049151 -0.0083121444 -0.0224036234 -0.038134900 -1.748925e-02
## 2016-11-30 -0.0259898102 -0.0451617676 -0.0179745854  0.125246386  3.617623e-02
## 2016-12-30  0.0025382059 -0.0025301341  0.0267030347  0.031491736  2.006905e-02
## 2017-01-31  0.0021257104  0.0644314586  0.0323817590 -0.012144064  1.773659e-02
## 2017-02-28  0.0064380329  0.0172576968  0.0118364457  0.013428755  3.853916e-02
## 2017-03-31 -0.0005528189  0.0361891619  0.0318056821 -0.006532923  1.249175e-03
## 2017-04-28  0.0090291870  0.0168662675  0.0239522302  0.005107939  9.877296e-03
## 2017-05-31  0.0068476155  0.0280599708  0.0348101443 -0.022862651  1.401413e-02
## 2017-06-30 -0.0001827819  0.0092238919  0.0029560630  0.029151917  6.354928e-03
## 2017-07-31  0.0033342548  0.0565943473  0.0261877148  0.007481460  2.034584e-02
## 2017-08-31  0.0093696188  0.0232439220 -0.0004481592 -0.027564351  2.913331e-03
## 2017-09-29 -0.0057326956 -0.0004461971  0.0233427901  0.082321270  1.994902e-02
## 2017-10-31  0.0009780402  0.0322784411  0.0166535746  0.005915878  2.329080e-02
## 2017-11-30 -0.0014836139 -0.0038971054  0.0068702098  0.036913503  3.010775e-02
## 2017-12-29  0.0047397901  0.0369256124  0.0133981527 -0.003731344  1.205521e-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(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

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.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")

6 Rolling Component Contribution

Column Chart of Component Contribution and weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
    
    # Transform to long form 
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weights
    add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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)