# 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.0062319723 -0.0029354288  0.0366061825  0.052133370  4.992306e-02
## 2013-02-28  0.0058915055 -0.0231052881 -0.0129692108  0.016175209  1.267787e-02
## 2013-03-28  0.0009848890 -0.0102349982  0.0129692108  0.040258140  3.726858e-02
## 2013-04-30  0.0096394460  0.0120848841  0.0489679631  0.001222425  1.902936e-02
## 2013-05-31 -0.0202139253 -0.0494834238 -0.0306554141  0.041976408  2.333574e-02
## 2013-06-28 -0.0157785140 -0.0547284477 -0.0271445445 -0.001402667 -1.343421e-02
## 2013-07-31  0.0026875775  0.0131596870  0.0518601924  0.063541273  5.038575e-02
## 2013-08-30 -0.0082980237 -0.0257056867 -0.0197462111 -0.034743863 -3.045118e-02
## 2013-09-30  0.0111443896  0.0695890421  0.0753385829  0.063873973  3.115581e-02
## 2013-10-31  0.0082917000  0.0408612799  0.0320816251  0.034234283  4.526618e-02
## 2013-11-29 -0.0025101096 -0.0025939865  0.0054496187  0.041660902  2.920744e-02
## 2013-12-31 -0.0055827574 -0.0040745031  0.0215280708  0.012892154  2.559625e-02
## 2014-01-31  0.0152913763 -0.0903227464 -0.0534131976 -0.035775219 -3.588469e-02
## 2014-02-28  0.0037570080  0.0332204068  0.0595050275  0.045257247  4.451019e-02
## 2014-03-31 -0.0014819793  0.0380219888 -0.0046027569  0.013315419  8.261281e-03
## 2014-04-30  0.0081839262  0.0077729133  0.0165293451 -0.023184385  6.927881e-03
## 2014-05-30  0.0117210675  0.0290907286  0.0158285788  0.006205357  2.294100e-02
## 2014-06-30 -0.0005751849  0.0237341836  0.0091653598  0.037718442  2.043470e-02
## 2014-07-31 -0.0025125326  0.0135556565 -0.0263798612 -0.052009082 -1.352876e-02
## 2014-08-29  0.0114306574  0.0279045993  0.0018003801  0.043657717  3.870485e-02
## 2014-09-30 -0.0061670910 -0.0808567758 -0.0395983254 -0.061260333 -1.389230e-02
## 2014-10-31  0.0105846781  0.0140965399 -0.0026547927  0.068874860  2.327790e-02
## 2014-11-28  0.0065485323 -0.0155412045  0.0006253148  0.004773722  2.710110e-02
## 2014-12-31  0.0014752984 -0.0404422091 -0.0407465842  0.025295592 -2.539733e-03
## 2015-01-30  0.0203148531 -0.0068957476  0.0062261943 -0.054627666 -3.007685e-02
## 2015-02-27 -0.0089882410  0.0431361342  0.0614506693  0.056914499  5.468167e-02
## 2015-03-31  0.0037403887 -0.0150863268 -0.0143886921  0.010156214 -1.583011e-02
## 2015-04-30 -0.0032327836  0.0662813466  0.0358164986 -0.018417684  9.785723e-03
## 2015-05-29 -0.0043836686 -0.0419110152  0.0019527483  0.007510123  1.277457e-02
## 2015-06-30 -0.0108256913 -0.0297464533 -0.0316787251  0.004171268 -2.052157e-02
## 2015-07-31  0.0085849591 -0.0651781647  0.0201142986 -0.027375279  2.233794e-02
## 2015-08-31 -0.0033639428 -0.0925126244 -0.0771524393 -0.047268693 -6.288676e-02
## 2015-09-30  0.0080809617 -0.0318248663 -0.0451948693 -0.038464648 -2.584691e-02
## 2015-10-30  0.0006859612  0.0618083440  0.0640260094  0.063589971  8.163477e-02
## 2015-11-30 -0.0038983158 -0.0255604653 -0.0075557785  0.024414973  3.648385e-03
## 2015-12-31 -0.0019188536 -0.0389470837 -0.0235951980 -0.052156995 -1.743346e-02
## 2016-01-29  0.0123298354 -0.0516366096 -0.0567579904 -0.060306744 -5.106862e-02
## 2016-02-29  0.0088314456 -0.0082116909 -0.0339137376  0.020605140 -8.262157e-04
## 2016-03-31  0.0087089105  0.1218790178  0.0637456772  0.089910477  6.510009e-02
## 2016-04-29  0.0025463825  0.0040794252  0.0219750380  0.021044148  3.933541e-03
## 2016-05-31  0.0001353388 -0.0376285971 -0.0008559750  0.004397202  1.686845e-02
## 2016-06-30  0.0191665835  0.0445820965 -0.0244913860  0.008292079  3.469836e-03
## 2016-07-29  0.0054300419  0.0524424499  0.0390000275  0.049348401  3.582195e-02
## 2016-08-31 -0.0021561676  0.0087986308  0.0053270066  0.011261093  1.196769e-03
## 2016-09-30  0.0005157794  0.0248729051  0.0132789414  0.008614784  5.803735e-05
## 2016-10-31 -0.0082052033 -0.0083126009 -0.0224036442 -0.038134905 -1.748909e-02
## 2016-11-30 -0.0259896051 -0.0451616348 -0.0179743636  0.125246534  3.617629e-02
## 2016-12-30  0.0025377851 -0.0025299621  0.0267028055  0.031491822  2.006882e-02
## 2017-01-31  0.0021260130  0.0644313576  0.0323819384 -0.012143953  1.773644e-02
## 2017-02-28  0.0064383023  0.0172582227  0.0118364598  0.013428634  3.853931e-02
## 2017-03-31 -0.0005533517  0.0361887852  0.0318055200 -0.006533003  1.249175e-03
## 2017-04-28  0.0090294173  0.0168662832  0.0239523867  0.005107645  9.877296e-03
## 2017-05-31  0.0068475863  0.0280598805  0.0348102689 -0.022862787  1.401427e-02
## 2017-06-30 -0.0001829075  0.0092238050  0.0029558504  0.029151980  6.354715e-03
## 2017-07-31  0.0033348137  0.0565944936  0.0261879158  0.007481694  2.034578e-02
## 2017-08-31  0.0093688294  0.0232439862 -0.0004483665 -0.027564863  2.913398e-03
## 2017-09-29 -0.0057319510 -0.0004462544  0.0233429095  0.082321712  1.994915e-02
## 2017-10-31  0.0009779869  0.0322784833  0.0166534531  0.005916258  2.329059e-02
## 2017-11-30 -0.0014844784 -0.0038971225  0.0068701206  0.036913150  3.010794e-02
## 2017-12-29  0.0047404947  0.0369254846  0.0133983017 -0.003731268  1.205527e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AGG          EEM          EFA           IJS           SPY
## AGG  7.398389e-05 0.0001042117 4.178484e-05 -7.811821e-05 -9.030033e-06
## EEM  1.042117e-04 0.0017547127 1.039017e-03  6.437755e-04  6.795417e-04
## EFA  4.178484e-05 0.0010390171 1.064236e-03  6.490313e-04  6.975388e-04
## IJS -7.811821e-05 0.0006437755 6.490313e-04  1.565450e-03  8.290253e-04
## SPY -9.030033e-06 0.0006795417 6.975388e-04  8.290253e-04  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.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.0003874264 0.00925715 0.005815633 0.005684474 0.002330245
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")
# 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") 

6 Rolling Component Contribution

calculate_comp_contrib_by_window <- function(asset_returns_wide_tbl,
                                             start = 1,
                                             window = 24,
                                             weights) {
    # 1 Define start date
    start_date <- rownames(asset_returns_wide_tbl)[start]
    # 2 Define end date
    end_date <- rownames(asset_returns_wide_tbl)[start + window]
    # 3 Subset df
    df_subset <- asset_returns_wide_tbl %>%
        rownames_to_column(var = "date") %>%
        filter(date >= start_date & date < end_date) %>%
        column_to_rownames(var = "date")
    # 4 Calculate component contribution
    component_percentages <-df_subset %>%
        calculate_component_contribution(w = weights)
    # 5 Add end date to df
    component_percentages %>%
        mutate(date = ymd(end_date)) %>%
        select(date, everything())
}