# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
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("WMT", "TGT", "LOW", "GOOG", "AMZN")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2025-12-31")
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 GOOG LOW TGT WMT
## 2013-01-31 0.0566799395 0.0660632143 0.076811127 0.0207400316 0.0248966915
## 2013-02-28 -0.0046435024 0.0584792542 -0.001047516 0.0470669615 0.0117955252
## 2013-03-28 0.0083654162 -0.0087877335 -0.006047041 0.0836041494 0.0620728638
## 2013-04-30 -0.0487507497 0.0375393543 0.017304968 0.0303600385 0.0378942976
## 2013-05-31 0.0588686246 0.0550324079 0.091707632 -0.0099609366 -0.0317802606
## 2013-06-28 0.0310507506 0.0104476626 -0.029154870 -0.0092516751 -0.0046874004
## 2013-07-31 0.0813355350 0.0083480993 0.090229000 0.0341190110 0.0452739768
## 2013-08-30 -0.0695574090 -0.0471077584 0.027435460 -0.1118615142 -0.0596995788
## 2013-09-30 0.1067688897 0.0336809394 0.038322237 0.0105273466 0.0133388632
## 2013-10-31 0.1521839116 0.1626135454 0.048353994 0.0125808881 0.0370290146
## 2013-11-29 0.0781496860 0.0277602490 -0.047304393 -0.0069134825 0.0540190966
## 2013-12-31 0.0130490386 0.0560804504 0.042673506 -0.0103775114 -0.0232523550
## 2014-01-31 -0.1059765119 0.0523737741 -0.064313085 -0.1106957466 -0.0523036391
## 2014-02-28 0.0094619003 0.0289427656 0.077696830 0.1066819598 0.0002679101
## 2014-03-31 -0.0737086161 -0.0863761532 -0.022845742 -0.0329973431 0.0293261985
## 2014-04-30 -0.1007565303 -0.0559562551 -0.059240725 0.0202853825 0.0420195524
## 2014-05-30 0.0273091844 0.0611851973 0.025165603 -0.0769025640 -0.0314090939
## 2014-06-30 0.0383836202 0.0271166108 0.019144236 0.0207485829 -0.0223929464
## 2014-07-31 -0.0369768154 -0.0064174868 0.001900449 0.0279074741 -0.0200475542
## 2014-08-29 0.0799468404 0.0000000000 0.092932857 0.0169976350 0.0323255811
## 2014-09-30 -0.0502010184 0.0100264991 0.007777453 0.0425320963 0.0127658877
## 2014-10-31 -0.0540982347 -0.0321733943 0.082162362 -0.0138158949 -0.0026188538
## 2014-11-28 0.1031187277 -0.0313402746 0.109669574 0.1874996810 0.1378162421
## 2014-12-31 -0.0872368614 -0.0288908547 0.074980304 0.0254836670 -0.0135738090
## 2015-01-30 0.1330922557 0.0153076687 -0.011759287 -0.0307675940 -0.0105352046
## 2015-02-27 0.0697992426 0.0437065185 0.089308788 0.0496023588 -0.0124325497
## 2015-03-31 -0.0214295755 -0.0188004544 0.004041092 0.0659772729 -0.0142312821
## 2015-04-30 0.1253212736 -0.0169023585 -0.074071127 -0.0402789394 -0.0524140810
## 2015-05-29 0.0175090293 -0.0097807394 0.016134101 0.0128398613 -0.0433507981
## 2015-06-30 0.0112589814 -0.0220412908 -0.043965090 0.0287066151 -0.0460137309
## 2015-07-31 0.2111621090 0.1839181660 0.039220724 0.0026917173 0.0146950127
## 2015-08-31 -0.0443525782 -0.0118342862 -0.002743207 -0.0448225247 -0.0993585049
## 2015-09-30 -0.0019516837 -0.0160275536 -0.003620246 0.0121514311 0.0016977313
## 2015-10-30 0.2010808743 0.1555397849 0.072673110 -0.0189944151 -0.1246694884
## 2015-11-30 0.0602956777 0.0437524505 0.036832108 -0.0547336759 0.0275684397
## 2015-12-31 0.0165440008 0.0216862133 -0.007337672 0.0015160979 0.0492992507
## 2016-01-29 -0.1410054620 -0.0212150567 -0.055313411 -0.0026201001 0.0793146423
## 2016-02-29 -0.0605352209 -0.0627392139 -0.059361447 0.0882422175 -0.0003015636
## 2016-03-31 0.0717834363 0.0654275231 0.114866718 0.0476664132 0.0392710508
## 2016-04-29 0.1053453760 -0.0722726430 0.007186378 -0.0343708594 -0.0239376812
## 2016-05-31 0.0915002899 0.0598051871 0.052653750 -0.1372354902 0.0641210321
## 2016-06-30 -0.0099694639 -0.0611192026 -0.012052997 0.0150078447 0.0311569240
## 2016-07-29 0.0586021229 0.1050874317 0.042825507 0.0759578099 -0.0006849048
## 2016-08-31 0.0135476418 -0.0022658078 -0.072053086 -0.0627265269 -0.0143680523
## 2016-09-30 0.0848953908 0.0132614832 -0.058496393 -0.0217478776 0.0094736245
## 2016-10-31 -0.0583893058 0.0092840887 -0.075206079 0.0007277867 -0.0295507996
## 2016-11-30 -0.0509721927 -0.0343614246 0.056866858 0.1251765128 0.0058387473
## 2016-12-30 -0.0009330556 0.0180151545 0.008046668 -0.0670620303 -0.0116437876
## 2017-01-31 0.0936394059 0.0318398016 0.032075530 -0.1135002855 -0.0350399112
## 2017-02-28 0.0258446800 0.0326201727 0.017497702 -0.0835534446 0.0608891859
## 2017-03-31 0.0479423007 0.0076841277 0.100224641 -0.0628496875 0.0234093052
## 2017-04-28 0.0424566944 0.0880996076 0.036163261 0.0118875578 0.0421084520
## 2017-05-31 0.0725778018 0.0629879886 -0.074706312 -0.0018018367 0.0511560019
## 2017-06-30 -0.0271286156 -0.0599349774 -0.015866826 -0.0532517257 -0.0378576603
## 2017-07-31 0.0202278808 0.0236740193 0.003889688 0.0804400759 0.0553876756
## 2017-08-31 -0.0072953953 0.0094448081 -0.046409565 -0.0272906399 -0.0180253514
## 2017-09-29 -0.0198260355 0.0208388483 0.078699013 0.0789561964 0.0008962109
## 2017-10-31 0.1395154056 0.0582526525 0.005237150 0.0005085013 0.1109629215
## 2017-11-30 0.0626577318 0.0046808237 0.041886840 0.0247789181 0.1076143316
## 2017-12-29 -0.0062057845 0.0241717805 0.108665697 0.0855493500 0.0207685919
## 2018-01-31 0.2156265497 0.1115968081 0.123266937 0.1421912611 0.0764917552
## 2018-02-28 0.0415536279 -0.0573515104 -0.156142241 0.0107464189 -0.1691624740
## 2018-03-29 -0.0440034760 -0.0683059587 -0.020751823 -0.0826206537 -0.0056770425
## 2018-04-30 0.0788803060 -0.0141135512 -0.057669920 0.0446460727 -0.0057489504
## 2018-05-31 0.0397392430 0.0643893530 0.142033002 0.0125276585 -0.0629874335
## 2018-06-29 0.0421636787 0.0278665513 0.005876523 0.0433596749 0.0369862065
## 2018-07-31 0.0446635734 0.0871649404 0.043445568 0.0581795386 0.0409484143
## 2018-08-31 0.1243079079 0.0007637621 0.090503334 0.0889779206 0.0774627314
## 2018-09-28 -0.0048359814 -0.0205010003 0.054314338 0.0080812377 -0.0205520816
## 2018-10-31 -0.2258869989 -0.1028992561 -0.182285372 -0.0533178363 0.0656294151
## 2018-11-30 0.0560700324 0.0162678395 -0.008966623 -0.1560245194 -0.0265767856
## 2018-12-31 -0.1180514843 -0.0552429269 -0.021529135 -0.0710991724 -0.0417362936
## 2019-01-31 0.1348080312 0.0750914767 0.045385817 0.0994420603 0.0283647644
## 2019-02-28 -0.0469930640 0.0031750436 0.088803596 0.0038816107 0.0324430592
## 2019-03-29 0.0824420184 0.0465714666 0.040833422 0.0997557449 -0.0094924845
## 2019-04-30 0.0786806224 0.0128464576 0.037215988 -0.0360262648 0.0530142260
## 2019-05-31 -0.0818753491 -0.0740705070 -0.193020196 0.0473595236 -0.0084088352
## 2019-06-28 0.0646557767 -0.0208015802 0.078623324 0.0737790245 0.0854577496
## 2019-07-31 -0.0142806686 0.1183226844 0.010221788 -0.0024275400 -0.0009962745
## 2019-08-30 -0.0496880810 -0.0237704490 0.101209881 0.2218673794 0.0394581764
## 2019-09-30 -0.0229951159 0.0256754374 -0.020166095 -0.0012151101 0.0379537867
## 2019-10-31 0.0232034080 0.0331682797 0.019818727 0.0000000000 -0.0120368029
## 2019-11-29 0.0134958216 0.0349734756 0.049809296 0.1623186740 0.0154858695
## 2019-12-31 0.0257863501 0.0242706317 0.020669703 0.0252756871 0.0023739217
## 2020-01-31 0.0834803026 0.0701850913 -0.025327617 -0.1464847768 -0.0372904948
## 2020-02-28 -0.0642332026 -0.0684587179 -0.086854885 -0.0667809556 -0.0613234671
## 2020-03-31 0.0344213022 -0.1413299772 -0.213873507 -0.1024519625 0.0581103499
## 2020-04-30 0.2381504762 0.1482719517 0.202446429 0.1658368243 0.0674663473
## 2020-05-29 -0.0128673719 0.0578072257 0.218646480 0.1138943442 0.0248287900
## 2020-06-30 0.1218341331 -0.0107720701 0.035940014 -0.0198142951 -0.0351085682
## 2020-07-31 0.1372488933 0.0478934004 0.101002908 0.0484208467 0.0772513601
## 2020-08-31 0.0866005735 0.0971009905 0.100722990 0.1882717070 0.0745886437
## 2020-09-30 -0.0916533253 -0.1061508704 0.007079122 0.0402476814 0.0076052899
## 2020-10-30 -0.0364089187 0.0980593294 -0.044484224 -0.0335905348 -0.0083255848
## 2020-11-30 0.0425228214 0.0826848063 -0.014526254 0.1691404858 0.0963908123
## 2020-12-31 0.0276719582 -0.0050449336 0.029654493 -0.0168512080 -0.0545616155
## 2021-01-29 -0.0156985929 0.0467583226 0.042247212 0.0259452234 -0.0257179187
## 2021-02-26 -0.0359675607 0.1039616568 -0.043485196 0.0160102464 -0.0782173499
## 2021-03-31 0.0003717151 0.0154772726 0.174360831 0.0767328559 0.0486516933
## 2021-04-30 0.1139202399 0.1527896950 0.034363724 0.0453531896 0.0295952250
## 2021-05-28 -0.0730764715 0.0005975810 -0.007261670 0.0938665693 0.0189586643
## 2021-06-30 0.0651836137 0.0385415587 -0.004424415 0.0632653908 -0.0071369608
## 2021-07-30 -0.0332696301 0.0760718874 -0.002464747 0.0768491096 0.0107911270
## 2021-08-31 0.0421339363 0.0730044252 0.056497725 -0.0519787309 0.0418681160
## 2021-09-30 -0.0550034409 -0.0875713867 -0.005064401 -0.0765900007 -0.0606837221
## 2021-10-29 0.0262547651 0.1066948244 0.145649992 0.1265017723 0.0695570947
## 2021-11-30 0.0391473463 -0.0400330621 0.045031499 -0.0592960688 -0.0606289227
## 2021-12-31 -0.0505062029 0.0155157713 0.055235183 -0.0521915352 0.0324796751
## 2022-01-31 -0.1085098142 -0.0640855155 -0.081990409 -0.0487407072 -0.0343093144
## 2022-02-28 0.0263230079 -0.0059681510 -0.071101513 -0.0940888014 -0.0338251901
## 2022-03-31 0.0596239190 0.0346681061 -0.089226442 0.0604566645 0.1008102429
## 2022-04-29 -0.2711856801 -0.1944946923 -0.018267565 0.0745690374 0.0269634667
## 2022-05-31 -0.0333130879 -0.0081002450 -0.012365385 -0.3412236794 -0.1698039857
## 2022-06-30 -0.1238178226 -0.0417811122 -0.111638494 -0.1364658562 -0.0563677339
## 2022-07-29 0.2394860591 0.0643329182 0.097813570 0.1456893025 0.0826081966
## 2022-08-31 -0.0625299224 -0.0663692603 0.013535035 -0.0125340812 0.0081251583
## 2022-09-30 -0.1149865758 -0.1268135127 -0.033148894 -0.0774527512 -0.0217359664
## 2022-10-31 -0.0981105335 -0.0156179594 0.042737164 0.1015460581 0.0929241551
## 2022-11-30 -0.0593198454 0.0692745099 0.086433834 0.0232758947 0.0684915967
## 2022-12-30 -0.1391406409 -0.1339679712 -0.064666861 -0.1141982313 -0.0685300147
## 2023-01-31 0.2051735029 0.1182713718 0.049361470 0.1440934805 0.0145630113
## 2023-02-28 -0.0902516639 -0.1007318965 -0.012077517 -0.0151218001 -0.0121680782
## 2023-03-31 0.0918019370 0.1412532136 -0.028494434 -0.0171790997 0.0408373487
## 2023-04-28 0.0206963031 0.0397754404 0.043540930 -0.0487448794 0.0235919433
## 2023-05-31 0.1340765703 0.1310217737 -0.032768993 -0.1796001404 -0.0237417901
## 2023-06-30 0.0779864104 -0.0196453104 0.115255035 0.0073813007 0.0678438755
## 2023-07-31 0.0251489708 0.0956333764 0.041979779 0.0340608184 0.0169068486
## 2023-08-31 0.0318772770 0.0313563929 -0.016310346 -0.0669316284 0.0206049657
## 2023-09-29 -0.0821945627 -0.0408676158 -0.103395408 -0.1349886235 -0.0166186959
## 2023-10-31 0.0458940197 -0.0509539152 -0.080892795 0.0019875912 0.0215262630
## 2023-11-30 0.0931972815 0.0665318800 0.042430723 0.1990889548 -0.0483954568
## 2023-12-29 0.0392628771 0.0510203866 0.112701705 0.0623594877 0.0162177558
## 2024-01-31 0.0212288659 0.0061544811 -0.039595451 -0.0237311195 0.0470819921
## 2024-02-29 0.1300782611 -0.0143477890 0.122885937 0.1022453705 0.0620582008
## 2024-03-28 0.0202729146 0.0855197979 0.056777474 0.1474205622 0.0296685791
## 2024-04-30 -0.0302797899 0.0781716710 -0.106133759 -0.0960552996 -0.0137219113
## 2024-05-31 0.0081949152 0.0550641510 -0.029827771 -0.0235389336 0.1060151621
## 2024-06-28 0.0910037984 0.0540903453 -0.003757757 -0.0533988871 0.0292220692
## 2024-07-31 -0.0329830511 -0.0576202220 0.112458771 0.0158824541 0.0136416096
## 2024-08-30 -0.0464130352 -0.0475463006 0.012105160 0.0289074559 0.1207587931
## 2024-09-30 0.0429307038 0.0138344862 0.086122400 0.0144761314 0.0445701425
## 2024-10-31 0.0003755644 0.0323671074 -0.029636385 -0.0380563863 0.0147512425
## 2024-11-29 0.1091142212 -0.0128214215 0.039686341 -0.1185485987 0.1210993580
## 2024-12-31 0.0538418744 0.1117945240 -0.098803472 0.0214593434 -0.0213044051
## 2025-01-31 0.0800742352 0.0765952573 0.056616994 0.0199942809 0.0829076642
## 2025-02-28 -0.1130190481 -0.1771600154 -0.044829420 -0.0958942767 0.0045738382
## 2025-03-31 -0.1095146233 -0.0963046959 -0.063981004 -0.1743686309 -0.1134826167
## 2025-04-30 -0.0311757737 0.0293916011 -0.037081937 -0.0762331517 0.1023372540
## 2025-05-30 0.1058429765 0.0717032759 0.009659662 -0.0168450653 0.0174176213
## 2025-06-30 0.0677922335 0.0271278968 -0.017247908 0.0481771190 -0.0095675582
## 2025-07-31 0.0649401278 0.0836137412 0.012937315 0.0185794873 0.0020433225
## 2025-08-29 -0.0220690894 0.1018128387 0.143467569 -0.0352314565 -0.0079256801
## 2025-09-30 -0.0420508819 0.1324383700 -0.026504947 -0.0676692131 0.0608065605
## 2025-10-31 0.1063983418 0.1459463321 -0.048965820 0.0331135761 -0.0184103109
## 2025-11-28 -0.0460872441 0.1274272801 0.018102434 -0.0103830209 0.0882049463
## 2025-12-30 -0.0029629756 -0.0169007268 0.002882657 0.0724591569 0.0147146211
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMZN GOOG LOW TGT WMT
## AMZN 0.0070362759 0.0034746543 0.002209928 0.001642557 0.0008347586
## GOOG 0.0034746543 0.0046544815 0.001712603 0.001256460 0.0006007319
## LOW 0.0022099283 0.0017126030 0.005037341 0.002589632 0.0010103162
## TGT 0.0016425568 0.0012564600 0.002589632 0.006798418 0.0017771637
## WMT 0.0008347586 0.0006007319 0.001010316 0.001777164 0.0027378783
# 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.2, 0.2, 0.2, 0.2, 0.2)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.04918618
# 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 GOOG LOW TGT WMT
## [1,] 0.01235971 0.009513998 0.01021411 0.01143755 0.005660817
rowSums(component_contribution)
## [1] 0.04918618
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## AMZN GOOG LOW TGT WMT
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.251 0.193 0.208 0.233 0.115
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AMZN 0.251
## 2 GOOG 0.193
## 3 LOW 0.208
## 4 TGT 0.233
## 5 WMT 0.115
# Transform data into wide worm
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 GOOG LOW TGT WMT
## 2013-01-31 0.0566799395 0.0660632143 0.076811127 0.0207400316 0.0248966915
## 2013-02-28 -0.0046435024 0.0584792542 -0.001047516 0.0470669615 0.0117955252
## 2013-03-28 0.0083654162 -0.0087877335 -0.006047041 0.0836041494 0.0620728638
## 2013-04-30 -0.0487507497 0.0375393543 0.017304968 0.0303600385 0.0378942976
## 2013-05-31 0.0588686246 0.0550324079 0.091707632 -0.0099609366 -0.0317802606
## 2013-06-28 0.0310507506 0.0104476626 -0.029154870 -0.0092516751 -0.0046874004
## 2013-07-31 0.0813355350 0.0083480993 0.090229000 0.0341190110 0.0452739768
## 2013-08-30 -0.0695574090 -0.0471077584 0.027435460 -0.1118615142 -0.0596995788
## 2013-09-30 0.1067688897 0.0336809394 0.038322237 0.0105273466 0.0133388632
## 2013-10-31 0.1521839116 0.1626135454 0.048353994 0.0125808881 0.0370290146
## 2013-11-29 0.0781496860 0.0277602490 -0.047304393 -0.0069134825 0.0540190966
## 2013-12-31 0.0130490386 0.0560804504 0.042673506 -0.0103775114 -0.0232523550
## 2014-01-31 -0.1059765119 0.0523737741 -0.064313085 -0.1106957466 -0.0523036391
## 2014-02-28 0.0094619003 0.0289427656 0.077696830 0.1066819598 0.0002679101
## 2014-03-31 -0.0737086161 -0.0863761532 -0.022845742 -0.0329973431 0.0293261985
## 2014-04-30 -0.1007565303 -0.0559562551 -0.059240725 0.0202853825 0.0420195524
## 2014-05-30 0.0273091844 0.0611851973 0.025165603 -0.0769025640 -0.0314090939
## 2014-06-30 0.0383836202 0.0271166108 0.019144236 0.0207485829 -0.0223929464
## 2014-07-31 -0.0369768154 -0.0064174868 0.001900449 0.0279074741 -0.0200475542
## 2014-08-29 0.0799468404 0.0000000000 0.092932857 0.0169976350 0.0323255811
## 2014-09-30 -0.0502010184 0.0100264991 0.007777453 0.0425320963 0.0127658877
## 2014-10-31 -0.0540982347 -0.0321733943 0.082162362 -0.0138158949 -0.0026188538
## 2014-11-28 0.1031187277 -0.0313402746 0.109669574 0.1874996810 0.1378162421
## 2014-12-31 -0.0872368614 -0.0288908547 0.074980304 0.0254836670 -0.0135738090
## 2015-01-30 0.1330922557 0.0153076687 -0.011759287 -0.0307675940 -0.0105352046
## 2015-02-27 0.0697992426 0.0437065185 0.089308788 0.0496023588 -0.0124325497
## 2015-03-31 -0.0214295755 -0.0188004544 0.004041092 0.0659772729 -0.0142312821
## 2015-04-30 0.1253212736 -0.0169023585 -0.074071127 -0.0402789394 -0.0524140810
## 2015-05-29 0.0175090293 -0.0097807394 0.016134101 0.0128398613 -0.0433507981
## 2015-06-30 0.0112589814 -0.0220412908 -0.043965090 0.0287066151 -0.0460137309
## 2015-07-31 0.2111621090 0.1839181660 0.039220724 0.0026917173 0.0146950127
## 2015-08-31 -0.0443525782 -0.0118342862 -0.002743207 -0.0448225247 -0.0993585049
## 2015-09-30 -0.0019516837 -0.0160275536 -0.003620246 0.0121514311 0.0016977313
## 2015-10-30 0.2010808743 0.1555397849 0.072673110 -0.0189944151 -0.1246694884
## 2015-11-30 0.0602956777 0.0437524505 0.036832108 -0.0547336759 0.0275684397
## 2015-12-31 0.0165440008 0.0216862133 -0.007337672 0.0015160979 0.0492992507
## 2016-01-29 -0.1410054620 -0.0212150567 -0.055313411 -0.0026201001 0.0793146423
## 2016-02-29 -0.0605352209 -0.0627392139 -0.059361447 0.0882422175 -0.0003015636
## 2016-03-31 0.0717834363 0.0654275231 0.114866718 0.0476664132 0.0392710508
## 2016-04-29 0.1053453760 -0.0722726430 0.007186378 -0.0343708594 -0.0239376812
## 2016-05-31 0.0915002899 0.0598051871 0.052653750 -0.1372354902 0.0641210321
## 2016-06-30 -0.0099694639 -0.0611192026 -0.012052997 0.0150078447 0.0311569240
## 2016-07-29 0.0586021229 0.1050874317 0.042825507 0.0759578099 -0.0006849048
## 2016-08-31 0.0135476418 -0.0022658078 -0.072053086 -0.0627265269 -0.0143680523
## 2016-09-30 0.0848953908 0.0132614832 -0.058496393 -0.0217478776 0.0094736245
## 2016-10-31 -0.0583893058 0.0092840887 -0.075206079 0.0007277867 -0.0295507996
## 2016-11-30 -0.0509721927 -0.0343614246 0.056866858 0.1251765128 0.0058387473
## 2016-12-30 -0.0009330556 0.0180151545 0.008046668 -0.0670620303 -0.0116437876
## 2017-01-31 0.0936394059 0.0318398016 0.032075530 -0.1135002855 -0.0350399112
## 2017-02-28 0.0258446800 0.0326201727 0.017497702 -0.0835534446 0.0608891859
## 2017-03-31 0.0479423007 0.0076841277 0.100224641 -0.0628496875 0.0234093052
## 2017-04-28 0.0424566944 0.0880996076 0.036163261 0.0118875578 0.0421084520
## 2017-05-31 0.0725778018 0.0629879886 -0.074706312 -0.0018018367 0.0511560019
## 2017-06-30 -0.0271286156 -0.0599349774 -0.015866826 -0.0532517257 -0.0378576603
## 2017-07-31 0.0202278808 0.0236740193 0.003889688 0.0804400759 0.0553876756
## 2017-08-31 -0.0072953953 0.0094448081 -0.046409565 -0.0272906399 -0.0180253514
## 2017-09-29 -0.0198260355 0.0208388483 0.078699013 0.0789561964 0.0008962109
## 2017-10-31 0.1395154056 0.0582526525 0.005237150 0.0005085013 0.1109629215
## 2017-11-30 0.0626577318 0.0046808237 0.041886840 0.0247789181 0.1076143316
## 2017-12-29 -0.0062057845 0.0241717805 0.108665697 0.0855493500 0.0207685919
## 2018-01-31 0.2156265497 0.1115968081 0.123266937 0.1421912611 0.0764917552
## 2018-02-28 0.0415536279 -0.0573515104 -0.156142241 0.0107464189 -0.1691624740
## 2018-03-29 -0.0440034760 -0.0683059587 -0.020751823 -0.0826206537 -0.0056770425
## 2018-04-30 0.0788803060 -0.0141135512 -0.057669920 0.0446460727 -0.0057489504
## 2018-05-31 0.0397392430 0.0643893530 0.142033002 0.0125276585 -0.0629874335
## 2018-06-29 0.0421636787 0.0278665513 0.005876523 0.0433596749 0.0369862065
## 2018-07-31 0.0446635734 0.0871649404 0.043445568 0.0581795386 0.0409484143
## 2018-08-31 0.1243079079 0.0007637621 0.090503334 0.0889779206 0.0774627314
## 2018-09-28 -0.0048359814 -0.0205010003 0.054314338 0.0080812377 -0.0205520816
## 2018-10-31 -0.2258869989 -0.1028992561 -0.182285372 -0.0533178363 0.0656294151
## 2018-11-30 0.0560700324 0.0162678395 -0.008966623 -0.1560245194 -0.0265767856
## 2018-12-31 -0.1180514843 -0.0552429269 -0.021529135 -0.0710991724 -0.0417362936
## 2019-01-31 0.1348080312 0.0750914767 0.045385817 0.0994420603 0.0283647644
## 2019-02-28 -0.0469930640 0.0031750436 0.088803596 0.0038816107 0.0324430592
## 2019-03-29 0.0824420184 0.0465714666 0.040833422 0.0997557449 -0.0094924845
## 2019-04-30 0.0786806224 0.0128464576 0.037215988 -0.0360262648 0.0530142260
## 2019-05-31 -0.0818753491 -0.0740705070 -0.193020196 0.0473595236 -0.0084088352
## 2019-06-28 0.0646557767 -0.0208015802 0.078623324 0.0737790245 0.0854577496
## 2019-07-31 -0.0142806686 0.1183226844 0.010221788 -0.0024275400 -0.0009962745
## 2019-08-30 -0.0496880810 -0.0237704490 0.101209881 0.2218673794 0.0394581764
## 2019-09-30 -0.0229951159 0.0256754374 -0.020166095 -0.0012151101 0.0379537867
## 2019-10-31 0.0232034080 0.0331682797 0.019818727 0.0000000000 -0.0120368029
## 2019-11-29 0.0134958216 0.0349734756 0.049809296 0.1623186740 0.0154858695
## 2019-12-31 0.0257863501 0.0242706317 0.020669703 0.0252756871 0.0023739217
## 2020-01-31 0.0834803026 0.0701850913 -0.025327617 -0.1464847768 -0.0372904948
## 2020-02-28 -0.0642332026 -0.0684587179 -0.086854885 -0.0667809556 -0.0613234671
## 2020-03-31 0.0344213022 -0.1413299772 -0.213873507 -0.1024519625 0.0581103499
## 2020-04-30 0.2381504762 0.1482719517 0.202446429 0.1658368243 0.0674663473
## 2020-05-29 -0.0128673719 0.0578072257 0.218646480 0.1138943442 0.0248287900
## 2020-06-30 0.1218341331 -0.0107720701 0.035940014 -0.0198142951 -0.0351085682
## 2020-07-31 0.1372488933 0.0478934004 0.101002908 0.0484208467 0.0772513601
## 2020-08-31 0.0866005735 0.0971009905 0.100722990 0.1882717070 0.0745886437
## 2020-09-30 -0.0916533253 -0.1061508704 0.007079122 0.0402476814 0.0076052899
## 2020-10-30 -0.0364089187 0.0980593294 -0.044484224 -0.0335905348 -0.0083255848
## 2020-11-30 0.0425228214 0.0826848063 -0.014526254 0.1691404858 0.0963908123
## 2020-12-31 0.0276719582 -0.0050449336 0.029654493 -0.0168512080 -0.0545616155
## 2021-01-29 -0.0156985929 0.0467583226 0.042247212 0.0259452234 -0.0257179187
## 2021-02-26 -0.0359675607 0.1039616568 -0.043485196 0.0160102464 -0.0782173499
## 2021-03-31 0.0003717151 0.0154772726 0.174360831 0.0767328559 0.0486516933
## 2021-04-30 0.1139202399 0.1527896950 0.034363724 0.0453531896 0.0295952250
## 2021-05-28 -0.0730764715 0.0005975810 -0.007261670 0.0938665693 0.0189586643
## 2021-06-30 0.0651836137 0.0385415587 -0.004424415 0.0632653908 -0.0071369608
## 2021-07-30 -0.0332696301 0.0760718874 -0.002464747 0.0768491096 0.0107911270
## 2021-08-31 0.0421339363 0.0730044252 0.056497725 -0.0519787309 0.0418681160
## 2021-09-30 -0.0550034409 -0.0875713867 -0.005064401 -0.0765900007 -0.0606837221
## 2021-10-29 0.0262547651 0.1066948244 0.145649992 0.1265017723 0.0695570947
## 2021-11-30 0.0391473463 -0.0400330621 0.045031499 -0.0592960688 -0.0606289227
## 2021-12-31 -0.0505062029 0.0155157713 0.055235183 -0.0521915352 0.0324796751
## 2022-01-31 -0.1085098142 -0.0640855155 -0.081990409 -0.0487407072 -0.0343093144
## 2022-02-28 0.0263230079 -0.0059681510 -0.071101513 -0.0940888014 -0.0338251901
## 2022-03-31 0.0596239190 0.0346681061 -0.089226442 0.0604566645 0.1008102429
## 2022-04-29 -0.2711856801 -0.1944946923 -0.018267565 0.0745690374 0.0269634667
## 2022-05-31 -0.0333130879 -0.0081002450 -0.012365385 -0.3412236794 -0.1698039857
## 2022-06-30 -0.1238178226 -0.0417811122 -0.111638494 -0.1364658562 -0.0563677339
## 2022-07-29 0.2394860591 0.0643329182 0.097813570 0.1456893025 0.0826081966
## 2022-08-31 -0.0625299224 -0.0663692603 0.013535035 -0.0125340812 0.0081251583
## 2022-09-30 -0.1149865758 -0.1268135127 -0.033148894 -0.0774527512 -0.0217359664
## 2022-10-31 -0.0981105335 -0.0156179594 0.042737164 0.1015460581 0.0929241551
## 2022-11-30 -0.0593198454 0.0692745099 0.086433834 0.0232758947 0.0684915967
## 2022-12-30 -0.1391406409 -0.1339679712 -0.064666861 -0.1141982313 -0.0685300147
## 2023-01-31 0.2051735029 0.1182713718 0.049361470 0.1440934805 0.0145630113
## 2023-02-28 -0.0902516639 -0.1007318965 -0.012077517 -0.0151218001 -0.0121680782
## 2023-03-31 0.0918019370 0.1412532136 -0.028494434 -0.0171790997 0.0408373487
## 2023-04-28 0.0206963031 0.0397754404 0.043540930 -0.0487448794 0.0235919433
## 2023-05-31 0.1340765703 0.1310217737 -0.032768993 -0.1796001404 -0.0237417901
## 2023-06-30 0.0779864104 -0.0196453104 0.115255035 0.0073813007 0.0678438755
## 2023-07-31 0.0251489708 0.0956333764 0.041979779 0.0340608184 0.0169068486
## 2023-08-31 0.0318772770 0.0313563929 -0.016310346 -0.0669316284 0.0206049657
## 2023-09-29 -0.0821945627 -0.0408676158 -0.103395408 -0.1349886235 -0.0166186959
## 2023-10-31 0.0458940197 -0.0509539152 -0.080892795 0.0019875912 0.0215262630
## 2023-11-30 0.0931972815 0.0665318800 0.042430723 0.1990889548 -0.0483954568
## 2023-12-29 0.0392628771 0.0510203866 0.112701705 0.0623594877 0.0162177558
## 2024-01-31 0.0212288659 0.0061544811 -0.039595451 -0.0237311195 0.0470819921
## 2024-02-29 0.1300782611 -0.0143477890 0.122885937 0.1022453705 0.0620582008
## 2024-03-28 0.0202729146 0.0855197979 0.056777474 0.1474205622 0.0296685791
## 2024-04-30 -0.0302797899 0.0781716710 -0.106133759 -0.0960552996 -0.0137219113
## 2024-05-31 0.0081949152 0.0550641510 -0.029827771 -0.0235389336 0.1060151621
## 2024-06-28 0.0910037984 0.0540903453 -0.003757757 -0.0533988871 0.0292220692
## 2024-07-31 -0.0329830511 -0.0576202220 0.112458771 0.0158824541 0.0136416096
## 2024-08-30 -0.0464130352 -0.0475463006 0.012105160 0.0289074559 0.1207587931
## 2024-09-30 0.0429307038 0.0138344862 0.086122400 0.0144761314 0.0445701425
## 2024-10-31 0.0003755644 0.0323671074 -0.029636385 -0.0380563863 0.0147512425
## 2024-11-29 0.1091142212 -0.0128214215 0.039686341 -0.1185485987 0.1210993580
## 2024-12-31 0.0538418744 0.1117945240 -0.098803472 0.0214593434 -0.0213044051
## 2025-01-31 0.0800742352 0.0765952573 0.056616994 0.0199942809 0.0829076642
## 2025-02-28 -0.1130190481 -0.1771600154 -0.044829420 -0.0958942767 0.0045738382
## 2025-03-31 -0.1095146233 -0.0963046959 -0.063981004 -0.1743686309 -0.1134826167
## 2025-04-30 -0.0311757737 0.0293916011 -0.037081937 -0.0762331517 0.1023372540
## 2025-05-30 0.1058429765 0.0717032759 0.009659662 -0.0168450653 0.0174176213
## 2025-06-30 0.0677922335 0.0271278968 -0.017247908 0.0481771190 -0.0095675582
## 2025-07-31 0.0649401278 0.0836137412 0.012937315 0.0185794873 0.0020433225
## 2025-08-29 -0.0220690894 0.1018128387 0.143467569 -0.0352314565 -0.0079256801
## 2025-09-30 -0.0420508819 0.1324383700 -0.026504947 -0.0676692131 0.0608065605
## 2025-10-31 0.1063983418 0.1459463321 -0.048965820 0.0331135761 -0.0184103109
## 2025-11-28 -0.0460872441 0.1274272801 0.018102434 -0.0103830209 0.0882049463
## 2025-12-30 -0.0029629756 -0.0169007268 0.002882657 0.0724591569 0.0147146211
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(.data)
# Standard deviation of portfolio
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
# Component contribution
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
## AMZN GOOG LOW TGT WMT
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.251 0.193 0.208 0.233 0.115
Column Chart of Component Contribution
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")
plot_data %>%
ggplot(aes(x = asset, y = contribution)) +
geom_col(fill = "cornflowerblue") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
labs(title = "Percent Contribution to Portfolio Volatility",
x = "Asset",
y = "Contribution")
Column Chart of Component Contribution and Weight
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?
The risk of the portfolio is not determined by a single asset in this particular portfolio. Each stock holds volatility and contributes to the risk of the other stocks. AMZN and TGT contribute more than the remaining assets. AMZN has the largest volatility at 25%, followed by TGT at 23%, LOW at 21%, GOOG at 19%, and WMT at 11%.