# 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.0062313041 -0.0029353124  0.0366064355  0.052133422  4.992325e-02
## 2013-02-28  0.0058916191 -0.0231052744 -0.0129696171  0.016174972  1.267811e-02
## 2013-03-28  0.0009850262 -0.0102348677  0.0129696171  0.040258218  3.726811e-02
## 2013-04-30  0.0096386457  0.0120844530  0.0489676327  0.001222605  1.903027e-02
## 2013-05-31 -0.0202138402 -0.0494833338 -0.0306556296  0.041976445  2.333494e-02
## 2013-06-28 -0.0157781851 -0.0547283691 -0.0271443704 -0.001402900 -1.343421e-02
## 2013-07-31  0.0026879943  0.0131598886  0.0518602672  0.063541159  5.038586e-02
## 2013-08-30 -0.0082986740 -0.0257056700 -0.0197461504 -0.034743610 -3.045118e-02
## 2013-09-30  0.0111439959  0.0695887817  0.0753385205  0.063873724  3.115602e-02
## 2013-10-31  0.0082919544  0.0408615063  0.0320816411  0.034234144  4.526617e-02
## 2013-11-29 -0.0025094516 -0.0025941817  0.0054497373  0.041661091  2.920695e-02
## 2013-12-31 -0.0055836107 -0.0040742756  0.0215279437  0.012892367  2.559664e-02
## 2014-01-31  0.0152919283 -0.0903227151 -0.0534132614 -0.035775541 -3.588448e-02
## 2014-02-28  0.0037570294  0.0332207522  0.0595051336  0.045257351  4.450999e-02
## 2014-03-31 -0.0014811583  0.0380214150 -0.0046027887  0.013315406  8.261468e-03
## 2014-04-30  0.0081825347  0.0077728543  0.0165296677 -0.023184269  6.927308e-03
## 2014-05-30  0.0117217996  0.0290911140  0.0158281734  0.006205324  2.294128e-02
## 2014-06-30 -0.0005753527  0.0237339392  0.0091655182  0.037718692  2.043460e-02
## 2014-07-31 -0.0025124933  0.0135557339 -0.0263798058 -0.052009333 -1.352867e-02
## 2014-08-29  0.0114310783  0.0279045096  0.0018003562  0.043657791  3.870502e-02
## 2014-09-30 -0.0061671012 -0.0808568044 -0.0395984830 -0.061260462 -1.389265e-02
## 2014-10-31  0.0105840938  0.0140966491 -0.0026548068  0.068874697  2.327807e-02
## 2014-11-28  0.0065487385 -0.0155413696  0.0006253064  0.004773576  2.710136e-02
## 2014-12-31  0.0014748581 -0.0404421779 -0.0407465234  0.025296133 -2.539648e-03
## 2015-01-30  0.0203156117 -0.0068955808  0.0062264252 -0.054627933 -3.007745e-02
## 2015-02-27 -0.0089882687  0.0431358027  0.0614505048  0.056914515  5.468210e-02
## 2015-03-31  0.0037402186 -0.0150860192 -0.0143889049  0.010156385 -1.583002e-02
## 2015-04-30 -0.0032330465  0.0662811751  0.0358167484 -0.018417793  9.785643e-03
## 2015-05-29 -0.0043837525 -0.0419108359  0.0019526185  0.007510148  1.277431e-02
## 2015-06-30 -0.0108256687 -0.0297466973 -0.0316789046  0.004171037 -2.052132e-02
## 2015-07-31  0.0085849663 -0.0651782181  0.0201145412 -0.027375097  2.233769e-02
## 2015-08-31 -0.0033636664 -0.0925122172 -0.0771524349 -0.047268601 -6.288677e-02
## 2015-09-30  0.0080813377 -0.0318250881 -0.0451948211 -0.038464611 -2.584691e-02
## 2015-10-30  0.0006850351  0.0618083177  0.0640259263  0.063589745  8.163502e-02
## 2015-11-30 -0.0038976607 -0.0255602848 -0.0075558630  0.024415187  3.648553e-03
## 2015-12-31 -0.0019196470 -0.0389472414 -0.0235950985 -0.052157049 -1.743379e-02
## 2016-01-29  0.0123306159 -0.0516366466 -0.0567579170 -0.060306590 -5.106862e-02
## 2016-02-29  0.0088316485 -0.0082115806 -0.0339138580  0.020605088 -8.262157e-04
## 2016-03-31  0.0087084100  0.1218790120  0.0637457289  0.089910327  6.510017e-02
## 2016-04-29  0.0025462500  0.0040792678  0.0219752093  0.021044021  3.933459e-03
## 2016-05-31  0.0001358536 -0.0376283437 -0.0008561489  0.004397250  1.686853e-02
## 2016-06-30  0.0191664230  0.0445823071 -0.0244915330  0.008292369  3.469675e-03
## 2016-07-29  0.0054298346  0.0524421428  0.0390002318  0.049348297  3.582203e-02
## 2016-08-31 -0.0021563825  0.0087985889  0.0053268061  0.011261070  1.196923e-03
## 2016-09-30  0.0005158470  0.0248729101  0.0132791638  0.008614610  5.804248e-05
## 2016-10-31 -0.0082049878 -0.0083123874 -0.0224036854 -0.038134779 -1.748917e-02
## 2016-11-30 -0.0259898122 -0.0451619009 -0.0179742708  0.125246347  3.617599e-02
## 2016-12-30  0.0025382804 -0.0025299428  0.0267027002  0.031492083  2.006913e-02
## 2017-01-31  0.0021261131  0.0644315225  0.0323818409 -0.012143809  1.773644e-02
## 2017-02-28  0.0064375459  0.0172578147  0.0118365830  0.013428265  3.853916e-02
## 2017-03-31 -0.0005525661  0.0361891292  0.0318055449 -0.006532986  1.249175e-03
## 2017-04-28  0.0090290915  0.0168662940  0.0239523044  0.005108111  9.877084e-03
## 2017-05-31  0.0068473869  0.0280597502  0.0348101417 -0.022862888  1.401469e-02
## 2017-06-30 -0.0001823048  0.0092240007  0.0029559199  0.029151812  6.354578e-03
## 2017-07-31  0.0033341710  0.0565945255  0.0261880645  0.007481585  2.034578e-02
## 2017-08-31  0.0093689202  0.0232436443 -0.0004484375 -0.027564845  2.913398e-03
## 2017-09-29 -0.0057319979 -0.0004462966  0.0233427901  0.082321756  1.994922e-02
## 2017-10-31  0.0009776490  0.0322787125  0.0166538254  0.005916111  2.329059e-02
## 2017-11-30 -0.0014836245 -0.0038970809  0.0068698428  0.036913387  3.010819e-02
## 2017-12-29  0.0047399550  0.0369253525  0.0133984000 -0.003731344  1.205496e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398397e-05 0.0001042065 4.178074e-05 -7.811971e-05 -9.032440e-06
## EEM  1.042065e-04 0.0017547099 1.039017e-03  6.437738e-04  6.795434e-04
## EFA  4.178074e-05 0.0010390169 1.064237e-03  6.490305e-04  6.975412e-04
## IJS -7.811971e-05 0.0006437738 6.490305e-04  1.565449e-03  8.290263e-04
## SPY -9.032440e-06 0.0006795434 6.975412e-04  8.290263e-04  7.408295e-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.0003873988 0.009257139 0.005815634 0.005684473 0.002330252
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

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.0062313041 -0.0029353124  0.0366064355  0.052133422  4.992325e-02
## 2013-02-28  0.0058916191 -0.0231052744 -0.0129696171  0.016174972  1.267811e-02
## 2013-03-28  0.0009850262 -0.0102348677  0.0129696171  0.040258218  3.726811e-02
## 2013-04-30  0.0096386457  0.0120844530  0.0489676327  0.001222605  1.903027e-02
## 2013-05-31 -0.0202138402 -0.0494833338 -0.0306556296  0.041976445  2.333494e-02
## 2013-06-28 -0.0157781851 -0.0547283691 -0.0271443704 -0.001402900 -1.343421e-02
## 2013-07-31  0.0026879943  0.0131598886  0.0518602672  0.063541159  5.038586e-02
## 2013-08-30 -0.0082986740 -0.0257056700 -0.0197461504 -0.034743610 -3.045118e-02
## 2013-09-30  0.0111439959  0.0695887817  0.0753385205  0.063873724  3.115602e-02
## 2013-10-31  0.0082919544  0.0408615063  0.0320816411  0.034234144  4.526617e-02
## 2013-11-29 -0.0025094516 -0.0025941817  0.0054497373  0.041661091  2.920695e-02
## 2013-12-31 -0.0055836107 -0.0040742756  0.0215279437  0.012892367  2.559664e-02
## 2014-01-31  0.0152919283 -0.0903227151 -0.0534132614 -0.035775541 -3.588448e-02
## 2014-02-28  0.0037570294  0.0332207522  0.0595051336  0.045257351  4.450999e-02
## 2014-03-31 -0.0014811583  0.0380214150 -0.0046027887  0.013315406  8.261468e-03
## 2014-04-30  0.0081825347  0.0077728543  0.0165296677 -0.023184269  6.927308e-03
## 2014-05-30  0.0117217996  0.0290911140  0.0158281734  0.006205324  2.294128e-02
## 2014-06-30 -0.0005753527  0.0237339392  0.0091655182  0.037718692  2.043460e-02
## 2014-07-31 -0.0025124933  0.0135557339 -0.0263798058 -0.052009333 -1.352867e-02
## 2014-08-29  0.0114310783  0.0279045096  0.0018003562  0.043657791  3.870502e-02
## 2014-09-30 -0.0061671012 -0.0808568044 -0.0395984830 -0.061260462 -1.389265e-02
## 2014-10-31  0.0105840938  0.0140966491 -0.0026548068  0.068874697  2.327807e-02
## 2014-11-28  0.0065487385 -0.0155413696  0.0006253064  0.004773576  2.710136e-02
## 2014-12-31  0.0014748581 -0.0404421779 -0.0407465234  0.025296133 -2.539648e-03
## 2015-01-30  0.0203156117 -0.0068955808  0.0062264252 -0.054627933 -3.007745e-02
## 2015-02-27 -0.0089882687  0.0431358027  0.0614505048  0.056914515  5.468210e-02
## 2015-03-31  0.0037402186 -0.0150860192 -0.0143889049  0.010156385 -1.583002e-02
## 2015-04-30 -0.0032330465  0.0662811751  0.0358167484 -0.018417793  9.785643e-03
## 2015-05-29 -0.0043837525 -0.0419108359  0.0019526185  0.007510148  1.277431e-02
## 2015-06-30 -0.0108256687 -0.0297466973 -0.0316789046  0.004171037 -2.052132e-02
## 2015-07-31  0.0085849663 -0.0651782181  0.0201145412 -0.027375097  2.233769e-02
## 2015-08-31 -0.0033636664 -0.0925122172 -0.0771524349 -0.047268601 -6.288677e-02
## 2015-09-30  0.0080813377 -0.0318250881 -0.0451948211 -0.038464611 -2.584691e-02
## 2015-10-30  0.0006850351  0.0618083177  0.0640259263  0.063589745  8.163502e-02
## 2015-11-30 -0.0038976607 -0.0255602848 -0.0075558630  0.024415187  3.648553e-03
## 2015-12-31 -0.0019196470 -0.0389472414 -0.0235950985 -0.052157049 -1.743379e-02
## 2016-01-29  0.0123306159 -0.0516366466 -0.0567579170 -0.060306590 -5.106862e-02
## 2016-02-29  0.0088316485 -0.0082115806 -0.0339138580  0.020605088 -8.262157e-04
## 2016-03-31  0.0087084100  0.1218790120  0.0637457289  0.089910327  6.510017e-02
## 2016-04-29  0.0025462500  0.0040792678  0.0219752093  0.021044021  3.933459e-03
## 2016-05-31  0.0001358536 -0.0376283437 -0.0008561489  0.004397250  1.686853e-02
## 2016-06-30  0.0191664230  0.0445823071 -0.0244915330  0.008292369  3.469675e-03
## 2016-07-29  0.0054298346  0.0524421428  0.0390002318  0.049348297  3.582203e-02
## 2016-08-31 -0.0021563825  0.0087985889  0.0053268061  0.011261070  1.196923e-03
## 2016-09-30  0.0005158470  0.0248729101  0.0132791638  0.008614610  5.804248e-05
## 2016-10-31 -0.0082049878 -0.0083123874 -0.0224036854 -0.038134779 -1.748917e-02
## 2016-11-30 -0.0259898122 -0.0451619009 -0.0179742708  0.125246347  3.617599e-02
## 2016-12-30  0.0025382804 -0.0025299428  0.0267027002  0.031492083  2.006913e-02
## 2017-01-31  0.0021261131  0.0644315225  0.0323818409 -0.012143809  1.773644e-02
## 2017-02-28  0.0064375459  0.0172578147  0.0118365830  0.013428265  3.853916e-02
## 2017-03-31 -0.0005525661  0.0361891292  0.0318055449 -0.006532986  1.249175e-03
## 2017-04-28  0.0090290915  0.0168662940  0.0239523044  0.005108111  9.877084e-03
## 2017-05-31  0.0068473869  0.0280597502  0.0348101417 -0.022862888  1.401469e-02
## 2017-06-30 -0.0001823048  0.0092240007  0.0029559199  0.029151812  6.354578e-03
## 2017-07-31  0.0033341710  0.0565945255  0.0261880645  0.007481585  2.034578e-02
## 2017-08-31  0.0093689202  0.0232436443 -0.0004484375 -0.027564845  2.913398e-03
## 2017-09-29 -0.0057319979 -0.0004462966  0.0233427901  0.082321756  1.994922e-02
## 2017-10-31  0.0009776490  0.0322787125  0.0166538254  0.005916111  2.329059e-02
## 2017-11-30 -0.0014836245 -0.0038970809  0.0068698428  0.036913387  3.010819e-02
## 2017-12-29  0.0047399550  0.0369253525  0.0133984000 -0.003731344  1.205496e-02
calculate_component_contribution <- function (.data, w) {
        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()
    
    (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

plot_data <- asset_returns_wide_tbl %>% 
    
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform to long form
    pivot_longer(col = 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(col = 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)