# 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

symbols <- c("SPY", "EFA", "BB", "JBLU", "ARKK")

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

2 Convert prices to returns (monthly)

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 Calculate Component Contribution to Portfolio Volatility

asset_returns_wide_tbl <- asset_returns_tbl %>%

    pivot_wider(names_from = asset, values_from = returns) %>%

    column_to_rownames(var = "date")

asset_returns_wide_tbl
##                     ARKK           BB           EFA          JBLU           SPY
## 2014-11-28  0.0102514583 -0.010531452  0.0006252274  0.2372549540  2.710127e-02
## 2014-12-31 -0.0211051951  0.055231631 -0.0407467496  0.0807260012 -2.539817e-03
## 2015-01-30  0.0039605951 -0.078601731  0.0062265084  0.0569833144 -3.007711e-02
## 2015-02-27  0.0608569697  0.062997926  0.0614506478  0.0235443469  5.468185e-02
## 2015-03-31 -0.0318836834 -0.191055237 -0.0143889049  0.1131831832 -1.582986e-02
## 2015-04-30  0.0057429824  0.129042047  0.0358165979  0.0643762190  9.785473e-03
## 2015-05-29  0.0301311160 -0.036076056  0.0019528441 -0.0181868365  1.277423e-02
## 2015-06-30 -0.0114565741 -0.180690235 -0.0316788440  0.0293276151 -2.052132e-02
## 2015-07-31  0.0070010258 -0.055290463  0.0201144055  0.1015962141  2.233778e-02
## 2015-08-31 -0.0673328782 -0.019570096 -0.0771524349 -0.0291411349 -6.288651e-02
## 2015-09-30 -0.0673950757 -0.213636841 -0.0451948211  0.1437278872 -2.584718e-02
## 2015-10-30  0.0573948324  0.173308796  0.0640258457 -0.0367557676  8.163521e-02
## 2015-11-30  0.0632814505  0.085409729 -0.0075559244 -0.0040338901  3.648385e-03
## 2015-12-31 -0.0134219142  0.155948272 -0.0235950396 -0.0882615150 -1.743354e-02
## 2016-01-29 -0.2006706312 -0.264953821 -0.0567577679 -0.0609834527 -5.106879e-02
## 2016-02-29  0.0212772937  0.092497238 -0.0339140832  0.0318660542 -8.261283e-04
## 2016-03-31  0.1121793040  0.035223767  0.0637459735 -0.0408219472  6.510000e-02
## 2016-04-29 -0.0131546201 -0.136183680  0.0219749569 -0.0650436960  3.933629e-03
## 2016-05-31  0.0317984811  0.029311240 -0.0008558983 -0.0987015225  1.686845e-02
## 2016-06-30 -0.0103147278 -0.080157341 -0.0244915310 -0.0794851990  3.469921e-03
## 2016-07-29  0.0652186864  0.124549296  0.0390002285  0.1015489733  3.582171e-02
## 2016-08-31  0.0011648777 -0.001316656  0.0053268056 -0.1390802326  1.197001e-03
## 2016-09-30  0.0732206291  0.050106820  0.0132790819  0.0777734360  5.811958e-05
## 2016-10-31 -0.1076174766 -0.123910795 -0.0224037681  0.0138251050 -1.748925e-02
## 2016-11-30  0.0096437705  0.089490571 -0.0179745038  0.1391648086  3.617614e-02
## 2016-12-30 -0.0029882225 -0.112447103  0.0267030978  0.1097312388  2.006890e-02
## 2017-01-31  0.0969412506  0.024373966  0.0323816796 -0.1339137264  1.773660e-02
## 2017-02-28  0.0457439793 -0.014265577  0.0118365251  0.0176905296  3.853931e-02
## 2017-03-31  0.0354714265  0.107513369  0.0318056821  0.0320462226  1.249240e-03
## 2017-04-28  0.0492671019  0.186613409  0.0239522302  0.0575087271  9.877087e-03
## 2017-05-31  0.1189741736  0.123713548  0.0348102875  0.0266682471  1.401420e-02
## 2017-06-30  0.0209431829 -0.056435207  0.0029558483  0.0181220429  6.354856e-03
## 2017-07-31  0.0229471211 -0.063004830  0.0261879254 -0.0402198981  2.034578e-02
## 2017-08-31  0.1420109656 -0.011796383 -0.0004484375 -0.1016687522  2.913398e-03
## 2017-09-29  0.0042669760  0.187343088  0.0233427933 -0.0667957160  1.994915e-02
## 2017-10-31  0.0350189734 -0.021700671  0.0166537774  0.0329116214  2.329059e-02
## 2017-11-30  0.0464945147 -0.015661306  0.0068699603  0.1143538497  3.010806e-02
## 2017-12-29  0.0096059183  0.036467122  0.0133982698  0.0397222283  1.205508e-02
## 2018-01-31  0.1063308397  0.125215804  0.0489866771 -0.0685452961  5.482828e-02
## 2018-02-28 -0.0080340998 -0.041941631 -0.0495554504  0.0090670151 -3.703789e-02
## 2018-03-29 -0.0460196162 -0.054158750 -0.0084317399 -0.0352948899 -2.779332e-02
## 2018-04-30  0.0005118438 -0.093833010  0.0150978282 -0.0572162606  5.154953e-03
## 2018-05-31  0.1064342673  0.122124653 -0.0191242241 -0.0157567336  2.401831e-02
## 2018-06-29  0.0339168612 -0.203680763 -0.0159679488  0.0047531646  5.734077e-03
## 2018-07-31 -0.0096058265  0.016444358  0.0281211778 -0.0530140353  3.637728e-02
## 2018-08-31  0.1093928730  0.084033792 -0.0226115462  0.0582689081  3.142096e-02
## 2018-09-28 -0.0486492790  0.064421363  0.0096062691  0.0145684675  5.927661e-03
## 2018-10-31 -0.1059709122 -0.207233877 -0.0848342154 -0.1460056185 -7.160801e-02
## 2018-11-30  0.0429803060 -0.051008840  0.0049509127  0.1542360660  1.837955e-02
## 2018-12-31 -0.1467002467 -0.212112468 -0.0549818397 -0.1951079347 -9.216855e-02
## 2019-01-31  0.1463224112  0.126651238  0.0642408158  0.1134844017  7.702168e-02
## 2019-02-28  0.0783795741  0.075169543  0.0250504673 -0.0744072687  3.190165e-02
## 2019-03-29  0.0036444721  0.148221809  0.0091381932 -0.0205693870  1.793877e-02
## 2019-04-30  0.0104312581 -0.094517630  0.0288730090  0.1256303428  4.003991e-02
## 2019-05-31 -0.1478526612 -0.156513673 -0.0516402309 -0.0738176846 -6.589547e-02
## 2019-06-28  0.1638192982 -0.050958118  0.0574350202  0.0705779949  6.727220e-02
## 2019-07-31  0.0099545161 -0.021681066 -0.0196658065  0.0392415142  1.500630e-02
## 2019-08-30 -0.0893027434 -0.060710242 -0.0194271725 -0.1046096565 -1.688486e-02
## 2019-09-30 -0.0327972999 -0.268936030  0.0311503447 -0.0334636449  1.927071e-02
## 2019-10-31  0.0307645795  0.001902950  0.0333289390  0.1417067858  2.186390e-02
## 2019-11-29  0.1311829283  0.057264027  0.0112095584 -0.0015555617  3.555839e-02
## 2019-12-31 -0.0038675181  0.142023064  0.0295139716 -0.0289570648  2.864149e-02
## 2020-01-31  0.0343676417 -0.051129347 -0.0286316192  0.0576035249 -4.038359e-04
## 2020-02-28  0.0198783786 -0.165416083 -0.0808334222 -0.2284526271 -8.247531e-02
## 2020-03-31 -0.1830788855 -0.224595282 -0.1520642190 -0.5670897831 -1.333843e-01
## 2020-04-30  0.2291256333  0.035675603  0.0565450593  0.0845875854  1.195447e-01
## 2020-05-29  0.1269582301  0.080761357  0.0528476450  0.0333195891  4.654481e-02
## 2020-06-30  0.1267631168  0.052477937  0.0345095920  0.0792020825  1.757909e-02
## 2020-07-31  0.1196044790 -0.031155168  0.0192000606 -0.0527429202  5.722327e-02
## 2020-08-31  0.1707034918  0.096460266  0.0461390881  0.1080647862  6.746870e-02
## 2020-09-30 -0.0355559755 -0.128617378 -0.0206801507 -0.0166305802 -3.816269e-02
## 2020-10-30 -0.0132393901 -0.022027322 -0.0361523964  0.0549494445 -2.524965e-02
## 2020-11-30  0.2132478402  0.268001932  0.1333887574  0.2316287532  1.032576e-01
## 2020-12-31  0.1183009556  0.121750170  0.0489369140 -0.0371288007  3.637835e-02
## 2021-01-29  0.0989621007  0.754569993 -0.0078431899 -0.0138506369 -1.024266e-02
## 2021-02-26 -0.0528876330 -0.338602163  0.0221321451  0.2509269365  2.742601e-02
## 2021-03-31 -0.0832248935 -0.175775862  0.0248211632  0.0986096189  4.439893e-02
## 2021-04-30  0.0068129214  0.046358243  0.0290966885  0.0009828502  5.155810e-02
## 2021-05-28 -0.0744965427  0.131405692  0.0342300305 -0.0128524257  6.544557e-03
## 2021-06-30  0.1541252129  0.193513247 -0.0108386334 -0.1805320544  2.217969e-02
## 2021-07-30 -0.0860247677 -0.184615512  0.0077034648 -0.1262364839  2.411924e-02
## 2021-08-31  0.0167751340  0.116031722  0.0143635673  0.0227282511  2.932572e-02
## 2021-09-30 -0.0989799219 -0.159276268 -0.0331576261  0.0105194921 -4.772647e-02
## 2021-10-29  0.0928149370  0.104332238  0.0312958251 -0.0860011258  6.781137e-02
## 2021-11-30 -0.1375916148 -0.126151285 -0.0464076354 -0.0444517626 -8.067257e-03
## 2021-12-31 -0.1027370530 -0.018018506  0.0429726450  0.0593087744  4.521022e-02
## 2022-01-31 -0.2263466461 -0.127590329 -0.0370268651  0.0270193090 -5.418300e-02
## 2022-02-28 -0.0680179723 -0.180621908 -0.0348934896  0.0428159042 -2.996138e-02
## 2022-03-31 -0.0611480302  0.082391308  0.0051763877 -0.0211788194  3.690106e-02
## 2022-04-29 -0.3411293092 -0.265586609 -0.0697695948 -0.3059073491 -9.186208e-02
## 2022-05-31 -0.0666767628  0.056089467  0.0197626294 -0.0248288617  2.254734e-03
## 2022-06-30 -0.1003580292 -0.115512887 -0.0917495313 -0.2493212046 -8.605957e-02
## 2022-07-29  0.1236722658  0.128649365  0.0503967324  0.0059559438  8.809096e-02
## 2022-08-31 -0.0747388261 -0.031485617 -0.0631193613 -0.0777689684 -4.165775e-02
## 2022-09-30 -0.1043528759 -0.234146625 -0.0967537366 -0.1612360557 -9.700206e-02
## 2022-10-31  0.0144720065 -0.008547061  0.0572477322  0.1928242790  7.814140e-02
## 2022-11-29 -0.0953101824  0.037899273  0.1061188910 -0.0380273956  2.308666e-02
## 2013-01-31            NA  0.089395503  0.0366062071  0.0156117655  4.992313e-02
## 2013-02-28            NA  0.028855457 -0.0129692988  0.0404777012  1.267811e-02
## 2013-03-28            NA  0.078429246  0.0129692988  0.1314631396  3.726787e-02
## 2013-04-30            NA  0.119857069  0.0489677909 -0.0014503266  1.902982e-02
## 2013-05-31            NA -0.154355387 -0.0306555409 -0.1039101891  2.333529e-02
## 2013-06-28            NA -0.288637639 -0.0271442085  0.0128001748 -1.343388e-02
## 2013-07-31            NA -0.168271582  0.0518600165  0.0389760948  5.038586e-02
## 2013-08-30            NA  0.135226787 -0.0197463049 -0.0614850837 -3.045118e-02
## 2013-09-30            NA -0.241341735  0.0753385931  0.0811677781  3.115581e-02
## 2013-10-31            NA -0.002518893  0.0320817229  0.0610654806  4.526679e-02
## 2013-11-29            NA -0.225352799  0.0054497373  0.2262417090  2.920643e-02
## 2013-12-31            NA  0.161570613  0.0215277893 -0.0401660417  2.559646e-02
## 2014-01-31            NA  0.239143893 -0.0534132495  0.0254348971 -3.588430e-02
## 2014-02-28            NA  0.056570351  0.0595051994  0.0079591097  4.451028e-02
## 2014-03-31            NA -0.213193220 -0.0046026350 -0.0159820753  8.261085e-03
## 2014-04-30            NA -0.053379889  0.0165293064 -0.0940451575  6.927694e-03
## 2014-05-30            NA -0.007863736  0.0158286069  0.1998658664  2.294118e-02
## 2014-06-30            NA  0.298153372  0.0091654429  0.1161714318  2.043488e-02
## 2014-07-31            NA -0.093066605 -0.0263800316 -0.0120539243 -1.352922e-02
## 2014-08-29            NA  0.087189996  0.0018005080  0.1317807941  3.870512e-02
## 2014-09-30            NA -0.023857990 -0.0395984830 -0.1411529339 -1.389257e-02
## 2014-10-31            NA  0.054808236 -0.0026547278  0.0830802453  2.327807e-02
calculate_component_contribution <- function (.data, w) {
        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 in percentage
    component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
        round(3) %>%
        as_tibble()
    
    (component_percentages)
}

asset_returns_wide_tbl %>% calculate_component_contribution(w = c(.25, .25, .2, .2, .1))
## # A tibble: 1 × 5
##    ARKK    BB   EFA  JBLU   SPY
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1    NA    NA    NA    NA    NA

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>% 
    
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform to long form
    pivot_longer(col = everything(), names_to = "Asset", values_to = "Contribution") %>%

    # Add weights
    add_column(weight = c(.25, .25, .2, .2, .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 Volatility and Weight", y = "Percent", x = NULL)

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?

Both Arkk and Blackberry are my most volatile stocks that disproportionately leads to an increase in the portfolio risk. While Arkk and Blackberry both add to the overall risk of the portfolio, the rest of the portfolio is almsot as volitle.