# 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.

1 Import stock prices

symbols <- c("AAPL", "TGT", "AMZN", "WMT", "PEP")

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
##                     AAPL          AMZN           PEP           TGT
## 2013-01-31 -1.555889e-01  0.0566799395  0.0625911683  0.0207399364
## 2013-02-28 -2.561096e-02 -0.0046435024  0.0464266350  0.0470671838
## 2013-03-28  2.850330e-03  0.0083654162  0.0431368037  0.0836041496
## 2013-04-30  2.711615e-04 -0.0487507497  0.0415950987  0.0303598241
## 2013-05-31  2.217173e-02  0.0588686246 -0.0208286184 -0.0099611675
## 2013-06-28 -1.258958e-01  0.0310507506  0.0195320071 -0.0092512085
## 2013-07-31  1.321022e-01  0.0813355350  0.0211702203  0.0341193490
## 2013-08-30  8.044322e-02 -0.0695574090 -0.0466793531 -0.1118616188
## 2013-09-30 -2.172386e-02  0.1067688897  0.0042184476  0.0105269024
## 2013-10-31  9.201510e-02  0.1521839116  0.0561302315  0.0125809791
## 2013-11-29  6.770799e-02  0.0781496860  0.0043905080 -0.0069133995
## 2013-12-31  8.862879e-03  0.0130490386 -0.0113592004 -0.0103772510
## 2014-01-31 -1.139498e-01 -0.1059765119 -0.0316006713 -0.1106960080
## 2014-02-28  5.591854e-02  0.0094619003 -0.0036150290  0.1066821271
## 2014-03-31  1.975607e-02 -0.0737086161  0.0489951972 -0.0329978216
## 2014-04-30  9.476128e-02 -0.1007565303  0.0282205738  0.0202853927
## 2014-05-30  7.576540e-02  0.0273091844  0.0280123802 -0.0769022135
## 2014-06-30  2.728618e-02  0.0383836202  0.0188216406  0.0207489687
## 2014-07-31  2.832615e-02 -0.0369768154 -0.0139767466  0.0279067617
## 2014-08-29  7.465195e-02  0.0799468404  0.0486280911  0.0169978674
## 2014-09-30 -1.722061e-02 -0.0502010184  0.0135742371  0.0425319434
## 2014-10-31  6.948891e-02 -0.0540982347  0.0325508040 -0.0138153886
## 2014-11-28  1.007307e-01  0.1031187277  0.0400521890  0.1874998166
## 2014-12-31 -7.460629e-02 -0.0872368614 -0.0503893945  0.0254832748
## 2015-01-30  5.961177e-02  0.1330922557 -0.0082826795 -0.0307675545
## 2015-02-27  9.601610e-02  0.0697992426  0.0539662358  0.0496021165
## 2015-03-31 -3.187455e-02 -0.0214295755 -0.0278572653  0.0659769989
## 2015-04-30  5.769772e-03  0.1253212736 -0.0052424692 -0.0402785800
## 2015-05-29  4.434124e-02  0.0175090293  0.0136777299  0.0128402106
## 2015-06-30 -3.793831e-02  0.0112589814 -0.0252227650  0.0287064164
## 2015-07-31 -3.348063e-02  0.2111621090  0.0317391278  0.0026916360
## 2015-08-31 -6.848917e-02 -0.0443525782 -0.0361411998 -0.0448221894
## 2015-09-30 -2.205791e-02 -0.0019516837  0.0223621774  0.0121510349
## 2015-10-30  8.011270e-02  0.2010808743  0.0803525832 -0.0189944304
## 2015-11-30 -5.821348e-03  0.0602956777 -0.0200650898 -0.0547336205
## 2015-12-31 -1.167903e-01  0.0165440008  0.0045818655  0.0015158699
## 2016-01-29 -7.822345e-02 -0.1410054620 -0.0062246328 -0.0026201718
## 2016-02-29 -1.288088e-03 -0.0605352209 -0.0150162051  0.0882424844
## 2016-03-31  1.197460e-01  0.0717834363  0.0536582301  0.0476669211
## 2016-04-29 -1.507313e-01  0.1053453760  0.0046729064 -0.0343712324
## 2016-05-31  6.931429e-02  0.0915002899 -0.0175382309 -0.1372355970
## 2016-06-30 -4.359633e-02 -0.0099694639  0.0535411636  0.0150075932
## 2016-07-29  8.623519e-02  0.0586021229  0.0277405386  0.0759581171
## 2016-08-31  2.337661e-02  0.0135476418 -0.0130754680 -0.0627265606
## 2016-09-30  6.344806e-02  0.0848953908  0.0187459848 -0.0217478023
## 2016-10-31  4.324889e-03 -0.0583893058 -0.0145391694  0.0007275473
## 2016-11-30 -2.183761e-02 -0.0509721927 -0.0611560485  0.1251765740
## 2016-12-30  4.684099e-02 -0.0009330556  0.0442609018 -0.0670618677
## 2017-01-31  4.664178e-02  0.0936394059 -0.0081573438 -0.1135005300
## 2017-02-28  1.255549e-01  0.0258446800  0.0616556740 -0.0835533873
## 2017-03-31  4.754169e-02  0.0479423007  0.0201644701 -0.0628499147
## 2017-04-28 -6.986092e-05  0.0424566944  0.0126145579  0.0118878203
## 2017-05-31  6.560791e-02  0.0725778018  0.0380442043 -0.0018016474
## 2017-06-30 -5.891590e-02 -0.0271286156 -0.0118784061 -0.0532516131
## 2017-07-31  3.218012e-02  0.0202278808  0.0096513462  0.0804397045
## 2017-08-31  1.016529e-01 -0.0072953953 -0.0006017527 -0.0272904770
## 2017-09-29 -6.213458e-02 -0.0198260355 -0.0378631524  0.0789560154
## 2017-10-31  9.240350e-02  0.1395154056 -0.0108275356  0.0005084695
## 2017-11-30  2.007531e-02  0.0626577318  0.0624075714  0.0247790384
## 2017-12-29 -1.536326e-02 -0.0062057845  0.0287617232  0.0855496098
##                      WMT
## 2013-01-31  0.0248963913
## 2013-02-28  0.0117957358
## 2013-03-28  0.0620732336
## 2013-04-30  0.0378932666
## 2013-05-31 -0.0317795670
## 2013-06-28 -0.0046878858
## 2013-07-31  0.0452745145
## 2013-08-30 -0.0596996795
## 2013-09-30  0.0133391599
## 2013-10-31  0.0370285815
## 2013-11-29  0.0540191996
## 2013-12-31 -0.0232522450
## 2014-01-31 -0.0523036082
## 2014-02-28  0.0002675095
## 2014-03-31  0.0293260735
## 2014-04-30  0.0420198962
## 2014-05-30 -0.0314092566
## 2014-06-30 -0.0223929236
## 2014-07-31 -0.0200473363
## 2014-08-29  0.0323259003
## 2014-09-30  0.0127659115
## 2014-10-31 -0.0026192415
## 2014-11-28  0.1378162397
## 2014-12-31 -0.0135735995
## 2015-01-30 -0.0105351112
## 2015-02-27 -0.0124328896
## 2015-03-31 -0.0142313553
## 2015-04-30 -0.0524138480
## 2015-05-29 -0.0433513037
## 2015-06-30 -0.0460136507
## 2015-07-31  0.0146950821
## 2015-08-31 -0.0993580345
## 2015-09-30  0.0016974198
## 2015-10-30 -0.1246696153
## 2015-11-30  0.0275687911
## 2015-12-31  0.0492990864
## 2016-01-29  0.0793149329
## 2016-02-29 -0.0003019371
## 2016-03-31  0.0392706623
## 2016-04-29 -0.0239371028
## 2016-05-31  0.0641212881
## 2016-06-30  0.0311567244
## 2016-07-29 -0.0006849209
## 2016-08-31 -0.0143683082
## 2016-09-30  0.0094733768
## 2016-10-31 -0.0295503588
## 2016-11-30  0.0058383167
## 2016-12-30 -0.0116430074
## 2017-01-31 -0.0350401409
## 2017-02-28  0.0608888664
## 2017-03-31  0.0234095158
## 2017-04-28  0.0421083629
## 2017-05-31  0.0511562822
## 2017-06-30 -0.0378577120
## 2017-07-31  0.0553877522
## 2017-08-31 -0.0180254181
## 2017-09-29  0.0008961626
## 2017-10-31  0.1109625570
## 2017-11-30  0.1076146601
## 2017-12-29  0.0207685161
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AAPL          AMZN          PEP           TGT          WMT
## AAPL  4.830146e-03  0.0015511133 0.0006789485 -1.384739e-05 0.0006412761
## AMZN  1.551113e-03  0.0054660218 0.0008712211 -2.147201e-04 0.0003511319
## PEP   6.789485e-04  0.0008712211 0.0010461777  2.506920e-04 0.0004913971
## TGT  -1.384739e-05 -0.0002147201 0.0002506920  3.709011e-03 0.0009852575
## WMT   6.412761e-04  0.0003511319 0.0004913971  9.852575e-04 0.0022213949
# 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.03620391
# 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
##            AAPL       AMZN         PEP         TGT         WMT
## [1,] 0.01237755 0.01326305 0.003845198 0.004603505 0.002114612
rowSums(component_contribution)
## [1] 0.03620391
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AAPL  AMZN   PEP   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.342 0.366 0.106 0.127 0.058
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.342
## 2 AMZN         0.366
## 3 PEP          0.106
## 4 TGT          0.127
## 5 WMT          0.058

6 Plot: Colum Chart of Component Contribution and Weight

# 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
##    AAPL  AMZN   PEP   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.342 0.366 0.106 0.127 0.058
# 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 Portfolio Volatility and weight",
         y = "percent",
         x = "asset") 

Which of the assets in your portfolio the largest contributor to the portfolio volatility? Do you think your portfolio risk is concentrated in any one asset?

AAPL is the largest contributor to the portfolio’s volatility, and while risk isn’t concentrated in only one asset, it is somewhat heavier in AAPL and AMZN than in the others.