# 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.0062314271 -0.0029357363  0.0366062698  0.052132838  4.992335e-02
## 2013-02-28  0.0058913338 -0.0231054028 -0.0129693233  0.016175802  1.267813e-02
## 2013-03-28  0.0009848806 -0.0102351153  0.0129693233  0.040257735  3.726836e-02
## 2013-04-30  0.0096394934  0.0120851159  0.0489677817  0.001222760  1.902973e-02
## 2013-05-31 -0.0202143674 -0.0494835442 -0.0306554809  0.041976061  2.333529e-02
## 2013-06-28 -0.0157778946 -0.0547282002 -0.0271446607 -0.001402819 -1.343432e-02
## 2013-07-31  0.0026877244  0.0131594971  0.0518604624  0.063541482  5.038548e-02
## 2013-08-30 -0.0082989512 -0.0257053987 -0.0197464753 -0.034743814 -3.045078e-02
## 2013-09-30  0.0111443656  0.0695888168  0.0753387601  0.063873785  3.115583e-02
## 2013-10-31  0.0082919206  0.0408611647  0.0320813863  0.034233830  4.526677e-02
## 2013-11-29 -0.0025095174 -0.0025943047  0.0054500752  0.041661450  2.920686e-02
## 2013-12-31 -0.0055834401 -0.0040739826  0.0215278531  0.012892061  2.559650e-02
## 2014-01-31  0.0152917970 -0.0903225477 -0.0534133611 -0.035775316 -3.588493e-02
## 2014-02-28  0.0037569362  0.0332203667  0.0595050562  0.045257731  4.451031e-02
## 2014-03-31 -0.0014816759  0.0380217431 -0.0046024868  0.013315314  8.261015e-03
## 2014-04-30  0.0081831116  0.0077729133  0.0165293619 -0.023184519  6.927853e-03
## 2014-05-30  0.0117214864  0.0290910706  0.0158282955  0.006205219  2.294108e-02
## 2014-06-30 -0.0005752144  0.0237338416  0.0091656393  0.037718727  2.043464e-02
## 2014-07-31 -0.0025123425  0.0135555467 -0.0263798763 -0.052009545 -1.352887e-02
## 2014-08-29  0.0114312865  0.0279047091  0.0018004559  0.043658052  3.870509e-02
## 2014-09-30 -0.0061678487 -0.0808567758 -0.0395985939 -0.061260317 -1.389244e-02
## 2014-10-31  0.0105846879  0.0140966541 -0.0026546540  0.068874777  2.327789e-02
## 2014-11-28  0.0065488393 -0.0155415506  0.0006251561  0.004773465  2.710113e-02
## 2014-12-31  0.0014749572 -0.0404419772 -0.0407466701  0.025295825 -2.539677e-03
## 2015-01-30  0.0203151042 -0.0068957476  0.0062264210 -0.054627985 -3.007695e-02
## 2015-02-27 -0.0089880982  0.0431362506  0.0614506850  0.056914738  5.468186e-02
## 2015-03-31  0.0037405803 -0.0150863546 -0.0143888476  0.010156341 -1.583030e-02
## 2015-04-30 -0.0032333965  0.0662814792  0.0358165769 -0.018417754  9.785811e-03
## 2015-05-29 -0.0043835033 -0.0419112365  0.0019527483  0.007510086  1.277433e-02
## 2015-06-30 -0.0108260533 -0.0297465425 -0.0316788808  0.004171318 -2.052134e-02
## 2015-07-31  0.0085852344 -0.0651782975  0.0201143017 -0.027375530  2.233806e-02
## 2015-08-31 -0.0033638703 -0.0925121937 -0.0771520601 -0.047268398 -6.288685e-02
## 2015-09-30  0.0080815989 -0.0318251469 -0.0451949450 -0.038464594 -2.584693e-02
## 2015-10-30  0.0006852516  0.0618083483  0.0640257777  0.063589589  8.163478e-02
## 2015-11-30 -0.0038983764 -0.0255603285 -0.0075558606  0.024415195  3.648837e-03
## 2015-12-31 -0.0019183267 -0.0389471530 -0.0235950350 -0.052156897 -1.743396e-02
## 2016-01-29  0.0123298055 -0.0516365338 -0.0567578359 -0.060307011 -5.106853e-02
## 2016-02-29  0.0088308511 -0.0082118433 -0.0339138922  0.020605286 -8.262749e-04
## 2016-03-31  0.0087094672  0.1218792297  0.0637455915  0.089910599  6.510009e-02
## 2016-04-29  0.0025459487  0.0040790876  0.0219752075  0.021044055  3.933529e-03
## 2016-05-31  0.0001352822 -0.0376283248 -0.0008561427  0.004397327  1.686864e-02
## 2016-06-30  0.0191669731  0.0445821604 -0.0244913881  0.008292171  3.470050e-03
## 2016-07-29  0.0054293896  0.0524421890  0.0390002789  0.049348244  3.582163e-02
## 2016-08-31 -0.0021560459  0.0087986004  0.0053266767  0.011261140  1.196796e-03
## 2016-09-30  0.0005160948  0.0248729397  0.0132792682  0.008614818  5.805468e-05
## 2016-10-31 -0.0082052618 -0.0083121374 -0.0224036405 -0.038135050 -1.748917e-02
## 2016-11-30 -0.0259898187 -0.0451617810 -0.0179745296  0.125246372  3.617619e-02
## 2016-12-30  0.0025382854 -0.0025302864  0.0267028672  0.031491858  2.006915e-02
## 2017-01-31  0.0021258048  0.0644316093  0.0323817970 -0.012143972  1.773642e-02
## 2017-02-28  0.0064381561  0.0172577716  0.0118365395  0.013428768  3.853903e-02
## 2017-03-31 -0.0005527965  0.0361888835  0.0318056725 -0.006532961  1.249422e-03
## 2017-04-28  0.0090292023  0.0168665993  0.0239521597  0.005107756  9.877108e-03
## 2017-05-31  0.0068470041  0.0280598781  0.0348102715 -0.022862738  1.401433e-02
## 2017-06-30 -0.0001824935  0.0092237222  0.0029559940  0.029151896  6.354502e-03
## 2017-07-31  0.0033347036  0.0565945969  0.0261877742  0.007481497  2.034585e-02
## 2017-08-31  0.0093689866  0.0232437818 -0.0004482966 -0.027564696  2.913538e-03
## 2017-09-29 -0.0057321280 -0.0004463555  0.0233427730  0.082321803  1.994912e-02
## 2017-10-31  0.0009778875  0.0322785876  0.0166536400  0.005916089  2.329052e-02
## 2017-11-30 -0.0014841323 -0.0038971229  0.0068702036  0.036913305  3.010824e-02
## 2017-12-29  0.0047402138  0.0369255118  0.0133982341 -0.003731543  1.205509e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398529e-05 0.0001042116 4.178443e-05 -7.811741e-05 -9.029826e-06
## EEM  1.042116e-04 0.0017547117 1.039017e-03  6.437729e-04  6.795415e-04
## EFA  4.178443e-05 0.0010390169 1.064237e-03  6.490312e-04  6.975401e-04
## IJS -7.811741e-05 0.0006437729 6.490312e-04  1.565451e-03  8.290253e-04
## SPY -9.029826e-06 0.0006795415 6.975401e-04  8.290253e-04  7.408285e-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.02347493
# 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.0003874308 0.009257142 0.005815634 0.005684473 0.002330247
rowSums(component_contribution)
## [1] 0.02347493
# 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.0062314271 -0.0029357363  0.0366062698  0.052132838  4.992335e-02
## 2013-02-28  0.0058913338 -0.0231054028 -0.0129693233  0.016175802  1.267813e-02
## 2013-03-28  0.0009848806 -0.0102351153  0.0129693233  0.040257735  3.726836e-02
## 2013-04-30  0.0096394934  0.0120851159  0.0489677817  0.001222760  1.902973e-02
## 2013-05-31 -0.0202143674 -0.0494835442 -0.0306554809  0.041976061  2.333529e-02
## 2013-06-28 -0.0157778946 -0.0547282002 -0.0271446607 -0.001402819 -1.343432e-02
## 2013-07-31  0.0026877244  0.0131594971  0.0518604624  0.063541482  5.038548e-02
## 2013-08-30 -0.0082989512 -0.0257053987 -0.0197464753 -0.034743814 -3.045078e-02
## 2013-09-30  0.0111443656  0.0695888168  0.0753387601  0.063873785  3.115583e-02
## 2013-10-31  0.0082919206  0.0408611647  0.0320813863  0.034233830  4.526677e-02
## 2013-11-29 -0.0025095174 -0.0025943047  0.0054500752  0.041661450  2.920686e-02
## 2013-12-31 -0.0055834401 -0.0040739826  0.0215278531  0.012892061  2.559650e-02
## 2014-01-31  0.0152917970 -0.0903225477 -0.0534133611 -0.035775316 -3.588493e-02
## 2014-02-28  0.0037569362  0.0332203667  0.0595050562  0.045257731  4.451031e-02
## 2014-03-31 -0.0014816759  0.0380217431 -0.0046024868  0.013315314  8.261015e-03
## 2014-04-30  0.0081831116  0.0077729133  0.0165293619 -0.023184519  6.927853e-03
## 2014-05-30  0.0117214864  0.0290910706  0.0158282955  0.006205219  2.294108e-02
## 2014-06-30 -0.0005752144  0.0237338416  0.0091656393  0.037718727  2.043464e-02
## 2014-07-31 -0.0025123425  0.0135555467 -0.0263798763 -0.052009545 -1.352887e-02
## 2014-08-29  0.0114312865  0.0279047091  0.0018004559  0.043658052  3.870509e-02
## 2014-09-30 -0.0061678487 -0.0808567758 -0.0395985939 -0.061260317 -1.389244e-02
## 2014-10-31  0.0105846879  0.0140966541 -0.0026546540  0.068874777  2.327789e-02
## 2014-11-28  0.0065488393 -0.0155415506  0.0006251561  0.004773465  2.710113e-02
## 2014-12-31  0.0014749572 -0.0404419772 -0.0407466701  0.025295825 -2.539677e-03
## 2015-01-30  0.0203151042 -0.0068957476  0.0062264210 -0.054627985 -3.007695e-02
## 2015-02-27 -0.0089880982  0.0431362506  0.0614506850  0.056914738  5.468186e-02
## 2015-03-31  0.0037405803 -0.0150863546 -0.0143888476  0.010156341 -1.583030e-02
## 2015-04-30 -0.0032333965  0.0662814792  0.0358165769 -0.018417754  9.785811e-03
## 2015-05-29 -0.0043835033 -0.0419112365  0.0019527483  0.007510086  1.277433e-02
## 2015-06-30 -0.0108260533 -0.0297465425 -0.0316788808  0.004171318 -2.052134e-02
## 2015-07-31  0.0085852344 -0.0651782975  0.0201143017 -0.027375530  2.233806e-02
## 2015-08-31 -0.0033638703 -0.0925121937 -0.0771520601 -0.047268398 -6.288685e-02
## 2015-09-30  0.0080815989 -0.0318251469 -0.0451949450 -0.038464594 -2.584693e-02
## 2015-10-30  0.0006852516  0.0618083483  0.0640257777  0.063589589  8.163478e-02
## 2015-11-30 -0.0038983764 -0.0255603285 -0.0075558606  0.024415195  3.648837e-03
## 2015-12-31 -0.0019183267 -0.0389471530 -0.0235950350 -0.052156897 -1.743396e-02
## 2016-01-29  0.0123298055 -0.0516365338 -0.0567578359 -0.060307011 -5.106853e-02
## 2016-02-29  0.0088308511 -0.0082118433 -0.0339138922  0.020605286 -8.262749e-04
## 2016-03-31  0.0087094672  0.1218792297  0.0637455915  0.089910599  6.510009e-02
## 2016-04-29  0.0025459487  0.0040790876  0.0219752075  0.021044055  3.933529e-03
## 2016-05-31  0.0001352822 -0.0376283248 -0.0008561427  0.004397327  1.686864e-02
## 2016-06-30  0.0191669731  0.0445821604 -0.0244913881  0.008292171  3.470050e-03
## 2016-07-29  0.0054293896  0.0524421890  0.0390002789  0.049348244  3.582163e-02
## 2016-08-31 -0.0021560459  0.0087986004  0.0053266767  0.011261140  1.196796e-03
## 2016-09-30  0.0005160948  0.0248729397  0.0132792682  0.008614818  5.805468e-05
## 2016-10-31 -0.0082052618 -0.0083121374 -0.0224036405 -0.038135050 -1.748917e-02
## 2016-11-30 -0.0259898187 -0.0451617810 -0.0179745296  0.125246372  3.617619e-02
## 2016-12-30  0.0025382854 -0.0025302864  0.0267028672  0.031491858  2.006915e-02
## 2017-01-31  0.0021258048  0.0644316093  0.0323817970 -0.012143972  1.773642e-02
## 2017-02-28  0.0064381561  0.0172577716  0.0118365395  0.013428768  3.853903e-02
## 2017-03-31 -0.0005527965  0.0361888835  0.0318056725 -0.006532961  1.249422e-03
## 2017-04-28  0.0090292023  0.0168665993  0.0239521597  0.005107756  9.877108e-03
## 2017-05-31  0.0068470041  0.0280598781  0.0348102715 -0.022862738  1.401433e-02
## 2017-06-30 -0.0001824935  0.0092237222  0.0029559940  0.029151896  6.354502e-03
## 2017-07-31  0.0033347036  0.0565945969  0.0261877742  0.007481497  2.034585e-02
## 2017-08-31  0.0093689866  0.0232437818 -0.0004482966 -0.027564696  2.913538e-03
## 2017-09-29 -0.0057321280 -0.0004463555  0.0233427730  0.082321803  1.994912e-02
## 2017-10-31  0.0009778875  0.0322785876  0.0166536400  0.005916089  2.329052e-02
## 2017-11-30 -0.0014841323 -0.0038971229  0.0068702036  0.036913305  3.010824e-02
## 2017-12-29  0.0047402138  0.0369255118  0.0133982341 -0.003731543  1.205509e-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
    

    # 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

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

plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>% 
    # transform to long from
    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 Volitility and Weight", 
         y = "Percent",
         x = NULL)