# 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("AMZN", "TGT", "WMT", "COST")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-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 COST TGT WMT
## 2013-01-31 0.0566799395 0.0359115455 0.0207400307 0.0248965023
## 2013-02-28 -0.0046435024 -0.0076472084 0.0470674482 0.0117956325
## 2013-03-28 0.0083654162 0.0464885617 0.0836039663 0.0620732462
## 2013-04-30 -0.0487507497 0.0216285477 0.0303598217 0.0378938412
## 2013-05-31 0.0588686246 0.0137927808 -0.0099614050 -0.0317801404
## 2013-06-28 0.0310507506 0.0085380123 -0.0092512902 -0.0046873964
## 2013-07-31 0.0813355350 0.0601082736 0.0341199768 0.0452741266
## 2013-08-30 -0.0695574090 -0.0458222652 -0.1118621795 -0.0596996852
## 2013-09-30 0.1067688897 0.0290719168 0.0105270757 0.0133391612
## 2013-10-31 0.1521839116 0.0242752606 0.0125809791 0.0370286796
## 2013-11-29 0.0781496860 0.0635981330 -0.0069133995 0.0540191100
## 2013-12-31 0.0130490386 -0.0524564636 -0.0103773371 -0.0232518802
## 2014-01-31 -0.1059765119 -0.0575835831 -0.1106957294 -0.0523040767
## 2014-02-28 0.0094619003 0.0414547106 0.1066817617 0.0002679927
## 2014-03-31 -0.0737086161 -0.0448255000 -0.0329974699 0.0293261589
## 2014-04-30 -0.1007565303 0.0351901297 0.0202853891 0.0420197008
## 2014-05-30 0.0273091844 0.0059922618 -0.0769024833 -0.0314095294
## 2014-06-30 0.0383836202 -0.0074400122 0.0207488780 -0.0223925459
## 2014-07-31 -0.0369768154 0.0234827586 0.0279071272 -0.0200475243
## 2014-08-29 0.0799468404 0.0296731192 0.0169975101 0.0323257096
## 2014-09-30 -0.0502010184 0.0344189023 0.0425324601 0.0127655423
## 2014-10-31 -0.0540982347 0.0622568672 -0.0138158143 -0.0026187784
## 2014-11-28 0.1031187277 0.0661377264 0.1874999026 0.1378164824
## 2014-12-31 -0.0872368614 -0.0026066471 0.0254833443 -0.0135738422
## 2015-01-30 0.1330922557 0.0087099738 -0.0307676958 -0.0105352770
## 2015-02-27 0.0697992426 0.0623771108 0.0496019152 -0.0124327239
## 2015-03-31 -0.0214295755 0.0304247270 0.0659772080 -0.0142312702
## 2015-04-30 0.1253212736 -0.0546594843 -0.0402785827 -0.0524138434
## 2015-05-29 0.0175090293 -0.0032205374 0.0128405399 -0.0433512997
## 2015-06-30 0.0112589814 -0.0542543446 0.0287062814 -0.0460136463
## 2015-07-31 0.2111621090 0.0730815785 0.0026915084 0.0146951773
## 2015-08-31 -0.0443525782 -0.0340552998 -0.0448220563 -0.0993589749
## 2015-09-30 -0.0019516837 0.0317642154 0.0121510333 0.0016982735
## 2015-10-30 0.2010808743 0.0895902082 -0.0189946960 -0.1246697822
## 2015-11-30 0.0602956777 0.0232366463 -0.0547334157 0.0275688514
## 2015-12-31 0.0165440008 0.0004956372 0.0015157991 0.0492993099
## 2016-01-29 -0.1410054620 -0.0664311856 -0.0026201718 0.0793146061
## 2016-02-29 -0.0605352209 -0.0045311647 0.0882423546 -0.0003016273
## 2016-03-31 0.0717834363 0.0490981550 0.0476667415 0.0392707537
## 2016-04-29 0.1053453760 -0.0588900077 -0.0343710512 -0.0239372990
## 2016-05-31 0.0915002899 0.0043110328 -0.1372353220 0.0641209956
## 2016-06-30 -0.0099694639 0.0540993625 0.0150075187 0.0311571001
## 2016-07-29 0.0586021229 0.0628096916 0.0759579776 -0.0006853834
## 2016-08-31 0.0135476418 -0.0284741562 -0.0627267078 -0.0143680306
## 2016-09-30 0.0848953908 -0.0609214552 -0.0217477341 0.0094737487
## 2016-10-31 -0.0583893058 -0.0308967844 0.0007276933 -0.0295509222
## 2016-11-30 -0.0509721927 0.0181079153 0.1251764453 0.0058383179
## 2016-12-30 -0.0009330556 0.0644924560 -0.0670616702 -0.0116432023
## 2017-01-31 0.0936394059 0.0237006445 -0.1135002134 -0.0350397556
## 2017-02-28 0.0258446800 0.0802946469 -0.0835536889 0.0608890541
## 2017-03-31 0.0479423007 -0.0550493088 -0.0628499093 0.0234090530
## 2017-04-28 0.0424566944 0.0569664578 0.0118879956 0.0421088139
## 2017-05-31 0.0725778018 0.0587794756 -0.0018017353 0.0511560228
## 2017-06-30 -0.0271286156 -0.1206065172 -0.0532517898 -0.0378578019
## 2017-07-31 0.0202278808 -0.0089189705 0.0804396186 0.0553877616
## 2017-08-31 -0.0072953953 -0.0080365037 -0.0272903910 -0.0180251704
## 2017-09-29 -0.0198260355 0.0470442859 0.0789560970 0.0008959121
## 2017-10-31 0.1395154056 -0.0197316813 0.0005083879 0.1109628735
## 2017-11-30 0.0626577318 0.1383318573 0.0247790384 0.1076144435
## 2017-12-29 -0.0062057845 0.0091214101 0.0855496098 0.0207682546
## 2018-01-31 0.2156265497 0.0459413204 0.1421910628 0.0764924230
## 2018-02-28 0.0415536279 -0.0179107223 0.0107467626 -0.1691629276
## 2018-03-29 -0.0440034760 -0.0130233556 -0.0826208105 -0.0056772976
## 2018-04-30 0.0788803060 0.0452894164 0.0446459974 -0.0057488058
## 2018-05-31 0.0397392430 0.0083746742 0.0125276277 -0.0629870472
## 2018-06-29 0.0421636787 0.0527598637 0.0433594066 0.0369861254
## 2018-07-31 0.0446635734 0.0455082632 0.0581796388 0.0409481163
## 2018-08-31 0.1243079079 0.0663288054 0.0889778214 0.0774627491
## 2018-09-28 -0.0048359814 0.0074786149 0.0080813972 -0.0205518711
## 2018-10-31 -0.2258869989 -0.0269699172 -0.0533177924 0.0656294442
## 2018-11-30 0.0560700324 0.0138982767 -0.1560246285 -0.0265768604
## 2018-12-31 -0.1180514843 -0.1269317655 -0.0710989030 -0.0417362710
## 2019-01-31 0.1348080312 0.0522183063 0.0994419480 0.0283647335
## 2019-02-28 -0.0469930640 0.0216655621 0.0038813567 0.0324430858
## 2019-03-29 0.0824420184 0.1016322026 0.0997558020 -0.0094927016
## 2019-04-30 0.0786806224 0.0139032058 -0.0360261283 0.0530145638
## 2019-05-31 -0.0818753491 -0.0218349835 0.0473593620 -0.0084089462
## 2019-06-28 0.0646557767 0.0980463398 0.0737793655 0.0854577877
## 2019-07-31 -0.0142806686 0.0421257860 -0.0024277971 -0.0009964762
## 2019-08-30 -0.0496880810 0.0693116891 0.2218672667 0.0394582218
## 2019-09-30 -0.0229951159 -0.0228190139 -0.0012151422 0.0379543026
## 2019-10-31 0.0232034080 0.0329300011 0.0000000000 -0.0120371498
## 2019-11-29 0.0134958216 0.0090466311 0.1623187814 0.0154858750
## 2019-12-31 0.0257863501 -0.0198415109 0.0252759098 0.0023737850
## 2020-01-31 0.0834803026 0.0387079454 -0.1464845114 -0.0372903459
## 2020-02-28 -0.0642332026 -0.0810565058 -0.0667811335 -0.0613236860
## 2020-03-31 0.0344213022 0.0140925371 -0.1024521179 0.0581105413
## 2020-04-30 0.2381504762 0.0630695435 0.1658369125 0.0674661309
## 2020-05-29 -0.0128673719 0.0178918490 0.1138940346 0.0248288969
## 2020-06-30 0.1218341331 -0.0171990136 -0.0198140085 -0.0351086937
## 2020-07-31 0.1372488933 0.0731774685 0.0484206233 0.0772515641
## 2020-08-31 0.0866005735 0.0657707005 0.1882717424 0.0745888454
## 2020-09-30 -0.0916533253 0.0208926840 0.0402477659 0.0076050787
## 2020-10-30 -0.0364089187 0.0092731709 -0.0335904697 -0.0083256352
## 2020-11-30 0.0425228214 0.0912038758 0.1691407119 0.0963906675
## 2020-12-31 0.0276719582 -0.0131570143 -0.0168516975 -0.0545612581
## 2021-01-29 -0.0156985929 -0.0668093576 0.0259449240 -0.0257180990
## 2021-02-26 -0.0359675607 -0.0607610556 0.0160105140 -0.0782173849
## 2021-03-31 0.0003717151 0.0628757165 0.0767328490 0.0486517785
## 2021-04-30 0.1139202399 0.0562816646 0.0453533467 0.0295951019
## 2021-05-28 -0.0730764715 0.0164722102 0.0938666178 0.0189582675
## 2021-06-30 0.0651836137 0.0449722742 0.0632654296 -0.0071365773
## 2021-07-30 -0.0332696301 0.0844262092 0.0768490669 0.0107911683
## 2021-08-31 0.0421339363 0.0582398269 -0.0519788094 0.0418682913
## 2021-09-30 -0.0550034409 -0.0135715972 -0.0765899301 -0.0606838456
## 2021-10-29 0.0262547651 0.0913576735 0.1265017387 0.0695571958
## 2021-11-30 0.0391473463 0.0928771116 -0.0592959037 -0.0606289272
## 2021-12-31 -0.0505062029 0.0511727223 -0.0521916296 0.0324794597
## 2022-01-31 -0.1085098142 -0.1167773425 -0.0487406657 -0.0343092140
## 2022-02-28 0.0263230079 0.0290843033 -0.0940889447 -0.0338251362
## 2022-03-31 0.0596239190 0.1034614441 0.0604566147 0.1008103491
## 2022-04-29 -0.2711856801 -0.0781043513 0.0745691342 0.0269632996
## 2022-05-31 -0.0333130879 -0.1314595111 -0.3412238746 -0.1698041845
## 2022-06-30 -0.1238178226 0.0276274388 -0.1364653362 -0.0563672820
## 2022-07-29 0.2394860591 0.1234129523 0.1456889168 0.0826077557
## 2022-08-31 -0.0625299224 -0.0361141927 -0.0125342602 0.0081253261
## 2022-09-30 -0.1149865758 -0.1003084071 -0.0774523383 -0.0217358620
## 2022-10-31 -0.0981105335 0.0618565368 0.1015457524 0.0929241334
## 2022-11-30 -0.0593198454 0.0725756332 0.0232762079 0.0684915368
## 2022-12-30 -0.1391406409 -0.1665906557 -0.1141982991 -0.0685301354
## 2023-01-31 0.2051735029 0.1130548913 0.1440934121 0.0145631794
## 2023-02-28 -0.0902516639 -0.0524475190 -0.0151217121 -0.0121681309
## 2023-03-31 0.0918019370 0.0258716438 -0.0171792921 0.0408373905
## 2023-04-28 0.0206963031 0.0126990069 -0.0487449077 0.0235919382
## 2023-05-31 0.1340765703 0.0185209146 -0.1795999544 -0.0237418143
## 2023-06-30 0.0779864104 0.0510998591 0.0073812174 0.0678438763
## 2023-07-31 0.0251489708 0.0405677172 0.0340608474 0.0169067151
## 2023-08-31 0.0318772770 -0.0186362041 -0.0669315381 0.0206049547
## 2023-09-29 -0.0821945627 0.0281464816 -0.1349887807 -0.0166184345
## 2023-10-31 0.0458940197 -0.0224101386 0.0019877316 0.0215261952
## 2023-11-30 0.0931972815 0.0722445867 0.1990890513 -0.0483956352
## 2023-12-29 0.0392628771 0.1300908554 0.0623594587 0.0162177792
## 2024-01-31 0.0212288659 0.0513779428 -0.0237309856 0.0470820558
## 2024-02-29 0.1300782611 0.0696232620 0.1022450425 0.0620581686
## 2024-03-28 0.0202729146 -0.0152523909 0.1474206462 0.0296686019
## 2024-04-30 -0.0302797899 -0.0117662719 -0.0960553532 -0.0137218293
## 2024-05-31 0.0081949152 0.1136275244 -0.0235390114 0.1060152067
## 2024-06-28 0.0910037984 0.0483261034 -0.0533986753 0.0292219100
## 2024-07-31 -0.0329830511 -0.0320614633 0.0158824483 0.0136416039
## 2024-08-30 -0.0464130352 0.0821516652 0.0289072194 0.1207588853
## 2024-09-30 0.0429307038 -0.0065883578 0.0144762086 0.0445699564
## 2024-10-31 0.0003755644 -0.0140173715 -0.0380562836 0.0147513827
## 2024-11-29 0.1091142212 0.1072738018 -0.1185488492 0.1210993655
## 2024-12-31 0.0538418744 -0.0589211571 0.0214596308 -0.0213044751
## 2025-01-31 0.0800742352 0.0671190206 0.0199941180 0.0829078191
## 2025-02-28 -0.1130190481 0.0688949351 -0.0958941758 0.0045737771
## 2025-03-31 -0.1095146233 -0.1032107172 -0.1743687392 -0.1134826443
## 2025-04-30 -0.0311757737 0.0502300978 -0.0762330254 0.1023372927
## 2025-05-30 0.1058429765 0.0462111097 -0.0168449944 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-14 -0.0398039586 0.0125707933 -0.0183600029 0.0127665785
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMZN COST TGT WMT
## AMZN 0.0070738495 0.002153574 0.001665095 0.0008724543
## COST 0.0021535737 0.003132181 0.001953744 0.0015494821
## TGT 0.0016650948 0.001953744 0.006815404 0.0017950241
## WMT 0.0008724543 0.001549482 0.001795024 0.0027175214
# 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.05121753
# 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.01904414 0.0108725 0.01489149 0.006409396
rowSums(component_contribution)
## [1] 0.05121753
# 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.212 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.212
## 3 TGT 0.291
## 4 WMT 0.125
# 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.0207400307 0.0248965023
## 2013-02-28 -0.0046435024 -0.0076472084 0.0470674482 0.0117956325
## 2013-03-28 0.0083654162 0.0464885617 0.0836039663 0.0620732462
## 2013-04-30 -0.0487507497 0.0216285477 0.0303598217 0.0378938412
## 2013-05-31 0.0588686246 0.0137927808 -0.0099614050 -0.0317801404
## 2013-06-28 0.0310507506 0.0085380123 -0.0092512902 -0.0046873964
## 2013-07-31 0.0813355350 0.0601082736 0.0341199768 0.0452741266
## 2013-08-30 -0.0695574090 -0.0458222652 -0.1118621795 -0.0596996852
## 2013-09-30 0.1067688897 0.0290719168 0.0105270757 0.0133391612
## 2013-10-31 0.1521839116 0.0242752606 0.0125809791 0.0370286796
## 2013-11-29 0.0781496860 0.0635981330 -0.0069133995 0.0540191100
## 2013-12-31 0.0130490386 -0.0524564636 -0.0103773371 -0.0232518802
## 2014-01-31 -0.1059765119 -0.0575835831 -0.1106957294 -0.0523040767
## 2014-02-28 0.0094619003 0.0414547106 0.1066817617 0.0002679927
## 2014-03-31 -0.0737086161 -0.0448255000 -0.0329974699 0.0293261589
## 2014-04-30 -0.1007565303 0.0351901297 0.0202853891 0.0420197008
## 2014-05-30 0.0273091844 0.0059922618 -0.0769024833 -0.0314095294
## 2014-06-30 0.0383836202 -0.0074400122 0.0207488780 -0.0223925459
## 2014-07-31 -0.0369768154 0.0234827586 0.0279071272 -0.0200475243
## 2014-08-29 0.0799468404 0.0296731192 0.0169975101 0.0323257096
## 2014-09-30 -0.0502010184 0.0344189023 0.0425324601 0.0127655423
## 2014-10-31 -0.0540982347 0.0622568672 -0.0138158143 -0.0026187784
## 2014-11-28 0.1031187277 0.0661377264 0.1874999026 0.1378164824
## 2014-12-31 -0.0872368614 -0.0026066471 0.0254833443 -0.0135738422
## 2015-01-30 0.1330922557 0.0087099738 -0.0307676958 -0.0105352770
## 2015-02-27 0.0697992426 0.0623771108 0.0496019152 -0.0124327239
## 2015-03-31 -0.0214295755 0.0304247270 0.0659772080 -0.0142312702
## 2015-04-30 0.1253212736 -0.0546594843 -0.0402785827 -0.0524138434
## 2015-05-29 0.0175090293 -0.0032205374 0.0128405399 -0.0433512997
## 2015-06-30 0.0112589814 -0.0542543446 0.0287062814 -0.0460136463
## 2015-07-31 0.2111621090 0.0730815785 0.0026915084 0.0146951773
## 2015-08-31 -0.0443525782 -0.0340552998 -0.0448220563 -0.0993589749
## 2015-09-30 -0.0019516837 0.0317642154 0.0121510333 0.0016982735
## 2015-10-30 0.2010808743 0.0895902082 -0.0189946960 -0.1246697822
## 2015-11-30 0.0602956777 0.0232366463 -0.0547334157 0.0275688514
## 2015-12-31 0.0165440008 0.0004956372 0.0015157991 0.0492993099
## 2016-01-29 -0.1410054620 -0.0664311856 -0.0026201718 0.0793146061
## 2016-02-29 -0.0605352209 -0.0045311647 0.0882423546 -0.0003016273
## 2016-03-31 0.0717834363 0.0490981550 0.0476667415 0.0392707537
## 2016-04-29 0.1053453760 -0.0588900077 -0.0343710512 -0.0239372990
## 2016-05-31 0.0915002899 0.0043110328 -0.1372353220 0.0641209956
## 2016-06-30 -0.0099694639 0.0540993625 0.0150075187 0.0311571001
## 2016-07-29 0.0586021229 0.0628096916 0.0759579776 -0.0006853834
## 2016-08-31 0.0135476418 -0.0284741562 -0.0627267078 -0.0143680306
## 2016-09-30 0.0848953908 -0.0609214552 -0.0217477341 0.0094737487
## 2016-10-31 -0.0583893058 -0.0308967844 0.0007276933 -0.0295509222
## 2016-11-30 -0.0509721927 0.0181079153 0.1251764453 0.0058383179
## 2016-12-30 -0.0009330556 0.0644924560 -0.0670616702 -0.0116432023
## 2017-01-31 0.0936394059 0.0237006445 -0.1135002134 -0.0350397556
## 2017-02-28 0.0258446800 0.0802946469 -0.0835536889 0.0608890541
## 2017-03-31 0.0479423007 -0.0550493088 -0.0628499093 0.0234090530
## 2017-04-28 0.0424566944 0.0569664578 0.0118879956 0.0421088139
## 2017-05-31 0.0725778018 0.0587794756 -0.0018017353 0.0511560228
## 2017-06-30 -0.0271286156 -0.1206065172 -0.0532517898 -0.0378578019
## 2017-07-31 0.0202278808 -0.0089189705 0.0804396186 0.0553877616
## 2017-08-31 -0.0072953953 -0.0080365037 -0.0272903910 -0.0180251704
## 2017-09-29 -0.0198260355 0.0470442859 0.0789560970 0.0008959121
## 2017-10-31 0.1395154056 -0.0197316813 0.0005083879 0.1109628735
## 2017-11-30 0.0626577318 0.1383318573 0.0247790384 0.1076144435
## 2017-12-29 -0.0062057845 0.0091214101 0.0855496098 0.0207682546
## 2018-01-31 0.2156265497 0.0459413204 0.1421910628 0.0764924230
## 2018-02-28 0.0415536279 -0.0179107223 0.0107467626 -0.1691629276
## 2018-03-29 -0.0440034760 -0.0130233556 -0.0826208105 -0.0056772976
## 2018-04-30 0.0788803060 0.0452894164 0.0446459974 -0.0057488058
## 2018-05-31 0.0397392430 0.0083746742 0.0125276277 -0.0629870472
## 2018-06-29 0.0421636787 0.0527598637 0.0433594066 0.0369861254
## 2018-07-31 0.0446635734 0.0455082632 0.0581796388 0.0409481163
## 2018-08-31 0.1243079079 0.0663288054 0.0889778214 0.0774627491
## 2018-09-28 -0.0048359814 0.0074786149 0.0080813972 -0.0205518711
## 2018-10-31 -0.2258869989 -0.0269699172 -0.0533177924 0.0656294442
## 2018-11-30 0.0560700324 0.0138982767 -0.1560246285 -0.0265768604
## 2018-12-31 -0.1180514843 -0.1269317655 -0.0710989030 -0.0417362710
## 2019-01-31 0.1348080312 0.0522183063 0.0994419480 0.0283647335
## 2019-02-28 -0.0469930640 0.0216655621 0.0038813567 0.0324430858
## 2019-03-29 0.0824420184 0.1016322026 0.0997558020 -0.0094927016
## 2019-04-30 0.0786806224 0.0139032058 -0.0360261283 0.0530145638
## 2019-05-31 -0.0818753491 -0.0218349835 0.0473593620 -0.0084089462
## 2019-06-28 0.0646557767 0.0980463398 0.0737793655 0.0854577877
## 2019-07-31 -0.0142806686 0.0421257860 -0.0024277971 -0.0009964762
## 2019-08-30 -0.0496880810 0.0693116891 0.2218672667 0.0394582218
## 2019-09-30 -0.0229951159 -0.0228190139 -0.0012151422 0.0379543026
## 2019-10-31 0.0232034080 0.0329300011 0.0000000000 -0.0120371498
## 2019-11-29 0.0134958216 0.0090466311 0.1623187814 0.0154858750
## 2019-12-31 0.0257863501 -0.0198415109 0.0252759098 0.0023737850
## 2020-01-31 0.0834803026 0.0387079454 -0.1464845114 -0.0372903459
## 2020-02-28 -0.0642332026 -0.0810565058 -0.0667811335 -0.0613236860
## 2020-03-31 0.0344213022 0.0140925371 -0.1024521179 0.0581105413
## 2020-04-30 0.2381504762 0.0630695435 0.1658369125 0.0674661309
## 2020-05-29 -0.0128673719 0.0178918490 0.1138940346 0.0248288969
## 2020-06-30 0.1218341331 -0.0171990136 -0.0198140085 -0.0351086937
## 2020-07-31 0.1372488933 0.0731774685 0.0484206233 0.0772515641
## 2020-08-31 0.0866005735 0.0657707005 0.1882717424 0.0745888454
## 2020-09-30 -0.0916533253 0.0208926840 0.0402477659 0.0076050787
## 2020-10-30 -0.0364089187 0.0092731709 -0.0335904697 -0.0083256352
## 2020-11-30 0.0425228214 0.0912038758 0.1691407119 0.0963906675
## 2020-12-31 0.0276719582 -0.0131570143 -0.0168516975 -0.0545612581
## 2021-01-29 -0.0156985929 -0.0668093576 0.0259449240 -0.0257180990
## 2021-02-26 -0.0359675607 -0.0607610556 0.0160105140 -0.0782173849
## 2021-03-31 0.0003717151 0.0628757165 0.0767328490 0.0486517785
## 2021-04-30 0.1139202399 0.0562816646 0.0453533467 0.0295951019
## 2021-05-28 -0.0730764715 0.0164722102 0.0938666178 0.0189582675
## 2021-06-30 0.0651836137 0.0449722742 0.0632654296 -0.0071365773
## 2021-07-30 -0.0332696301 0.0844262092 0.0768490669 0.0107911683
## 2021-08-31 0.0421339363 0.0582398269 -0.0519788094 0.0418682913
## 2021-09-30 -0.0550034409 -0.0135715972 -0.0765899301 -0.0606838456
## 2021-10-29 0.0262547651 0.0913576735 0.1265017387 0.0695571958
## 2021-11-30 0.0391473463 0.0928771116 -0.0592959037 -0.0606289272
## 2021-12-31 -0.0505062029 0.0511727223 -0.0521916296 0.0324794597
## 2022-01-31 -0.1085098142 -0.1167773425 -0.0487406657 -0.0343092140
## 2022-02-28 0.0263230079 0.0290843033 -0.0940889447 -0.0338251362
## 2022-03-31 0.0596239190 0.1034614441 0.0604566147 0.1008103491
## 2022-04-29 -0.2711856801 -0.0781043513 0.0745691342 0.0269632996
## 2022-05-31 -0.0333130879 -0.1314595111 -0.3412238746 -0.1698041845
## 2022-06-30 -0.1238178226 0.0276274388 -0.1364653362 -0.0563672820
## 2022-07-29 0.2394860591 0.1234129523 0.1456889168 0.0826077557
## 2022-08-31 -0.0625299224 -0.0361141927 -0.0125342602 0.0081253261
## 2022-09-30 -0.1149865758 -0.1003084071 -0.0774523383 -0.0217358620
## 2022-10-31 -0.0981105335 0.0618565368 0.1015457524 0.0929241334
## 2022-11-30 -0.0593198454 0.0725756332 0.0232762079 0.0684915368
## 2022-12-30 -0.1391406409 -0.1665906557 -0.1141982991 -0.0685301354
## 2023-01-31 0.2051735029 0.1130548913 0.1440934121 0.0145631794
## 2023-02-28 -0.0902516639 -0.0524475190 -0.0151217121 -0.0121681309
## 2023-03-31 0.0918019370 0.0258716438 -0.0171792921 0.0408373905
## 2023-04-28 0.0206963031 0.0126990069 -0.0487449077 0.0235919382
## 2023-05-31 0.1340765703 0.0185209146 -0.1795999544 -0.0237418143
## 2023-06-30 0.0779864104 0.0510998591 0.0073812174 0.0678438763
## 2023-07-31 0.0251489708 0.0405677172 0.0340608474 0.0169067151
## 2023-08-31 0.0318772770 -0.0186362041 -0.0669315381 0.0206049547
## 2023-09-29 -0.0821945627 0.0281464816 -0.1349887807 -0.0166184345
## 2023-10-31 0.0458940197 -0.0224101386 0.0019877316 0.0215261952
## 2023-11-30 0.0931972815 0.0722445867 0.1990890513 -0.0483956352
## 2023-12-29 0.0392628771 0.1300908554 0.0623594587 0.0162177792
## 2024-01-31 0.0212288659 0.0513779428 -0.0237309856 0.0470820558
## 2024-02-29 0.1300782611 0.0696232620 0.1022450425 0.0620581686
## 2024-03-28 0.0202729146 -0.0152523909 0.1474206462 0.0296686019
## 2024-04-30 -0.0302797899 -0.0117662719 -0.0960553532 -0.0137218293
## 2024-05-31 0.0081949152 0.1136275244 -0.0235390114 0.1060152067
## 2024-06-28 0.0910037984 0.0483261034 -0.0533986753 0.0292219100
## 2024-07-31 -0.0329830511 -0.0320614633 0.0158824483 0.0136416039
## 2024-08-30 -0.0464130352 0.0821516652 0.0289072194 0.1207588853
## 2024-09-30 0.0429307038 -0.0065883578 0.0144762086 0.0445699564
## 2024-10-31 0.0003755644 -0.0140173715 -0.0380562836 0.0147513827
## 2024-11-29 0.1091142212 0.1072738018 -0.1185488492 0.1210993655
## 2024-12-31 0.0538418744 -0.0589211571 0.0214596308 -0.0213044751
## 2025-01-31 0.0800742352 0.0671190206 0.0199941180 0.0829078191
## 2025-02-28 -0.1130190481 0.0688949351 -0.0958941758 0.0045737771
## 2025-03-31 -0.1095146233 -0.1032107172 -0.1743687392 -0.1134826443
## 2025-04-30 -0.0311757737 0.0502300978 -0.0762330254 0.1023372927
## 2025-05-30 0.1058429765 0.0462111097 -0.0168449944 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-14 -0.0398039586 0.0125707933 -0.0183600029 0.0127665785
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.212 0.291 0.125
Column Chart of Component Contribution and weight
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.