# 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("AAPL", "GOOG", "VOO", "MSFT", "NFLX")

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

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

# 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          GOOG         MSFT          NFLX           VOO
## 2013-01-31 -1.555884e-01  0.0660635351  0.027327840  0.5792178352  0.0504032137
## 2013-02-28 -2.561142e-02  0.0584791519  0.020915355  0.1294686037  0.0131857692
## 2013-03-28  2.850870e-03 -0.0087878309  0.028720366  0.0063600533  0.0354874497
## 2013-04-30  2.707566e-04  0.0375394516  0.145776556  0.1323749474  0.0207219060
## 2013-05-31  2.217172e-02  0.0550324079  0.059941794  0.0460382575  0.0229769431
## 2013-06-28 -1.258960e-01  0.0104477504 -0.010369014 -0.0693561240 -0.0151925351
## 2013-07-31  1.321025e-01  0.0083478373 -0.081394653  0.1468489367  0.0516816892
## 2013-08-30  8.044307e-02 -0.0471074016  0.054854317  0.1495238797 -0.0313016725
## 2013-09-30 -2.172369e-02  0.0336807569 -0.003599384  0.0853634080  0.0333347677
## 2013-10-31  9.201512e-02  0.1626135454  0.062037571  0.0420203453  0.0437059645
## 2013-11-29  6.770791e-02  0.0277605408  0.081562434  0.1260456715  0.0295204274
## 2013-12-31  8.862933e-03  0.0560801585 -0.019063051  0.0064580000  0.0260315110
## 2014-01-31 -1.139495e-01  0.0523736432  0.011428385  0.1059768648 -0.0359320228
## 2014-02-28  5.591817e-02  0.0289428329  0.019815322  0.0849674837  0.0446435257
## 2014-03-31  1.975657e-02 -0.0863761589  0.067616929 -0.2357727088  0.0087659216
## 2014-04-30  9.476104e-02 -0.0559560391 -0.014498365 -0.0890406585  0.0072104104
## 2014-05-30  7.576490e-02  0.0611849817  0.020307645  0.2603989261  0.0226853026
## 2014-06-30  2.728638e-02  0.0271166127  0.018393808  0.0530628157  0.0206740459
## 2014-07-31  2.832647e-02 -0.0064174872  0.034413145 -0.0414274490 -0.0138611203
## 2014-08-29  7.465155e-02  0.0000000000  0.057485183  0.1221473944  0.0389572602
## 2014-09-30 -1.722054e-02  0.0100266335  0.020263825 -0.0569909733 -0.0139019615
## 2014-10-31  6.948923e-02 -0.0321734612  0.012646386 -0.1386422062  0.0237479960
## 2014-11-28  1.007305e-01 -0.0313402746  0.024438902 -0.1250816593  0.0272046296
## 2014-12-31 -7.460614e-02 -0.0288909280 -0.028858469 -0.0144727517 -0.0086143910
## 2015-01-30  5.961160e-02  0.0153078143 -0.139546565  0.2571875640 -0.0291357399
## 2015-02-27  9.601599e-02  0.0437063080  0.089036476  0.0722680237  0.0542943002
## 2015-03-31 -3.187419e-02 -0.0188001047 -0.075530190 -0.1307829000 -0.0158322204
## 2015-04-30  5.769612e-03 -0.0169025699  0.179201555  0.2893246603  0.0099920844
## 2015-05-29  4.434128e-02 -0.0097808117 -0.030803836  0.1145793884  0.0124292393
## 2015-06-30 -3.793844e-02 -0.0220411444 -0.059571450  0.0513461588 -0.0196619775
## 2015-07-31 -3.348057e-02  0.1839180305  0.056151468  0.1972314899  0.0215312931
## 2015-08-31 -6.848897e-02 -0.0118341623 -0.063950995  0.0062789518 -0.0633270219
## 2015-09-30 -2.205785e-02 -0.0160275526  0.016859971 -0.1079428863 -0.0249649338
## 2015-10-30  8.011236e-02  0.1555397216  0.173395590  0.0483934975  0.0811324266
## 2015-11-30 -5.821022e-03  0.0437524505  0.038685802  0.1292201068  0.0042415771
## 2015-12-31 -1.167906e-01  0.0216861118  0.020577931 -0.0753374900 -0.0175351614
## 2016-01-29 -7.822339e-02 -0.0212149552 -0.007054368 -0.2194782802 -0.0503560633
## 2016-02-29 -1.288649e-03 -0.0627391036 -0.072344049  0.0169504292 -0.0020838157
## 2016-03-31  1.197464e-01  0.0654275161  0.082036506  0.0902267716  0.0664745721
## 2016-04-29 -1.507315e-01 -0.0722727464 -0.102086844 -0.1270821580  0.0034942380
## 2016-05-31  6.931474e-02  0.0598051871  0.067842490  0.1304025124  0.0173936994
## 2016-06-30 -4.359637e-02 -0.0611192026 -0.035138403 -0.1144251460  0.0032173641
## 2016-07-29  8.623513e-02  0.1050872314  0.102267841 -0.0025173508  0.0361742258
## 2016-08-31  2.337647e-02 -0.0022655071  0.019880911  0.0657364217  0.0012039097
## 2016-09-30  6.344827e-02  0.0132613828  0.002433611  0.0112245729  0.0002903070
## 2016-10-31  4.324999e-03  0.0092839905  0.039487882  0.2367092462 -0.0180800360
## 2016-11-30 -2.183781e-02 -0.0343614281  0.012390837 -0.0650993149  0.0365797558
## 2016-12-30  4.684081e-02  0.0180151563  0.030721548  0.0564934510  0.0205085098
## 2017-01-31  4.664158e-02  0.0318399014  0.039598070  0.1280336724  0.0176696497
## 2017-02-28  1.255556e-01  0.0326200791 -0.004373306  0.0100411108  0.0380287850
## 2017-03-31  4.754146e-02  0.0076842212  0.028960594  0.0391855218  0.0013177509
## 2017-04-28 -6.975895e-05  0.0880996076  0.038718587  0.0292677885  0.0103460647
## 2017-05-31  6.560726e-02  0.0629879087  0.025672918  0.0689840448  0.0139465530
## 2017-06-30 -5.891568e-02 -0.0599348976 -0.013115527 -0.0874853040  0.0062766077
## 2017-07-31  3.218038e-02  0.0236741849  0.053249785  0.1954426244  0.0204153412
## 2017-08-31  1.016530e-01  0.0094446425  0.033389298 -0.0390093333  0.0029079018
## 2017-09-29 -6.213502e-02  0.0208388483 -0.003752036  0.0373013834  0.0202164484
## 2017-10-31  9.240398e-02  0.0582525010  0.110342607  0.0798772375  0.0230042953
## 2017-11-30  2.007530e-02  0.0046812014  0.016840395 -0.0461007257  0.0301184065
## 2017-12-29 -1.536359e-02  0.0241716279  0.016145912  0.0230815823  0.0127451447
## 2018-01-31 -1.069329e-02  0.1115966687  0.104997991  0.3422455062  0.0543483803
## 2018-02-28  6.596096e-02 -0.0573515840 -0.008450717  0.0750957953 -0.0379721849
## 2018-03-29 -5.980403e-02 -0.0683056700 -0.027023257  0.0135328146 -0.0249858567
## 2018-04-30 -1.513319e-02 -0.0141135490  0.024353619  0.0563153817  0.0034639201
## 2018-05-31  1.267417e-01  0.0643892016  0.059651797  0.1180176912  0.0238769525
## 2018-06-29 -9.462972e-03  0.0278662752 -0.002329406  0.1073125782  0.0075559701
## 2018-07-31  2.759902e-02  0.0871652798  0.073020726 -0.1483894202  0.0350100284
## 2018-08-31  1.826733e-01  0.0007636989  0.061088528  0.0857957175  0.0316904499
## 2018-09-28 -8.337545e-03 -0.0205011294  0.017997627  0.0173902455  0.0057448456
## 2018-10-31 -3.095184e-02 -0.1028989839 -0.068387250 -0.2149050241 -0.0708267180
## 2018-11-30 -1.999124e-01  0.0162676261  0.041798017 -0.0532519750  0.0186758454
## 2018-12-31 -1.240886e-01 -0.0552430053 -0.087790627 -0.0667287371 -0.0926329021
## 2019-01-31  5.368689e-02  0.0750917634  0.027768857  0.2377564232  0.0762162318
## 2019-02-28  4.380301e-02  0.0031748369  0.074511127  0.0533383459  0.0319818538
## 2019-03-29  9.260273e-02  0.0465716010  0.051409450 -0.0043098364  0.0190734189
## 2019-04-30  5.490068e-02  0.0128463272  0.101963237  0.0384589452  0.0395482099
## 2019-05-31 -1.326321e-01 -0.0740702328 -0.050746798 -0.0764150093 -0.0655836620
## 2019-06-28  1.226770e-01 -0.0208016471  0.079843892  0.0676869455  0.0675764380
## 2019-07-31  7.361703e-02  0.1183225419  0.017096816 -0.1286120040  0.0144955485
## 2019-08-30 -1.659842e-02 -0.0237705139  0.014924715 -0.0948922909 -0.0165414275
## 2019-09-30  7.042302e-02  0.0256755023  0.008451156 -0.0931610341  0.0195308140
## 2019-10-31  1.049761e-01  0.0331682185  0.030738711  0.0713417093  0.0215921511
## 2019-11-29  7.469361e-02  0.0349734187  0.057761847  0.0905829375  0.0356172974
## 2019-12-31  9.420440e-02  0.0242709802  0.040901189  0.0279227475  0.0293029099
## 2020-01-31  5.260153e-02  0.0701846461  0.076455888  0.0643897941 -0.0003718000
## 2020-02-28 -1.218305e-01 -0.0684586181 -0.046764874  0.0670727184 -0.0844656158
## 2020-03-31 -7.231412e-02 -0.1413299285 -0.026899979  0.0173805126 -0.1328622145
## 2020-04-30  1.444238e-01  0.1482720179  0.127800402  0.1116390683  0.1203229350
## 2020-05-29  8.166675e-02  0.0578072257  0.025074246 -0.0002858819  0.0462730685
## 2020-06-30  1.374867e-01 -0.0107721791  0.104863827  0.0807737255  0.0182535852
## 2020-07-31  1.528342e-01  0.0478937171  0.007343777  0.0717317079  0.0571507139
## 2020-08-31  1.960348e-01  0.0971008771  0.097808763  0.0799294267  0.0673876495
## 2020-09-30 -1.081716e-01 -0.1061508599 -0.069775500 -0.0573784336 -0.0382866625
## 2020-10-30 -6.188833e-02  0.0980589395 -0.038086123 -0.0497965496 -0.0257806867
## 2020-11-30  9.120489e-02  0.0826850039  0.058326264  0.0309614228  0.1038787808
## 2020-12-31  1.084719e-01 -0.0050447582  0.038264016  0.0970871760  0.0367600546
## 2021-01-29 -5.516434e-03  0.0467583185  0.041997752 -0.0155438392 -0.0102651462
## 2021-02-26 -8.306903e-02  0.1039615729  0.004109328  0.0120609342  0.0272863019
## 2021-03-31  7.312924e-03  0.0154772726  0.014482810 -0.0324211662  0.0447367068
## 2021-04-30  7.345285e-02  0.1527898867  0.067286329 -0.0158244342  0.0515440867
## 2021-05-28 -5.181681e-02  0.0005973254 -0.007656461 -0.0209792775  0.0066520679
## 2021-06-30  9.450015e-02  0.0385417455  0.081569758  0.0492815858  0.0223438198
## 2021-07-30  6.295816e-02  0.0760718784  0.050423519 -0.0203491346  0.0241769833
## 2021-08-31  4.161137e-02  0.0730043113  0.059768856  0.0950694939  0.0290901940
## 2021-09-30 -7.046187e-02 -0.0875713867 -0.068406723  0.0698019104 -0.0477590729
## 2021-10-29  5.700177e-02  0.1066950322  0.162366717  0.1231245610  0.0680188974
## 2021-11-30  9.991923e-02 -0.0400334861 -0.001282816 -0.0727081801 -0.0073701325
## 2021-12-31  7.160269e-02  0.0155159876  0.017183901 -0.0634444996  0.0445927793
## 2022-01-31 -1.583678e-02 -0.0640854020 -0.078334426 -0.3438762220 -0.0538318569
## 2022-02-28 -5.558245e-02 -0.0059686071 -0.037922253 -0.0794420172 -0.0302580641
## 2022-03-31  5.588267e-02  0.0346687796  0.031365138 -0.0518377428  0.0371917022
## 2022-04-29 -1.021776e-01 -0.1944949563 -0.105212689 -0.6769151054 -0.0919439087
## 2022-05-31 -5.603736e-02 -0.0081002444 -0.018242743  0.0365177351  0.0025844588
## 2022-06-30 -8.493681e-02 -0.0417810389 -0.056909562 -0.1213919016 -0.0862602394
## 2022-07-29  1.728047e-01  0.0643327114  0.089014522  0.2516130360  0.0880027841
## 2022-08-31 -3.170540e-02 -0.0663691943 -0.068989234 -0.0059759741 -0.0421659130
## 2022-09-30 -1.289440e-01 -0.1268135928 -0.115710310  0.0517762558 -0.0964984454
## 2022-10-31  1.039557e-01 -0.0156178793 -0.003311625  0.2148866190  0.0780488548
## 2022-11-30 -3.358528e-02  0.0692745859  0.097329187  0.0457052075  0.0535881295
## 2022-12-30 -1.304190e-01 -0.1339679603 -0.061923715 -0.0354794336 -0.0590328977
## 2023-01-31  1.048292e-01  0.1182710537  0.032773700  0.1823328761  0.0610028758
## 2023-02-28  2.291847e-02 -0.1007317504  0.008977266 -0.0939461699 -0.0253014696
## 2023-03-31  1.121214e-01  0.1412533729  0.144863502  0.0699795345  0.0364283215
## 2023-04-28  2.857485e-02  0.0397752952  0.063692739 -0.0460542525  0.0157761282
## 2023-05-31  4.502918e-02  0.1310219073  0.068691132  0.1805873321  0.0048046522
## 2023-06-30  9.014238e-02 -0.0196455001  0.036330821  0.1084198950  0.0630675798
## 2023-07-31  1.270440e-02  0.0956335037 -0.013659610 -0.0034566798  0.0323712927
## 2023-08-31 -4.330812e-02  0.0313565051 -0.022476329 -0.0121241234 -0.0164169300
## 2023-09-29 -9.285938e-02 -0.0408676111 -0.037330889 -0.1384715458 -0.0486016229
## 2023-10-31 -2.573212e-03 -0.0509540935  0.068420424  0.0864352180 -0.0219608550
## 2023-11-30  1.077598e-01  0.0665318264  0.115954990  0.1408734072  0.0877399392
## 2023-12-29  1.349107e-02  0.0510207203 -0.007603116  0.0268736655  0.0447688605
## 2024-01-31 -4.314472e-02  0.0061543712  0.055700656  0.1472315750  0.0159437082
## 2024-02-29 -1.871708e-02 -0.0143481181  0.041447365  0.0665352594  0.0507601538
## 2024-03-28 -5.264831e-02  0.0855200183  0.016971616  0.0072878139  0.0322748762
## 2024-04-30 -6.728906e-03  0.0781716710 -0.077540238 -0.0979911057 -0.0409130910
## 2024-05-31  1.224144e-01  0.0550641510  0.065966489  0.1529150450  0.0490347132
## 2024-06-28  9.125878e-02  0.0540904291  0.073855041  0.0505386138  0.0350499660
## 2024-07-31  5.298225e-02 -0.0576203059 -0.066128237 -0.0714375604  0.0115303112
## 2024-08-30  3.184107e-02 -0.0475463938 -0.001095873  0.1099097581  0.0236541927
## 2024-09-30  1.731659e-02  0.0138345793  0.031060581  0.0112292200  0.0215292456
## 2024-10-31 -3.090192e-02  0.0323670184 -0.057267528  0.0638447163 -0.0095206873
## 2024-11-29  5.041454e-02 -0.0128214226  0.043243815  0.1595497355  0.0572213509
## 2024-12-31  5.368800e-02  0.1117944529 -0.004639362  0.0050728010 -0.0236131297
## 2025-01-31 -5.930776e-02  0.0765954185 -0.015396713  0.0915374451  0.0265917685
## 2025-02-28  2.554339e-02 -0.1771598371 -0.042508861  0.0038828370 -0.0127313583
## 2025-03-31 -8.501353e-02 -0.0963048741 -0.055945476 -0.0502225296 -0.0577140681
## 2025-04-30 -4.432085e-02  0.0293915058  0.051578150  0.1935925475 -0.0081473833
## 2025-05-30 -5.507304e-02  0.0717033712  0.154299173  0.0645899020  0.0609222505
## 2025-06-30  2.128055e-02  0.0271278104  0.077405910  0.1036916319  0.0504337177
## 2025-07-31  1.162973e-02  0.0836138275  0.070044452 -0.1441174565  0.0226111154
## 2025-08-29  1.130062e-01  0.1018127670 -0.049917206  0.0412703555  0.0205440340
## 2025-09-30  9.247086e-02  0.1324383789  0.021979388 -0.0077518692  0.0348795224
## 2025-10-31  5.997989e-02  0.1459463949 -0.000270493 -0.0691108133  0.0236573170
## 2025-11-28  3.185163e-02  0.1274272801 -0.049239348 -0.0392457361  0.0021824400
## 2025-12-31 -2.538687e-02 -0.0192879324 -0.017199527 -0.1374964253  0.0007870270
## 2026-01-30 -4.660755e-02  0.0758569663 -0.116840091 -0.1160115100  0.0143905423
## 2026-02-27  1.888623e-02 -0.0834381650 -0.089031733  0.1421182109 -0.0081751581
## 2026-03-31 -4.012348e-02 -0.0814761422 -0.059185407 -0.0009355616 -0.0513795459
## 2026-04-30  6.690241e-02  0.2862693112  0.096765312 -0.0267722660  0.1002802576
## 2026-05-29  1.407068e-01 -0.0145314204  0.101216581 -0.0845574336  0.0514983071
## 2026-06-30 -7.552386e-02 -0.0627280388 -0.188148815 -0.1862818976 -0.0096510334
## 2026-07-31  6.537844e-02  0.0093525153  0.219803114  0.0043323044 -0.0002329243
## 2026-08-31  2.624055e-02 -0.0614012657  0.089528560  0.1224360907  0.0262169982
## 2026-09-30  4.977405e-02  0.0164150504  0.010998059 -0.1525890898 -0.0031656145
## 2026-10-30  3.426886e-02 -0.2027075224  0.017414121 -0.1140532907 -0.0086697081
## 2026-11-27  4.032839e-02 -0.0209937567 -0.055360281 -0.0186982361 -0.0366742258
calculate_component_contribution <- function(.data, w) {
    
        # Covariance of asset returns
    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
    
    # 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(.1, .45, .2, .15, .1))
## # A tibble: 1 × 5
##    AAPL  GOOG  MSFT  NFLX   VOO
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.074 0.492  0.16 0.221 0.054

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
    
    # transform to long form
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # add weights
    add_column(weight = c(.1, .45, .2, .15, .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 = .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?

NFLX contributes the most volatility in this portfolio. I think you could make an argument that the risk is concentrated in NFLX but that could be fixed by shifting the weights and having less weight in NFLX. NFLX contributes around 10% more than AAPL and GOOG.