# 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.0062309008 -0.002935032  0.0366059316  0.052133308  4.992270e-02
## 2013-02-28  0.0058910567 -0.023105689 -0.0129690974  0.016175232  1.267799e-02
## 2013-03-28  0.0009848158 -0.010234998  0.0129690974  0.040257880  3.726843e-02
## 2013-04-30  0.0096392903  0.012084894  0.0489679870  0.001222744  1.902992e-02
## 2013-05-31 -0.0202139843 -0.049483402 -0.0306555872  0.041976681  2.333514e-02
## 2013-06-28 -0.0157784247 -0.054728469 -0.0271447143 -0.001403099 -1.343408e-02
## 2013-07-31  0.0026879993  0.013159862  0.0518603108  0.063541125  5.038627e-02
## 2013-08-30 -0.0082983732 -0.025705575 -0.0197462437 -0.034743763 -3.045155e-02
## 2013-09-30  0.0111439341  0.069588829  0.0753385261  0.063873994  3.115592e-02
## 2013-10-31  0.0082918989  0.040861059  0.0320817420  0.034233988  4.526662e-02
## 2013-11-29 -0.0025097175 -0.002593842  0.0054495999  0.041661076  2.920700e-02
## 2013-12-31 -0.0055833363 -0.004074474  0.0215282490  0.012891966  2.559595e-02
## 2014-01-31  0.0152912071 -0.090322601 -0.0534135125 -0.035774976 -3.588445e-02
## 2014-02-28  0.0037574237  0.033220422  0.0595050079  0.045257177  4.451005e-02
## 2014-03-31 -0.0014808797  0.038021524 -0.0046024936  0.013315313  8.261302e-03
## 2014-04-30  0.0081824896  0.007773151  0.0165292653 -0.023184437  6.927853e-03
## 2014-05-30  0.0117219334  0.029090843  0.0158286006  0.006205451  2.294126e-02
## 2014-06-30 -0.0005762189  0.023734060  0.0091651859  0.037718814  2.043435e-02
## 2014-07-31 -0.0025121562  0.013555689 -0.0263795816 -0.052009250 -1.352841e-02
## 2014-08-29  0.0114313774  0.027904391  0.0018003196  0.043657856  3.870449e-02
## 2014-09-30 -0.0061675399 -0.080856446 -0.0395984613 -0.061260638 -1.389209e-02
## 2014-10-31  0.0105851102  0.014096456 -0.0026548519  0.068874884  2.327752e-02
## 2014-11-28  0.0065480550 -0.015541326  0.0006251659  0.004773337  2.710168e-02
## 2014-12-31  0.0014754354 -0.040441996 -0.0407463134  0.025295915 -2.539914e-03
## 2015-01-30  0.0203148037 -0.006895942  0.0062263652 -0.054627652 -3.007710e-02
## 2015-02-27 -0.0089875682  0.043136265  0.0614504237  0.056914448  5.468205e-02
## 2015-03-31  0.0037401652 -0.015086181 -0.0143886336  0.010156466 -1.583032e-02
## 2015-04-30 -0.0032334165  0.066281232  0.0358165012 -0.018417570  9.785698e-03
## 2015-05-29 -0.0043836266 -0.041911155  0.0019527115  0.007509707  1.277408e-02
## 2015-06-30 -0.0108250421 -0.029746529 -0.0316787621  0.004171207 -2.052089e-02
## 2015-07-31  0.0085842184 -0.065178275  0.0201144152 -0.027375274  2.233793e-02
## 2015-08-31 -0.0033638763 -0.092512275 -0.0771523348 -0.047268355 -6.288680e-02
## 2015-09-30  0.0080816215 -0.031824888 -0.0451949307 -0.038465098 -2.584735e-02
## 2015-10-30  0.0006851777  0.061808142  0.0640258227  0.063590031  8.163517e-02
## 2015-11-30 -0.0038983516 -0.025560268 -0.0075556441  0.024415275  3.648335e-03
## 2015-12-31 -0.0019189932 -0.038947137 -0.0235950480 -0.052156763 -1.743377e-02
## 2016-01-29  0.0123302958 -0.051636623 -0.0567578728 -0.060306975 -5.106843e-02
## 2016-02-29  0.0088316770 -0.008211692 -0.0339139220  0.020605128 -8.260582e-04
## 2016-03-31  0.0087088647  0.121879106  0.0637456621  0.089910384  6.510042e-02
## 2016-04-29  0.0025461171  0.004079241  0.0219751028  0.021044167  3.932995e-03
## 2016-05-31  0.0001355098 -0.037628532 -0.0008560850  0.004397256  1.686851e-02
## 2016-06-30  0.0191668000  0.044582357 -0.0244916138  0.008292095  3.470100e-03
## 2016-07-29  0.0054295064  0.052442166  0.0390002419  0.049348431  3.582166e-02
## 2016-08-31 -0.0021564405  0.008798726  0.0053269298  0.011261131  1.196892e-03
## 2016-09-30  0.0005161298  0.024872727  0.0132789111  0.008614665  5.794374e-05
## 2016-10-31 -0.0082047505 -0.008312139 -0.0224034129 -0.038134959 -1.748914e-02
## 2016-11-30 -0.0259904210 -0.045161884 -0.0179745731  0.125246798  3.617616e-02
## 2016-12-30  0.0025382379 -0.002529917  0.0267029875  0.031491544  2.006914e-02
## 2017-01-31  0.0021261272  0.064431352  0.0323818267 -0.012144083  1.773656e-02
## 2017-02-28  0.0064380503  0.017258200  0.0118364314  0.013428710  3.853903e-02
## 2017-03-31 -0.0005531281  0.036188787  0.0318057497 -0.006532745  1.249270e-03
## 2017-04-28  0.0090295244  0.016866291  0.0239520970  0.005107514  9.877155e-03
## 2017-05-31  0.0068472691  0.028059788  0.0348101469 -0.022862550  1.401449e-02
## 2017-06-30 -0.0001830050  0.009223772  0.0029560003  0.029151768  6.354505e-03
## 2017-07-31  0.0033351957  0.056594640  0.0261880260  0.007481714  2.034594e-02
## 2017-08-31  0.0093688057  0.023243714 -0.0004483592 -0.027564899  2.913357e-03
## 2017-09-29 -0.0057320898 -0.000446055  0.0233428112  0.082321566  1.994914e-02
## 2017-10-31  0.0009779146  0.032278395  0.0166537895  0.005916449  2.329081e-02
## 2017-11-30 -0.0014843220 -0.003897018  0.0068696856  0.036913032  3.010795e-02
## 2017-12-29  0.0047404250  0.036925324  0.0133984810 -0.003731191  1.205495e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398424e-05 0.0001042127 4.178319e-05 -0.0000781181 -9.030421e-06
## EEM  1.042127e-04 0.0017547085 1.039016e-03  0.0006437744  6.795429e-04
## EFA  4.178319e-05 0.0010390164 1.064236e-03  0.0006490293  6.975395e-04
## IJS -7.811810e-05 0.0006437744 6.490293e-04  0.0015654508  8.290245e-04
## SPY -9.030421e-06 0.0006795429 6.975395e-04  0.0008290245  7.408289e-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.0003874265 0.009257144 0.005815627 0.005684472 0.002330248
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")

# Custom function
calculate_component_contribution <- function(asset_returns_wide_tbl, w) {

    # Covariance of asset returns
    covariance_matrix <- cov(asset_returns_wide_tbl)
    
    # Standard deviation of portfolio
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

    # Component contribution
    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(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

# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%

    ggplot(aes(asset, contribution)) +
    geom_col(fill = "cornflowerblue") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    
    labs(title = "Percent Contribution to Portfolio Standard Deviation",
         y = "Percent Contribution to Risk",
         x = NULL)

# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%
    add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
    pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%

    ggplot(aes(asset, value, fill = type)) +
    geom_col(position = "dodge") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    theme_tq() +
    scale_fill_tq() +

    labs(title = "Percent Contribution to Volatility",
         y = "percent",
         x = "asset")