# 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("WMT", "AMZN", "KO", "PG", "GE")

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
##                     AMZN            GE            KO            PG
## 2013-01-31  0.0566799395  0.0596431169  0.0269441137  0.1098058550
## 2013-02-28 -0.0046435024  0.0494736985  0.0389727881  0.0134796347
## 2013-03-28  0.0083654162 -0.0043159371  0.0506757076  0.0114854815
## 2013-04-30 -0.0487507497 -0.0365598125  0.0456769201  0.0035493154
## 2013-05-31  0.0588686246  0.0451733933 -0.0568667554 -0.0001298608
## 2013-06-28  0.0310507506  0.0023643784  0.0098846397  0.0029917136
## 2013-07-31  0.0813355350  0.0496317337 -0.0007483379  0.0495566745
## 2013-08-30 -0.0695574090 -0.0517899682 -0.0485655621 -0.0304723511
## 2013-09-30  0.1067688897  0.0395690264 -0.0006420593 -0.0299733225
## 2013-10-31  0.1521839116  0.0900069893  0.0436477965  0.0738222211
## 2013-11-29  0.0781496860  0.0196978272  0.0225279247  0.0420739484
## 2013-12-31  0.0130490386  0.0581697137  0.0274862565 -0.0339339582
## 2014-01-31 -0.1059765119 -0.1092132193 -0.0882662830 -0.0531036679
## 2014-02-28  0.0094619003  0.0221379404  0.0099971869  0.0262769016
## 2014-03-31 -0.0737086161  0.0163555913  0.0198617435  0.0243634686
## 2014-04-30 -0.1007565303  0.0378974791  0.0536319174  0.0318635919
## 2014-05-30  0.0273091844 -0.0037257391  0.0029372408 -0.0215512943
## 2014-06-30  0.0383836202 -0.0110054204  0.0423225638 -0.0276082115
## 2014-07-31 -0.0369768154 -0.0439499642 -0.0752344416 -0.0083356178
## 2014-08-29  0.0799468404  0.0324688655  0.0600103215  0.0722124305
## 2014-09-30 -0.0502010184 -0.0055437636  0.0295403256  0.0075520232
## 2014-10-31 -0.0540982347  0.0073885895 -0.0184533886  0.0489303275
## 2014-11-28  0.1031187277  0.0260053924  0.0749573238  0.0355693518
## 2014-12-31 -0.0872368614 -0.0377786426 -0.0599838382  0.0072716147
## 2015-01-30  0.1330922557 -0.0561583942 -0.0251838309 -0.0704972613
## 2015-02-27  0.0697992426  0.0934029178  0.0504428924  0.0099163853
## 2015-03-31 -0.0214295755 -0.0464649845 -0.0573531543 -0.0381922268
## 2015-04-30  0.1253212736  0.0875483192  0.0002463483 -0.0220933296
## 2015-05-29  0.0175090293  0.0069921027  0.0098137115 -0.0141860422
## 2015-06-30  0.0112589814 -0.0175346474 -0.0349381111 -0.0019158060
## 2015-07-31  0.2111621090 -0.0178475370  0.0460798239 -0.0117239066
## 2015-08-31 -0.0443525782 -0.0502853515 -0.0437883562 -0.0818810602
## 2015-09-30 -0.0019516837  0.0248972665  0.0287680016  0.0178118220
## 2015-10-30  0.2010808743  0.1368959384  0.0540934517  0.0688358941
## 2015-11-30  0.0602956777  0.0346616233  0.0139949447 -0.0203683633
## 2015-12-31  0.0165440008  0.0470712248  0.0079460004  0.0592719304
## 2016-01-29 -0.1410054620 -0.0680764784 -0.0009317095  0.0369856223
## 2016-02-29 -0.0605352209  0.0093473319  0.0048809469 -0.0172867751
## 2016-03-31  0.0717834363  0.0870403542  0.0806335761  0.0248475568
## 2016-04-29  0.1053453760 -0.0332621288 -0.0348758892 -0.0188089055
## 2016-05-31  0.0915002899 -0.0170547780 -0.0044744690  0.0114176655
## 2016-06-30 -0.0099694639  0.0480646274  0.0238750853  0.0438184505
## 2016-07-29  0.0586021229 -0.0108590431 -0.0382245303  0.0186123394
## 2016-08-31  0.0135476418  0.0032057081 -0.0045942641  0.0198968146
## 2016-09-30  0.0848953908 -0.0454747856 -0.0177541656  0.0275629474
## 2016-10-31 -0.0583893058 -0.0177117406  0.0018888731 -0.0257302441
## 2016-11-30 -0.0509721927  0.0554767948 -0.0411385337 -0.0512932826
## 2016-12-30 -0.0009330556  0.0344398294  0.0271374699  0.0194555360
## 2017-01-31  0.0936394059 -0.0620102217  0.0026501303  0.0489062212
## 2017-02-28  0.0258446800  0.0116415339  0.0093376688  0.0388475148
## 2017-03-31  0.0479423007 -0.0003357140  0.0201621348 -0.0134870143
## 2017-04-28  0.0424566944 -0.0275575531  0.0165913498 -0.0208193529
## 2017-05-31  0.0725778018 -0.0571378756  0.0523702384  0.0086648681
## 2017-06-30 -0.0271286156 -0.0052052825 -0.0055332648 -0.0107278284
## 2017-07-31  0.0202278808 -0.0532244329  0.0218331770  0.0490629986
## 2017-08-31 -0.0072953953 -0.0422710049 -0.0063460995  0.0158398147
## 2017-09-29 -0.0198260355 -0.0052438179 -0.0040008237 -0.0140794493
## 2017-10-31  0.1395154056 -0.1818256122  0.0213219141 -0.0448811236
## 2017-11-30  0.0626577318 -0.0973460636  0.0035878693  0.0414055969
## 2017-12-29 -0.0062057845 -0.0401339490  0.0024005661  0.0207847890
## 2018-01-31  0.2156265497 -0.0761817930  0.0365929066 -0.0545956889
## 2018-02-28  0.0415536279 -0.1280320299 -0.0963190908 -0.0949397994
## 2018-03-29 -0.0440034760 -0.0456767146  0.0136356687  0.0096325758
## 2018-04-30  0.0788803060  0.0428377693 -0.0050783712 -0.0823977162
## 2018-05-31  0.0397392430  0.0007103436 -0.0048717744  0.0114078245
## 2018-06-29  0.0421636787 -0.0251139703  0.0286696688  0.0646927771
## 2018-07-31  0.0446635734  0.0014685730  0.0612407481  0.0445233280
## 2018-08-31  0.1243079079 -0.0519501476 -0.0451829229  0.0252717866
## 2018-09-28 -0.0048359814 -0.1269197287  0.0441722434  0.0033698191
## 2018-10-31 -0.2258869989 -0.1113818105  0.0359348386  0.0722236617
## 2018-11-30  0.0560700324 -0.2976322720  0.0591693616  0.0636715278
## 2018-12-31 -0.1180514843  0.0106645479 -0.0624242379 -0.0277874129
## 2019-01-31  0.1348080312  0.2942652239  0.0163389266  0.0561919023
## 2019-02-28 -0.0469930640  0.0616059697 -0.0597160541  0.0213318331
## 2019-03-29  0.0824420184 -0.0382003083  0.0416665492  0.0543077488
## 2019-04-30  0.0786806224  0.0178577138  0.0458797216  0.0301200445
## 2019-05-31 -0.0818753491 -0.0744861610  0.0014257850 -0.0341022188
## 2019-06-28  0.0646557767  0.1073811767  0.0435620825  0.0634385794
## 2019-07-31 -0.0142806686 -0.0047732342  0.0330305735  0.0801853294
## 2019-08-30 -0.0496880810 -0.2363886725  0.0447740484  0.0183829383
## 2019-09-30 -0.0229951159  0.0814025444 -0.0036764248  0.0339346289
## 2019-10-31  0.0232034080  0.1100477554 -0.0001838942  0.0074123421
## 2019-11-29  0.0134958216  0.1215612906 -0.0116627406 -0.0198731868
## 2019-12-31  0.0257863501 -0.0089022585  0.0358658221  0.0230006549
## 2020-01-31  0.0834803026  0.1093844935  0.0536393384  0.0036791636
## 2020-02-28 -0.0642332026 -0.1347941573 -0.0878211327 -0.0958478877
## 2020-03-31  0.0344213022 -0.3140204561 -0.1809074615 -0.0289406008
## 2020-04-30  0.2381504762 -0.1549907256  0.0363915221  0.0757486533
## 2020-05-29 -0.0128673719 -0.0344087278  0.0170687976 -0.0166821578
## 2020-06-30  0.1218341331  0.0403088378 -0.0347459350  0.0310016298
## 2020-07-31  0.1372488933 -0.1179660198  0.0557151815  0.0985015614
## 2020-08-31  0.0866005735  0.0435202090  0.0473373002  0.0535292188
## 2020-09-30 -0.0916533253 -0.0158509719  0.0048264545  0.0047598888
## 2020-10-30 -0.0364089187  0.1748028358 -0.0268924149 -0.0081542293
## 2020-11-30  0.0425228214  0.3162459305  0.0788816564  0.0128276295
## 2020-12-31  0.0276719582  0.0600407223  0.0608981742  0.0019423189
## 2021-01-29 -0.0156985929 -0.0111733925 -0.1300984227 -0.0757972967
## 2021-02-26 -0.0359675607  0.1605507441  0.0172949536 -0.0371853582
## 2021-03-31  0.0003717151  0.0467131537  0.0814776444  0.0919709722
## 2021-04-30  0.1139202399 -0.0007616573  0.0238086497 -0.0085756576
## 2021-05-28 -0.0730764715  0.0691960527  0.0239784077  0.0106612852
## 2021-06-30  0.0651836137 -0.0428509506 -0.0140661519  0.0005930703
## 2021-07-30 -0.0332696301 -0.0386265590  0.0525581446  0.0589589941
## 2021-08-31  0.0421339363  0.0173201573 -0.0127052828  0.0011241265
## 2021-09-30 -0.0550034409 -0.0220597685 -0.0631117424 -0.0183567408
## 2021-10-29  0.0262547651  0.0177011811  0.0716956387  0.0287418163
## 2021-11-30  0.0391473463 -0.0989499365 -0.0643519796  0.0110582169
## 2021-12-31 -0.0505062029 -0.0046141627  0.1212302978  0.1234694175
## 2022-01-31 -0.1085098142  0.0001057567  0.0299472049 -0.0139352060
## 2022-02-28  0.0263230079  0.0108428767  0.0199601209 -0.0288315389
## 2022-03-31  0.0596239190 -0.0419940771  0.0037621984 -0.0200207750
## 2022-04-29 -0.2711856801 -0.2048688061  0.0412348219  0.0550700022
## 2022-05-31 -0.0333130879  0.0489498850 -0.0192208115 -0.0822041538
## 2022-06-30 -0.1238178226 -0.2055132365 -0.0002442438 -0.0280472763
## 2022-07-29  0.2394860591  0.1491346873  0.0198306296 -0.0280567312
## 2022-08-31 -0.0625299224 -0.0063794061 -0.0390899045 -0.0070075750
## 2022-09-30 -0.1149865758 -0.1695469553 -0.0894728891 -0.0885546558
## 2022-10-31 -0.0981105335  0.2285881777  0.0661328247  0.0716350710
## 2022-11-30 -0.0593198454  0.0997284238  0.0679963508  0.1021924547
## 2022-12-30 -0.1391406409 -0.0247189139  0.0000000000  0.0159618226
## 2023-01-31  0.2051735029  0.2073360463 -0.0366646164 -0.0562264450
## 2023-02-28 -0.0902516639  0.0512246707 -0.0299617430 -0.0344394087
## 2023-03-31  0.0918019370  0.1218659690  0.0491150713  0.0778033901
## 2023-04-28  0.0206963031  0.0346439920  0.0336058912  0.0566666362
## 2023-05-31  0.1340765703  0.0255373955 -0.0725622403 -0.0929468422
## 2023-06-30  0.0779864104  0.0787615838  0.0169297942  0.0628265249
## 2023-07-31  0.0251489708  0.0399250781  0.0280000966  0.0358893303
## 2023-08-31  0.0318772770  0.0019237991 -0.0344974353 -0.0126190125
## 2023-09-29 -0.0821945627 -0.0340383230 -0.0586102843 -0.0565108180
## 2023-10-31  0.0458940197 -0.0175202303  0.0090692147  0.0344799012
## 2023-11-30  0.0931972815  0.1144328110  0.0418680594  0.0229955170
## 2023-12-29  0.0392628771  0.0473829214  0.0083495674 -0.0465324084
## 2024-01-31  0.0212288659  0.0368432707  0.0094580981  0.0761343132
## 2024-02-29  0.1300782611  0.1695663037  0.0088696122  0.0113898576
## 2024-03-28  0.0202729146  0.1122648134  0.0271092510  0.0206113629
## 2024-04-30 -0.0302797899  0.1459930090  0.0095974443  0.0122743955
## 2024-05-31  0.0081949152  0.0203086936  0.0186051498  0.0081773382
## 2024-06-28  0.0910037984 -0.0380782232  0.0191057946  0.0023069373
## 2024-07-31 -0.0329830511  0.0699501883  0.0474051408 -0.0195517732
## 2024-08-30 -0.0464130352  0.0256378393  0.0823681227  0.0649038060
## 2024-09-30  0.0429307038  0.0783902905 -0.0016205468  0.0096307056
## 2024-10-31  0.0003755644 -0.0933078378 -0.0955746612 -0.0415489792
## 2024-11-29  0.1091142212  0.0586708047 -0.0114568172  0.0818014514
## 2024-12-31  0.0538418744 -0.0865221511 -0.0288132355 -0.0669587923
## 2025-01-31  0.0800742352  0.1992744595  0.0194057677 -0.0038717266
## 2025-02-28 -0.1130190481  0.0166122721  0.1149083268  0.0462078194
## 2025-03-31 -0.1095146233 -0.0316962414  0.0130936604 -0.0198693176
## 2025-04-30 -0.0311757737  0.0069207729  0.0129015920 -0.0409432661
## 2025-05-30  0.1058429765  0.1989777780 -0.0062219068  0.0440423600
## 2025-06-30  0.0677922335  0.0456268674 -0.0118167672 -0.0642363493
## 2025-07-31  0.0649401278  0.0532806713 -0.0412638026 -0.0503356864
## 2025-08-29 -0.0220690894  0.0150843049  0.0160728325  0.0427367754
## 2025-09-30 -0.0420508819  0.0902350688 -0.0318301032 -0.0218232396
## 2025-10-31  0.1063983418  0.0266673874  0.0381646939 -0.0146097704
## 2025-11-18 -0.0929176542 -0.0427862205  0.0331174948 -0.0227342964
##                      WMT
## 2013-01-31  0.0248962829
## 2013-02-28  0.0117956313
## 2013-03-28  0.0620730435
## 2013-04-30  0.0378941285
## 2013-05-31 -0.0317803295
## 2013-06-28 -0.0046876906
## 2013-07-31  0.0452745145
## 2013-08-30 -0.0596998784
## 2013-09-30  0.0133390644
## 2013-10-31  0.0370289706
## 2013-11-29  0.0540192843
## 2013-12-31 -0.0232521491
## 2014-01-31 -0.0523039801
## 2014-02-28  0.0002676062
## 2014-03-31  0.0293267303
## 2014-04-30  0.0420193293
## 2014-05-30 -0.0314089752
## 2014-06-30 -0.0223929152
## 2014-07-31 -0.0200482005
## 2014-08-29  0.0323262908
## 2014-09-30  0.0127655423
## 2014-10-31 -0.0026185928
## 2014-11-28  0.1378161349
## 2014-12-31 -0.0135738444
## 2015-01-30 -0.0105351958
## 2015-02-27 -0.0124326410
## 2015-03-31 -0.0142312702
## 2015-04-30 -0.0524135744
## 2015-05-29 -0.0433514751
## 2015-06-30 -0.0460136419
## 2015-07-31  0.0146946927
## 2015-08-31 -0.0993582682
## 2015-09-30  0.0016980599
## 2015-10-30 -0.1246698284
## 2015-11-30  0.0275687911
## 2015-12-31  0.0492990864
## 2016-01-29  0.0793149329
## 2016-02-29 -0.0003018338
## 2016-03-31  0.0392708570
## 2016-04-29 -0.0239375024
## 2016-05-31  0.0641213898
## 2016-06-30  0.0311567244
## 2016-07-29 -0.0006848284
## 2016-08-31 -0.0143683068
## 2016-09-30  0.0094732829
## 2016-10-31 -0.0295502630
## 2016-11-30  0.0058382210
## 2016-12-30 -0.0116434891
## 2017-01-31 -0.0350393600
## 2017-02-28  0.0608890364
## 2017-03-31  0.0234089548
## 2017-04-28  0.0421086305
## 2017-05-31  0.0511562734
## 2017-06-30 -0.0378581393
## 2017-07-31  0.0553879304
## 2017-08-31 -0.0180255867
## 2017-09-29  0.0008959959
## 2017-10-31  0.1109632734
## 2017-11-30  0.1076142941
## 2017-12-29  0.0207684518
## 2018-01-31  0.0764918605
## 2018-02-28 -0.1691624181
## 2018-03-29 -0.0056773693
## 2018-04-30 -0.0057488783
## 2018-05-31 -0.0629872802
## 2018-06-29  0.0369863584
## 2018-07-31  0.0409481881
## 2018-08-31  0.0774626772
## 2018-09-28 -0.0205520748
## 2018-10-31  0.0656294572
## 2018-11-30 -0.0265766697
## 2018-12-31 -0.0417362029
## 2019-01-31  0.0283645993
## 2019-02-28  0.0324430238
## 2019-03-29 -0.0094925735
## 2019-04-30  0.0530143798
## 2019-05-31 -0.0084088859
## 2019-06-28  0.0854577979
## 2019-07-31 -0.0009962490
## 2019-08-30  0.0394583267
## 2019-09-30  0.0379537685
## 2019-10-31 -0.0120367278
## 2019-11-29  0.0154857685
## 2019-12-31  0.0023737850
## 2020-01-31 -0.0372901288
## 2020-02-28 -0.0613237877
## 2020-03-31  0.0581104259
## 2020-04-30  0.0674661309
## 2020-05-29  0.0248286983
## 2020-06-30 -0.0351083922
## 2020-07-31  0.0772515564
## 2020-08-31  0.0745887502
## 2020-09-30  0.0076052540
## 2020-10-30 -0.0083255454
## 2020-11-30  0.0963904022
## 2020-12-31 -0.0545611733
## 2021-01-29 -0.0257182708
## 2021-02-26 -0.0782174860
## 2021-03-31  0.0486520563
## 2021-04-30  0.0295950993
## 2021-05-28  0.0189582659
## 2021-06-30 -0.0071364907
## 2021-07-30  0.0107910814
## 2021-08-31  0.0418681246
## 2021-09-30 -0.0606838507
## 2021-10-29  0.0695571208
## 2021-11-30 -0.0606285078
## 2021-12-31  0.0324792020
## 2022-01-31 -0.0343092140
## 2022-02-28 -0.0338249582
## 2022-03-31  0.1008100101
## 2022-04-29  0.0269633038
## 2022-05-31 -0.1698040278
## 2022-06-30 -0.0563675767
## 2022-07-29  0.0826081408
## 2022-08-31  0.0081250562
## 2022-09-30 -0.0217357743
## 2022-10-31  0.0929243086
## 2022-11-30  0.0684914533
## 2022-12-30 -0.0685301354
## 2023-01-31  0.0145631794
## 2023-02-28 -0.0121679643
## 2023-03-31  0.0408373838
## 2023-04-28  0.0235917782
## 2023-05-31 -0.0237417342
## 2023-06-30  0.0678438710
## 2023-07-31  0.0169066403
## 2023-08-31  0.0206050267
## 2023-09-29 -0.0166185065
## 2023-10-31  0.0215261236
## 2023-11-30 -0.0483955635
## 2023-12-29  0.0162177792
## 2024-01-31  0.0470821970
## 2024-02-29  0.0620580273
## 2024-03-28  0.0296686019
## 2024-04-30 -0.0137218293
## 2024-05-31  0.1060149718
## 2024-06-28  0.0292222590
## 2024-07-31  0.0136414898
## 2024-08-30  0.1207589850
## 2024-09-30  0.0445700475
## 2024-10-31  0.0147511919
## 2024-11-29  0.1210993655
## 2024-12-31 -0.0213044751
## 2025-01-31  0.0829078191
## 2025-02-28  0.0045738551
## 2025-03-31 -0.1134827222
## 2025-04-30  0.1023373715
## 2025-05-30  0.0174174596
## 2025-06-30 -0.0095675353
## 2025-07-31  0.0020433725
## 2025-08-29 -0.0079257195
## 2025-09-30  0.0608065129
## 2025-10-31 -0.0184102073
## 2025-11-18  0.0020733491
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AMZN           GE           KO           PG          WMT
## AMZN 0.0071325413 0.0018405605 0.0003852710 0.0002190491 0.0008797123
## GE   0.0018405605 0.0087338809 0.0011317576 0.0006541303 0.0005712993
## KO   0.0003852710 0.0011317576 0.0020507307 0.0012015450 0.0008412343
## PG   0.0002190491 0.0006541303 0.0012015450 0.0020192513 0.0010309272
## WMT  0.0008797123 0.0005712993 0.0008412343 0.0010309272 0.0027180618
# 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.04352336
# 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
##            AMZN         GE          KO          PG         WMT
## [1,] 0.01408503 0.01756481 0.005118335 0.004436913 0.002318276
rowSums(component_contribution)
## [1] 0.04352336
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AMZN    GE    KO    PG   WMT
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.324 0.404 0.118 0.102 0.053
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.324
## 2 GE           0.404
## 3 KO           0.118
## 4 PG           0.102
## 5 WMT          0.053

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

asset_returns_wide_tbl
##                     AMZN            GE            KO            PG
## 2013-01-31  0.0566799395  0.0596431169  0.0269441137  0.1098058550
## 2013-02-28 -0.0046435024  0.0494736985  0.0389727881  0.0134796347
## 2013-03-28  0.0083654162 -0.0043159371  0.0506757076  0.0114854815
## 2013-04-30 -0.0487507497 -0.0365598125  0.0456769201  0.0035493154
## 2013-05-31  0.0588686246  0.0451733933 -0.0568667554 -0.0001298608
## 2013-06-28  0.0310507506  0.0023643784  0.0098846397  0.0029917136
## 2013-07-31  0.0813355350  0.0496317337 -0.0007483379  0.0495566745
## 2013-08-30 -0.0695574090 -0.0517899682 -0.0485655621 -0.0304723511
## 2013-09-30  0.1067688897  0.0395690264 -0.0006420593 -0.0299733225
## 2013-10-31  0.1521839116  0.0900069893  0.0436477965  0.0738222211
## 2013-11-29  0.0781496860  0.0196978272  0.0225279247  0.0420739484
## 2013-12-31  0.0130490386  0.0581697137  0.0274862565 -0.0339339582
## 2014-01-31 -0.1059765119 -0.1092132193 -0.0882662830 -0.0531036679
## 2014-02-28  0.0094619003  0.0221379404  0.0099971869  0.0262769016
## 2014-03-31 -0.0737086161  0.0163555913  0.0198617435  0.0243634686
## 2014-04-30 -0.1007565303  0.0378974791  0.0536319174  0.0318635919
## 2014-05-30  0.0273091844 -0.0037257391  0.0029372408 -0.0215512943
## 2014-06-30  0.0383836202 -0.0110054204  0.0423225638 -0.0276082115
## 2014-07-31 -0.0369768154 -0.0439499642 -0.0752344416 -0.0083356178
## 2014-08-29  0.0799468404  0.0324688655  0.0600103215  0.0722124305
## 2014-09-30 -0.0502010184 -0.0055437636  0.0295403256  0.0075520232
## 2014-10-31 -0.0540982347  0.0073885895 -0.0184533886  0.0489303275
## 2014-11-28  0.1031187277  0.0260053924  0.0749573238  0.0355693518
## 2014-12-31 -0.0872368614 -0.0377786426 -0.0599838382  0.0072716147
## 2015-01-30  0.1330922557 -0.0561583942 -0.0251838309 -0.0704972613
## 2015-02-27  0.0697992426  0.0934029178  0.0504428924  0.0099163853
## 2015-03-31 -0.0214295755 -0.0464649845 -0.0573531543 -0.0381922268
## 2015-04-30  0.1253212736  0.0875483192  0.0002463483 -0.0220933296
## 2015-05-29  0.0175090293  0.0069921027  0.0098137115 -0.0141860422
## 2015-06-30  0.0112589814 -0.0175346474 -0.0349381111 -0.0019158060
## 2015-07-31  0.2111621090 -0.0178475370  0.0460798239 -0.0117239066
## 2015-08-31 -0.0443525782 -0.0502853515 -0.0437883562 -0.0818810602
## 2015-09-30 -0.0019516837  0.0248972665  0.0287680016  0.0178118220
## 2015-10-30  0.2010808743  0.1368959384  0.0540934517  0.0688358941
## 2015-11-30  0.0602956777  0.0346616233  0.0139949447 -0.0203683633
## 2015-12-31  0.0165440008  0.0470712248  0.0079460004  0.0592719304
## 2016-01-29 -0.1410054620 -0.0680764784 -0.0009317095  0.0369856223
## 2016-02-29 -0.0605352209  0.0093473319  0.0048809469 -0.0172867751
## 2016-03-31  0.0717834363  0.0870403542  0.0806335761  0.0248475568
## 2016-04-29  0.1053453760 -0.0332621288 -0.0348758892 -0.0188089055
## 2016-05-31  0.0915002899 -0.0170547780 -0.0044744690  0.0114176655
## 2016-06-30 -0.0099694639  0.0480646274  0.0238750853  0.0438184505
## 2016-07-29  0.0586021229 -0.0108590431 -0.0382245303  0.0186123394
## 2016-08-31  0.0135476418  0.0032057081 -0.0045942641  0.0198968146
## 2016-09-30  0.0848953908 -0.0454747856 -0.0177541656  0.0275629474
## 2016-10-31 -0.0583893058 -0.0177117406  0.0018888731 -0.0257302441
## 2016-11-30 -0.0509721927  0.0554767948 -0.0411385337 -0.0512932826
## 2016-12-30 -0.0009330556  0.0344398294  0.0271374699  0.0194555360
## 2017-01-31  0.0936394059 -0.0620102217  0.0026501303  0.0489062212
## 2017-02-28  0.0258446800  0.0116415339  0.0093376688  0.0388475148
## 2017-03-31  0.0479423007 -0.0003357140  0.0201621348 -0.0134870143
## 2017-04-28  0.0424566944 -0.0275575531  0.0165913498 -0.0208193529
## 2017-05-31  0.0725778018 -0.0571378756  0.0523702384  0.0086648681
## 2017-06-30 -0.0271286156 -0.0052052825 -0.0055332648 -0.0107278284
## 2017-07-31  0.0202278808 -0.0532244329  0.0218331770  0.0490629986
## 2017-08-31 -0.0072953953 -0.0422710049 -0.0063460995  0.0158398147
## 2017-09-29 -0.0198260355 -0.0052438179 -0.0040008237 -0.0140794493
## 2017-10-31  0.1395154056 -0.1818256122  0.0213219141 -0.0448811236
## 2017-11-30  0.0626577318 -0.0973460636  0.0035878693  0.0414055969
## 2017-12-29 -0.0062057845 -0.0401339490  0.0024005661  0.0207847890
## 2018-01-31  0.2156265497 -0.0761817930  0.0365929066 -0.0545956889
## 2018-02-28  0.0415536279 -0.1280320299 -0.0963190908 -0.0949397994
## 2018-03-29 -0.0440034760 -0.0456767146  0.0136356687  0.0096325758
## 2018-04-30  0.0788803060  0.0428377693 -0.0050783712 -0.0823977162
## 2018-05-31  0.0397392430  0.0007103436 -0.0048717744  0.0114078245
## 2018-06-29  0.0421636787 -0.0251139703  0.0286696688  0.0646927771
## 2018-07-31  0.0446635734  0.0014685730  0.0612407481  0.0445233280
## 2018-08-31  0.1243079079 -0.0519501476 -0.0451829229  0.0252717866
## 2018-09-28 -0.0048359814 -0.1269197287  0.0441722434  0.0033698191
## 2018-10-31 -0.2258869989 -0.1113818105  0.0359348386  0.0722236617
## 2018-11-30  0.0560700324 -0.2976322720  0.0591693616  0.0636715278
## 2018-12-31 -0.1180514843  0.0106645479 -0.0624242379 -0.0277874129
## 2019-01-31  0.1348080312  0.2942652239  0.0163389266  0.0561919023
## 2019-02-28 -0.0469930640  0.0616059697 -0.0597160541  0.0213318331
## 2019-03-29  0.0824420184 -0.0382003083  0.0416665492  0.0543077488
## 2019-04-30  0.0786806224  0.0178577138  0.0458797216  0.0301200445
## 2019-05-31 -0.0818753491 -0.0744861610  0.0014257850 -0.0341022188
## 2019-06-28  0.0646557767  0.1073811767  0.0435620825  0.0634385794
## 2019-07-31 -0.0142806686 -0.0047732342  0.0330305735  0.0801853294
## 2019-08-30 -0.0496880810 -0.2363886725  0.0447740484  0.0183829383
## 2019-09-30 -0.0229951159  0.0814025444 -0.0036764248  0.0339346289
## 2019-10-31  0.0232034080  0.1100477554 -0.0001838942  0.0074123421
## 2019-11-29  0.0134958216  0.1215612906 -0.0116627406 -0.0198731868
## 2019-12-31  0.0257863501 -0.0089022585  0.0358658221  0.0230006549
## 2020-01-31  0.0834803026  0.1093844935  0.0536393384  0.0036791636
## 2020-02-28 -0.0642332026 -0.1347941573 -0.0878211327 -0.0958478877
## 2020-03-31  0.0344213022 -0.3140204561 -0.1809074615 -0.0289406008
## 2020-04-30  0.2381504762 -0.1549907256  0.0363915221  0.0757486533
## 2020-05-29 -0.0128673719 -0.0344087278  0.0170687976 -0.0166821578
## 2020-06-30  0.1218341331  0.0403088378 -0.0347459350  0.0310016298
## 2020-07-31  0.1372488933 -0.1179660198  0.0557151815  0.0985015614
## 2020-08-31  0.0866005735  0.0435202090  0.0473373002  0.0535292188
## 2020-09-30 -0.0916533253 -0.0158509719  0.0048264545  0.0047598888
## 2020-10-30 -0.0364089187  0.1748028358 -0.0268924149 -0.0081542293
## 2020-11-30  0.0425228214  0.3162459305  0.0788816564  0.0128276295
## 2020-12-31  0.0276719582  0.0600407223  0.0608981742  0.0019423189
## 2021-01-29 -0.0156985929 -0.0111733925 -0.1300984227 -0.0757972967
## 2021-02-26 -0.0359675607  0.1605507441  0.0172949536 -0.0371853582
## 2021-03-31  0.0003717151  0.0467131537  0.0814776444  0.0919709722
## 2021-04-30  0.1139202399 -0.0007616573  0.0238086497 -0.0085756576
## 2021-05-28 -0.0730764715  0.0691960527  0.0239784077  0.0106612852
## 2021-06-30  0.0651836137 -0.0428509506 -0.0140661519  0.0005930703
## 2021-07-30 -0.0332696301 -0.0386265590  0.0525581446  0.0589589941
## 2021-08-31  0.0421339363  0.0173201573 -0.0127052828  0.0011241265
## 2021-09-30 -0.0550034409 -0.0220597685 -0.0631117424 -0.0183567408
## 2021-10-29  0.0262547651  0.0177011811  0.0716956387  0.0287418163
## 2021-11-30  0.0391473463 -0.0989499365 -0.0643519796  0.0110582169
## 2021-12-31 -0.0505062029 -0.0046141627  0.1212302978  0.1234694175
## 2022-01-31 -0.1085098142  0.0001057567  0.0299472049 -0.0139352060
## 2022-02-28  0.0263230079  0.0108428767  0.0199601209 -0.0288315389
## 2022-03-31  0.0596239190 -0.0419940771  0.0037621984 -0.0200207750
## 2022-04-29 -0.2711856801 -0.2048688061  0.0412348219  0.0550700022
## 2022-05-31 -0.0333130879  0.0489498850 -0.0192208115 -0.0822041538
## 2022-06-30 -0.1238178226 -0.2055132365 -0.0002442438 -0.0280472763
## 2022-07-29  0.2394860591  0.1491346873  0.0198306296 -0.0280567312
## 2022-08-31 -0.0625299224 -0.0063794061 -0.0390899045 -0.0070075750
## 2022-09-30 -0.1149865758 -0.1695469553 -0.0894728891 -0.0885546558
## 2022-10-31 -0.0981105335  0.2285881777  0.0661328247  0.0716350710
## 2022-11-30 -0.0593198454  0.0997284238  0.0679963508  0.1021924547
## 2022-12-30 -0.1391406409 -0.0247189139  0.0000000000  0.0159618226
## 2023-01-31  0.2051735029  0.2073360463 -0.0366646164 -0.0562264450
## 2023-02-28 -0.0902516639  0.0512246707 -0.0299617430 -0.0344394087
## 2023-03-31  0.0918019370  0.1218659690  0.0491150713  0.0778033901
## 2023-04-28  0.0206963031  0.0346439920  0.0336058912  0.0566666362
## 2023-05-31  0.1340765703  0.0255373955 -0.0725622403 -0.0929468422
## 2023-06-30  0.0779864104  0.0787615838  0.0169297942  0.0628265249
## 2023-07-31  0.0251489708  0.0399250781  0.0280000966  0.0358893303
## 2023-08-31  0.0318772770  0.0019237991 -0.0344974353 -0.0126190125
## 2023-09-29 -0.0821945627 -0.0340383230 -0.0586102843 -0.0565108180
## 2023-10-31  0.0458940197 -0.0175202303  0.0090692147  0.0344799012
## 2023-11-30  0.0931972815  0.1144328110  0.0418680594  0.0229955170
## 2023-12-29  0.0392628771  0.0473829214  0.0083495674 -0.0465324084
## 2024-01-31  0.0212288659  0.0368432707  0.0094580981  0.0761343132
## 2024-02-29  0.1300782611  0.1695663037  0.0088696122  0.0113898576
## 2024-03-28  0.0202729146  0.1122648134  0.0271092510  0.0206113629
## 2024-04-30 -0.0302797899  0.1459930090  0.0095974443  0.0122743955
## 2024-05-31  0.0081949152  0.0203086936  0.0186051498  0.0081773382
## 2024-06-28  0.0910037984 -0.0380782232  0.0191057946  0.0023069373
## 2024-07-31 -0.0329830511  0.0699501883  0.0474051408 -0.0195517732
## 2024-08-30 -0.0464130352  0.0256378393  0.0823681227  0.0649038060
## 2024-09-30  0.0429307038  0.0783902905 -0.0016205468  0.0096307056
## 2024-10-31  0.0003755644 -0.0933078378 -0.0955746612 -0.0415489792
## 2024-11-29  0.1091142212  0.0586708047 -0.0114568172  0.0818014514
## 2024-12-31  0.0538418744 -0.0865221511 -0.0288132355 -0.0669587923
## 2025-01-31  0.0800742352  0.1992744595  0.0194057677 -0.0038717266
## 2025-02-28 -0.1130190481  0.0166122721  0.1149083268  0.0462078194
## 2025-03-31 -0.1095146233 -0.0316962414  0.0130936604 -0.0198693176
## 2025-04-30 -0.0311757737  0.0069207729  0.0129015920 -0.0409432661
## 2025-05-30  0.1058429765  0.1989777780 -0.0062219068  0.0440423600
## 2025-06-30  0.0677922335  0.0456268674 -0.0118167672 -0.0642363493
## 2025-07-31  0.0649401278  0.0532806713 -0.0412638026 -0.0503356864
## 2025-08-29 -0.0220690894  0.0150843049  0.0160728325  0.0427367754
## 2025-09-30 -0.0420508819  0.0902350688 -0.0318301032 -0.0218232396
## 2025-10-31  0.1063983418  0.0266673874  0.0381646939 -0.0146097704
## 2025-11-18 -0.0929176542 -0.0427862205  0.0331174948 -0.0227342964
##                      WMT
## 2013-01-31  0.0248962829
## 2013-02-28  0.0117956313
## 2013-03-28  0.0620730435
## 2013-04-30  0.0378941285
## 2013-05-31 -0.0317803295
## 2013-06-28 -0.0046876906
## 2013-07-31  0.0452745145
## 2013-08-30 -0.0596998784
## 2013-09-30  0.0133390644
## 2013-10-31  0.0370289706
## 2013-11-29  0.0540192843
## 2013-12-31 -0.0232521491
## 2014-01-31 -0.0523039801
## 2014-02-28  0.0002676062
## 2014-03-31  0.0293267303
## 2014-04-30  0.0420193293
## 2014-05-30 -0.0314089752
## 2014-06-30 -0.0223929152
## 2014-07-31 -0.0200482005
## 2014-08-29  0.0323262908
## 2014-09-30  0.0127655423
## 2014-10-31 -0.0026185928
## 2014-11-28  0.1378161349
## 2014-12-31 -0.0135738444
## 2015-01-30 -0.0105351958
## 2015-02-27 -0.0124326410
## 2015-03-31 -0.0142312702
## 2015-04-30 -0.0524135744
## 2015-05-29 -0.0433514751
## 2015-06-30 -0.0460136419
## 2015-07-31  0.0146946927
## 2015-08-31 -0.0993582682
## 2015-09-30  0.0016980599
## 2015-10-30 -0.1246698284
## 2015-11-30  0.0275687911
## 2015-12-31  0.0492990864
## 2016-01-29  0.0793149329
## 2016-02-29 -0.0003018338
## 2016-03-31  0.0392708570
## 2016-04-29 -0.0239375024
## 2016-05-31  0.0641213898
## 2016-06-30  0.0311567244
## 2016-07-29 -0.0006848284
## 2016-08-31 -0.0143683068
## 2016-09-30  0.0094732829
## 2016-10-31 -0.0295502630
## 2016-11-30  0.0058382210
## 2016-12-30 -0.0116434891
## 2017-01-31 -0.0350393600
## 2017-02-28  0.0608890364
## 2017-03-31  0.0234089548
## 2017-04-28  0.0421086305
## 2017-05-31  0.0511562734
## 2017-06-30 -0.0378581393
## 2017-07-31  0.0553879304
## 2017-08-31 -0.0180255867
## 2017-09-29  0.0008959959
## 2017-10-31  0.1109632734
## 2017-11-30  0.1076142941
## 2017-12-29  0.0207684518
## 2018-01-31  0.0764918605
## 2018-02-28 -0.1691624181
## 2018-03-29 -0.0056773693
## 2018-04-30 -0.0057488783
## 2018-05-31 -0.0629872802
## 2018-06-29  0.0369863584
## 2018-07-31  0.0409481881
## 2018-08-31  0.0774626772
## 2018-09-28 -0.0205520748
## 2018-10-31  0.0656294572
## 2018-11-30 -0.0265766697
## 2018-12-31 -0.0417362029
## 2019-01-31  0.0283645993
## 2019-02-28  0.0324430238
## 2019-03-29 -0.0094925735
## 2019-04-30  0.0530143798
## 2019-05-31 -0.0084088859
## 2019-06-28  0.0854577979
## 2019-07-31 -0.0009962490
## 2019-08-30  0.0394583267
## 2019-09-30  0.0379537685
## 2019-10-31 -0.0120367278
## 2019-11-29  0.0154857685
## 2019-12-31  0.0023737850
## 2020-01-31 -0.0372901288
## 2020-02-28 -0.0613237877
## 2020-03-31  0.0581104259
## 2020-04-30  0.0674661309
## 2020-05-29  0.0248286983
## 2020-06-30 -0.0351083922
## 2020-07-31  0.0772515564
## 2020-08-31  0.0745887502
## 2020-09-30  0.0076052540
## 2020-10-30 -0.0083255454
## 2020-11-30  0.0963904022
## 2020-12-31 -0.0545611733
## 2021-01-29 -0.0257182708
## 2021-02-26 -0.0782174860
## 2021-03-31  0.0486520563
## 2021-04-30  0.0295950993
## 2021-05-28  0.0189582659
## 2021-06-30 -0.0071364907
## 2021-07-30  0.0107910814
## 2021-08-31  0.0418681246
## 2021-09-30 -0.0606838507
## 2021-10-29  0.0695571208
## 2021-11-30 -0.0606285078
## 2021-12-31  0.0324792020
## 2022-01-31 -0.0343092140
## 2022-02-28 -0.0338249582
## 2022-03-31  0.1008100101
## 2022-04-29  0.0269633038
## 2022-05-31 -0.1698040278
## 2022-06-30 -0.0563675767
## 2022-07-29  0.0826081408
## 2022-08-31  0.0081250562
## 2022-09-30 -0.0217357743
## 2022-10-31  0.0929243086
## 2022-11-30  0.0684914533
## 2022-12-30 -0.0685301354
## 2023-01-31  0.0145631794
## 2023-02-28 -0.0121679643
## 2023-03-31  0.0408373838
## 2023-04-28  0.0235917782
## 2023-05-31 -0.0237417342
## 2023-06-30  0.0678438710
## 2023-07-31  0.0169066403
## 2023-08-31  0.0206050267
## 2023-09-29 -0.0166185065
## 2023-10-31  0.0215261236
## 2023-11-30 -0.0483955635
## 2023-12-29  0.0162177792
## 2024-01-31  0.0470821970
## 2024-02-29  0.0620580273
## 2024-03-28  0.0296686019
## 2024-04-30 -0.0137218293
## 2024-05-31  0.1060149718
## 2024-06-28  0.0292222590
## 2024-07-31  0.0136414898
## 2024-08-30  0.1207589850
## 2024-09-30  0.0445700475
## 2024-10-31  0.0147511919
## 2024-11-29  0.1210993655
## 2024-12-31 -0.0213044751
## 2025-01-31  0.0829078191
## 2025-02-28  0.0045738551
## 2025-03-31 -0.1134827222
## 2025-04-30  0.1023373715
## 2025-05-30  0.0174174596
## 2025-06-30 -0.0095675353
## 2025-07-31  0.0020433725
## 2025-08-29 -0.0079257195
## 2025-09-30  0.0608065129
## 2025-10-31 -0.0184102073
## 2025-11-18  0.0020733491
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]
    
    rowSums(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(0.25, 0.25, 0.2, 0.2, 0.1))
## # A tibble: 1 × 5
##    AMZN    GE    KO    PG   WMT
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.324 0.404 0.118 0.102 0.053
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 weight
    add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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? GE is the largest contributor to the portfolio volatility. I think the portfolio risk is concentrated in GE and AMZN more than the rest. This does make some sense because they have the largest weights of the portfolio.