# 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.0062318388 -0.0029353531  0.0366062066  0.052133194  4.992336e-02
## 2013-02-28  0.0058914520 -0.0231052330 -0.0129695687  0.016175347  1.267798e-02
## 2013-03-28  0.0009851637 -0.0102350107  0.0129695687  0.040257887  3.726749e-02
## 2013-04-30  0.0096391531  0.0120847911  0.0489674545  0.001222668  1.903072e-02
## 2013-05-31 -0.0202140044 -0.0494833772 -0.0306553350  0.041976377  2.333503e-02
## 2013-06-28 -0.0157784372 -0.0547285103 -0.0271445615 -0.001403043 -1.343423e-02
## 2013-07-31  0.0026870543  0.0131599478  0.0518603476  0.063541349  5.038588e-02
## 2013-08-30 -0.0082973395 -0.0257057991 -0.0197464655 -0.034743276 -3.045114e-02
## 2013-09-30  0.0111435118  0.0695890226  0.0753387580  0.063873462  3.115588e-02
## 2013-10-31  0.0082922221  0.0408614058  0.0320816335  0.034234195  4.526636e-02
## 2013-11-29 -0.0025094722 -0.0025940269  0.0054495424  0.041660925  2.920724e-02
## 2013-12-31 -0.0055837520 -0.0040743604  0.0215281336  0.012892046  2.559627e-02
## 2014-01-31  0.0152919957 -0.0903227778 -0.0534132206 -0.035775331 -3.588484e-02
## 2014-02-28  0.0037567361  0.0332202922  0.0595047722  0.045257503  4.451040e-02
## 2014-03-31 -0.0014815519  0.0380219391 -0.0046024125  0.013315276  8.261611e-03
## 2014-04-30  0.0081833688  0.0077726260  0.0165294466 -0.023184119  6.927566e-03
## 2014-05-30  0.0117218169  0.0290914162  0.0158282739  0.006205222  2.294079e-02
## 2014-06-30 -0.0005756920  0.0237337188  0.0091655576  0.037718725  2.043497e-02
## 2014-07-31 -0.0025126586  0.0135555730 -0.0263800086 -0.052009450 -1.352892e-02
## 2014-08-29  0.0114313154  0.0279046837  0.0018006513  0.043657894  3.870483e-02
## 2014-09-30 -0.0061675106 -0.0808567961 -0.0395984113 -0.061260214 -1.389247e-02
## 2014-10-31  0.0105841970  0.0140965659 -0.0026548903  0.068874574  2.327788e-02
## 2014-11-28  0.0065494174 -0.0155416393  0.0006253098  0.004773949  2.710122e-02
## 2014-12-31  0.0014744702 -0.0404420024 -0.0407469754  0.025295524 -2.539662e-03
## 2015-01-30  0.0203154493 -0.0068954378  0.0062268346 -0.054627737 -3.007726e-02
## 2015-02-27 -0.0089885401  0.0431360737  0.0614504530  0.056914555  5.468199e-02
## 2015-03-31  0.0037402471 -0.0150863669 -0.0143887845  0.010156164 -1.583028e-02
## 2015-04-30 -0.0032329421  0.0662815240  0.0358164310 -0.018417519  9.786039e-03
## 2015-05-29 -0.0043832065 -0.0419112382  0.0019529501  0.007509949  1.277413e-02
## 2015-06-30 -0.0108254481 -0.0297467314 -0.0316789785  0.004171296 -2.052118e-02
## 2015-07-31  0.0085843035 -0.0651779538  0.0201144838 -0.027375519  2.233755e-02
## 2015-08-31 -0.0033634631 -0.0925122670 -0.0771524098 -0.047268188 -6.288643e-02
## 2015-09-30  0.0080810235 -0.0318249895 -0.0451948966 -0.038464796 -2.584714e-02
## 2015-10-30  0.0006855612  0.0618082331  0.0640260539  0.063589951  8.163479e-02
## 2015-11-30 -0.0038982509 -0.0255605396 -0.0075557978  0.024415027  3.648620e-03
## 2015-12-31 -0.0019188056 -0.0389468944 -0.0235954429 -0.052156908 -1.743331e-02
## 2016-01-29  0.0123299162 -0.0516368216 -0.0567576577 -0.060306684 -5.106874e-02
## 2016-02-29  0.0088318385 -0.0082116810 -0.0339139958  0.020604695 -8.263021e-04
## 2016-03-31  0.0087083502  0.1218791360  0.0637459567  0.089910352  6.510015e-02
## 2016-04-29  0.0025465444  0.0040793491  0.0219749289  0.021044615  3.933479e-03
## 2016-05-31  0.0001352282 -0.0376285743 -0.0008561300  0.004397040  1.686855e-02
## 2016-06-30  0.0191666761  0.0445821375 -0.0244914543  0.008292177  3.469549e-03
## 2016-07-29  0.0054295306  0.0524425421  0.0390002001  0.049348449  3.582207e-02
## 2016-08-31 -0.0021557596  0.0087982260  0.0053268633  0.011261109  1.196978e-03
## 2016-09-30  0.0005160566  0.0248731807  0.0132793221  0.008614736  5.773221e-05
## 2016-10-31 -0.0082053127 -0.0083124950 -0.0224039973 -0.038134998 -1.748914e-02
## 2016-11-30 -0.0259896119 -0.0451615214 -0.0179741483  0.125246556  3.617607e-02
## 2016-12-30  0.0025378964 -0.0025300097  0.0267027760  0.031491796  2.006933e-02
## 2017-01-31  0.0021258151  0.0644312672  0.0323817970 -0.012143968  1.773631e-02
## 2017-02-28  0.0064381065  0.0172579571  0.0118365508  0.013428745  3.853949e-02
## 2017-03-31 -0.0005527881  0.0361888681  0.0318056345 -0.006533390  1.248867e-03
## 2017-04-28  0.0090289972  0.0168663993  0.0239522905  0.005108329  9.877456e-03
## 2017-05-31  0.0068477758  0.0280600953  0.0348101850 -0.022862934  1.401416e-02
## 2017-06-30 -0.0001828656  0.0092236560  0.0029558548  0.029151872  6.354690e-03
## 2017-07-31  0.0033343008  0.0565944397  0.0261878698  0.007481555  2.034579e-02
## 2017-08-31  0.0093689136  0.0232438736 -0.0004482236 -0.027564795  2.913656e-03
## 2017-09-29 -0.0057315843 -0.0004461961  0.0233429117  0.082321953  1.994901e-02
## 2017-10-31  0.0009777545  0.0322784718  0.0166535938  0.005915857  2.329068e-02
## 2017-11-30 -0.0014838910 -0.0038969521  0.0068699713  0.036913176  3.010813e-02
## 2017-12-29  0.0047402860  0.0369252995  0.0133983361 -0.003731247  1.205493e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398398e-05 0.0001042076 4.178037e-05 -7.812141e-05 -9.033462e-06
## EEM  1.042076e-04 0.0017547126 1.039018e-03  6.437746e-04  6.795440e-04
## EFA  4.178037e-05 0.0010390184 1.064238e-03  6.490318e-04  6.975416e-04
## IJS -7.812141e-05 0.0006437746 6.490318e-04  1.565448e-03  8.290252e-04
## SPY -9.033462e-06 0.0006795440 6.975416e-04  8.290252e-04  7.408286e-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.0003873959 0.009257152 0.005815638 0.005684469 0.00233025
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.0062318388 -0.0029353531  0.0366062066  0.052133194  4.992336e-02
## 2013-02-28  0.0058914520 -0.0231052330 -0.0129695687  0.016175347  1.267798e-02
## 2013-03-28  0.0009851637 -0.0102350107  0.0129695687  0.040257887  3.726749e-02
## 2013-04-30  0.0096391531  0.0120847911  0.0489674545  0.001222668  1.903072e-02
## 2013-05-31 -0.0202140044 -0.0494833772 -0.0306553350  0.041976377  2.333503e-02
## 2013-06-28 -0.0157784372 -0.0547285103 -0.0271445615 -0.001403043 -1.343423e-02
## 2013-07-31  0.0026870543  0.0131599478  0.0518603476  0.063541349  5.038588e-02
## 2013-08-30 -0.0082973395 -0.0257057991 -0.0197464655 -0.034743276 -3.045114e-02
## 2013-09-30  0.0111435118  0.0695890226  0.0753387580  0.063873462  3.115588e-02
## 2013-10-31  0.0082922221  0.0408614058  0.0320816335  0.034234195  4.526636e-02
## 2013-11-29 -0.0025094722 -0.0025940269  0.0054495424  0.041660925  2.920724e-02
## 2013-12-31 -0.0055837520 -0.0040743604  0.0215281336  0.012892046  2.559627e-02
## 2014-01-31  0.0152919957 -0.0903227778 -0.0534132206 -0.035775331 -3.588484e-02
## 2014-02-28  0.0037567361  0.0332202922  0.0595047722  0.045257503  4.451040e-02
## 2014-03-31 -0.0014815519  0.0380219391 -0.0046024125  0.013315276  8.261611e-03
## 2014-04-30  0.0081833688  0.0077726260  0.0165294466 -0.023184119  6.927566e-03
## 2014-05-30  0.0117218169  0.0290914162  0.0158282739  0.006205222  2.294079e-02
## 2014-06-30 -0.0005756920  0.0237337188  0.0091655576  0.037718725  2.043497e-02
## 2014-07-31 -0.0025126586  0.0135555730 -0.0263800086 -0.052009450 -1.352892e-02
## 2014-08-29  0.0114313154  0.0279046837  0.0018006513  0.043657894  3.870483e-02
## 2014-09-30 -0.0061675106 -0.0808567961 -0.0395984113 -0.061260214 -1.389247e-02
## 2014-10-31  0.0105841970  0.0140965659 -0.0026548903  0.068874574  2.327788e-02
## 2014-11-28  0.0065494174 -0.0155416393  0.0006253098  0.004773949  2.710122e-02
## 2014-12-31  0.0014744702 -0.0404420024 -0.0407469754  0.025295524 -2.539662e-03
## 2015-01-30  0.0203154493 -0.0068954378  0.0062268346 -0.054627737 -3.007726e-02
## 2015-02-27 -0.0089885401  0.0431360737  0.0614504530  0.056914555  5.468199e-02
## 2015-03-31  0.0037402471 -0.0150863669 -0.0143887845  0.010156164 -1.583028e-02
## 2015-04-30 -0.0032329421  0.0662815240  0.0358164310 -0.018417519  9.786039e-03
## 2015-05-29 -0.0043832065 -0.0419112382  0.0019529501  0.007509949  1.277413e-02
## 2015-06-30 -0.0108254481 -0.0297467314 -0.0316789785  0.004171296 -2.052118e-02
## 2015-07-31  0.0085843035 -0.0651779538  0.0201144838 -0.027375519  2.233755e-02
## 2015-08-31 -0.0033634631 -0.0925122670 -0.0771524098 -0.047268188 -6.288643e-02
## 2015-09-30  0.0080810235 -0.0318249895 -0.0451948966 -0.038464796 -2.584714e-02
## 2015-10-30  0.0006855612  0.0618082331  0.0640260539  0.063589951  8.163479e-02
## 2015-11-30 -0.0038982509 -0.0255605396 -0.0075557978  0.024415027  3.648620e-03
## 2015-12-31 -0.0019188056 -0.0389468944 -0.0235954429 -0.052156908 -1.743331e-02
## 2016-01-29  0.0123299162 -0.0516368216 -0.0567576577 -0.060306684 -5.106874e-02
## 2016-02-29  0.0088318385 -0.0082116810 -0.0339139958  0.020604695 -8.263021e-04
## 2016-03-31  0.0087083502  0.1218791360  0.0637459567  0.089910352  6.510015e-02
## 2016-04-29  0.0025465444  0.0040793491  0.0219749289  0.021044615  3.933479e-03
## 2016-05-31  0.0001352282 -0.0376285743 -0.0008561300  0.004397040  1.686855e-02
## 2016-06-30  0.0191666761  0.0445821375 -0.0244914543  0.008292177  3.469549e-03
## 2016-07-29  0.0054295306  0.0524425421  0.0390002001  0.049348449  3.582207e-02
## 2016-08-31 -0.0021557596  0.0087982260  0.0053268633  0.011261109  1.196978e-03
## 2016-09-30  0.0005160566  0.0248731807  0.0132793221  0.008614736  5.773221e-05
## 2016-10-31 -0.0082053127 -0.0083124950 -0.0224039973 -0.038134998 -1.748914e-02
## 2016-11-30 -0.0259896119 -0.0451615214 -0.0179741483  0.125246556  3.617607e-02
## 2016-12-30  0.0025378964 -0.0025300097  0.0267027760  0.031491796  2.006933e-02
## 2017-01-31  0.0021258151  0.0644312672  0.0323817970 -0.012143968  1.773631e-02
## 2017-02-28  0.0064381065  0.0172579571  0.0118365508  0.013428745  3.853949e-02
## 2017-03-31 -0.0005527881  0.0361888681  0.0318056345 -0.006533390  1.248867e-03
## 2017-04-28  0.0090289972  0.0168663993  0.0239522905  0.005108329  9.877456e-03
## 2017-05-31  0.0068477758  0.0280600953  0.0348101850 -0.022862934  1.401416e-02
## 2017-06-30 -0.0001828656  0.0092236560  0.0029558548  0.029151872  6.354690e-03
## 2017-07-31  0.0033343008  0.0565944397  0.0261878698  0.007481555  2.034579e-02
## 2017-08-31  0.0093689136  0.0232438736 -0.0004482236 -0.027564795  2.913656e-03
## 2017-09-29 -0.0057315843 -0.0004461961  0.0233429117  0.082321953  1.994901e-02
## 2017-10-31  0.0009777545  0.0322784718  0.0166535938  0.005915857  2.329068e-02
## 2017-11-30 -0.0014838910 -0.0038969521  0.0068699713  0.036913176  3.010813e-02
## 2017-12-29  0.0047402860  0.0369252995  0.0133983361 -0.003731247  1.205493e-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)) +
    theme_tq() +
    
    
    labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
         x = NULL)

6 Rolling Component Contribution