# Load packages

# Core
library(tidyverse)
library(tidyquant)

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

Choose your stocks from 2012-12-31 to present.

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",    
                 from = "2012-12-31",
                 to   = "2024-12-31")

2 Convert prices to returns (monthly)

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

# 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 -6.230998e-03 -0.0029351456  0.0366062193  0.052133978  4.992285e-02
## 2013-02-28  5.891154e-03 -0.0231051102 -0.0129695702  0.016174477  1.267837e-02
## 2013-03-28  9.847196e-04 -0.0102355802  0.0129695702  0.040258103  3.726830e-02
## 2013-04-30  9.639292e-03  0.0120853593  0.0489677151  0.001222949  1.902963e-02
## 2013-05-31 -2.021418e-02 -0.0494838714 -0.0306556847  0.041976188  2.333561e-02
## 2013-06-28 -1.577813e-02 -0.0547284119 -0.0271441554 -0.001403001 -1.343431e-02
## 2013-07-31  2.687802e-03  0.0131597360  0.0518600275  0.063541705  5.038582e-02
## 2013-08-30 -8.297879e-03 -0.0257053847 -0.0197461523 -0.034744044 -3.045122e-02
## 2013-09-30  1.114374e-02  0.0695886461  0.0753386042  0.063874089  3.115592e-02
## 2013-10-31  8.291704e-03  0.0408613580  0.0320815724  0.034233640  4.526683e-02
## 2013-11-29 -2.509229e-03 -0.0025943101  0.0054495182  0.041661257  2.920648e-02
## 2013-12-31 -5.583236e-03 -0.0040742400  0.0215280909  0.012892051  2.559647e-02
## 2014-01-31  1.529198e-02 -0.0903224834 -0.0534134413 -0.035775064 -3.588445e-02
## 2014-02-28  3.756455e-03  0.0332206084  0.0595053354  0.045257347  4.451034e-02
## 2014-03-31 -1.481362e-03  0.0380215764 -0.0046027323  0.013315232  8.260901e-03
## 2014-04-30  8.183162e-03  0.0077727927  0.0165294236 -0.023184110  6.927754e-03
## 2014-05-30  1.172146e-02  0.0290910768  0.0158285994  0.006205449  2.294126e-02
## 2014-06-30 -5.761243e-04  0.0237341703  0.0091652618  0.037718726  2.043464e-02
## 2014-07-31 -2.511491e-03  0.0135554634 -0.0263798920 -0.052009490 -1.352860e-02
## 2014-08-29  1.143034e-02  0.0279049320  0.0018005554  0.043657701  3.870467e-02
## 2014-09-30 -6.167259e-03 -0.0808569866 -0.0395986214 -0.061260146 -1.389237e-02
## 2014-10-31  1.058436e-02  0.0140964557 -0.0026546884  0.068874473  2.327771e-02
## 2014-11-28  6.549175e-03 -0.0155413256  0.0006254113  0.004773887  2.710141e-02
## 2014-12-31  1.474693e-03 -0.0404419346 -0.0407468111  0.025295829 -2.539647e-03
## 2015-01-30  2.031536e-02 -0.0068960644  0.0062264509 -0.054628281 -3.007719e-02
## 2015-02-27 -8.987842e-03  0.0431360909  0.0614505880  0.056914695  5.468179e-02
## 2015-03-31  3.739801e-03 -0.0150863045 -0.0143887132  0.010156769 -1.583015e-02
## 2015-04-30 -3.232960e-03  0.0662814793  0.0358165791 -0.018417951  9.786049e-03
## 2015-05-29 -4.383719e-03 -0.0419110434  0.0019526336  0.007509785  1.277407e-02
## 2015-06-30 -1.082504e-02 -0.0297462277 -0.0316788424  0.004171512 -2.052141e-02
## 2015-07-31  8.584404e-03 -0.0651783188  0.0201144955 -0.027375424  2.233802e-02
## 2015-08-31 -3.363876e-03 -0.0925123205 -0.0771524198 -0.047268269 -6.288681e-02
## 2015-09-30  8.081346e-03 -0.0318251723 -0.0451948457 -0.038464666 -2.584726e-02
## 2015-10-30  6.852697e-04  0.0618084883  0.0640259062  0.063589355  8.163527e-02
## 2015-11-30 -3.897800e-03 -0.0255605417 -0.0075558957  0.024415433  3.648508e-03
## 2015-12-31 -1.918716e-03 -0.0389470640 -0.0235950520 -0.052157170 -1.743359e-02
## 2016-01-29  1.232929e-02 -0.0516365427 -0.0567578828 -0.060306734 -5.106850e-02
## 2016-02-29  8.831409e-03 -0.0082117681 -0.0339138341  0.020605215 -8.266144e-04
## 2016-03-31  8.708870e-03  0.1218790281  0.0637456680  0.089910306  6.510026e-02
## 2016-04-29  2.546476e-03  0.0040794458  0.0219751047  0.021044322  3.933602e-03
## 2016-05-31  1.356884e-04 -0.0376285243 -0.0008559985  0.004397027  1.686850e-02
## 2016-06-30  1.916680e-02  0.0445820084 -0.0244915251  0.008292474  3.469675e-03
## 2016-07-29  5.429594e-03  0.0524423019  0.0390004091  0.049348420  3.582200e-02
## 2016-08-31 -2.156091e-03  0.0087986623  0.0053266739  0.011260773  1.196974e-03
## 2016-09-30  5.156932e-04  0.0248727288  0.0132791624  0.008614736  5.778028e-05
## 2016-10-31 -8.205543e-03 -0.0083120768 -0.0224037498 -0.038134962 -1.748931e-02
## 2016-11-30 -2.598927e-02 -0.0451619493 -0.0179745747  0.125246548  3.617633e-02
## 2016-12-30  2.538237e-03 -0.0025300492  0.0267029049  0.031491805  2.006937e-02
## 2017-01-31  2.126037e-03  0.0644317341  0.0323818322 -0.012144084  1.773625e-02
## 2017-02-28  6.437781e-03  0.0172577120  0.0118365958  0.013428836  3.853933e-02
## 2017-03-31 -5.529494e-04  0.0361890901  0.0318056710 -0.006532682  1.249270e-03
## 2017-04-28  9.029524e-03  0.0168665207  0.0239522525  0.005107889  9.876859e-03
## 2017-05-31  6.847093e-03  0.0280595578  0.0348101442 -0.022863116  1.401442e-02
## 2017-06-30 -1.820372e-04  0.0092238832  0.0029558521  0.029151956  6.354722e-03
## 2017-07-31  3.333702e-03  0.0565945288  0.0261880279  0.007481651  2.034580e-02
## 2017-08-31  9.369593e-03  0.0232435095 -0.0004482150 -0.027564707  2.913287e-03
## 2017-09-29 -5.732264e-03 -0.0004459528  0.0233426686  0.082321663  1.994921e-02
## 2017-10-31  9.775654e-04  0.0322786957  0.0166535135  0.005916098  2.329068e-02
## 2017-11-30 -1.483798e-03 -0.0038971170  0.0068703072  0.036913480  3.010815e-02
## 2017-12-29  4.740250e-03  0.0369253201  0.0133982058 -0.003731527  1.205488e-02
## 2018-01-31 -1.131416e-02  0.0797160686  0.0489863233  0.013195887  5.482864e-02
## 2018-02-28 -1.013849e-02 -0.0607960007 -0.0495554117 -0.041260945 -3.703825e-02
## 2018-03-29  6.688896e-03  0.0053999327 -0.0084314301  0.013535042 -2.779341e-02
## 2018-04-30 -9.454674e-03 -0.0285735948  0.0150977345  0.017481022  5.155436e-03
## 2018-05-31  6.596403e-03 -0.0265642414 -0.0191241849  0.057038837  2.401823e-02
## 2018-06-29  1.025591e-03 -0.0465225383 -0.0159681228  0.007965070  5.734314e-03
## 2018-07-31 -2.918947e-04  0.0347011860  0.0281210571  0.025817158  3.637692e-02
## 2018-08-31  5.664184e-03 -0.0384007139 -0.0226112036  0.030912592  3.142050e-02
## 2018-09-28 -6.216974e-03 -0.0058079021  0.0096060840 -0.031022083  5.928153e-03
## 2018-10-31 -6.460989e-03 -0.0916821695 -0.0848343016 -0.104674930 -7.160814e-02
## 2018-11-30  7.685655e-03  0.0478656579  0.0049511788  0.005704097  1.837956e-02
## 2018-12-31  1.961809e-02 -0.0355119927 -0.0549818372 -0.132288322 -9.216852e-02
## 2019-01-31  9.067767e-03  0.0984238762  0.0642407077  0.115614962  7.702186e-02
## 2019-02-28 -1.138910e-03 -0.0154317205  0.0250505841  0.040516451  3.190121e-02
## 2019-03-29  2.100423e-02  0.0112467903  0.0091379600 -0.039919733  1.793905e-02
## 2019-04-30 -2.005543e-03  0.0232593173  0.0288730112  0.042268388  4.003977e-02
## 2019-05-31  1.892793e-02 -0.0761234459 -0.0516401691 -0.105136606 -6.589513e-02
## 2019-06-28  1.092624e-02  0.0602266274  0.0574349472  0.073797669  6.727197e-02
## 2019-07-31  1.816181e-03 -0.0269263853 -0.0196657269  0.012902297  1.500602e-02
## 2019-08-30  2.744791e-02 -0.0385601615 -0.0194270500 -0.052780475 -1.688502e-02
## 2019-09-30 -6.140045e-03  0.0167779440  0.0311502929  0.054209481  1.927098e-02
## 2019-10-31  2.078808e-03  0.0409881630  0.0333288569  0.019842926  2.186398e-02
## 2019-11-29 -3.451026e-04 -0.0009397023  0.0112094909  0.026728301  3.555852e-02
## 2019-12-31 -4.682642e-04  0.0744607861  0.0295142535  0.027995203  2.864140e-02
## 2020-01-31  2.008713e-02 -0.0634844295 -0.0286319795 -0.065135508 -4.039740e-04
## 2020-02-28  1.572293e-02 -0.0384894538 -0.0808332274 -0.107014214 -8.247527e-02
## 2020-03-31 -5.282343e-03 -0.1716190194 -0.1520643188 -0.297850039 -1.333842e-01
## 2020-04-30  1.704448e-02  0.0709637977  0.0565451265  0.130048150  1.195446e-01
## 2020-05-29  6.703300e-03  0.0293151157  0.0528477015  0.025159619  4.654515e-02
## 2020-06-30  6.569591e-03  0.0639594340  0.0345095761  0.035215346  1.757893e-02
## 2020-07-31  1.324359e-02  0.0792922441  0.0192001108  0.025427814  5.722311e-02
## 2020-08-31 -8.269947e-03  0.0284658570  0.0461390359  0.049457108  6.746883e-02
## 2020-09-30 -9.740481e-04 -0.0101546118 -0.0206802686 -0.053334640 -3.816258e-02
## 2020-10-30 -5.598560e-03  0.0139640640 -0.0361523173  0.035941739 -2.524972e-02
## 2020-11-30  1.199567e-02  0.0860977364  0.1333888185  0.175732806  1.032575e-01
## 2020-12-31  8.565544e-04  0.0688678335  0.0489367934  0.072283948  3.637857e-02
## 2021-01-29 -7.473243e-03  0.0312465370 -0.0078431718  0.060603054 -1.024269e-02
## 2021-02-26 -1.529780e-02  0.0078475008  0.0221321471  0.102555285  2.742590e-02
## 2021-03-31 -1.152950e-02 -0.0072849181  0.0248210432  0.054641596  4.439889e-02
## 2021-04-30  7.293286e-03  0.0119271346  0.0290968037  0.016734426  5.155851e-02
## 2021-05-28  2.036374e-03  0.0163529911  0.0342301433  0.038210221  6.544362e-03
## 2021-06-30  8.244842e-03  0.0094591693 -0.0108388345 -0.007172743  2.217954e-02
## 2021-07-30  1.110409e-02 -0.0665349405  0.0077036522 -0.044611422  2.411947e-02
## 2021-08-31 -2.005483e-03  0.0155756623  0.0143635657  0.018084019  2.932554e-02
## 2021-09-30 -9.214583e-03 -0.0395030932 -0.0331577540 -0.014646663 -4.772634e-02
## 2021-10-29 -8.708432e-05  0.0106615322  0.0312958948  0.028206867  6.781130e-02
## 2021-11-30  2.704470e-03 -0.0417061648 -0.0464075548 -0.026519526 -8.067350e-03
## 2021-12-31 -3.593603e-03  0.0149583224  0.0429726115  0.040315701  4.521030e-02
## 2022-01-31 -2.018825e-02 -0.0002047545 -0.0370267653 -0.045021818 -5.418308e-02
## 2022-02-28 -1.152265e-02 -0.0441632762 -0.0348936815  0.022568474 -2.996136e-02
## 2022-03-31 -2.852506e-02 -0.0343962634  0.0051764963  0.004963339  3.690119e-02
## 2022-04-29 -3.880181e-02 -0.0633134901 -0.0697695730 -0.065900394 -9.186213e-02
## 2022-05-31  7.576793e-03  0.0061161041  0.0197624897  0.022894673  2.254569e-03
## 2022-06-30 -1.565185e-02 -0.0529542660 -0.0917494794 -0.093939189 -8.605932e-02
## 2022-07-29  2.506386e-02 -0.0034973025  0.0503967729  0.081812576  8.809091e-02
## 2022-08-31 -3.089481e-02 -0.0133520631 -0.0631193189 -0.042496820 -4.165785e-02
## 2022-09-30 -4.233392e-02 -0.1226133888 -0.0967538044 -0.110038647 -9.700210e-02
## 2022-10-31 -1.286732e-02 -0.0199805048  0.0572476645  0.134501412  7.814146e-02
## 2022-11-30  3.737729e-02  0.1448737381  0.1237041250  0.037355273  5.410122e-02
## 2022-12-30 -8.742996e-03 -0.0267289948 -0.0182952543 -0.067057685 -5.935537e-02
## 2023-01-31  3.276003e-02  0.0873631512  0.0862113097  0.112867138  6.098916e-02
## 2023-02-28 -2.701847e-02 -0.0786936984 -0.0312304105 -0.017467379 -2.546413e-02
## 2023-03-31  2.606940e-02  0.0316668558  0.0308108962 -0.067070023  3.640678e-02
## 2023-04-28  5.738653e-03 -0.0083978748  0.0289396471 -0.024225262  1.584888e-02
## 2023-05-31 -1.150899e-02 -0.0243159259 -0.0408956444 -0.037247980  4.605404e-03
## 2023-06-30 -3.689094e-03  0.0430486360  0.0437302568  0.081591691  6.278691e-02
## 2023-07-31 -1.535254e-04  0.0586599793  0.0266754783  0.058513067  3.220894e-02
## 2023-08-31 -6.333006e-03 -0.0685673490 -0.0401450615 -0.051387245 -1.638546e-02
## 2023-09-29 -2.623729e-02 -0.0316416702 -0.0371704416 -0.065000099 -4.859644e-02
## 2023-10-31 -1.583114e-02 -0.0334927947 -0.0294485330 -0.065659749 -2.194771e-02
## 2023-11-30  4.490579e-02  0.0750418345  0.0789845517  0.086841606  8.740968e-02
## 2023-12-29  3.630912e-02  0.0350323346  0.0521273007  0.125245678  4.464393e-02
## 2024-01-31 -1.512499e-03 -0.0463187983 -0.0045224161 -0.056069094  1.580103e-02
## 2024-02-29 -1.485561e-02  0.0408325878  0.0294254056  0.023427960  5.087057e-02
## 2024-03-28  8.984301e-03  0.0268919829  0.0332281663  0.033267598  3.217872e-02
## 2024-04-30 -2.509447e-02 -0.0021933378 -0.0329693590 -0.067214981 -4.115499e-02
## 2024-05-31  1.655772e-02  0.0193289287  0.0493632161  0.044578498  4.934208e-02
## 2024-06-28  8.815900e-03  0.0258433718 -0.0184362782 -0.027426544  3.467199e-02
## 2024-07-31  2.390370e-02  0.0084171962  0.0255857949  0.111469097  1.203652e-02
## 2024-08-30  1.450878e-02  0.0097313307  0.0320830856 -0.013890389  2.309676e-02
## 2024-09-30  1.324018e-02  0.0558253157  0.0078026286  0.009172770  2.078700e-02
## 2024-10-31 -2.550989e-02 -0.0312283091 -0.0541734661 -0.017711253 -8.963702e-03
## 2024-11-29  1.104842e-02 -0.0271366314 -0.0031607404  0.102273552  5.792310e-02
## 2024-12-30 -1.584133e-02 -0.0137702427 -0.0299455782 -0.074997011 -2.070951e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AGG          EEM          EFA          IJS         SPY
## AGG 0.0001942940 0.0002651849 0.0002509926 0.0001813495 0.000209118
## EEM 0.0002651849 0.0022992801 0.0016457968 0.0017858382 0.001357489
## EFA 0.0002509926 0.0016457968 0.0017941290 0.0019197080 0.001515584
## IJS 0.0001813495 0.0017858382 0.0019197080 0.0036919743 0.002065790
## SPY 0.0002091180 0.0013574889 0.0015155836 0.0020657900 0.001730244
# 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.03445726
# 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.001612505 0.01061599 0.007943308 0.01056795 0.003717506
rowSums(component_contribution)
## [1] 0.03445726
# 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.047 0.308 0.231 0.307 0.108
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 Ă— 2
##   asset contribution
##   <chr>        <dbl>
## 1 AGG          0.047
## 2 EEM          0.308
## 3 EFA          0.231
## 4 IJS          0.307
## 5 SPY          0.108

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.047 0.308 0.231 0.307 0.108

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

}


# Check the custom function
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 1, window = 24,
                                                            w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 Ă— 6
##   date         AGG   EEM   EFA   IJS   SPY
##   <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-01-30 0.039 0.372 0.256 0.245 0.088
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 2, window = 24,
                                                            w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 Ă— 6
##   date         AGG   EEM   EFA   IJS   SPY
##   <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-02-27 0.036 0.374 0.249 0.252 0.089
dump(list = c("calculate_component_contribution",
              "calculate_comp_contrib_by_window"),
     file = "../00_scripts/calculate_comp_contrib_to_portfolio_volatility.R")
# Iterate the custom function
w <- c(0.25,0.25,0.2,0.2,0.1)
window <- 24

rolling_comp_contrib_tbl <- 1:(nrow(asset_returns_wide_tbl) - window) %>%

    map_df(.x = ., .f = ~calculate_comp_contrib_by_window(asset_returns_wide_tbl,
                                                          start = .x,
                                                          weights = w,
                                                          window = window))
rolling_comp_contrib_tbl
## # A tibble: 120 Ă— 6
##    date         AGG   EEM   EFA   IJS   SPY
##    <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
##  1 2015-01-30 0.039 0.372 0.256 0.245 0.088
##  2 2015-02-27 0.036 0.374 0.249 0.252 0.089
##  3 2015-03-31 0.027 0.37  0.255 0.255 0.092
##  4 2015-04-30 0.027 0.372 0.256 0.252 0.093
##  5 2015-05-29 0.024 0.385 0.254 0.246 0.092
##  6 2015-06-30 0.018 0.383 0.248 0.257 0.094
##  7 2015-07-31 0.014 0.375 0.253 0.261 0.097
##  8 2015-08-31 0.013 0.404 0.237 0.257 0.09 
##  9 2015-09-30 0.012 0.407 0.248 0.238 0.094
## 10 2015-10-30 0.003 0.405 0.244 0.243 0.105
## # ℹ 110 more rows
# Figure 10.3 Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%

    # Transform data to long form
    pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%

    # Plot
    ggplot(aes(date, contribution, color = asset)) +
    geom_line() +

    scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
    scale_y_continuous(labels = scales::percent_format()) +

    annotate(geom = "text",
             x = as.Date("2016-07-01"),
             y = 0.03,
             color = "red", size = 5,
             label = str_glue("AGG dips below zero sometimes, indicating
                              it reduces the portfolio volatility."))

# Figure 10.4 Stacked Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%

    # Transform data to long form
    pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%

    # Plot
    ggplot(aes(date, contribution, fill = asset)) +
    geom_area() +

    scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
    scale_y_continuous(labels = scales::percent_format()) +

    annotate(geom = "text",
             x = as.Date("2016-07-01"),
             y = 0.08,
             color = "red", size = 5,
             label = str_glue("AGG dips below zero sometimes, indicating
                              it reduces the portfolio volatility."))

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? EEM and IJS both equally contribute the most to the portfolios volatility. I don’t think my portfolio is overly concentrated in one asset, as it is pretty evenly distributed.