# 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.0062317008 -0.0029357537  0.0366059211  0.052133335  0.0499231865
## 2013-02-28  0.0058912842 -0.0231050963 -0.0129693926  0.016175435  0.0126778945
## 2013-03-28  0.0009855158 -0.0102353256  0.0129693926  0.040258216  0.0372683634
## 2013-04-30  0.0096387940  0.0120851121  0.0489677970  0.001222411  0.0190297319
## 2013-05-31 -0.0202141718 -0.0494835811 -0.0306555884  0.041976334  0.0233350220
## 2013-06-28 -0.0157782207 -0.0547283028 -0.0271442170 -0.001402677 -0.0134338570
## 2013-07-31  0.0026881639  0.0131598215  0.0518604055  0.063541075  0.0503860079
## 2013-08-30 -0.0082983238 -0.0257054510 -0.0197463255 -0.034743744 -0.0304513126
## 2013-09-30  0.0111437810  0.0695883609  0.0753380877  0.063874133  0.0311559258
## 2013-10-31  0.0082921133  0.0408615363  0.0320820830  0.034233631  0.0452670094
## 2013-11-29 -0.0025097610 -0.0025938791  0.0054496320  0.041661322  0.0292063626
## 2013-12-31 -0.0055830842 -0.0040745248  0.0215279720  0.012892030  0.0255964688
## 2014-01-31  0.0152912184 -0.0903226276 -0.0534130868 -0.035775253 -0.0358847509
## 2014-02-28  0.0037570789  0.0332205491  0.0595047418  0.045257557  0.0445103724
## 2014-03-31 -0.0014818718  0.0380215912 -0.0046025710  0.013314934  0.0082614055
## 2014-04-30  0.0081842927  0.0077728529  0.0165294481 -0.023184021  0.0069271684
## 2014-05-30  0.0117208432  0.0290912214  0.0158284562  0.006205424  0.0229412801
## 2014-06-30 -0.0005754331  0.0237337969  0.0091652344  0.037718481  0.0204348367
## 2014-07-31 -0.0025124721  0.0135557649 -0.0263797155 -0.052009334 -0.0135287613
## 2014-08-29  0.0114311509  0.0279043666  0.0018006138  0.043657791  0.0387047155
## 2014-09-30 -0.0061673653 -0.0808565903 -0.0395984327 -0.061260661 -0.0138924520
## 2014-10-31  0.0105839626  0.0140966517 -0.0026549203  0.068875145  0.0232779274
## 2014-11-28  0.0065493297 -0.0155415851  0.0006253215  0.004773640  0.0271012992
## 2014-12-31  0.0014753240 -0.0404419649 -0.0407467581  0.025295983 -0.0025396013
## 2015-01-30  0.0203154014 -0.0068957710  0.0062264480 -0.054628011 -0.0300769057
## 2015-02-27 -0.0089887814  0.0431362568  0.0614504602  0.056914792  0.0546818428
## 2015-03-31  0.0037403616 -0.0150862510 -0.0143886515  0.010156170 -0.0158302752
## 2015-04-30 -0.0032324671  0.0662809891  0.0358164943 -0.018417746  0.0097859548
## 2015-05-29 -0.0043843764 -0.0419107383  0.0019529490  0.007509870  0.0127737489
## 2015-06-30 -0.0108252342 -0.0297465664 -0.0316789350  0.004171367 -0.0205208570
## 2015-07-31  0.0085843746 -0.0651782877  0.0201145423 -0.027375662  0.0223377850
## 2015-08-31 -0.0033638907 -0.0925123597 -0.0771525139 -0.047268066 -0.0628866670
## 2015-09-30  0.0080820914 -0.0318249529 -0.0451949944 -0.038464679 -0.0258470529
## 2015-10-30  0.0006854386  0.0618083585  0.0640258643  0.063589635  0.0816348625
## 2015-11-30 -0.0038987221 -0.0255603113 -0.0075559226  0.024415100  0.0036487034
## 2015-12-31 -0.0019186599 -0.0389473767 -0.0235948315 -0.052156962 -0.0174339290
## 2016-01-29  0.0123302562 -0.0516365444 -0.0567576579 -0.060306768 -0.0510685309
## 2016-02-29  0.0088311182 -0.0082115773 -0.0339139719  0.020605691 -0.0008261623
## 2016-03-31  0.0087088638  0.1218790627  0.0637455480  0.089909928  0.0651000464
## 2016-04-29  0.0025463514  0.0040790657  0.0219751197  0.021044303  0.0039337885
## 2016-05-31  0.0001357971 -0.0376284815 -0.0008561249  0.004397079  0.0168683135
## 2016-06-30  0.0191668575  0.0445824101 -0.0244914410  0.008292239  0.0034698388
## 2016-07-29  0.0054290543  0.0524424123  0.0390003784  0.049348361  0.0358217420
## 2016-08-31 -0.0021561726  0.0087985223  0.0053265698  0.011260897  0.0011969104
## 2016-09-30  0.0005156878  0.0248727649  0.0132790987  0.008614709  0.0000578254
## 2016-10-31 -0.0082048206 -0.0083121619 -0.0224033586 -0.038134854 -0.0174887522
## 2016-11-30 -0.0259895176 -0.0451620175 -0.0179746733  0.125246458  0.0361759809
## 2016-12-30  0.0025376211 -0.0025300308  0.0267031586  0.031492051  0.0200690203
## 2017-01-31  0.0021261909  0.0644315962  0.0323817459 -0.012144057  0.0177364427
## 2017-02-28  0.0064376279  0.0172579235  0.0118364650  0.013428509  0.0385392497
## 2017-03-31 -0.0005525875  0.0361889046  0.0318054842 -0.006532792  0.0012493857
## 2017-04-28  0.0090295780  0.0168661879  0.0239522182  0.005107696  0.0098769617
## 2017-05-31  0.0068470322  0.0280599242  0.0348103046 -0.022862459  0.0140143968
## 2017-06-30 -0.0001826743  0.0092241272  0.0029559671  0.029151588  0.0063546427
## 2017-07-31  0.0033345456  0.0565942050  0.0261878278  0.007481285  0.0203456958
## 2017-08-31  0.0093693043  0.0232437522 -0.0004482007 -0.027564577  0.0029136662
## 2017-09-29 -0.0057328984 -0.0004460301  0.0233426645  0.082321746  0.0199489836
## 2017-10-31  0.0009786895  0.0322783727  0.0166535275  0.005916345  0.0232908767
## 2017-11-30 -0.0014839597 -0.0038969326  0.0068702324  0.036912892  0.0301078810
## 2017-12-29  0.0047400523  0.0369255364  0.0133983610 -0.003730606  0.0120551373
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398420e-05 0.0001042102 4.178148e-05 -0.0000781228 -9.032467e-06
## EEM  1.042102e-04 0.0017547092 1.039014e-03  0.0006437722  6.795429e-04
## EFA  4.178148e-05 0.0010390144 1.064234e-03  0.0006490267  6.975392e-04
## IJS -7.812280e-05 0.0006437722 6.490267e-04  0.0015654486  8.290233e-04
## SPY -9.032467e-06 0.0006795429 6.975392e-04  0.0008290233  7.408273e-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.02347488
# 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.0003874045 0.009257146 0.005815622 0.005684458 0.002330248
rowSums(component_contribution)
## [1] 0.02347488
# 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 Componibution 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.0062317008 -0.0029357537  0.0366059211  0.052133335  0.0499231865
## 2013-02-28  0.0058912842 -0.0231050963 -0.0129693926  0.016175435  0.0126778945
## 2013-03-28  0.0009855158 -0.0102353256  0.0129693926  0.040258216  0.0372683634
## 2013-04-30  0.0096387940  0.0120851121  0.0489677970  0.001222411  0.0190297319
## 2013-05-31 -0.0202141718 -0.0494835811 -0.0306555884  0.041976334  0.0233350220
## 2013-06-28 -0.0157782207 -0.0547283028 -0.0271442170 -0.001402677 -0.0134338570
## 2013-07-31  0.0026881639  0.0131598215  0.0518604055  0.063541075  0.0503860079
## 2013-08-30 -0.0082983238 -0.0257054510 -0.0197463255 -0.034743744 -0.0304513126
## 2013-09-30  0.0111437810  0.0695883609  0.0753380877  0.063874133  0.0311559258
## 2013-10-31  0.0082921133  0.0408615363  0.0320820830  0.034233631  0.0452670094
## 2013-11-29 -0.0025097610 -0.0025938791  0.0054496320  0.041661322  0.0292063626
## 2013-12-31 -0.0055830842 -0.0040745248  0.0215279720  0.012892030  0.0255964688
## 2014-01-31  0.0152912184 -0.0903226276 -0.0534130868 -0.035775253 -0.0358847509
## 2014-02-28  0.0037570789  0.0332205491  0.0595047418  0.045257557  0.0445103724
## 2014-03-31 -0.0014818718  0.0380215912 -0.0046025710  0.013314934  0.0082614055
## 2014-04-30  0.0081842927  0.0077728529  0.0165294481 -0.023184021  0.0069271684
## 2014-05-30  0.0117208432  0.0290912214  0.0158284562  0.006205424  0.0229412801
## 2014-06-30 -0.0005754331  0.0237337969  0.0091652344  0.037718481  0.0204348367
## 2014-07-31 -0.0025124721  0.0135557649 -0.0263797155 -0.052009334 -0.0135287613
## 2014-08-29  0.0114311509  0.0279043666  0.0018006138  0.043657791  0.0387047155
## 2014-09-30 -0.0061673653 -0.0808565903 -0.0395984327 -0.061260661 -0.0138924520
## 2014-10-31  0.0105839626  0.0140966517 -0.0026549203  0.068875145  0.0232779274
## 2014-11-28  0.0065493297 -0.0155415851  0.0006253215  0.004773640  0.0271012992
## 2014-12-31  0.0014753240 -0.0404419649 -0.0407467581  0.025295983 -0.0025396013
## 2015-01-30  0.0203154014 -0.0068957710  0.0062264480 -0.054628011 -0.0300769057
## 2015-02-27 -0.0089887814  0.0431362568  0.0614504602  0.056914792  0.0546818428
## 2015-03-31  0.0037403616 -0.0150862510 -0.0143886515  0.010156170 -0.0158302752
## 2015-04-30 -0.0032324671  0.0662809891  0.0358164943 -0.018417746  0.0097859548
## 2015-05-29 -0.0043843764 -0.0419107383  0.0019529490  0.007509870  0.0127737489
## 2015-06-30 -0.0108252342 -0.0297465664 -0.0316789350  0.004171367 -0.0205208570
## 2015-07-31  0.0085843746 -0.0651782877  0.0201145423 -0.027375662  0.0223377850
## 2015-08-31 -0.0033638907 -0.0925123597 -0.0771525139 -0.047268066 -0.0628866670
## 2015-09-30  0.0080820914 -0.0318249529 -0.0451949944 -0.038464679 -0.0258470529
## 2015-10-30  0.0006854386  0.0618083585  0.0640258643  0.063589635  0.0816348625
## 2015-11-30 -0.0038987221 -0.0255603113 -0.0075559226  0.024415100  0.0036487034
## 2015-12-31 -0.0019186599 -0.0389473767 -0.0235948315 -0.052156962 -0.0174339290
## 2016-01-29  0.0123302562 -0.0516365444 -0.0567576579 -0.060306768 -0.0510685309
## 2016-02-29  0.0088311182 -0.0082115773 -0.0339139719  0.020605691 -0.0008261623
## 2016-03-31  0.0087088638  0.1218790627  0.0637455480  0.089909928  0.0651000464
## 2016-04-29  0.0025463514  0.0040790657  0.0219751197  0.021044303  0.0039337885
## 2016-05-31  0.0001357971 -0.0376284815 -0.0008561249  0.004397079  0.0168683135
## 2016-06-30  0.0191668575  0.0445824101 -0.0244914410  0.008292239  0.0034698388
## 2016-07-29  0.0054290543  0.0524424123  0.0390003784  0.049348361  0.0358217420
## 2016-08-31 -0.0021561726  0.0087985223  0.0053265698  0.011260897  0.0011969104
## 2016-09-30  0.0005156878  0.0248727649  0.0132790987  0.008614709  0.0000578254
## 2016-10-31 -0.0082048206 -0.0083121619 -0.0224033586 -0.038134854 -0.0174887522
## 2016-11-30 -0.0259895176 -0.0451620175 -0.0179746733  0.125246458  0.0361759809
## 2016-12-30  0.0025376211 -0.0025300308  0.0267031586  0.031492051  0.0200690203
## 2017-01-31  0.0021261909  0.0644315962  0.0323817459 -0.012144057  0.0177364427
## 2017-02-28  0.0064376279  0.0172579235  0.0118364650  0.013428509  0.0385392497
## 2017-03-31 -0.0005525875  0.0361889046  0.0318054842 -0.006532792  0.0012493857
## 2017-04-28  0.0090295780  0.0168661879  0.0239522182  0.005107696  0.0098769617
## 2017-05-31  0.0068470322  0.0280599242  0.0348103046 -0.022862459  0.0140143968
## 2017-06-30 -0.0001826743  0.0092241272  0.0029559671  0.029151588  0.0063546427
## 2017-07-31  0.0033345456  0.0565942050  0.0261878278  0.007481285  0.0203456958
## 2017-08-31  0.0093693043  0.0232437522 -0.0004482007 -0.027564577  0.0029136662
## 2017-09-29 -0.0057328984 -0.0004460301  0.0233426645  0.082321746  0.0199489836
## 2017-10-31  0.0009786895  0.0322783727  0.0166535275  0.005916345  0.0232908767
## 2017-11-30 -0.0014839597 -0.0038969326  0.0068702324  0.036912892  0.0301078810
## 2017-12-29  0.0047400523  0.0369255364  0.0133983610 -0.003730606  0.0120551373
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, .1))
## # A tibble: 1 × 5
##     AGG   EEM   EFA   IJS   SPY
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     0     0     0 0.432 0.568

5 Visualizing Component

column chart of conponent contribution

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

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")