# 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("TSLA", "AMZN", "WMT", "RGR")

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

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           RGR         TSLA           WMT
## 2013-01-31  0.0566799395  0.1117935991  0.102078114  0.0248965336
## 2013-02-28 -0.0046435024  0.0729114522 -0.074128640  0.0117958103
## 2013-03-28  0.0083654162 -0.0665751386  0.084208138  0.0620730284
## 2013-04-30 -0.0487507497  0.0105877744  0.354111531  0.0378933889
## 2013-05-31  0.0588686246 -0.0099508099  0.593716684 -0.0317796797
## 2013-06-28  0.0310507506 -0.0455738647  0.093672163 -0.0046875722
## 2013-07-31  0.0813355350  0.0572395845  0.223739522  0.0452740106
## 2013-08-30 -0.0695574090  0.0408137347  0.229971642 -0.0596995599
## 2013-09-30  0.1067688897  0.1789105490  0.134706620  0.0133389542
## 2013-10-31  0.1521839116  0.0434311659 -0.189806595  0.0370289815
## 2013-11-29  0.0781496860  0.1701084272 -0.228409405  0.0540193675
## 2013-12-31  0.0130490386 -0.0512045164  0.167108541 -0.0232522823
## 2014-01-31 -0.1059765119  0.0412758251  0.187261714 -0.0523037258
## 2014-02-28  0.0094619003 -0.1781551686  0.299722785  0.0002677976
## 2014-03-31 -0.0737086161 -0.0551841764 -0.160783242  0.0293257896
## 2014-04-30 -0.1007565303  0.0733307142 -0.002690122  0.0420200534
## 2014-05-30  0.0273091844 -0.0524000919 -0.000577422 -0.0314091748
## 2014-06-30  0.0383836202 -0.0269180972  0.144457224 -0.0223928721
## 2014-07-31 -0.0369768154 -0.1664840843 -0.072372676 -0.0200479846
## 2014-08-29  0.0799468404  0.0178876931  0.188794007  0.0323258326
## 2014-09-30 -0.0502010184 -0.0347160505 -0.105566477  0.0127657660
## 2014-10-31 -0.0540982347 -0.1554521925 -0.004046477 -0.0026185595
## 2014-11-28  0.1031187277 -0.0863213010  0.011599801  0.1378161286
## 2014-12-31 -0.0872368614 -0.0952312640 -0.094774519 -0.0135739178
## 2015-01-30  0.1330922557  0.1541096343 -0.088365289 -0.0105347712
## 2015-02-27  0.0697992426  0.2516438994 -0.001277808 -0.0124328162
## 2015-03-31 -0.0214295755 -0.0426221975 -0.074350051 -0.0142314252
## 2015-04-30  0.1253212736  0.0992775275  0.180226808 -0.0524138973
## 2015-05-29  0.0175090293 -0.0139702406  0.103899574 -0.0433510761
## 2015-06-30  0.0112589814  0.0667572086  0.067300935 -0.0460134624
## 2015-07-31  0.2111621090  0.0437629897 -0.007896616  0.0146946112
## 2015-08-31 -0.0443525782  0.0524474767 -0.066366250 -0.0993583525
## 2015-09-30 -0.0019516837 -0.0691179511 -0.002653519  0.0016978216
## 2015-10-30  0.2010808743 -0.0302712288 -0.182659777 -0.1246697320
## 2015-11-30  0.0602956777 -0.0837943479  0.106828586  0.0275690152
## 2015-12-31  0.0165440008  0.1346585020  0.041471519  0.0492989163
## 2016-01-29 -0.1410054620 -0.0128315236 -0.227360626  0.0793151295
## 2016-02-29 -0.0605352209  0.1779219739  0.003810669 -0.0003016803
## 2016-03-31  0.0717834363 -0.0230996181  0.179948109  0.0392704160
## 2016-04-29  0.1053453760 -0.0657286618  0.046721797 -0.0239371460
## 2016-05-31  0.0915002899  0.0409794035 -0.075597968  0.0641208402
## 2016-06-30 -0.0099694639 -0.0340944146 -0.050296440  0.0311569244
## 2016-07-29  0.0586021229  0.0604686464  0.100785334 -0.0006850857
## 2016-08-31  0.0135476418 -0.0965152682 -0.102058091 -0.0143678175
## 2016-09-30  0.0848953908 -0.0593202081 -0.038366372  0.0094732138
## 2016-10-31 -0.0583893058  0.0627405413 -0.031364583 -0.0295503095
## 2016-11-30 -0.0509721927 -0.1715812509 -0.043041267  0.0058382073
## 2016-12-30 -0.0009330556  0.0249770530  0.120665178 -0.0116434430
## 2017-01-31  0.0936394059  0.0009483082  0.164624916 -0.0350398141
## 2017-02-28  0.0258446800 -0.0565450751 -0.007730364  0.0608894235
## 2017-03-31  0.0479423007  0.0799576726  0.107278727  0.0234087490
## 2017-04-28  0.0424566944  0.1212008660  0.120916212  0.0421087086
## 2017-05-31  0.0725778018  0.0600098119  0.082295892  0.0511562600
## 2017-06-30 -0.0271286156 -0.0246336802  0.058654468 -0.0378575839
## 2017-07-31  0.0202278808 -0.0760281521 -0.111459860  0.0553873556
## 2017-08-31 -0.0072953953 -0.2246871844  0.095543446 -0.0180254134
## 2017-09-29 -0.0198260355  0.1211738002 -0.042474144  0.0008962784
## 2017-10-31  0.1395154056 -0.0424757960 -0.028457409  0.1109631648
## 2017-11-30  0.0626577318  0.1041035415 -0.070862541  0.1076144189
## 2017-12-29 -0.0062057845  0.0198920302  0.008061928  0.0207679702
## 2018-01-31  0.2156265497 -0.0533213456  0.129254571  0.0764922267
## 2018-02-28  0.0415536279 -0.2069860954 -0.032266877 -0.1691627848
## 2018-03-29 -0.0440034760  0.2030203710 -0.253920408 -0.0056771880
## 2018-04-30  0.0788803060  0.0510553117  0.099254576 -0.0057487508
## 2018-05-31  0.0397392430  0.1084520492 -0.031698139 -0.0629872811
## 2018-06-29  0.0421636787 -0.0896120459  0.186043257  0.0369862285
## 2018-07-31  0.0446635734 -0.0326708412 -0.140021491  0.0409480133
## 2018-08-31  0.1243079079  0.1944195575  0.011737389  0.0774629818
## 2018-09-28 -0.0048359814  0.0535442430 -0.130439038 -0.0205520186
## 2018-10-31 -0.2258869989 -0.1507046963  0.242170576  0.0656295882
## 2018-11-30  0.0560700324 -0.0986986669  0.038271580 -0.0265767952
## 2018-12-31 -0.1180514843 -0.0073012420 -0.051761952 -0.0417364136
## 2019-01-31  0.1348080312  0.0233992739 -0.080628789  0.0283648123
## 2019-02-28 -0.0469930640  0.0443399556  0.041032981  0.0324428277
## 2019-03-29  0.0824420184 -0.0659510016 -0.133656413 -0.0094925511
## 2019-04-30  0.0786806224  0.0545037125 -0.159123803  0.0530142824
## 2019-05-31 -0.0818753491 -0.1135098912 -0.253945372 -0.0084086083
## 2019-06-28  0.0646557767  0.0916273450  0.188012109  0.0854574477
## 2019-07-31 -0.0142806686  0.0364068016  0.078092373 -0.0009960790
## 2019-08-30 -0.0496880810 -0.3171597303 -0.068516948  0.0394581272
## 2019-09-30 -0.0229951159  0.0181231418  0.065449565  0.0379542720
## 2019-10-31  0.0232034080  0.0914714435  0.268061253 -0.0120372439
## 2019-11-29  0.0134958216 -0.0020946140  0.046592176  0.0154859448
## 2019-12-31  0.0257863501  0.0319753984  0.237359743  0.0023739856
## 2020-01-31  0.0834803026  0.0515909476  0.441578342 -0.0372904658
## 2020-02-28 -0.0642332026 -0.0303428333  0.026424253 -0.0613239258
## 2020-03-31  0.0344213022  0.0623450118 -0.242781458  0.0581107980
## 2020-04-30  0.2381504762  0.0439992465  0.400209535  0.0674664185
## 2020-05-29 -0.0128673719  0.1645788840  0.065730500  0.0248284930
## 2020-06-30  0.1218341331  0.1981298091  0.257108657 -0.0351086313
## 2020-07-31  0.1372488933  0.0682735308  0.281420674  0.0772518251
## 2020-08-31  0.0866005735 -0.0743259289  0.554719320  0.0745886051
## 2020-09-30 -0.0916533253 -0.1472128424 -0.149762306  0.0076052146
## 2020-10-30 -0.0364089187  0.0891076337 -0.100371771 -0.0083257286
## 2020-11-30  0.0425228214 -0.0794432600  0.380308519  0.0963907125
## 2020-12-31  0.0276719582  0.0606627856  0.217730753 -0.0545614279
## 2021-01-29 -0.0156985929 -0.0266308359  0.117343701 -0.0257180631
## 2021-02-26 -0.0359675607  0.0736117539 -0.161038203 -0.0782174555
## 2021-03-31  0.0003717151 -0.0217062161 -0.011269836  0.0486518235
## 2021-04-30  0.1139202399 -0.0172510816  0.060292565  0.0295951012
## 2021-05-28 -0.0730764715  0.2067930370 -0.126372343  0.0189583652
## 2021-06-30  0.0651836137  0.1308991104  0.083547955 -0.0071365619
## 2021-07-30 -0.0332696301 -0.1906690741  0.010973846  0.0107910550
## 2021-08-31  0.0421339363  0.0623582843  0.068224268  0.0418680322
## 2021-09-30 -0.0550034409 -0.0580542375  0.052632612 -0.0606835312
## 2021-10-29  0.0262547651  0.0668401024  0.362230202  0.0695570156
## 2021-11-30  0.0391473463 -0.0847208837  0.027237848 -0.0606288206
## 2021-12-31 -0.0505062029 -0.0525497281 -0.079968440  0.0324796268
## 2022-01-31 -0.1085098142 -0.0116820613 -0.120597475 -0.0343094247
## 2022-02-28  0.0263230079  0.0742250229 -0.073397011 -0.0338249542
## 2022-03-31  0.0596239190 -0.0268810128  0.213504290  0.1008100953
## 2022-04-29 -0.2711856801 -0.0213404989 -0.213125289  0.0269634706
## 2022-05-31 -0.0333130879  0.0065877259 -0.138340062 -0.1698040947
## 2022-06-30 -0.1238178226 -0.0644893746 -0.118657126 -0.0563675846
## 2022-07-29  0.2394860591  0.0382231386  0.280480149  0.0826081279
## 2022-08-31 -0.0625299224 -0.2275967845 -0.075250271  0.0081250972
## 2022-09-30 -0.1149865758 -0.0283403752 -0.038313992 -0.0217359565
## 2022-10-31 -0.0981105335  0.0999708519 -0.153346760  0.0929242741
## 2022-11-30 -0.0593198454 -0.0141226666 -0.155866121  0.0684915613
## 2022-12-30 -0.1391406409  0.0080976496 -0.457813194 -0.0685300354
## 2023-01-31  0.2051735029  0.1169487376  0.340915767  0.0145630125
## 2023-02-28 -0.0902516639  0.0237919766  0.171904973 -0.0121679518
## 2023-03-31  0.0918019370 -0.0068650989  0.008471140  0.0408372881
## 2023-04-28  0.0206963031  0.0020869804 -0.233183710  0.0235918517
## 2023-05-31  0.1340765703 -0.1041115347  0.216021889 -0.0237417069
## 2023-06-30  0.0779864104  0.0267906576  0.249689452  0.0678437903
## 2023-07-31  0.0251489708 -0.0001887841  0.021391609  0.0169069122
## 2023-08-31  0.0318772770 -0.0196254193 -0.035588260  0.0206048836
## 2023-09-29 -0.0821945627  0.0104146964 -0.030929027 -0.0166184904
## 2023-10-31  0.0458940197  0.0601279925 -0.219831983  0.0215261766
## 2023-11-30  0.0931972815 -0.2265672449  0.178463656 -0.0483956164
## 2023-12-29  0.0392628771  0.0333326007  0.034390133  0.0162177594
## 2024-01-31  0.0212288659 -0.0401804228 -0.282704160  0.0470820679
## 2024-02-29  0.1300782611 -0.0078179792  0.075015304  0.0620581657
## 2024-03-28  0.0202729146  0.0683526277 -0.138383423  0.0296685050
## 2024-04-30 -0.0302797899  0.0017319138  0.041724969 -0.0137217222
## 2024-05-31  0.0081949152 -0.0357689147 -0.028782134  0.1060150534
## 2024-06-28  0.0910037984 -0.0648386184  0.105427913  0.0292221364
## 2024-07-31 -0.0329830511  0.0798025852  0.159378271  0.0136415209
## 2024-08-30 -0.0464130352 -0.0639510689 -0.080549177  0.1207589765
## 2024-09-30  0.0429307038 -0.0105013175  0.200441406  0.0445699361
## 2024-10-31  0.0003755644 -0.0582881199 -0.046070548  0.0147513058
## 2024-11-19  0.0932108617 -0.0240913280  0.325578013  0.0551905300
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AMZN          RGR         TSLA          WMT
## AMZN 0.0071603669 0.0015549155 0.0052094637 0.0008307755
## RGR  0.0015549155 0.0092376627 0.0002915426 0.0003232826
## TSLA 0.0052094637 0.0002915426 0.0292501230 0.0009392985
## WMT  0.0008307755 0.0003232826 0.0009392985 0.0026338275
# 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.2, 0.2, 0.1)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.05296391
# 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         RGR       TSLA       WMT
## [1,] 0.01522756 0.008786731 0.02758342 0.0013662
rowSums(component_contribution)
## [1] 0.05296391
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 4
##    AMZN   RGR  TSLA   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.288 0.166 0.521 0.026
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.288
## 2 RGR          0.166
## 3 TSLA         0.521
## 4 WMT          0.026
# 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           RGR         TSLA           WMT
## 2013-01-31  0.0566799395  0.1117935991  0.102078114  0.0248965336
## 2013-02-28 -0.0046435024  0.0729114522 -0.074128640  0.0117958103
## 2013-03-28  0.0083654162 -0.0665751386  0.084208138  0.0620730284
## 2013-04-30 -0.0487507497  0.0105877744  0.354111531  0.0378933889
## 2013-05-31  0.0588686246 -0.0099508099  0.593716684 -0.0317796797
## 2013-06-28  0.0310507506 -0.0455738647  0.093672163 -0.0046875722
## 2013-07-31  0.0813355350  0.0572395845  0.223739522  0.0452740106
## 2013-08-30 -0.0695574090  0.0408137347  0.229971642 -0.0596995599
## 2013-09-30  0.1067688897  0.1789105490  0.134706620  0.0133389542
## 2013-10-31  0.1521839116  0.0434311659 -0.189806595  0.0370289815
## 2013-11-29  0.0781496860  0.1701084272 -0.228409405  0.0540193675
## 2013-12-31  0.0130490386 -0.0512045164  0.167108541 -0.0232522823
## 2014-01-31 -0.1059765119  0.0412758251  0.187261714 -0.0523037258
## 2014-02-28  0.0094619003 -0.1781551686  0.299722785  0.0002677976
## 2014-03-31 -0.0737086161 -0.0551841764 -0.160783242  0.0293257896
## 2014-04-30 -0.1007565303  0.0733307142 -0.002690122  0.0420200534
## 2014-05-30  0.0273091844 -0.0524000919 -0.000577422 -0.0314091748
## 2014-06-30  0.0383836202 -0.0269180972  0.144457224 -0.0223928721
## 2014-07-31 -0.0369768154 -0.1664840843 -0.072372676 -0.0200479846
## 2014-08-29  0.0799468404  0.0178876931  0.188794007  0.0323258326
## 2014-09-30 -0.0502010184 -0.0347160505 -0.105566477  0.0127657660
## 2014-10-31 -0.0540982347 -0.1554521925 -0.004046477 -0.0026185595
## 2014-11-28  0.1031187277 -0.0863213010  0.011599801  0.1378161286
## 2014-12-31 -0.0872368614 -0.0952312640 -0.094774519 -0.0135739178
## 2015-01-30  0.1330922557  0.1541096343 -0.088365289 -0.0105347712
## 2015-02-27  0.0697992426  0.2516438994 -0.001277808 -0.0124328162
## 2015-03-31 -0.0214295755 -0.0426221975 -0.074350051 -0.0142314252
## 2015-04-30  0.1253212736  0.0992775275  0.180226808 -0.0524138973
## 2015-05-29  0.0175090293 -0.0139702406  0.103899574 -0.0433510761
## 2015-06-30  0.0112589814  0.0667572086  0.067300935 -0.0460134624
## 2015-07-31  0.2111621090  0.0437629897 -0.007896616  0.0146946112
## 2015-08-31 -0.0443525782  0.0524474767 -0.066366250 -0.0993583525
## 2015-09-30 -0.0019516837 -0.0691179511 -0.002653519  0.0016978216
## 2015-10-30  0.2010808743 -0.0302712288 -0.182659777 -0.1246697320
## 2015-11-30  0.0602956777 -0.0837943479  0.106828586  0.0275690152
## 2015-12-31  0.0165440008  0.1346585020  0.041471519  0.0492989163
## 2016-01-29 -0.1410054620 -0.0128315236 -0.227360626  0.0793151295
## 2016-02-29 -0.0605352209  0.1779219739  0.003810669 -0.0003016803
## 2016-03-31  0.0717834363 -0.0230996181  0.179948109  0.0392704160
## 2016-04-29  0.1053453760 -0.0657286618  0.046721797 -0.0239371460
## 2016-05-31  0.0915002899  0.0409794035 -0.075597968  0.0641208402
## 2016-06-30 -0.0099694639 -0.0340944146 -0.050296440  0.0311569244
## 2016-07-29  0.0586021229  0.0604686464  0.100785334 -0.0006850857
## 2016-08-31  0.0135476418 -0.0965152682 -0.102058091 -0.0143678175
## 2016-09-30  0.0848953908 -0.0593202081 -0.038366372  0.0094732138
## 2016-10-31 -0.0583893058  0.0627405413 -0.031364583 -0.0295503095
## 2016-11-30 -0.0509721927 -0.1715812509 -0.043041267  0.0058382073
## 2016-12-30 -0.0009330556  0.0249770530  0.120665178 -0.0116434430
## 2017-01-31  0.0936394059  0.0009483082  0.164624916 -0.0350398141
## 2017-02-28  0.0258446800 -0.0565450751 -0.007730364  0.0608894235
## 2017-03-31  0.0479423007  0.0799576726  0.107278727  0.0234087490
## 2017-04-28  0.0424566944  0.1212008660  0.120916212  0.0421087086
## 2017-05-31  0.0725778018  0.0600098119  0.082295892  0.0511562600
## 2017-06-30 -0.0271286156 -0.0246336802  0.058654468 -0.0378575839
## 2017-07-31  0.0202278808 -0.0760281521 -0.111459860  0.0553873556
## 2017-08-31 -0.0072953953 -0.2246871844  0.095543446 -0.0180254134
## 2017-09-29 -0.0198260355  0.1211738002 -0.042474144  0.0008962784
## 2017-10-31  0.1395154056 -0.0424757960 -0.028457409  0.1109631648
## 2017-11-30  0.0626577318  0.1041035415 -0.070862541  0.1076144189
## 2017-12-29 -0.0062057845  0.0198920302  0.008061928  0.0207679702
## 2018-01-31  0.2156265497 -0.0533213456  0.129254571  0.0764922267
## 2018-02-28  0.0415536279 -0.2069860954 -0.032266877 -0.1691627848
## 2018-03-29 -0.0440034760  0.2030203710 -0.253920408 -0.0056771880
## 2018-04-30  0.0788803060  0.0510553117  0.099254576 -0.0057487508
## 2018-05-31  0.0397392430  0.1084520492 -0.031698139 -0.0629872811
## 2018-06-29  0.0421636787 -0.0896120459  0.186043257  0.0369862285
## 2018-07-31  0.0446635734 -0.0326708412 -0.140021491  0.0409480133
## 2018-08-31  0.1243079079  0.1944195575  0.011737389  0.0774629818
## 2018-09-28 -0.0048359814  0.0535442430 -0.130439038 -0.0205520186
## 2018-10-31 -0.2258869989 -0.1507046963  0.242170576  0.0656295882
## 2018-11-30  0.0560700324 -0.0986986669  0.038271580 -0.0265767952
## 2018-12-31 -0.1180514843 -0.0073012420 -0.051761952 -0.0417364136
## 2019-01-31  0.1348080312  0.0233992739 -0.080628789  0.0283648123
## 2019-02-28 -0.0469930640  0.0443399556  0.041032981  0.0324428277
## 2019-03-29  0.0824420184 -0.0659510016 -0.133656413 -0.0094925511
## 2019-04-30  0.0786806224  0.0545037125 -0.159123803  0.0530142824
## 2019-05-31 -0.0818753491 -0.1135098912 -0.253945372 -0.0084086083
## 2019-06-28  0.0646557767  0.0916273450  0.188012109  0.0854574477
## 2019-07-31 -0.0142806686  0.0364068016  0.078092373 -0.0009960790
## 2019-08-30 -0.0496880810 -0.3171597303 -0.068516948  0.0394581272
## 2019-09-30 -0.0229951159  0.0181231418  0.065449565  0.0379542720
## 2019-10-31  0.0232034080  0.0914714435  0.268061253 -0.0120372439
## 2019-11-29  0.0134958216 -0.0020946140  0.046592176  0.0154859448
## 2019-12-31  0.0257863501  0.0319753984  0.237359743  0.0023739856
## 2020-01-31  0.0834803026  0.0515909476  0.441578342 -0.0372904658
## 2020-02-28 -0.0642332026 -0.0303428333  0.026424253 -0.0613239258
## 2020-03-31  0.0344213022  0.0623450118 -0.242781458  0.0581107980
## 2020-04-30  0.2381504762  0.0439992465  0.400209535  0.0674664185
## 2020-05-29 -0.0128673719  0.1645788840  0.065730500  0.0248284930
## 2020-06-30  0.1218341331  0.1981298091  0.257108657 -0.0351086313
## 2020-07-31  0.1372488933  0.0682735308  0.281420674  0.0772518251
## 2020-08-31  0.0866005735 -0.0743259289  0.554719320  0.0745886051
## 2020-09-30 -0.0916533253 -0.1472128424 -0.149762306  0.0076052146
## 2020-10-30 -0.0364089187  0.0891076337 -0.100371771 -0.0083257286
## 2020-11-30  0.0425228214 -0.0794432600  0.380308519  0.0963907125
## 2020-12-31  0.0276719582  0.0606627856  0.217730753 -0.0545614279
## 2021-01-29 -0.0156985929 -0.0266308359  0.117343701 -0.0257180631
## 2021-02-26 -0.0359675607  0.0736117539 -0.161038203 -0.0782174555
## 2021-03-31  0.0003717151 -0.0217062161 -0.011269836  0.0486518235
## 2021-04-30  0.1139202399 -0.0172510816  0.060292565  0.0295951012
## 2021-05-28 -0.0730764715  0.2067930370 -0.126372343  0.0189583652
## 2021-06-30  0.0651836137  0.1308991104  0.083547955 -0.0071365619
## 2021-07-30 -0.0332696301 -0.1906690741  0.010973846  0.0107910550
## 2021-08-31  0.0421339363  0.0623582843  0.068224268  0.0418680322
## 2021-09-30 -0.0550034409 -0.0580542375  0.052632612 -0.0606835312
## 2021-10-29  0.0262547651  0.0668401024  0.362230202  0.0695570156
## 2021-11-30  0.0391473463 -0.0847208837  0.027237848 -0.0606288206
## 2021-12-31 -0.0505062029 -0.0525497281 -0.079968440  0.0324796268
## 2022-01-31 -0.1085098142 -0.0116820613 -0.120597475 -0.0343094247
## 2022-02-28  0.0263230079  0.0742250229 -0.073397011 -0.0338249542
## 2022-03-31  0.0596239190 -0.0268810128  0.213504290  0.1008100953
## 2022-04-29 -0.2711856801 -0.0213404989 -0.213125289  0.0269634706
## 2022-05-31 -0.0333130879  0.0065877259 -0.138340062 -0.1698040947
## 2022-06-30 -0.1238178226 -0.0644893746 -0.118657126 -0.0563675846
## 2022-07-29  0.2394860591  0.0382231386  0.280480149  0.0826081279
## 2022-08-31 -0.0625299224 -0.2275967845 -0.075250271  0.0081250972
## 2022-09-30 -0.1149865758 -0.0283403752 -0.038313992 -0.0217359565
## 2022-10-31 -0.0981105335  0.0999708519 -0.153346760  0.0929242741
## 2022-11-30 -0.0593198454 -0.0141226666 -0.155866121  0.0684915613
## 2022-12-30 -0.1391406409  0.0080976496 -0.457813194 -0.0685300354
## 2023-01-31  0.2051735029  0.1169487376  0.340915767  0.0145630125
## 2023-02-28 -0.0902516639  0.0237919766  0.171904973 -0.0121679518
## 2023-03-31  0.0918019370 -0.0068650989  0.008471140  0.0408372881
## 2023-04-28  0.0206963031  0.0020869804 -0.233183710  0.0235918517
## 2023-05-31  0.1340765703 -0.1041115347  0.216021889 -0.0237417069
## 2023-06-30  0.0779864104  0.0267906576  0.249689452  0.0678437903
## 2023-07-31  0.0251489708 -0.0001887841  0.021391609  0.0169069122
## 2023-08-31  0.0318772770 -0.0196254193 -0.035588260  0.0206048836
## 2023-09-29 -0.0821945627  0.0104146964 -0.030929027 -0.0166184904
## 2023-10-31  0.0458940197  0.0601279925 -0.219831983  0.0215261766
## 2023-11-30  0.0931972815 -0.2265672449  0.178463656 -0.0483956164
## 2023-12-29  0.0392628771  0.0333326007  0.034390133  0.0162177594
## 2024-01-31  0.0212288659 -0.0401804228 -0.282704160  0.0470820679
## 2024-02-29  0.1300782611 -0.0078179792  0.075015304  0.0620581657
## 2024-03-28  0.0202729146  0.0683526277 -0.138383423  0.0296685050
## 2024-04-30 -0.0302797899  0.0017319138  0.041724969 -0.0137217222
## 2024-05-31  0.0081949152 -0.0357689147 -0.028782134  0.1060150534
## 2024-06-28  0.0910037984 -0.0648386184  0.105427913  0.0292221364
## 2024-07-31 -0.0329830511  0.0798025852  0.159378271  0.0136415209
## 2024-08-30 -0.0464130352 -0.0639510689 -0.080549177  0.1207589765
## 2024-09-30  0.0429307038 -0.0105013175  0.200441406  0.0445699361
## 2024-10-31  0.0003755644 -0.0582881199 -0.046070548  0.0147513058
## 2024-11-19  0.0932108617 -0.0240913280  0.325578013  0.0551905300
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(.25, .25, .2, .3))
## # A tibble: 1 × 4
##    AMZN   RGR  TSLA   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.251 0.206 0.434  0.11

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.25, .25, .2, .3)) %>%
    
    # Transform to long form
    pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weights
    add_column(weight = c(.25, .25, .2, .3)) %>%
    
    # 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)) +
    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?

1. Tesla (TSLA) is this portfolios highest volatility contributer by over 20%. Although Amazon and Ruger are also moderately volatile, I believe my portfolio’s risk is concentrated in the Tesla stock