# 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.0062308443 -0.0029355732  0.0366062100  0.052132980  4.992279e-02
## 2013-02-28  0.0058911577 -0.0231052304 -0.0129693860  0.016175350  1.267829e-02
## 2013-03-28  0.0009851822 -0.0102351239  0.0129693860  0.040258401  3.726803e-02
## 2013-04-30  0.0096389282  0.0120847911  0.0489676317  0.001222365  1.903035e-02
## 2013-05-31 -0.0202141224 -0.0494830211 -0.0306553323  0.041976086  2.333503e-02
## 2013-06-28 -0.0157780844 -0.0547288037 -0.0271445590 -0.001402849 -1.343423e-02
## 2013-07-31  0.0026874686  0.0131598233  0.0518603429  0.063541446  5.038565e-02
## 2013-08-30 -0.0082975347 -0.0257057373 -0.0197466411 -0.034743276 -3.045080e-02
## 2013-09-30  0.0111435965  0.0695889042  0.0753386822  0.063873816  3.115554e-02
## 2013-10-31  0.0082923089  0.0408611832  0.0320817183  0.034233927  4.526658e-02
## 2013-11-29 -0.0025100792 -0.0025941418  0.0054497013  0.041661085  2.920714e-02
## 2013-12-31 -0.0055837464 -0.0040742478  0.0215279768  0.012891800  2.559607e-02
## 2014-01-31  0.0152926458 -0.0903227476 -0.0534132249 -0.035775080 -3.588464e-02
## 2014-02-28  0.0037567551  0.0332204236  0.0595049311  0.045257251  4.451040e-02
## 2014-03-31 -0.0014815359  0.0380221208 -0.0046023347  0.013315513  8.261415e-03
## 2014-04-30  0.0081829203  0.0077726260  0.0165291395 -0.023184437  6.927372e-03
## 2014-05-30  0.0117212292  0.0290913038  0.0158286510  0.006205464  2.294128e-02
## 2014-06-30 -0.0005760335  0.0237339410  0.0091654813  0.037718486  2.043469e-02
## 2014-07-31 -0.0025114694  0.0135554632 -0.0263800046 -0.052009372 -1.352864e-02
## 2014-08-29  0.0114314922  0.0279047890  0.0018004988  0.043657738  3.870473e-02
## 2014-09-30 -0.0061686548 -0.0808567872 -0.0395985696 -0.061260224 -1.389256e-02
## 2014-10-31  0.0105851629  0.0140963391 -0.0026548113  0.068874895  2.327815e-02
## 2014-11-28  0.0065493435 -0.0155410693  0.0006254685  0.004773485  2.710122e-02
## 2014-12-31  0.0014742534 -0.0404424598 -0.0407468895  0.025295757 -2.539661e-03
## 2015-01-30  0.0203155788 -0.0068956776  0.0062265051 -0.054627821 -3.007734e-02
## 2015-02-27 -0.0089883346  0.0431361987  0.0614506173  0.056914714  5.468207e-02
## 2015-03-31  0.0037401294 -0.0150863686 -0.0143887845  0.010156536 -1.583019e-02
## 2015-04-30 -0.0032330927  0.0662815314  0.0358167334 -0.018418042  9.785604e-03
## 2015-05-29 -0.0043833779 -0.0419112429  0.0019525723  0.007509800  1.277439e-02
## 2015-06-30 -0.0108259655 -0.0297463832 -0.0316787473  0.004171596 -2.052118e-02
## 2015-07-31  0.0085847356 -0.0651783133  0.0201143281 -0.027375365  2.233771e-02
## 2015-08-31 -0.0033634784 -0.0925122105 -0.0771523273 -0.047268261 -6.288660e-02
## 2015-09-30  0.0080811643 -0.0318249209 -0.0451949791 -0.038464793 -2.584714e-02
## 2015-10-30  0.0006854062  0.0618082331  0.0640258921  0.063589710  8.163521e-02
## 2015-11-30 -0.0038977771 -0.0255603347 -0.0075557175  0.024415031  3.648019e-03
## 2015-12-31 -0.0019190332 -0.0389471703 -0.0235951109 -0.052156997 -1.743339e-02
## 2016-01-29  0.0123295118 -0.0516368254 -0.0567577314 -0.060306871 -5.106857e-02
## 2016-02-29  0.0088316604 -0.0082114554 -0.0339139897  0.020604956 -8.263938e-04
## 2016-03-31  0.0087093268  0.1218791854  0.0637456882  0.089910827  6.510015e-02
## 2016-04-29  0.0025457170  0.0040791489  0.0219750146  0.021044308  3.933565e-03
## 2016-05-31  0.0001354794 -0.0376287124 -0.0008560461  0.004396965  1.686846e-02
## 2016-06-30  0.0191671219  0.0445825396 -0.0244913662  0.008292103  3.469633e-03
## 2016-07-29  0.0054296699  0.0524420901  0.0390001108  0.049348669  3.582240e-02
## 2016-08-31 -0.0021561973  0.0087984760  0.0053268628  0.011260899  1.196493e-03
## 2016-09-30  0.0005155668  0.0248729368  0.0132790773  0.008614876  5.821667e-05
## 2016-10-31 -0.0082051349 -0.0083121300 -0.0224036687 -0.038135142 -1.748954e-02
## 2016-11-30 -0.0259893724 -0.0451619603 -0.0179744835  0.125246065  3.617647e-02
## 2016-12-30  0.0025375760 -0.0025299463  0.0267027805  0.031492431  2.006901e-02
## 2017-01-31  0.0021265676  0.0644312792  0.0323819616 -0.012144342  1.773638e-02
## 2017-02-28  0.0064376887  0.0172579011  0.0118366296  0.013429304  3.853919e-02
## 2017-03-31 -0.0005523639  0.0361891043  0.0318055557 -0.006533203  1.249307e-03
## 2017-04-28  0.0090289405  0.0168663993  0.0239523650  0.005107588  9.877308e-03
## 2017-05-31  0.0068472470  0.0280599865  0.0348101824 -0.022862564  1.401401e-02
## 2017-06-30 -0.0001823347  0.0092238728  0.0029557828  0.029151750  6.354904e-03
## 2017-07-31  0.0033342327  0.0565943318  0.0261878698  0.007481617  2.034579e-02
## 2017-08-31  0.0093691963  0.0232439732 -0.0004482236 -0.027564422  2.913378e-03
## 2017-09-29 -0.0057323844 -0.0004463953  0.0233427751  0.082321526  1.994928e-02
## 2017-10-31  0.0009780360  0.0322786679  0.0166536632  0.005915744  2.329041e-02
## 2017-11-30 -0.0014837338 -0.0038969517  0.0068701719  0.036913625  3.010820e-02
## 2017-12-29  0.0047397328  0.0369251093  0.0133983343 -0.003731246  1.205506e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398459e-05 0.0001042110 4.178165e-05 -7.811832e-05 -9.031472e-06
## EEM  1.042110e-04 0.0017547138 1.039018e-03  6.437739e-04  6.795430e-04
## EFA  4.178165e-05 0.0010390178 1.064237e-03  6.490305e-04  6.975407e-04
## IJS -7.811832e-05 0.0006437739 6.490305e-04  1.565449e-03  8.290265e-04
## SPY -9.031472e-06 0.0006795430 6.975407e-04  8.290265e-04  7.408291e-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.02347492
# 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.0003874177 0.009257154 0.005815631 0.00568447 0.002330249
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062308443 -0.0029355732  0.0366062100  0.052132980  4.992279e-02
## 2013-02-28  0.0058911577 -0.0231052304 -0.0129693860  0.016175350  1.267829e-02
## 2013-03-28  0.0009851822 -0.0102351239  0.0129693860  0.040258401  3.726803e-02
## 2013-04-30  0.0096389282  0.0120847911  0.0489676317  0.001222365  1.903035e-02
## 2013-05-31 -0.0202141224 -0.0494830211 -0.0306553323  0.041976086  2.333503e-02
## 2013-06-28 -0.0157780844 -0.0547288037 -0.0271445590 -0.001402849 -1.343423e-02
## 2013-07-31  0.0026874686  0.0131598233  0.0518603429  0.063541446  5.038565e-02
## 2013-08-30 -0.0082975347 -0.0257057373 -0.0197466411 -0.034743276 -3.045080e-02
## 2013-09-30  0.0111435965  0.0695889042  0.0753386822  0.063873816  3.115554e-02
## 2013-10-31  0.0082923089  0.0408611832  0.0320817183  0.034233927  4.526658e-02
## 2013-11-29 -0.0025100792 -0.0025941418  0.0054497013  0.041661085  2.920714e-02
## 2013-12-31 -0.0055837464 -0.0040742478  0.0215279768  0.012891800  2.559607e-02
## 2014-01-31  0.0152926458 -0.0903227476 -0.0534132249 -0.035775080 -3.588464e-02
## 2014-02-28  0.0037567551  0.0332204236  0.0595049311  0.045257251  4.451040e-02
## 2014-03-31 -0.0014815359  0.0380221208 -0.0046023347  0.013315513  8.261415e-03
## 2014-04-30  0.0081829203  0.0077726260  0.0165291395 -0.023184437  6.927372e-03
## 2014-05-30  0.0117212292  0.0290913038  0.0158286510  0.006205464  2.294128e-02
## 2014-06-30 -0.0005760335  0.0237339410  0.0091654813  0.037718486  2.043469e-02
## 2014-07-31 -0.0025114694  0.0135554632 -0.0263800046 -0.052009372 -1.352864e-02
## 2014-08-29  0.0114314922  0.0279047890  0.0018004988  0.043657738  3.870473e-02
## 2014-09-30 -0.0061686548 -0.0808567872 -0.0395985696 -0.061260224 -1.389256e-02
## 2014-10-31  0.0105851629  0.0140963391 -0.0026548113  0.068874895  2.327815e-02
## 2014-11-28  0.0065493435 -0.0155410693  0.0006254685  0.004773485  2.710122e-02
## 2014-12-31  0.0014742534 -0.0404424598 -0.0407468895  0.025295757 -2.539661e-03
## 2015-01-30  0.0203155788 -0.0068956776  0.0062265051 -0.054627821 -3.007734e-02
## 2015-02-27 -0.0089883346  0.0431361987  0.0614506173  0.056914714  5.468207e-02
## 2015-03-31  0.0037401294 -0.0150863686 -0.0143887845  0.010156536 -1.583019e-02
## 2015-04-30 -0.0032330927  0.0662815314  0.0358167334 -0.018418042  9.785604e-03
## 2015-05-29 -0.0043833779 -0.0419112429  0.0019525723  0.007509800  1.277439e-02
## 2015-06-30 -0.0108259655 -0.0297463832 -0.0316787473  0.004171596 -2.052118e-02
## 2015-07-31  0.0085847356 -0.0651783133  0.0201143281 -0.027375365  2.233771e-02
## 2015-08-31 -0.0033634784 -0.0925122105 -0.0771523273 -0.047268261 -6.288660e-02
## 2015-09-30  0.0080811643 -0.0318249209 -0.0451949791 -0.038464793 -2.584714e-02
## 2015-10-30  0.0006854062  0.0618082331  0.0640258921  0.063589710  8.163521e-02
## 2015-11-30 -0.0038977771 -0.0255603347 -0.0075557175  0.024415031  3.648019e-03
## 2015-12-31 -0.0019190332 -0.0389471703 -0.0235951109 -0.052156997 -1.743339e-02
## 2016-01-29  0.0123295118 -0.0516368254 -0.0567577314 -0.060306871 -5.106857e-02
## 2016-02-29  0.0088316604 -0.0082114554 -0.0339139897  0.020604956 -8.263938e-04
## 2016-03-31  0.0087093268  0.1218791854  0.0637456882  0.089910827  6.510015e-02
## 2016-04-29  0.0025457170  0.0040791489  0.0219750146  0.021044308  3.933565e-03
## 2016-05-31  0.0001354794 -0.0376287124 -0.0008560461  0.004396965  1.686846e-02
## 2016-06-30  0.0191671219  0.0445825396 -0.0244913662  0.008292103  3.469633e-03
## 2016-07-29  0.0054296699  0.0524420901  0.0390001108  0.049348669  3.582240e-02
## 2016-08-31 -0.0021561973  0.0087984760  0.0053268628  0.011260899  1.196493e-03
## 2016-09-30  0.0005155668  0.0248729368  0.0132790773  0.008614876  5.821667e-05
## 2016-10-31 -0.0082051349 -0.0083121300 -0.0224036687 -0.038135142 -1.748954e-02
## 2016-11-30 -0.0259893724 -0.0451619603 -0.0179744835  0.125246065  3.617647e-02
## 2016-12-30  0.0025375760 -0.0025299463  0.0267027805  0.031492431  2.006901e-02
## 2017-01-31  0.0021265676  0.0644312792  0.0323819616 -0.012144342  1.773638e-02
## 2017-02-28  0.0064376887  0.0172579011  0.0118366296  0.013429304  3.853919e-02
## 2017-03-31 -0.0005523639  0.0361891043  0.0318055557 -0.006533203  1.249307e-03
## 2017-04-28  0.0090289405  0.0168663993  0.0239523650  0.005107588  9.877308e-03
## 2017-05-31  0.0068472470  0.0280599865  0.0348101824 -0.022862564  1.401401e-02
## 2017-06-30 -0.0001823347  0.0092238728  0.0029557828  0.029151750  6.354904e-03
## 2017-07-31  0.0033342327  0.0565943318  0.0261878698  0.007481617  2.034579e-02
## 2017-08-31  0.0093691963  0.0232439732 -0.0004482236 -0.027564422  2.913378e-03
## 2017-09-29 -0.0057323844 -0.0004463953  0.0233427751  0.082321526  1.994928e-02
## 2017-10-31  0.0009780360  0.0322786679  0.0166536632  0.005915744  2.329041e-02
## 2017-11-30 -0.0014837338 -0.0038969517  0.0068701719  0.036913625  3.010820e-02
## 2017-12-29  0.0047397328  0.0369251093  0.0133983343 -0.003731246  1.205506e-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

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 Compont 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)) +
    theme_tq() +
    
    
    labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
         x = NULL)

6 Rolling Component Contribution