# Load packages
# Core
library(tidyverse)
library(tidyquant)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
Choose your stocks from 2012-12-31 to present.
symbols <- c("AAPL", "MSFT", "AMZN", "NVDA", "GOOG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = Sys.Date())
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"))
# 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 AMZN GOOG MSFT NVDA
## 2013-01-31 -1.555892e-01 0.0566799395 0.0660633236 0.0273284037 0.000000000
## 2013-02-28 -2.561135e-02 -0.0046435024 0.0584793507 0.0209147276 0.038221794
## 2013-03-28 2.850586e-03 0.0083654162 -0.0087879274 0.0287205704 0.013338850
## 2013-04-30 2.708987e-04 -0.0487507497 0.0375393579 0.1457767830 0.070706352
## 2013-05-31 2.217172e-02 0.0588686246 0.0550324129 0.0599416061 0.054651657
## 2013-06-28 -1.258957e-01 0.0310507506 0.0104478392 -0.0103687649 -0.030167018
## 2013-07-31 1.321024e-01 0.0813355350 0.0083477502 -0.0813947444 0.028091528
## 2013-08-30 8.044300e-02 -0.0695574090 -0.0471074058 0.0548544134 0.026270553
## 2013-09-30 -2.172395e-02 0.1067688897 0.0336806716 -0.0035996453 0.053460484
## 2013-10-31 9.201550e-02 0.1521839116 0.1626138720 0.0620378566 -0.024066357
## 2013-11-29 6.770812e-02 0.0781496860 0.0277602449 0.0815619985 0.032034523
## 2013-12-31 8.861940e-03 0.0130490386 0.0560802355 -0.0190630542 0.026567266
## 2014-01-31 -1.139489e-01 -0.1059765119 0.0523738431 0.0114289172 -0.020177256
## 2014-02-28 5.591811e-02 0.0094619003 0.0289427020 0.0198146498 0.162107504
## 2014-03-31 1.975657e-02 -0.0737086161 -0.0863761589 0.0676173908 -0.025903696
## 2014-04-30 9.476126e-02 -0.1007565303 -0.0559561857 -0.0144985128 0.030788589
## 2014-05-30 7.576518e-02 0.0273091844 0.0611852662 0.0203078744 0.032885853
## 2014-06-30 2.728609e-02 0.0383836202 0.0271166090 0.0183939895 -0.024507853
## 2014-07-31 2.832637e-02 -0.0369768154 -0.0064176890 0.0344125777 -0.057729700
## 2014-08-29 7.465163e-02 0.0799468404 0.0000000000 0.0574851282 0.110060124
## 2014-09-30 -1.722080e-02 -0.0502010184 0.0100266342 0.0202644537 -0.052782840
## 2014-10-31 6.948916e-02 -0.0540982347 -0.0321733252 0.0126460917 0.057399066
## 2014-11-28 1.007307e-01 0.1031187277 -0.0313404149 0.0244391696 0.074852067
## 2014-12-31 -7.460591e-02 -0.0872368614 -0.0288909301 -0.0288584385 -0.044863328
## 2015-01-30 5.961123e-02 0.1330922557 0.0153077432 -0.1395469558 -0.043319075
## 2015-02-27 9.601615e-02 0.0697992426 0.0437064525 0.0890367288 0.142699189
## 2015-03-31 -3.187412e-02 -0.0214295755 -0.0188001752 -0.0755301321 -0.052582901
## 2015-04-30 5.769681e-03 0.1253212736 -0.0169027144 0.1792008143 0.058908761
## 2015-05-29 4.434102e-02 0.0175090293 -0.0097806691 -0.0308035525 0.001460551
## 2015-06-30 -3.793811e-02 0.0112589814 -0.0220411460 -0.0595714607 -0.095717255
## 2015-07-31 -3.348077e-02 0.2111621090 0.1839180429 0.0561513279 -0.007988161
## 2015-08-31 -6.848890e-02 -0.0443525782 -0.0118341631 -0.0639509738 0.123595346
## 2015-09-30 -2.205793e-02 -0.0019516837 -0.0160274904 0.0168606515 0.092150884
## 2015-10-30 8.011273e-02 0.2010808743 0.1555397216 0.1733951544 0.140555496
## 2015-11-30 -5.821522e-03 0.0602956777 0.0437523468 0.0386858885 0.115405505
## 2015-12-31 -1.167903e-01 0.0165440008 0.0216862155 0.0205782532 0.038346974
## 2016-01-29 -7.822339e-02 -0.1410054620 -0.0212150589 -0.0070544848 -0.118048062
## 2016-02-29 -1.288474e-03 -0.0605352209 -0.0627391103 -0.0723443121 0.071923198
## 2016-03-31 1.197460e-01 0.0717834363 0.0654276265 0.0820363770 0.127654865
## 2016-04-29 -1.507313e-01 0.1053453760 -0.0722727464 -0.1020865108 -0.002810490
## 2016-05-31 6.931440e-02 0.0915002899 0.0598051871 0.0678421928 0.276387962
## 2016-06-30 -4.359629e-02 -0.0099694639 -0.0611192026 -0.0351383907 0.006188300
## 2016-07-29 8.623522e-02 0.0586021229 0.1050873316 0.1022678129 0.194443832
## 2016-08-31 2.337655e-02 0.0135476418 -0.0022657077 0.0198813159 0.073469090
## 2016-09-30 6.344820e-02 0.0848953908 0.0132612850 0.0024335481 0.110693497
## 2016-10-31 4.325147e-03 -0.0583893058 0.0092842868 0.0394875016 0.037805359
## 2016-11-30 -2.183773e-02 -0.0509721927 -0.0343614246 0.0123911028 0.260525205
## 2016-12-30 4.684087e-02 -0.0009330556 0.0180151545 0.0307212410 0.146435753
## 2017-01-31 4.664164e-02 0.0936394059 0.0318398016 0.0395984702 0.022601791
## 2017-02-28 1.255553e-01 0.0258446800 0.0326203598 -0.0043734064 -0.071874708
## 2017-03-31 4.754135e-02 0.0479423007 0.0076839406 0.0289607483 0.070843891
## 2017-04-28 -6.952873e-05 0.0424566944 0.0880997776 0.0387184007 -0.043434184
## 2017-05-31 6.560757e-02 0.0725778018 0.0629877387 0.0256723576 0.326022052
## 2017-06-30 -5.891634e-02 -0.0271286156 -0.0599349823 -0.0131149811 0.001454068
## 2017-07-31 3.218028e-02 0.0202278808 0.0236741041 0.0532501016 0.117044187
## 2017-08-31 1.016535e-01 -0.0072953953 0.0094446441 0.0333888117 0.042639659
## 2017-09-29 -6.213522e-02 -0.0198260355 0.0208390926 -0.0037519974 0.053601328
## 2017-10-31 9.240378e-02 0.1395154056 0.0582524207 0.1103420138 0.145700463
## 2017-11-30 2.007531e-02 0.0626577318 0.0046810506 0.0168412366 -0.029244914
## 2017-12-29 -1.536331e-02 -0.0062057845 0.0241715579 0.0161454790 -0.036583488
## 2018-01-31 -1.069339e-02 0.2156265497 0.1115970212 0.1049980949 0.239240605
## 2018-02-28 6.596123e-02 0.0415536279 -0.0573516460 -0.0084504831 -0.014959063
## 2018-03-29 -5.980411e-02 -0.0440034760 -0.0683058144 -0.0270226992 -0.043969365
## 2018-04-30 -1.513378e-02 0.0788803060 -0.0141135501 0.0243527959 -0.029312634
## 2018-05-31 1.267417e-01 0.0397392430 0.0643892063 0.0596524073 0.115145132
## 2018-06-29 -9.462713e-03 0.0421636787 0.0278664152 -0.0023297747 -0.062544423
## 2018-07-31 2.759919e-02 0.0446635734 0.0871652108 0.0730207358 0.033048292
## 2018-08-31 1.826731e-01 0.1243079079 0.0007636989 0.0610885054 0.137075704
## 2018-09-28 -8.337474e-03 -0.0048359814 -0.0205010648 0.0179975380 0.001210433
## 2018-10-31 -3.095169e-02 -0.2258869989 -0.1028991200 -0.0683872083 -0.287374132
## 2018-11-30 -1.999127e-01 0.0560700324 0.0162677680 0.0417980510 -0.253667122
## 2018-12-31 -1.240883e-01 -0.1180514843 -0.0552430013 -0.0877907267 -0.202283047
## 2019-01-31 5.368659e-02 0.1348080312 0.0750916890 0.0277687835 0.073974004
## 2019-02-28 4.380320e-02 -0.0469930640 0.0031749056 0.0745114996 0.071594206
## 2019-03-29 9.260255e-02 0.0824420184 0.0465715978 0.0514094048 0.151869606
## 2019-04-30 5.490077e-02 0.0786806224 0.0128461967 0.1019630829 0.007987988
## 2019-05-31 -1.326322e-01 -0.0818753491 -0.0740703774 -0.0507467451 -0.288680190
## 2019-06-28 1.226770e-01 0.0646557767 -0.0208014377 0.0798436073 0.192591626
## 2019-07-31 7.361697e-02 -0.0142806686 0.1183226052 0.0170969950 0.026972321
## 2019-08-30 -1.659827e-02 -0.0496880810 -0.0237705123 0.0149248758 -0.006207768
## 2019-09-30 7.042281e-02 -0.0229951159 0.0256754374 0.0084510373 0.038414551
## 2019-10-31 1.049761e-01 0.0232034080 0.0331682185 0.0307389799 0.143946776
## 2019-11-29 7.469393e-02 0.0134958216 0.0349734187 0.0577613121 0.076031468
## 2019-12-31 9.420386e-02 0.0257863501 0.0242708650 0.0409012583 0.082162945
## 2020-01-31 5.260186e-02 0.0834803026 0.0701847613 0.0764559272 0.004790935
## 2020-02-28 -1.218298e-01 -0.0642332026 -0.0684583881 -0.0467645645 0.133627008
## 2020-03-31 -7.231458e-02 0.0344213022 -0.1413302909 -0.0268999738 -0.024248383
## 2020-04-30 1.444240e-01 0.2381504762 0.1482721504 0.1278001955 0.103279394
## 2020-05-29 8.166634e-02 -0.0128673719 0.0578074412 0.0250743402 0.194461773
## 2020-06-30 1.374869e-01 0.1218341331 -0.0107723947 0.1048636958 0.068216737
## 2020-07-31 1.528342e-01 0.1372488933 0.0478935093 0.0073434783 0.111189279
## 2020-08-31 1.960347e-01 0.0866005735 0.0971012733 0.0978091463 0.231105651
## 2020-09-30 -1.081715e-01 -0.0916533253 -0.1061510484 -0.0697755420 0.011895622
## 2020-10-30 -6.188790e-02 -0.0364089187 0.0980592246 -0.0380860869 -0.076501577
## 2020-11-30 9.120447e-02 0.0425228214 0.0826846313 0.0583260236 0.066921703
## 2020-12-31 1.084719e-01 0.0276719582 -0.0050446707 0.0382644593 -0.025900138
## 2021-01-29 -5.516434e-03 -0.0156985929 0.0467581507 0.0419975133 -0.005010660
## 2021-02-26 -8.306878e-02 -0.0359675607 0.1039616651 0.0041093923 0.054293292
## 2021-03-31 7.312794e-03 0.0003717151 0.0154773482 0.0144828538 -0.026723444
## 2021-04-30 7.345261e-02 0.1139202399 0.1527899506 0.0672862257 0.117298132
## 2021-05-28 -5.181625e-02 -0.0730764715 0.0005972615 -0.0076563812 0.079070956
## 2021-06-30 9.450041e-02 0.0651836137 0.0385416841 0.0815696193 0.208332176
## 2021-07-30 6.295802e-02 -0.0332696301 0.0760718259 0.0504237665 -0.025494177
## 2021-08-31 4.161115e-02 0.0421339363 0.0730046370 0.0597687493 0.138204218
## 2021-09-30 -7.046196e-02 -0.0550034409 -0.0875715985 -0.0684063567 -0.077484809
## 2021-10-29 5.700145e-02 0.0262547651 0.1066948244 0.1623663279 0.210396226
## 2021-11-30 9.991944e-02 0.0391473463 -0.0400331702 -0.0012829330 0.245338378
## 2021-12-31 7.160304e-02 -0.0505062029 0.0155158795 0.0171842756 -0.105149501
## 2022-01-31 -1.583687e-02 -0.1085098142 -0.0640855155 -0.0783347892 -0.183267302
## 2022-02-28 -5.558243e-02 0.0263230079 -0.0059684935 -0.0379218392 -0.004133389
## 2022-03-31 5.588256e-02 0.0596239190 0.0346687796 0.0313647704 0.112576066
## 2022-04-29 -1.021778e-01 -0.2711856801 -0.1944949563 -0.1052127918 -0.386065718
## 2022-05-31 -5.603715e-02 -0.0333130879 -0.0081002444 -0.0182427033 0.006717054
## 2022-06-30 -8.493679e-02 -0.1238178226 -0.0417811094 -0.0569094952 -0.208219251
## 2022-07-29 1.728043e-01 0.2394860591 0.0643327818 0.0890145524 0.180791933
## 2022-08-31 -3.170531e-02 -0.0625299224 -0.0663689826 -0.0689890162 -0.185089175
## 2022-09-30 -1.289442e-01 -0.1149865758 -0.1268135641 -0.1157104507 -0.217576782
## 2022-10-31 1.039559e-01 -0.0981105335 -0.0156181196 -0.0033116138 0.106044016
## 2022-11-30 -3.358539e-02 -0.0593198454 0.0692745859 0.0973292048 0.226462224
## 2022-12-30 -1.304189e-01 -0.1391406409 -0.1339680471 -0.0619238007 -0.146693898
## 2023-01-31 1.048292e-01 0.2051735029 0.1182713718 0.0327737138 0.290330233
## 2023-02-28 2.291836e-02 -0.0902516639 -0.1007320671 0.0089774528 0.172531573
## 2023-03-31 1.121214e-01 0.0918019370 0.1412534582 0.1448633325 0.179536504
## 2023-04-28 2.857503e-02 0.0206963031 0.0397753663 0.0636925942 -0.001008562
## 2023-05-31 4.502900e-02 0.1340765703 0.1310217737 0.0686913393 0.310008230
## 2023-06-30 9.014254e-02 0.0779864104 -0.0196453740 0.0363308679 0.111729806
## 2023-07-31 1.270424e-02 0.0251489708 0.0956332086 -0.0136597384 0.099530379
## 2023-08-31 -4.330828e-02 0.0318772770 0.0313567365 -0.0224764324 0.054674223
## 2023-09-29 -9.285921e-02 -0.0821945627 -0.0408676111 -0.0373307552 -0.126218621
## 2023-10-31 -2.573121e-03 0.0458940197 -0.0509540320 0.0684204856 -0.064546261
## 2023-11-30 1.077597e-01 0.0931972815 0.0665315349 0.1159548882 0.137050219
## 2023-12-29 1.349115e-02 0.0392628771 0.0510207317 -0.0076029900 0.057262953
## 2024-01-31 -4.314472e-02 0.0212288659 0.0061543725 0.0557006192 0.217059109
## 2024-02-29 -1.871708e-02 0.1300782611 -0.0143479008 0.0414473068 0.251388523
## 2024-03-28 -5.264849e-02 0.0202729146 0.0855200183 0.0169714852 0.132939730
## 2024-04-30 -6.728636e-03 -0.0302797899 0.0781717646 -0.0775402245 -0.044746762
## 2024-05-31 1.224142e-01 0.0081949152 0.0550640574 0.0659665505 0.238127759
## 2024-06-28 9.125908e-02 0.0910037984 0.0540904291 0.0738550798 0.119508647
## 2024-07-31 5.298210e-02 -0.0329830511 -0.0576203059 -0.0661284168 -0.054220221
## 2024-08-30 3.184107e-02 -0.0464130352 -0.0475463938 -0.0010955424 0.019883141
## 2024-09-30 1.731639e-02 0.0429307038 0.0138344874 0.0310605415 0.017278023
## 2024-10-31 -3.090171e-02 0.0003755644 0.0323672883 -0.0572677075 0.089122582
## 2024-11-29 5.041441e-02 0.1091142212 -0.0128216006 0.0432437544 0.040520675
## 2024-12-31 5.368794e-02 0.0538418744 0.1117945335 -0.0046392779 -0.028993067
## 2025-01-31 -5.930757e-02 0.0800742352 0.0765953379 -0.0153966144 -0.111926720
## 2025-02-28 2.554332e-02 -0.1130190481 -0.1771598371 -0.0425088975 0.039598581
## 2025-03-31 -8.501339e-02 -0.1095146233 -0.0963048741 -0.0559454919 -0.141937977
## 2025-04-30 -4.432099e-02 -0.0311757737 0.0293916011 0.0515781678 0.004970039
## 2025-05-30 -5.507320e-02 0.1058429765 0.0717033646 0.1542991101 0.215623787
## 2025-06-30 2.128063e-02 0.0677922335 0.0271278081 0.0774059246 0.156363843
## 2025-07-31 1.162973e-02 0.0649401278 0.0836138206 0.0700446416 0.118521158
## 2025-08-29 1.130063e-01 -0.0220690894 0.1018127593 -0.0499173132 -0.020963852
## 2025-09-30 9.247086e-02 -0.0420508819 0.1324383072 0.0219793529 0.068827314
## 2025-10-31 5.998000e-02 0.1063983418 0.1459463949 -0.0002704468 0.081830505
## 2025-11-28 3.185140e-02 -0.0460872441 0.1274272801 -0.0492393464 -0.134540851
## 2025-12-31 -2.538676e-02 -0.0103440016 -0.0192879324 -0.0171996077 0.052337279
## 2026-01-30 -4.660755e-02 0.0360797899 0.0758568760 -0.1168400918 0.024522560
## 2026-02-27 1.888634e-02 -0.1306104769 -0.0834380747 -0.0890317282 -0.075731212
## 2026-03-31 -4.012359e-02 -0.0082721954 -0.0814761422 -0.0591853948 -0.015817020
## 2026-04-30 6.690241e-02 0.2411208709 0.2862693112 0.0967654239 0.134813596
## 2026-05-29 1.407069e-01 0.0208333724 -0.0145314204 0.1012164995 0.056356344
## 2026-06-30 -7.552395e-02 -0.1270913683 -0.0627280388 -0.1881487760 -0.052589421
## 2026-07-31 6.537844e-02 0.1305584955 0.0093525153 0.2198030688 0.003293132
## 2026-08-31 2.624055e-02 -0.0444601240 -0.0614012657 0.0895285199 0.095106386
## 2026-09-30 4.977405e-02 -0.0417414816 0.0164150504 0.0109980930 0.034962527
## 2026-10-30 3.426886e-02 -0.0359588805 -0.2027075224 0.0174141658 -0.109758595
## 2026-11-13 3.397698e-02 0.1038782327 0.0501208308 0.0037675520 -0.228422766
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(.2, .2, .2, .2, .2))
## # A tibble: 1 × 5
## AAPL AMZN GOOG MSFT NVDA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.166 0.214 0.164 0.151 0.305
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.2, .2, .2, .2, .2)) %>%
# Transform to Long Form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add Weights
add_column(weight = c(.2, .2, .2, .2, .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?
NVDA (NVIDIA) is the largest contributor to my portfolio’s volatility at approximately 30.5%, despite only having a 20% portfolio weight. Amazon is the second-largest contributor at around 21.5%, while Apple, Google, and Microsoft contribute less than their portfolio weights. I think my portfolio has some risk concentration in NVIDIA because it contributes significantly more volatility than the other stocks. Since all five stocks are technology-focused, my portfolio also has exposure to similar market risks. Diversifying into other sectors could help reduce this concentration.