# 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 ("DLR", "NVDA", "GOOG", "MSFT", "AMZN")
prices <- tq_get(x = symbols,
get = "stockprices",
from = "2012-12-31",
to = "2026-11-25")
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
## AMZN DLR GOOG MSFT NVDA
## 2013-01-31 0.0566799395 2.944708e-04 0.0660630027 0.027328195 0.000000000
## 2013-02-28 -0.0046435024 -1.378895e-02 0.0584794530 0.020915013 0.038221778
## 2013-03-28 0.0083654162 1.042827e-02 -0.0087879274 0.028720208 0.013338444
## 2013-04-30 -0.0487507497 5.254770e-02 0.0375394516 0.145777042 0.070706072
## 2013-05-31 0.0588686246 -1.464991e-01 0.0550323191 0.059941251 0.054652203
## 2013-06-28 0.0310507506 1.432681e-02 0.0104477514 -0.010368679 -0.030167192
## 2013-07-31 0.0813355350 -9.828177e-02 0.0083480122 -0.081394296 0.028091526
## 2013-08-30 -0.0695574090 5.591396e-03 -0.0471076713 0.054854306 0.026270464
## 2013-09-30 0.1067688897 -3.128360e-02 0.0336807629 -0.003599664 0.053460320
## 2013-10-31 0.1521839116 -1.080845e-01 0.1626137969 0.062037773 -0.024066361
## 2013-11-29 0.0781496860 -8.851178e-03 0.0277601740 0.081563032 0.032034691
## 2013-12-31 0.0130490386 5.585397e-02 0.0560803814 -0.019063905 0.026567582
## 2014-01-31 -0.1059765119 3.736308e-02 0.0523738431 0.011428938 -0.020177249
## 2014-02-28 0.0094619003 6.031324e-02 0.0289427020 0.019815373 0.162107387
## 2014-03-31 -0.0737086161 -4.433126e-03 -0.0863761589 0.067616786 -0.025903972
## 2014-04-30 -0.1007565303 6.010487e-03 -0.0559561857 -0.014498702 0.030788728
## 2014-05-30 0.0273091844 7.397449e-02 0.0611852662 0.020307868 0.032886181
## 2014-06-30 0.0383836202 2.850148e-02 0.0271165419 0.018393366 -0.024508249
## 2014-07-31 -0.0369768154 9.901331e-02 -0.0064175543 0.034413047 -0.057729628
## 2014-08-29 0.0799468404 1.326765e-02 0.0000000000 0.057485001 0.110059860
## 2014-09-30 -0.0502010184 -3.242898e-02 0.0100266335 0.020264521 -0.052782715
## 2014-10-31 -0.0540982347 1.007167e-01 -0.0321735302 0.012646093 0.057399262
## 2014-11-28 0.1031187277 1.838354e-02 -0.0313402055 0.024438904 0.074852058
## 2014-12-31 -0.0872368614 -4.599288e-02 -0.0288909280 -0.028858181 -0.044863384
## 2015-01-30 0.1330922557 9.544729e-02 0.0153078143 -0.139546424 -0.043318877
## 2015-02-27 0.0697992426 -9.424148e-02 0.0437063771 0.089035630 0.142698930
## 2015-03-31 -0.0214295755 6.898040e-03 -0.0188002443 -0.075529569 -0.052582546
## 2015-04-30 0.1253212736 -3.942692e-02 -0.0169025711 0.179201261 0.058908852
## 2015-05-29 0.0175090293 4.063890e-02 -0.0097807401 -0.030803652 0.001459881
## 2015-06-30 0.0112589814 2.280750e-02 -0.0220412924 -0.059571652 -0.095717032
## 2015-07-31 0.2111621090 -3.681161e-02 0.1839181169 0.056151479 -0.007987976
## 2015-08-31 -0.0443525782 -1.489209e-02 -0.0118341631 -0.063951211 0.123595489
## 2015-09-30 -0.0019516837 4.495796e-02 -0.0160275536 0.016860677 0.092150855
## 2015-10-30 0.2010808743 1.242260e-01 0.1555397849 0.173395291 0.140555371
## 2015-11-30 0.0602956777 -2.533144e-02 0.0437523468 0.038685722 0.115404946
## 2015-12-31 0.0165440008 5.967842e-02 0.0216863170 0.020578091 0.038347509
## 2016-01-29 -0.1410054620 5.730562e-02 -0.0212151604 -0.007054447 -0.118048629
## 2016-02-29 -0.0605352209 -1.269258e-02 -0.0627389999 -0.072343963 0.071923927
## 2016-03-31 0.0717834363 1.229348e-01 0.0654275161 0.082036578 0.127654554
## 2016-04-29 0.1053453760 -5.779786e-03 -0.0722728576 -0.102086739 -0.002810696
## 2016-05-31 0.0915002899 8.149247e-02 0.0598052982 0.067842309 0.276388793
## 2016-06-30 -0.0099694639 1.412914e-01 -0.0611189800 -0.035138569 0.006187779
## 2016-07-29 0.0586021229 -4.245185e-02 0.1050872092 0.102267773 0.194443814
## 2016-08-31 0.0135476418 -5.277572e-02 -0.0022659082 0.019880988 0.073468926
## 2016-09-30 0.0848953908 -1.088186e-02 0.0132616827 0.002433611 0.110693435
## 2016-10-31 -0.0583893058 -3.873500e-02 0.0092839896 0.039487813 0.037805432
## 2016-11-30 -0.0509721927 -1.184301e-02 -0.0343615262 0.012391194 0.260525484
## 2016-12-30 -0.0009330556 7.141174e-02 0.0180152561 0.030721266 0.146435452
## 2017-01-31 0.0936394059 9.108228e-02 0.0318398983 0.039598271 0.022602593
## 2017-02-28 0.0258446800 3.431653e-03 0.0326201696 -0.004373571 -0.071875133
## 2017-03-31 0.0479423007 -6.028213e-03 0.0076839413 0.028960598 0.070843160
## 2017-04-28 0.0424566944 7.642809e-02 0.0880997855 0.038718654 -0.043433740
## 2017-05-31 0.0725778018 2.875374e-02 0.0629879035 0.025672738 0.326022186
## 2017-06-30 -0.0271286156 -3.720710e-02 -0.0599348927 -0.013114855 0.001453532
## 2017-07-31 0.0202278808 2.093897e-02 0.0236738518 0.053249356 0.117045185
## 2017-08-31 -0.0072953953 2.567728e-02 0.0094448089 0.033388961 0.042639177
## 2017-09-29 -0.0198260355 7.596737e-03 0.0208390909 -0.003751812 0.053601106
## 2017-10-31 0.1395154056 9.294561e-04 0.0582524162 0.110342214 0.145700571
## 2017-11-30 0.0626577318 -1.480001e-02 0.0046810502 0.016840999 -0.029245106
## 2017-12-29 -0.0062057845 -1.618763e-02 0.0241716297 0.016145813 -0.036583595
## 2018-01-31 0.2156265497 -1.726881e-02 0.1115967423 0.104997903 0.239241062
## 2018-02-28 0.0415536279 -1.065023e-01 -0.0573515143 -0.008450629 -0.014959380
## 2018-03-29 -0.0440034760 5.562796e-02 -0.0683057397 -0.027022892 -0.043968955
## 2018-04-30 0.0788803060 2.937526e-03 -0.0141136247 0.024353165 -0.029312970
## 2018-05-31 0.0397392430 1.679422e-02 0.0643892773 0.059651886 0.115145294
## 2018-06-29 0.0421636787 4.702841e-02 0.0278664132 -0.002329322 -0.062544662
## 2018-07-31 0.0446635734 8.451371e-02 0.0871650785 0.073020564 0.033048376
## 2018-08-31 0.1243079079 2.328142e-02 0.0007637621 0.061088680 0.137075577
## 2018-09-28 -0.0048359814 -9.151014e-02 -0.0205010648 0.017997843 0.001210571
## 2018-10-31 -0.2258869989 -8.552567e-02 -0.1028991915 -0.068387539 -0.287373606
## 2018-11-30 0.0560700324 1.080301e-01 0.0162679099 0.041797795 -0.253667462
## 2018-12-31 -0.1180514843 -6.788984e-02 -0.0552430716 -0.087790324 -0.202283237
## 2019-01-31 0.1348080312 1.665972e-02 0.0750917580 0.027768933 0.073974076
## 2019-02-28 -0.0469930640 4.317480e-02 0.0031749054 0.074511115 0.071593957
## 2019-03-29 0.0824420184 6.003068e-02 0.0465714634 0.051409304 0.151869962
## 2019-04-30 0.0786806224 -1.089945e-02 0.0128463272 0.101963425 0.007987774
## 2019-05-31 -0.0818753491 8.495106e-05 -0.0740705120 -0.050746920 -0.288680084
## 2019-06-28 0.0646557767 9.681552e-03 -0.0208012967 0.079843461 0.192591392
## 2019-07-31 -0.0142806686 -2.955185e-02 0.1183224706 0.017097182 0.026972669
## 2019-08-30 -0.0496880810 7.794161e-02 -0.0237704490 0.014924715 -0.006207882
## 2019-09-30 -0.0229951159 5.721891e-02 0.0256754374 0.008451039 0.038414441
## 2019-10-31 0.0232034080 -2.156980e-02 0.0331682797 0.030739282 0.143946887
## 2019-11-29 0.0134958216 -4.912475e-02 0.0349733575 0.057761393 0.076031645
## 2019-12-31 0.0257863501 -7.825634e-04 0.0242706346 0.040901086 0.082162686
## 2020-01-31 0.0834803026 2.678033e-02 0.0701850991 0.076455896 0.004791179
## 2020-02-28 -0.0642332026 -2.369507e-02 -0.0684587255 -0.046764679 0.133626774
## 2020-03-31 0.0344213022 1.532626e-01 -0.1413300609 -0.026900182 -0.024248312
## 2020-04-30 0.2381504762 7.340319e-02 0.1482721504 0.127800324 0.103279328
## 2020-05-29 -0.0128673719 -4.047656e-02 0.0578073334 0.025074251 0.194461838
## 2020-06-30 0.1218341331 -1.753671e-03 -0.0107722869 0.104864003 0.068216535
## 2020-07-31 0.1372488933 1.219414e-01 0.0478935093 0.007343698 0.111189571
## 2020-08-31 0.0866005735 -3.093293e-02 0.0971010848 0.097808628 0.231105345
## 2020-09-30 -0.0916533253 -5.108913e-02 -0.1061508599 -0.069775286 0.011895766
## 2020-10-30 -0.0364089187 -1.690430e-02 0.0980591296 -0.038086123 -0.076501353
## 2020-11-30 0.0425228214 -6.847323e-02 0.0826849013 0.058325965 0.066921479
## 2020-12-31 0.0276719582 4.334984e-02 -0.0050447577 0.038264315 -0.025900140
## 2021-01-29 -0.0156985929 3.132940e-02 0.0467579789 0.041997683 -0.005010365
## 2021-02-26 -0.0359675607 -6.619295e-02 0.1039618246 0.004109535 0.054293140
## 2021-03-31 0.0003717151 5.299111e-02 0.0154771237 0.014482605 -0.026723299
## 2021-04-30 0.1139202399 9.133906e-02 0.1527900356 0.067286333 0.117297980
## 2021-05-28 -0.0730764715 -1.798231e-02 0.0005972615 -0.007656334 0.079070892
## 2021-06-30 0.0651836137 -1.066357e-04 0.0385416865 0.081569341 0.208332272
## 2021-07-30 -0.0332696301 2.429361e-02 0.0760718874 0.050423761 -0.025494175
## 2021-08-31 0.0421339363 6.132657e-02 0.0730045311 0.059768757 0.138204206
## 2021-09-30 -0.0550034409 -1.188580e-01 -0.0875714926 -0.068406399 -0.077484710
## 2021-10-29 0.0262547651 8.845857e-02 0.1066950322 0.162366700 0.210396041
## 2021-11-30 0.0391473463 6.102332e-02 -0.0400333780 -0.001282912 0.245338378
## 2021-12-31 -0.0505062029 5.978963e-02 0.0155157730 0.017183996 -0.105149631
## 2022-01-31 -0.1085098142 -1.699263e-01 -0.0640855225 -0.078334419 -0.183267015
## 2022-02-28 0.0263230079 -1.008065e-01 -0.0059682658 -0.037922036 -0.004133467
## 2022-03-31 0.0596239190 5.876804e-02 0.0346687757 0.031364510 0.112575847
## 2022-04-29 -0.2711856801 3.001045e-02 -0.1944949996 -0.105212495 -0.386065577
## 2022-05-31 -0.0333130879 -4.571871e-02 -0.0081003789 -0.018242745 0.006716951
## 2022-06-30 -0.1238178226 -6.284458e-02 -0.0417810418 -0.056909569 -0.208219212
## 2022-07-29 0.2394860591 1.997944e-02 0.0643329138 0.089014645 0.180791996
## 2022-08-31 -0.0625299224 -6.891213e-02 -0.0663692558 -0.068989053 -0.185089238
## 2022-09-30 -0.1149865758 -2.101526e-01 -0.1268135832 -0.115710287 -0.217576798
## 2022-10-31 -0.0981105335 1.073060e-02 -0.0156180408 -0.003311760 0.106044095
## 2022-11-30 -0.0593198454 1.149306e-01 0.0692746672 0.097329180 0.226461884
## 2022-12-30 -0.1391406409 -1.036881e-01 -0.1339681339 -0.061923843 -0.146693624
## 2023-01-31 0.2051735029 1.337558e-01 0.1182714586 0.032773638 0.290330103
## 2023-02-28 -0.0902516639 -9.502219e-02 -0.1007319818 0.008977456 0.172531769
## 2023-03-31 0.0918019370 -4.689034e-02 0.1412533729 0.144863439 0.179536504
## 2023-04-28 0.0206963031 8.508036e-03 0.0397754375 0.063692841 -0.001008493
## 2023-05-31 0.1340765703 3.283861e-02 0.1310218274 0.068691221 0.310008464
## 2023-06-30 0.0779864104 1.172743e-01 -0.0196454989 0.036330630 0.111729503
## 2023-07-31 0.0251489708 9.021152e-02 0.0956332086 -0.013659516 0.099530543
## 2023-08-31 0.0318772770 5.540949e-02 0.0313567365 -0.022476518 0.054674214
## 2023-09-29 -0.0821945627 -7.516158e-02 -0.0408674943 -0.037330794 -0.126218864
## 2023-10-31 0.0458940197 2.722493e-02 -0.0509541488 0.068420424 -0.064546174
## 2023-11-30 0.0931972815 1.097094e-01 0.0665318800 0.115955072 0.137050137
## 2023-12-29 0.0392628771 -2.176463e-02 0.0510202774 -0.007603199 0.057262957
## 2024-01-31 0.0212288659 4.276391e-02 0.0061546991 0.055700578 0.217059124
## 2024-02-29 0.1300782611 4.421658e-02 -0.0143478977 0.041447519 0.251388489
## 2024-03-28 0.0202729146 -1.053587e-02 0.0855196967 0.016971540 0.132939827
## 2024-04-30 -0.0302797899 -3.720093e-02 0.0781717722 -0.077540318 -0.044746673
## 2024-05-31 0.0081949152 4.618561e-02 0.0550640624 0.065966719 0.238127601
## 2024-06-28 0.0910037984 5.331361e-02 0.0540906016 0.073854961 0.119508840
## 2024-07-31 -0.0329830511 -1.698002e-02 -0.0576203898 -0.066128455 -0.054220280
## 2024-08-30 -0.0464130352 1.408190e-02 -0.0475464869 -0.001095649 0.019883012
## 2024-09-30 0.0429307038 7.286577e-02 0.0138345806 0.031060651 0.017278087
## 2024-10-31 0.0003755644 9.652849e-02 0.0323671993 -0.057267749 0.089122582
## 2024-11-29 0.1091142212 9.345692e-02 -0.0128214215 0.043243818 0.040520675
## 2024-12-31 0.0538418744 -9.197852e-02 0.1117945240 -0.004639362 -0.028993067
## 2025-01-31 0.0800742352 -7.899998e-02 0.0765952573 -0.015396640 -0.111926783
## 2025-02-28 -0.1130190481 -4.710724e-02 -0.1771600154 -0.042508783 0.039598706
## 2025-03-31 -0.1095146233 -7.863994e-02 -0.0963047940 -0.055945554 -0.141938038
## 2025-04-30 -0.0311757737 1.136727e-01 0.0293916993 0.051578150 0.004970039
## 2025-05-30 0.1058429765 6.615678e-02 0.0717033646 0.154299173 0.215623787
## 2025-06-30 0.0677922335 2.318511e-02 0.0271278081 0.077405972 0.156363746
## 2025-07-31 0.0649401278 1.203086e-02 0.0836137412 0.070044274 0.118521255
## 2025-08-29 -0.0220690894 -5.116203e-02 0.1018128387 -0.049917090 -0.020963852
## 2025-09-30 -0.0420508819 3.783535e-02 0.1324383072 0.021979269 0.068827314
## 2025-10-31 0.1063983418 -1.439033e-02 0.1459463949 -0.000270374 0.081830505
## 2025-11-28 -0.0460872441 -6.228391e-02 0.1274272801 -0.049239285 -0.134540851
## 2025-12-31 -0.0103440016 -2.639814e-02 -0.0192879324 -0.017199590 0.052337197
## 2026-01-30 0.0360797899 7.013409e-02 0.0758568760 -0.116840091 0.024522642
## 2026-02-27 -0.1306104769 6.559253e-02 -0.0834380747 -0.089031733 -0.075731212
## 2026-03-31 -0.0082721954 2.362956e-02 -0.0814761422 -0.059185407 -0.015817020
## 2026-04-30 0.2411208709 1.088834e-01 0.2862693112 0.096765462 0.134813596
## 2026-05-29 0.0208333724 -5.598222e-02 -0.0145314204 0.101216431 0.056356344
## 2026-06-30 -0.1270913683 -4.975804e-02 -0.0627280388 -0.188148733 -0.052589421
## 2026-07-31 0.1305584955 4.858333e-02 0.0093525153 0.219803032 0.003293132
## 2026-08-31 -0.0444601240 -1.566426e-02 -0.0614012657 0.089528560 0.095106386
## 2026-09-30 -0.0417414816 -4.889632e-02 0.0164150504 0.010998059 0.034962527
## 2026-10-30 -0.0359588805 2.902822e-02 -0.2027075224 0.017414121 -0.109758595
## 2026-11-24 0.0484829732 -8.895515e-02 -0.0059121105 -0.048727163 -0.275378574
calculate_componet_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_componet_contribution(w = c(.2, .2, .2, .2,.2))
## # A tibble: 1 × 5
## AMZN DLR GOOG MSFT NVDA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.227 0.112 0.175 0.16 0.327
plot_data <- asset_returns_wide_tbl %>%
calculate_componet_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 is the largest
contributor to my portfolio volatility, accounting for 32.7% of the
total risk, even though it represents only 20% of the portfolio. AMZN is
the second-largest contributor at 22.7%. I would say that the portfolio
risk is somewhat concentrated in NVDA, but not entirely, since the other
assets also contribute to overall volatility. Overall, the portfolio is
still diversified across the five stocks, although NVDA has a greater
impact on its risk.