# 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("AMZN", "TGT", "WMT", "COST")

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          COST           TGT           WMT
## 2013-01-31  0.0566799395  0.0359115455  0.0207399364  0.0248960688
## 2013-02-28 -0.0046435024 -0.0076473037  0.0470674482  0.0117956325
## 2013-03-28  0.0083654162  0.0464886570  0.0836038042  0.0620733444
## 2013-04-30 -0.0487507497  0.0216286367  0.0303600624  0.0378940266
## 2013-05-31  0.0588686246  0.0137929552 -0.0099613248 -0.0317803265
## 2013-06-28  0.0310507506  0.0085376620 -0.0092510482 -0.0046876901
## 2013-07-31  0.0813355350  0.0601082787  0.0341191112  0.0452746039
## 2013-08-30 -0.0695574090 -0.0458220116 -0.1118618013 -0.0596996679
## 2013-09-30  0.1067688897  0.0290719119  0.0105272481  0.0133387646
## 2013-10-31  0.1521839116  0.0242752566  0.0125808934  0.0370288724
## 2013-11-29  0.0781496860  0.0635978940 -0.0069133143  0.0540191050
## 2013-12-31  0.0130490386 -0.0524564677 -0.0103774224 -0.0232521533
## 2014-01-31 -0.1059765119 -0.0575831618 -0.1106957294 -0.0523035066
## 2014-02-28  0.0094619003  0.0414543698  0.1066818482  0.0002677993
## 2014-03-31 -0.0737086161 -0.0448254145 -0.0329977351  0.0293258718
## 2014-04-30 -0.1007565303  0.0351900442  0.0202855679  0.0420197047
## 2014-05-30  0.0273091844  0.0059923438 -0.0769025779 -0.0314091608
## 2014-06-30  0.0383836202 -0.0074403423  0.0207487873 -0.0223929194
## 2014-07-31 -0.0369768154  0.0234833295  0.0279072224 -0.0200477199
## 2014-08-29  0.0799468404  0.0296726395  0.0169976887  0.0323258126
## 2014-09-30 -0.0502010184  0.0344192863  0.0425320320  0.0127659151
## 2014-10-31 -0.0540982347  0.0622564977 -0.0138156469 -0.0026188708
## 2014-11-28  0.1031187277  0.0661385347  0.1875000034  0.1378161469
## 2014-12-31 -0.0872368614 -0.0026070460  0.0254836939 -0.0135737635
## 2015-01-30  0.1330922557  0.0087093095 -0.0307679023 -0.0105350301
## 2015-02-27  0.0697992426  0.0623775080  0.0496017752 -0.0124327229
## 2015-03-31 -0.0214295755  0.0304251494  0.0659774041 -0.0142316094
## 2015-04-30  0.1253212736 -0.0546596517 -0.0402784443 -0.0524135881
## 2015-05-29  0.0175090293 -0.0032206005  0.0128400110 -0.0433509251
## 2015-06-30  0.0112589814 -0.0542541314  0.0287062888 -0.0460137267
## 2015-07-31  0.2111621090  0.0730809853  0.0026918910  0.0146945932
## 2015-08-31 -0.0443525782 -0.0340549818 -0.0448221836 -0.0993581514
## 2015-09-30 -0.0019516837  0.0317638341  0.0121506387  0.0016980595
## 2015-10-30  0.2010808743  0.0895905747 -0.0189941673 -0.1246701622
## 2015-11-30  0.0602956777  0.0232363066 -0.0547335497  0.0275691466
## 2015-12-31  0.0165440008  0.0004957496  0.0015160112  0.0492988515
## 2016-01-29 -0.1410054620 -0.0664310133 -0.0026202421  0.0793153459
## 2016-02-29 -0.0605352209 -0.0045311042  0.0882419533 -0.0003022468
## 2016-03-31  0.0717834363  0.0490975754  0.0476672486  0.0392706584
## 2016-04-29  0.1053453760 -0.0588895486 -0.0343712346 -0.0239369987
## 2016-05-31  0.0915002899  0.0043113358 -0.1372355329  0.0641208939
## 2016-06-30 -0.0099694639  0.0540991743  0.0150075932  0.0311569152
## 2016-07-29  0.0586021229  0.0628101159  0.0759577817 -0.0006851060
## 2016-08-31  0.0135476418 -0.0284746953 -0.0627263681 -0.0143680293
## 2016-09-30  0.0848953908 -0.0609213373 -0.0217478055  0.0094736549
## 2016-10-31 -0.0583893058 -0.0308968415  0.0007275474 -0.0295510180
## 2016-11-30 -0.0509721927  0.0181077948  0.1251768485  0.0058387945
## 2016-12-30 -0.0009330556  0.0644927397 -0.0670620652 -0.0116436795
## 2017-01-31  0.0936394059  0.0237004206 -0.1135003841 -0.0350396593
## 2017-02-28  0.0258446800  0.0802945460 -0.0835534644  0.0608890541
## 2017-03-31  0.0479423007 -0.0550494211 -0.0628498255  0.0234092364
## 2017-04-28  0.0424566944  0.0569665704  0.0118878192  0.0421085426
## 2017-05-31  0.0725778018  0.0587793863 -0.0018018239  0.0511563613
## 2017-06-30 -0.0271286156 -0.1206065415 -0.0532517111 -0.0378580525
## 2017-07-31  0.0202278808 -0.0089185400  0.0804398049  0.0553875153
## 2017-08-31 -0.0072953953 -0.0080370469 -0.0272904794 -0.0180253420
## 2017-09-29 -0.0198260355  0.0470449248  0.0789562670  0.0008964969
## 2017-10-31  0.1395154056 -0.0197320993  0.0005081430  0.1109625570
## 2017-11-30  0.0626577318  0.1383318710  0.0247793608  0.1076144588
## 2017-12-29 -0.0062057845  0.0091216854  0.0855493775  0.0207687832
## 2018-01-31  0.2156265497  0.0459410500  0.1421908823  0.0764918460
## 2018-02-28  0.0415536279 -0.0179106349  0.0107468907 -0.1691628171
## 2018-03-29 -0.0440034760 -0.0130231753 -0.0826207532 -0.0056772980
## 2018-04-30  0.0788803060  0.0452887192  0.0446455444 -0.0057486603
## 2018-05-31  0.0397392430  0.0083749348  0.0125279558 -0.0629871978
## 2018-06-29  0.0421636787  0.0527599579  0.0433595380  0.0369860533
## 2018-07-31  0.0446635734  0.0455083478  0.0581797004  0.0409482660
## 2018-08-31  0.1243079079  0.0663288828  0.0889778214  0.0774627491
## 2018-09-28 -0.0048359814  0.0074783991  0.0080816082 -0.0205521426
## 2018-10-31 -0.2258869989 -0.0269696276 -0.0533181146  0.0656295250
## 2018-11-30  0.0560700324  0.0138981299 -0.1560245173 -0.0265766044
## 2018-12-31 -0.1180514843 -0.1269316926 -0.0710990427 -0.0417362002
## 2019-01-31  0.1348080312  0.0522184633  0.0994422772  0.0283645312
## 2019-02-28 -0.0469930640  0.0216653282  0.0038811671  0.0324430238
## 2019-03-29  0.0824420184  0.1016324877  0.0997562579 -0.0094925735
## 2019-04-30  0.0786806224  0.0139029976 -0.0360267023  0.0530141959
## 2019-05-31 -0.0818753491 -0.0218349835  0.0473591422 -0.0084085165
## 2019-06-28  0.0646557767  0.0980462129  0.0737798082  0.0854576124
## 2019-07-31 -0.0142806686  0.0421259737 -0.0024274821 -0.0009959080
## 2019-08-30 -0.0496880810  0.0693116283  0.2218667630  0.0394578764
## 2019-09-30 -0.0229951159 -0.0228190139 -0.0012150581  0.0379542985
## 2019-10-31  0.0232034080  0.0329300011  0.0000000000 -0.0120373614
## 2019-11-29  0.0134958216  0.0090466311  0.1623187098  0.0154860863
## 2019-12-31  0.0257863501 -0.0198409431  0.0252760511  0.0023737847
## 2020-01-31  0.0834803026  0.0387074868 -0.1464845004 -0.0372905590
## 2020-02-28 -0.0642332026 -0.0810564966 -0.0667812142 -0.0613238083
## 2020-03-31  0.0344213022  0.0140924186 -0.1024522135  0.0581107721
## 2020-04-30  0.2381504762  0.0630696532  0.1658369271  0.0674661309
## 2020-05-29 -0.0128673719  0.0178918471  0.1138942603  0.0248289962
## 2020-06-30  0.1218341331 -0.0171987925 -0.0198143006 -0.0351087930
## 2020-07-31  0.1372488933  0.0731774453  0.0484208411  0.0772517545
## 2020-08-31  0.0866005735  0.0657702041  0.1882717886  0.0745885666
## 2020-09-30 -0.0916533253  0.0208929681  0.0402477613  0.0076051670
## 2020-10-30 -0.0364089187  0.0092728923 -0.0335905815 -0.0083258121
## 2020-11-30  0.0425228214  0.0912039764  0.1691406143  0.0963909247
## 2020-12-31  0.0276719582 -0.0131568442 -0.0168515998 -0.0545614232
## 2021-01-29 -0.0156985929 -0.0668094432  0.0259451175 -0.0257180142
## 2021-02-26 -0.0359675607 -0.0607610556  0.0160104157 -0.0782173849
## 2021-03-31  0.0003717151  0.0628753512  0.0767326655  0.0486516889
## 2021-04-30  0.1139202399  0.0562818573  0.0453536035  0.0295951915
## 2021-05-28 -0.0730764715  0.0164722979  0.0938666027  0.0189584382
## 2021-06-30  0.0651836137  0.0449722780  0.0632653481 -0.0071365761
## 2021-07-30 -0.0332696301  0.0844261411  0.0768491283  0.0107909963
## 2021-08-31  0.0421339363  0.0582399761 -0.0519787320  0.0418681281
## 2021-09-30 -0.0550034409 -0.0135715972 -0.0765901409 -0.0606838558
## 2021-10-29  0.0262547651  0.0913576735  0.1265018056  0.0695571266
## 2021-11-30  0.0391473463  0.0928767555 -0.0592959705 -0.0606285988
## 2021-12-31 -0.0505062029  0.0511731912 -0.0521917790  0.0324793738
## 2022-01-31 -0.1085098142 -0.1167773919 -0.0487403594 -0.0343092140
## 2022-02-28  0.0263230079  0.0290841783 -0.0940890154 -0.0338250472
## 2022-03-31  0.0596239190  0.1034613947  0.0604567720  0.1008101796
## 2022-04-29 -0.2711856801 -0.0781042403  0.0745687402  0.0269634584
## 2022-05-31 -0.0333130879 -0.1314593743 -0.3412237240 -0.1698042629
## 2022-06-30 -0.1238178226  0.0276272354 -0.1364653969 -0.0563673802
## 2022-07-29  0.2394860591  0.1234133719  0.1456888725  0.0826081252
## 2022-08-31 -0.0625299224 -0.0361147288 -0.0125340489  0.0081248754
## 2022-09-30 -0.1149865758 -0.1003082240 -0.0774525594 -0.0217355909
## 2022-10-31 -0.0981105335  0.0618564100  0.1015456597  0.0929240417
## 2022-11-30 -0.0593198454  0.0725755241  0.0232764154  0.0684913808
## 2022-12-30 -0.1391406409 -0.1665904895 -0.1141984128 -0.0685298958
## 2023-01-31  0.2051735029  0.1130548988  0.1440934273  0.0145631782
## 2023-02-28 -0.0902516639 -0.0524475224 -0.0151215138 -0.0121681299
## 2023-03-31  0.0918019370  0.0258717094 -0.0171794936  0.0408373072
## 2023-04-28  0.0206963031  0.0126988808 -0.0487449128  0.0235919382
## 2023-05-31  0.1340765703  0.0185211646 -0.1795999116 -0.0237417342
## 2023-06-30  0.0779864104  0.0510997353  0.0073812179  0.0678437215
## 2023-07-31  0.0251489708  0.0405679432  0.0340610333  0.0169068634
## 2023-08-31  0.0318772770 -0.0186364300 -0.0669317261  0.0206048812
## 2023-09-29 -0.0821945627  0.0281461458 -0.1349887152 -0.0166184345
## 2023-10-31  0.0458940197 -0.0224099173  0.0019878064  0.0215261952
## 2023-11-30  0.0931972815  0.0722445947  0.1990888539 -0.0483955600
## 2023-12-29  0.0392628771  0.1300909619  0.0623594661  0.0162177780
## 2024-01-31  0.0212288659  0.0513781204 -0.0237308705  0.0470820524
## 2024-02-29  0.1300782611  0.0696231672  0.1022451490  0.0620581643
## 2024-03-28  0.0202729146 -0.0152525578  0.1474205398  0.0296684711
## 2024-04-30 -0.0302797899 -0.0117661878 -0.0960554544 -0.0137217649
## 2024-05-31  0.0081949152  0.1136275244 -0.0235390138  0.1060150893
## 2024-06-28  0.0910037984  0.0483262482 -0.0533984625  0.0292221416
## 2024-07-31 -0.0329830511 -0.0320615334  0.0158824466  0.0136416023
## 2024-08-30 -0.0464130352  0.0821515904  0.0289072164  0.1207588725
## 2024-09-30  0.0429307038 -0.0065883578  0.0144761041  0.0445699521
## 2024-10-31  0.0003755644 -0.0140173715 -0.0380560697  0.0147512873
## 2024-11-29  0.1091142212  0.1072738018 -0.1185490029  0.1210993655
## 2024-12-31  0.0538418744 -0.0589210901  0.0214594528 -0.0213044751
## 2025-01-31  0.0800742352  0.0671190163  0.0199942359  0.0829078191
## 2025-02-28 -0.1130190481  0.0688948725 -0.0958942393  0.0045737771
## 2025-03-31 -0.1095146233 -0.1032107172 -0.1743686757 -0.1134826443
## 2025-04-30 -0.0311757737  0.0502300978 -0.0762331071  0.1023372927
## 2025-05-30  0.1058429765  0.0462111097 -0.0168449127  0.0174175384
## 2025-06-30  0.0677922335 -0.0495048309  0.0481769960 -0.0095675353
## 2025-07-31  0.0649401278 -0.0521474867  0.0185794438  0.0020433725
## 2025-08-29 -0.0220690894  0.0052931426 -0.0352314150 -0.0079257195
## 2025-09-30 -0.0420508819 -0.0189309483 -0.0676691760  0.0608065129
## 2025-10-31  0.1063983418 -0.0140240978  0.0331135151 -0.0184102073
## 2025-11-28 -0.0460872441  0.0023451712 -0.0103829918  0.0882049221
## 2025-12-02  0.0051321512  0.0091958702 -0.0016566520  0.0170469001
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AMZN        COST         TGT          WMT
## AMZN 0.0070344314 0.002144688 0.001652569 0.0008347007
## COST 0.0021446885 0.003113376 0.001942291 0.0015325770
## TGT  0.0016525687 0.001942291 0.006769723 0.0017756988
## WMT  0.0008347007 0.001532577 0.001775699 0.0027380000
# 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.3, 0.25, 0.25, 0.2)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.05102626
# 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       COST        TGT        WMT
## [1,] 0.01897014 0.01084657 0.01483998 0.00636958
rowSums(component_contribution)
## [1] 0.05102626
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 4
##    AMZN  COST   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.372 0.213 0.291 0.125
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.372
## 2 COST         0.213
## 3 TGT          0.291
## 4 WMT          0.125

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          COST           TGT           WMT
## 2013-01-31  0.0566799395  0.0359115455  0.0207399364  0.0248960688
## 2013-02-28 -0.0046435024 -0.0076473037  0.0470674482  0.0117956325
## 2013-03-28  0.0083654162  0.0464886570  0.0836038042  0.0620733444
## 2013-04-30 -0.0487507497  0.0216286367  0.0303600624  0.0378940266
## 2013-05-31  0.0588686246  0.0137929552 -0.0099613248 -0.0317803265
## 2013-06-28  0.0310507506  0.0085376620 -0.0092510482 -0.0046876901
## 2013-07-31  0.0813355350  0.0601082787  0.0341191112  0.0452746039
## 2013-08-30 -0.0695574090 -0.0458220116 -0.1118618013 -0.0596996679
## 2013-09-30  0.1067688897  0.0290719119  0.0105272481  0.0133387646
## 2013-10-31  0.1521839116  0.0242752566  0.0125808934  0.0370288724
## 2013-11-29  0.0781496860  0.0635978940 -0.0069133143  0.0540191050
## 2013-12-31  0.0130490386 -0.0524564677 -0.0103774224 -0.0232521533
## 2014-01-31 -0.1059765119 -0.0575831618 -0.1106957294 -0.0523035066
## 2014-02-28  0.0094619003  0.0414543698  0.1066818482  0.0002677993
## 2014-03-31 -0.0737086161 -0.0448254145 -0.0329977351  0.0293258718
## 2014-04-30 -0.1007565303  0.0351900442  0.0202855679  0.0420197047
## 2014-05-30  0.0273091844  0.0059923438 -0.0769025779 -0.0314091608
## 2014-06-30  0.0383836202 -0.0074403423  0.0207487873 -0.0223929194
## 2014-07-31 -0.0369768154  0.0234833295  0.0279072224 -0.0200477199
## 2014-08-29  0.0799468404  0.0296726395  0.0169976887  0.0323258126
## 2014-09-30 -0.0502010184  0.0344192863  0.0425320320  0.0127659151
## 2014-10-31 -0.0540982347  0.0622564977 -0.0138156469 -0.0026188708
## 2014-11-28  0.1031187277  0.0661385347  0.1875000034  0.1378161469
## 2014-12-31 -0.0872368614 -0.0026070460  0.0254836939 -0.0135737635
## 2015-01-30  0.1330922557  0.0087093095 -0.0307679023 -0.0105350301
## 2015-02-27  0.0697992426  0.0623775080  0.0496017752 -0.0124327229
## 2015-03-31 -0.0214295755  0.0304251494  0.0659774041 -0.0142316094
## 2015-04-30  0.1253212736 -0.0546596517 -0.0402784443 -0.0524135881
## 2015-05-29  0.0175090293 -0.0032206005  0.0128400110 -0.0433509251
## 2015-06-30  0.0112589814 -0.0542541314  0.0287062888 -0.0460137267
## 2015-07-31  0.2111621090  0.0730809853  0.0026918910  0.0146945932
## 2015-08-31 -0.0443525782 -0.0340549818 -0.0448221836 -0.0993581514
## 2015-09-30 -0.0019516837  0.0317638341  0.0121506387  0.0016980595
## 2015-10-30  0.2010808743  0.0895905747 -0.0189941673 -0.1246701622
## 2015-11-30  0.0602956777  0.0232363066 -0.0547335497  0.0275691466
## 2015-12-31  0.0165440008  0.0004957496  0.0015160112  0.0492988515
## 2016-01-29 -0.1410054620 -0.0664310133 -0.0026202421  0.0793153459
## 2016-02-29 -0.0605352209 -0.0045311042  0.0882419533 -0.0003022468
## 2016-03-31  0.0717834363  0.0490975754  0.0476672486  0.0392706584
## 2016-04-29  0.1053453760 -0.0588895486 -0.0343712346 -0.0239369987
## 2016-05-31  0.0915002899  0.0043113358 -0.1372355329  0.0641208939
## 2016-06-30 -0.0099694639  0.0540991743  0.0150075932  0.0311569152
## 2016-07-29  0.0586021229  0.0628101159  0.0759577817 -0.0006851060
## 2016-08-31  0.0135476418 -0.0284746953 -0.0627263681 -0.0143680293
## 2016-09-30  0.0848953908 -0.0609213373 -0.0217478055  0.0094736549
## 2016-10-31 -0.0583893058 -0.0308968415  0.0007275474 -0.0295510180
## 2016-11-30 -0.0509721927  0.0181077948  0.1251768485  0.0058387945
## 2016-12-30 -0.0009330556  0.0644927397 -0.0670620652 -0.0116436795
## 2017-01-31  0.0936394059  0.0237004206 -0.1135003841 -0.0350396593
## 2017-02-28  0.0258446800  0.0802945460 -0.0835534644  0.0608890541
## 2017-03-31  0.0479423007 -0.0550494211 -0.0628498255  0.0234092364
## 2017-04-28  0.0424566944  0.0569665704  0.0118878192  0.0421085426
## 2017-05-31  0.0725778018  0.0587793863 -0.0018018239  0.0511563613
## 2017-06-30 -0.0271286156 -0.1206065415 -0.0532517111 -0.0378580525
## 2017-07-31  0.0202278808 -0.0089185400  0.0804398049  0.0553875153
## 2017-08-31 -0.0072953953 -0.0080370469 -0.0272904794 -0.0180253420
## 2017-09-29 -0.0198260355  0.0470449248  0.0789562670  0.0008964969
## 2017-10-31  0.1395154056 -0.0197320993  0.0005081430  0.1109625570
## 2017-11-30  0.0626577318  0.1383318710  0.0247793608  0.1076144588
## 2017-12-29 -0.0062057845  0.0091216854  0.0855493775  0.0207687832
## 2018-01-31  0.2156265497  0.0459410500  0.1421908823  0.0764918460
## 2018-02-28  0.0415536279 -0.0179106349  0.0107468907 -0.1691628171
## 2018-03-29 -0.0440034760 -0.0130231753 -0.0826207532 -0.0056772980
## 2018-04-30  0.0788803060  0.0452887192  0.0446455444 -0.0057486603
## 2018-05-31  0.0397392430  0.0083749348  0.0125279558 -0.0629871978
## 2018-06-29  0.0421636787  0.0527599579  0.0433595380  0.0369860533
## 2018-07-31  0.0446635734  0.0455083478  0.0581797004  0.0409482660
## 2018-08-31  0.1243079079  0.0663288828  0.0889778214  0.0774627491
## 2018-09-28 -0.0048359814  0.0074783991  0.0080816082 -0.0205521426
## 2018-10-31 -0.2258869989 -0.0269696276 -0.0533181146  0.0656295250
## 2018-11-30  0.0560700324  0.0138981299 -0.1560245173 -0.0265766044
## 2018-12-31 -0.1180514843 -0.1269316926 -0.0710990427 -0.0417362002
## 2019-01-31  0.1348080312  0.0522184633  0.0994422772  0.0283645312
## 2019-02-28 -0.0469930640  0.0216653282  0.0038811671  0.0324430238
## 2019-03-29  0.0824420184  0.1016324877  0.0997562579 -0.0094925735
## 2019-04-30  0.0786806224  0.0139029976 -0.0360267023  0.0530141959
## 2019-05-31 -0.0818753491 -0.0218349835  0.0473591422 -0.0084085165
## 2019-06-28  0.0646557767  0.0980462129  0.0737798082  0.0854576124
## 2019-07-31 -0.0142806686  0.0421259737 -0.0024274821 -0.0009959080
## 2019-08-30 -0.0496880810  0.0693116283  0.2218667630  0.0394578764
## 2019-09-30 -0.0229951159 -0.0228190139 -0.0012150581  0.0379542985
## 2019-10-31  0.0232034080  0.0329300011  0.0000000000 -0.0120373614
## 2019-11-29  0.0134958216  0.0090466311  0.1623187098  0.0154860863
## 2019-12-31  0.0257863501 -0.0198409431  0.0252760511  0.0023737847
## 2020-01-31  0.0834803026  0.0387074868 -0.1464845004 -0.0372905590
## 2020-02-28 -0.0642332026 -0.0810564966 -0.0667812142 -0.0613238083
## 2020-03-31  0.0344213022  0.0140924186 -0.1024522135  0.0581107721
## 2020-04-30  0.2381504762  0.0630696532  0.1658369271  0.0674661309
## 2020-05-29 -0.0128673719  0.0178918471  0.1138942603  0.0248289962
## 2020-06-30  0.1218341331 -0.0171987925 -0.0198143006 -0.0351087930
## 2020-07-31  0.1372488933  0.0731774453  0.0484208411  0.0772517545
## 2020-08-31  0.0866005735  0.0657702041  0.1882717886  0.0745885666
## 2020-09-30 -0.0916533253  0.0208929681  0.0402477613  0.0076051670
## 2020-10-30 -0.0364089187  0.0092728923 -0.0335905815 -0.0083258121
## 2020-11-30  0.0425228214  0.0912039764  0.1691406143  0.0963909247
## 2020-12-31  0.0276719582 -0.0131568442 -0.0168515998 -0.0545614232
## 2021-01-29 -0.0156985929 -0.0668094432  0.0259451175 -0.0257180142
## 2021-02-26 -0.0359675607 -0.0607610556  0.0160104157 -0.0782173849
## 2021-03-31  0.0003717151  0.0628753512  0.0767326655  0.0486516889
## 2021-04-30  0.1139202399  0.0562818573  0.0453536035  0.0295951915
## 2021-05-28 -0.0730764715  0.0164722979  0.0938666027  0.0189584382
## 2021-06-30  0.0651836137  0.0449722780  0.0632653481 -0.0071365761
## 2021-07-30 -0.0332696301  0.0844261411  0.0768491283  0.0107909963
## 2021-08-31  0.0421339363  0.0582399761 -0.0519787320  0.0418681281
## 2021-09-30 -0.0550034409 -0.0135715972 -0.0765901409 -0.0606838558
## 2021-10-29  0.0262547651  0.0913576735  0.1265018056  0.0695571266
## 2021-11-30  0.0391473463  0.0928767555 -0.0592959705 -0.0606285988
## 2021-12-31 -0.0505062029  0.0511731912 -0.0521917790  0.0324793738
## 2022-01-31 -0.1085098142 -0.1167773919 -0.0487403594 -0.0343092140
## 2022-02-28  0.0263230079  0.0290841783 -0.0940890154 -0.0338250472
## 2022-03-31  0.0596239190  0.1034613947  0.0604567720  0.1008101796
## 2022-04-29 -0.2711856801 -0.0781042403  0.0745687402  0.0269634584
## 2022-05-31 -0.0333130879 -0.1314593743 -0.3412237240 -0.1698042629
## 2022-06-30 -0.1238178226  0.0276272354 -0.1364653969 -0.0563673802
## 2022-07-29  0.2394860591  0.1234133719  0.1456888725  0.0826081252
## 2022-08-31 -0.0625299224 -0.0361147288 -0.0125340489  0.0081248754
## 2022-09-30 -0.1149865758 -0.1003082240 -0.0774525594 -0.0217355909
## 2022-10-31 -0.0981105335  0.0618564100  0.1015456597  0.0929240417
## 2022-11-30 -0.0593198454  0.0725755241  0.0232764154  0.0684913808
## 2022-12-30 -0.1391406409 -0.1665904895 -0.1141984128 -0.0685298958
## 2023-01-31  0.2051735029  0.1130548988  0.1440934273  0.0145631782
## 2023-02-28 -0.0902516639 -0.0524475224 -0.0151215138 -0.0121681299
## 2023-03-31  0.0918019370  0.0258717094 -0.0171794936  0.0408373072
## 2023-04-28  0.0206963031  0.0126988808 -0.0487449128  0.0235919382
## 2023-05-31  0.1340765703  0.0185211646 -0.1795999116 -0.0237417342
## 2023-06-30  0.0779864104  0.0510997353  0.0073812179  0.0678437215
## 2023-07-31  0.0251489708  0.0405679432  0.0340610333  0.0169068634
## 2023-08-31  0.0318772770 -0.0186364300 -0.0669317261  0.0206048812
## 2023-09-29 -0.0821945627  0.0281461458 -0.1349887152 -0.0166184345
## 2023-10-31  0.0458940197 -0.0224099173  0.0019878064  0.0215261952
## 2023-11-30  0.0931972815  0.0722445947  0.1990888539 -0.0483955600
## 2023-12-29  0.0392628771  0.1300909619  0.0623594661  0.0162177780
## 2024-01-31  0.0212288659  0.0513781204 -0.0237308705  0.0470820524
## 2024-02-29  0.1300782611  0.0696231672  0.1022451490  0.0620581643
## 2024-03-28  0.0202729146 -0.0152525578  0.1474205398  0.0296684711
## 2024-04-30 -0.0302797899 -0.0117661878 -0.0960554544 -0.0137217649
## 2024-05-31  0.0081949152  0.1136275244 -0.0235390138  0.1060150893
## 2024-06-28  0.0910037984  0.0483262482 -0.0533984625  0.0292221416
## 2024-07-31 -0.0329830511 -0.0320615334  0.0158824466  0.0136416023
## 2024-08-30 -0.0464130352  0.0821515904  0.0289072164  0.1207588725
## 2024-09-30  0.0429307038 -0.0065883578  0.0144761041  0.0445699521
## 2024-10-31  0.0003755644 -0.0140173715 -0.0380560697  0.0147512873
## 2024-11-29  0.1091142212  0.1072738018 -0.1185490029  0.1210993655
## 2024-12-31  0.0538418744 -0.0589210901  0.0214594528 -0.0213044751
## 2025-01-31  0.0800742352  0.0671190163  0.0199942359  0.0829078191
## 2025-02-28 -0.1130190481  0.0688948725 -0.0958942393  0.0045737771
## 2025-03-31 -0.1095146233 -0.1032107172 -0.1743686757 -0.1134826443
## 2025-04-30 -0.0311757737  0.0502300978 -0.0762331071  0.1023372927
## 2025-05-30  0.1058429765  0.0462111097 -0.0168449127  0.0174175384
## 2025-06-30  0.0677922335 -0.0495048309  0.0481769960 -0.0095675353
## 2025-07-31  0.0649401278 -0.0521474867  0.0185794438  0.0020433725
## 2025-08-29 -0.0220690894  0.0052931426 -0.0352314150 -0.0079257195
## 2025-09-30 -0.0420508819 -0.0189309483 -0.0676691760  0.0608065129
## 2025-10-31  0.1063983418 -0.0140240978  0.0331135151 -0.0184102073
## 2025-11-28 -0.0460872441  0.0023451712 -0.0103829918  0.0882049221
## 2025-12-02  0.0051321512  0.0091958702 -0.0016566520  0.0170469001
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 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(.3, .25, .25, .2)))
## # A tibble: 1 × 4
##    AMZN  COST   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.372 0.213 0.291 0.125
plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.3, .25, .25, .2)) %>%
    
    # Transform to long form
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weight
    add_column(weight = c(.3, .25, .25, .2)) %>%

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

The largest contributor to my portfolio’s volatility is AMZN. Indeed, its the asset with the highest black bar (contribution bar) at ~37%. I think my portfolio’s risk is concentrated in that asset because its contribution is notably higher than its weight. TGT also contribute to a part of the portfolio’s risk with ~29% of contribution, which is much less than AMZN. Thus AMZN is the main contributor.