# 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.
symbols <- c("COST", "LOW", "HD", "KR")
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"))
asset_returns_tbl
## # A tibble: 668 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 COST 2013-01-31 0.0359
## 2 COST 2013-02-28 -0.00765
## 3 COST 2013-03-28 0.0465
## 4 COST 2013-04-30 0.0216
## 5 COST 2013-05-31 0.0138
## 6 COST 2013-06-28 0.00854
## 7 COST 2013-07-31 0.0601
## 8 COST 2013-08-30 -0.0458
## 9 COST 2013-09-30 0.0291
## 10 COST 2013-10-31 0.0243
## # ℹ 658 more rows
# 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
## COST HD KR LOW
## 2013-01-31 0.0359114051 0.0787858155 0.0625666642 0.076811612
## 2013-02-28 -0.0076474423 0.0233360558 0.0583482827 -0.001047772
## 2013-03-28 0.0464885373 0.0239965755 0.1262296338 -0.006047042
## 2013-04-30 0.0216286990 0.0498951433 0.0367342931 0.017305607
## 2013-05-31 0.0137930462 0.0698928274 -0.0165092912 0.091707136
## 2013-06-28 0.0085376392 -0.0103005834 0.0255110554 -0.029155112
## 2013-07-31 0.0601085043 0.0199368219 0.1283428035 0.090229357
## 2013-08-30 -0.0458223763 -0.0591625570 -0.0665549589 0.027435354
## 2013-09-30 0.0290721630 0.0233417500 0.0972951025 0.038322139
## 2013-10-31 0.0242749117 0.0265398612 0.0601288808 0.048353809
## 2013-11-29 0.0635982450 0.0350691644 -0.0218284153 -0.047304412
## 2013-12-31 -0.0524561185 0.0253915393 -0.0546394828 0.042674304
## 2014-01-31 -0.0575835484 -0.0690014383 -0.0907668390 -0.064313788
## 2014-02-28 0.0414544151 0.0652295509 0.1544052355 0.077696757
## 2014-03-31 -0.0448252518 -0.0302574895 0.0399635879 -0.022845160
## 2014-04-30 0.0351899830 0.0047908697 0.0533073561 -0.059240922
## 2014-05-30 0.0059920096 0.0090147519 0.0397995074 0.025165399
## 2014-06-30 -0.0074398239 0.0148883740 0.0347876707 0.019144340
## 2014-07-31 0.0234832003 -0.0013593524 -0.0091454680 0.001900649
## 2014-08-29 0.0296724073 0.1453656234 0.0433195191 0.092932757
## 2014-09-30 0.0344193367 -0.0139635433 0.0198102176 0.007777544
## 2014-10-31 0.0622565367 0.0610992844 0.0689159228 0.082162355
## 2014-11-28 0.0661384059 0.0190941617 0.0746942312 0.109669342
## 2014-12-31 -0.0026069933 0.0592874385 0.0704845356 0.074980453
## 2015-01-30 0.0087094473 -0.0052536193 0.0726721054 -0.011759497
## 2015-02-27 0.0623771861 0.0943350064 0.0325430964 0.089308870
## 2015-03-31 0.0304251193 -0.0048519655 0.0745899672 0.004041028
## 2015-04-30 -0.0546590651 -0.0601297392 -0.1065789357 -0.074070798
## 2015-05-29 -0.0032211058 0.0406648121 0.0575116354 0.016133693
## 2015-06-30 -0.0542538476 0.0026913870 -0.0039915468 -0.043964256
## 2015-07-31 0.0730810317 0.0517295393 0.0791190698 0.039220160
## 2015-08-31 -0.0340554559 -0.0048824691 -0.1259250793 -0.002743003
## 2015-09-30 0.0317645049 -0.0032849050 0.0445019512 -0.003620859
## 2015-10-30 0.0895902576 0.0681904586 0.0468475163 0.072673647
## 2015-11-30 0.0232365111 0.0795696758 -0.0008837221 0.036831369
## 2015-12-31 0.0004957361 -0.0078331488 0.1050156326 -0.007337738
## 2016-01-29 -0.0664308543 -0.0503188530 -0.0749360091 -0.055312994
## 2016-02-29 -0.0045317579 -0.0131265601 0.0308115233 -0.059361048
## 2016-03-31 0.0490983782 0.0778118258 -0.0424835203 0.114866950
## 2016-04-29 -0.0588896175 0.0034414843 -0.0777144299 0.007186069
## 2016-05-31 0.0043107998 -0.0081428729 0.0133849438 0.052653921
## 2016-06-30 0.0540991478 -0.0341049541 0.0283962470 -0.012053229
## 2016-07-29 0.0628098338 0.0793860335 -0.0732928878 0.042825394
## 2016-08-31 -0.0284746525 -0.0251414399 -0.0628109691 -0.072053155
## 2016-09-30 -0.0609210713 -0.0414062775 -0.0749499372 -0.058495955
## 2016-10-31 -0.0308969535 -0.0532257278 0.0428689745 -0.075206405
## 2016-11-30 0.0181080975 0.0641007678 0.0454482558 0.056866797
## 2016-12-30 0.0644928526 0.0355285235 0.0661820294 0.008047188
## 2017-01-31 0.0237005139 0.0257690138 -0.0160659088 0.032075267
## 2017-02-28 0.0802942209 0.0519070710 -0.0621261210 0.017497700
## 2017-03-31 -0.0550491149 0.0192301913 -0.0754152552 0.100224406
## 2017-04-28 0.0569664162 0.0612214167 0.0054109505 0.036163588
## 2017-05-31 0.0587791330 -0.0109689497 0.0083777638 -0.074706528
## 2017-06-30 -0.1206064175 -0.0007167782 -0.2445257997 -0.015866708
## 2017-07-31 -0.0089186375 -0.0250836360 0.0501777597 0.003889570
## 2017-08-31 -0.0080369573 0.0076990325 -0.1090560195 -0.046409319
## 2017-09-29 0.0470446882 0.0874116869 -0.0863880776 0.078698767
## 2017-10-31 -0.0197320953 0.0134819765 0.0314058919 0.005237037
## 2017-11-30 0.1383319336 0.0863481251 0.2282257061 0.041887170
## 2017-12-29 0.0091215820 0.0525904529 0.0596689584 0.108665577
## 2018-01-31 0.0459412886 0.0582600075 0.1007596189 0.123266496
## 2018-02-28 -0.0179105437 -0.0973179283 -0.1083994547 -0.156141897
## 2018-03-29 -0.0130231459 -0.0166714469 -0.1247204881 -0.020751515
## 2018-04-30 0.0452887771 0.0361427416 0.0508964736 -0.057670227
## 2018-05-31 0.0083747114 0.0150084179 -0.0296364793 0.142032908
## 2018-06-29 0.0527601587 0.0448131677 0.1564375330 0.005876899
## 2018-07-31 0.0455081679 0.0123274824 0.0191480582 0.043445556
## 2018-08-31 0.0663289513 0.0214330003 0.0873862077 0.090503311
## 2018-09-28 0.0074784268 0.0312832216 -0.0789052776 0.054313936
## 2018-10-31 -0.0269697937 -0.1636410492 0.0220832726 -0.182285030
## 2018-11-30 0.0138983928 0.0308949076 0.0011053995 -0.008966810
## 2018-12-31 -0.1269318230 -0.0482855151 -0.0756133088 -0.021529135
## 2019-01-31 0.0522183178 0.0659305592 0.0297353451 0.045385909
## 2019-02-28 0.0216656735 0.0087340265 0.0395356131 0.088803505
## 2019-03-29 0.1016321902 0.0432286726 -0.1758641899 0.040833180
## 2019-04-30 0.0139029013 0.0597263222 0.0468522810 0.037216075
## 2019-05-31 -0.0218347023 -0.0704143065 -0.1168706290 -0.193019852
## 2019-06-28 0.0980459995 0.0981496001 -0.0494261460 0.078623222
## 2019-07-31 0.0421258624 0.0271323922 -0.0256602682 0.010221442
## 2019-08-30 0.0693117305 0.0644244470 0.1194837226 0.101210061
## 2019-09-30 -0.0228188551 0.0239608187 0.0849681041 -0.020166335
## 2019-10-31 0.0329301874 0.0109733212 -0.0452275982 0.019819123
## 2019-11-29 0.0090464381 -0.0618536265 0.1099097778 0.049809218
## 2019-12-31 -0.0198409003 -0.0033309005 0.0586002427 0.020669776
## 2020-01-31 0.0387072285 0.0435472088 -0.0763128565 -0.025327541
## 2020-02-28 -0.0810560986 -0.0460232078 0.0517909033 -0.086855278
## 2020-03-31 0.0140922239 -0.1475357530 0.0683528853 -0.213873465
## 2020-04-30 0.0630700449 0.1632976423 0.0482839738 0.202446795
## 2020-05-29 0.0178914172 0.1225078084 0.0361550582 0.218646448
## 2020-06-30 -0.0171992380 0.0140897870 0.0370134841 0.035939882
## 2020-07-31 0.0731778422 0.0580784134 0.0273909439 0.101003023
## 2020-08-31 0.0657705815 0.0710522620 0.0304747870 0.100722979
## 2020-09-30 0.0208924757 -0.0207923168 -0.0508805236 0.007079018
## 2020-10-30 0.0092729646 -0.0404154682 -0.0514327333 -0.044484440
## 2020-11-30 0.0912040549 0.0393346883 0.0298555780 -0.014526038
## 2020-12-31 -0.0131565994 -0.0379921072 -0.0383000606 0.029654599
## 2021-01-29 -0.0668096764 0.0193876496 0.0827518746 0.042246901
## 2021-02-26 -0.0607610605 -0.0471779685 -0.0633124599 -0.043484992
## 2021-03-31 0.0628754118 0.1731014937 0.1109641320 0.174360831
## 2021-04-30 0.0562818747 0.0585932181 0.0151663585 0.034363724
## 2021-05-28 0.0164723916 -0.0148154667 0.0168955899 -0.007262193
## 2021-06-30 0.0449723740 0.0051675436 0.0353340133 -0.004423804
## 2021-07-30 0.0844262455 0.0287460618 0.0605171735 -0.002464747
## 2021-08-31 0.0582397418 -0.0061432405 0.1279610325 0.056497637
## 2021-09-30 -0.0135717329 0.0114281961 -0.1297213256 -0.005064485
## 2021-10-29 0.0913576346 0.1243885598 -0.0101926520 0.145649859
## 2021-11-30 0.0928770822 0.0747937851 0.0420167712 0.045031853
## 2021-12-31 0.0511727096 0.0394415090 0.0860076409 0.055234980
## 2022-01-31 -0.1167771434 -0.1229952035 -0.0375955794 -0.081990343
## 2022-02-28 0.0290840964 -0.1501033683 0.0756040943 -0.071101665
## 2022-03-31 0.1034616092 -0.0476407703 0.2036384853 -0.089226207
## 2022-04-29 -0.0781046685 0.0035683293 -0.0612787565 -0.018267479
## 2022-05-31 -0.1314591445 0.0077925609 -0.0146443628 -0.012365640
## 2022-06-30 0.0276272942 -0.0924989389 -0.1125812544 -0.111638312
## 2022-07-29 0.1234133306 0.0927978006 -0.0189834029 0.097813475
## 2022-08-31 -0.0361144997 -0.0359893266 0.0373139915 0.013535035
## 2022-09-30 -0.1003082995 -0.0442343362 -0.0914586836 -0.033148894
## 2022-10-31 0.0618563481 0.0706150493 0.0778073892 0.042736909
## 2022-11-30 0.0725755399 0.0959453203 0.0449267394 0.086434244
## 2022-12-30 -0.1665905207 -0.0254133260 -0.0984049457 -0.064667017
## 2023-01-31 0.1130549557 0.0259690585 0.0011208003 0.049361470
## 2023-02-28 -0.0524475945 -0.0890859797 -0.0281465831 -0.012077437
## 2023-03-31 0.0258718498 0.0024153355 0.1348922438 -0.028494596
## 2023-04-28 0.0126989425 0.0181989215 -0.0151023678 0.043540934
## 2023-05-31 0.0185210199 -0.0513716918 -0.0649686325 -0.032768833
## 2023-06-30 0.0510998332 0.0915987662 0.0361785722 0.115255026
## 2023-07-31 0.0405674912 0.0720273323 0.0342985937 0.041979567
## 2023-08-31 -0.0186361058 -0.0042950333 -0.0414240061 -0.016310277
## 2023-09-29 0.0281464824 -0.0890447797 -0.0359923908 -0.103395494
## 2023-10-31 -0.0224100220 -0.0595557029 0.0137595881 -0.080892553
## 2023-11-30 0.0722446763 0.1030589640 -0.0179579552 0.042430556
## 2023-12-29 0.1300906809 0.1002596994 0.0320097557 0.112701713
## 2024-01-31 0.0513780706 0.0183274865 0.0093630065 -0.039595378
## 2024-02-29 0.0696230446 0.0754204665 0.0788965168 0.122886071
## 2024-03-28 -0.0152525968 0.0137881220 0.1411369321 0.056777404
## 2024-04-30 -0.0117659705 -0.1378006064 -0.0311108217 -0.106133963
## 2024-05-31 0.1136274162 0.0088706032 -0.0506172307 -0.029827703
## 2024-06-28 0.0483262343 0.0275965735 -0.0477119509 -0.003757612
## 2024-07-31 -0.0320616920 0.0671785179 0.0875786877 0.112458633
## 2024-08-30 0.0821518280 0.0069784573 -0.0178636463 0.012105161
## 2024-09-30 -0.0065884475 0.0949400962 0.0740541662 0.086122347
## 2024-10-31 -0.0140172841 -0.0286643748 -0.0270644723 -0.029636328
## 2024-11-29 0.1072738571 0.0912959295 0.0963795041 0.039686285
## 2024-12-31 -0.0589213249 -0.0982063760 0.0011454137 -0.098803291
## 2025-01-31 0.0671191599 0.0574211568 0.0079811763 0.056616929
## 2025-02-28 0.0688948236 -0.0380465635 0.0551909038 -0.044829357
## 2025-03-31 -0.1032106050 -0.0726310403 0.0433242018 -0.063981000
## 2025-04-30 0.0502299867 -0.0165070305 0.0646401659 -0.037082076
## 2025-05-30 0.0462110575 0.0214064436 -0.0518588432 0.009659872
## 2025-06-30 -0.0495047089 0.0017052055 0.0500246639 -0.017247977
## 2025-07-31 -0.0521475308 0.0023701160 -0.0229863021 0.012937315
## 2025-08-29 0.0052932558 0.1014972862 -0.0277024224 0.143467499
## 2025-09-30 -0.0189310151 0.0017653944 -0.0063584832 -0.026504885
## 2025-10-31 -0.0140241387 -0.0652642496 -0.0577083908 -0.048965948
## 2025-11-28 0.0023452106 -0.0615800273 0.0610974906 0.018102500
## 2025-12-31 -0.0577322984 -0.0301324750 -0.0740164945 -0.005458519
## 2026-01-30 0.0878613212 0.0848997557 0.0059044243 0.106493697
## 2026-02-27 0.0723416643 0.0162321390 0.0872824495 -0.009367528
## 2026-03-31 -0.0143085153 -0.1396774602 0.0586228526 -0.113088339
## 2026-04-30 0.0180018788 -0.0002738028 -0.0611172645 0.015355702
## 2026-05-29 -0.0576382270 -0.0361062306 -0.0857315345 -0.107927669
## 2026-06-30 -0.0220435224 0.1136903557 -0.1126274040 0.028195481
## 2026-07-31 0.0189891303 -0.0605466366 0.0390268434 -0.053066991
## 2026-08-31 -0.0084398759 -0.0125193138 0.0033680471 -0.015078733
## 2026-09-30 -0.0361914793 -0.1344548258 0.0185955784 -0.105034897
## 2026-10-30 0.1076192535 0.1459674366 0.1300282289 0.132474766
## 2026-11-20 0.0850575984 0.0294526138 0.0303770361 0.054769282
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## COST HD KR LOW
## COST 0.003136082 0.0018482737 0.0016453488 0.001704791
## HD 0.001848274 0.0038988346 0.0009998052 0.003693636
## KR 0.001645349 0.0009998052 0.0053378875 0.001420872
## LOW 0.001704791 0.0036936356 0.0014208723 0.005157747
# 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.35, 0.25, 0.25, 0.15)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.048749
# 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
## COST HD KR LOW
## [1,] 0.01598727 0.01243923 0.01217166 0.008150831
rowSums(component_contribution)
## [1] 0.048749
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 4
## COST HD KR LOW
## <dbl> <dbl> <dbl> <dbl>
## 1 0.328 0.255 0.25 0.167
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
## asset contribution
## <chr> <dbl>
## 1 COST 0.328
## 2 HD 0.255
## 3 KR 0.25
## 4 LOW 0.167
# 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
## COST HD KR LOW
## 2013-01-31 0.0359114051 0.0787858155 0.0625666642 0.076811612
## 2013-02-28 -0.0076474423 0.0233360558 0.0583482827 -0.001047772
## 2013-03-28 0.0464885373 0.0239965755 0.1262296338 -0.006047042
## 2013-04-30 0.0216286990 0.0498951433 0.0367342931 0.017305607
## 2013-05-31 0.0137930462 0.0698928274 -0.0165092912 0.091707136
## 2013-06-28 0.0085376392 -0.0103005834 0.0255110554 -0.029155112
## 2013-07-31 0.0601085043 0.0199368219 0.1283428035 0.090229357
## 2013-08-30 -0.0458223763 -0.0591625570 -0.0665549589 0.027435354
## 2013-09-30 0.0290721630 0.0233417500 0.0972951025 0.038322139
## 2013-10-31 0.0242749117 0.0265398612 0.0601288808 0.048353809
## 2013-11-29 0.0635982450 0.0350691644 -0.0218284153 -0.047304412
## 2013-12-31 -0.0524561185 0.0253915393 -0.0546394828 0.042674304
## 2014-01-31 -0.0575835484 -0.0690014383 -0.0907668390 -0.064313788
## 2014-02-28 0.0414544151 0.0652295509 0.1544052355 0.077696757
## 2014-03-31 -0.0448252518 -0.0302574895 0.0399635879 -0.022845160
## 2014-04-30 0.0351899830 0.0047908697 0.0533073561 -0.059240922
## 2014-05-30 0.0059920096 0.0090147519 0.0397995074 0.025165399
## 2014-06-30 -0.0074398239 0.0148883740 0.0347876707 0.019144340
## 2014-07-31 0.0234832003 -0.0013593524 -0.0091454680 0.001900649
## 2014-08-29 0.0296724073 0.1453656234 0.0433195191 0.092932757
## 2014-09-30 0.0344193367 -0.0139635433 0.0198102176 0.007777544
## 2014-10-31 0.0622565367 0.0610992844 0.0689159228 0.082162355
## 2014-11-28 0.0661384059 0.0190941617 0.0746942312 0.109669342
## 2014-12-31 -0.0026069933 0.0592874385 0.0704845356 0.074980453
## 2015-01-30 0.0087094473 -0.0052536193 0.0726721054 -0.011759497
## 2015-02-27 0.0623771861 0.0943350064 0.0325430964 0.089308870
## 2015-03-31 0.0304251193 -0.0048519655 0.0745899672 0.004041028
## 2015-04-30 -0.0546590651 -0.0601297392 -0.1065789357 -0.074070798
## 2015-05-29 -0.0032211058 0.0406648121 0.0575116354 0.016133693
## 2015-06-30 -0.0542538476 0.0026913870 -0.0039915468 -0.043964256
## 2015-07-31 0.0730810317 0.0517295393 0.0791190698 0.039220160
## 2015-08-31 -0.0340554559 -0.0048824691 -0.1259250793 -0.002743003
## 2015-09-30 0.0317645049 -0.0032849050 0.0445019512 -0.003620859
## 2015-10-30 0.0895902576 0.0681904586 0.0468475163 0.072673647
## 2015-11-30 0.0232365111 0.0795696758 -0.0008837221 0.036831369
## 2015-12-31 0.0004957361 -0.0078331488 0.1050156326 -0.007337738
## 2016-01-29 -0.0664308543 -0.0503188530 -0.0749360091 -0.055312994
## 2016-02-29 -0.0045317579 -0.0131265601 0.0308115233 -0.059361048
## 2016-03-31 0.0490983782 0.0778118258 -0.0424835203 0.114866950
## 2016-04-29 -0.0588896175 0.0034414843 -0.0777144299 0.007186069
## 2016-05-31 0.0043107998 -0.0081428729 0.0133849438 0.052653921
## 2016-06-30 0.0540991478 -0.0341049541 0.0283962470 -0.012053229
## 2016-07-29 0.0628098338 0.0793860335 -0.0732928878 0.042825394
## 2016-08-31 -0.0284746525 -0.0251414399 -0.0628109691 -0.072053155
## 2016-09-30 -0.0609210713 -0.0414062775 -0.0749499372 -0.058495955
## 2016-10-31 -0.0308969535 -0.0532257278 0.0428689745 -0.075206405
## 2016-11-30 0.0181080975 0.0641007678 0.0454482558 0.056866797
## 2016-12-30 0.0644928526 0.0355285235 0.0661820294 0.008047188
## 2017-01-31 0.0237005139 0.0257690138 -0.0160659088 0.032075267
## 2017-02-28 0.0802942209 0.0519070710 -0.0621261210 0.017497700
## 2017-03-31 -0.0550491149 0.0192301913 -0.0754152552 0.100224406
## 2017-04-28 0.0569664162 0.0612214167 0.0054109505 0.036163588
## 2017-05-31 0.0587791330 -0.0109689497 0.0083777638 -0.074706528
## 2017-06-30 -0.1206064175 -0.0007167782 -0.2445257997 -0.015866708
## 2017-07-31 -0.0089186375 -0.0250836360 0.0501777597 0.003889570
## 2017-08-31 -0.0080369573 0.0076990325 -0.1090560195 -0.046409319
## 2017-09-29 0.0470446882 0.0874116869 -0.0863880776 0.078698767
## 2017-10-31 -0.0197320953 0.0134819765 0.0314058919 0.005237037
## 2017-11-30 0.1383319336 0.0863481251 0.2282257061 0.041887170
## 2017-12-29 0.0091215820 0.0525904529 0.0596689584 0.108665577
## 2018-01-31 0.0459412886 0.0582600075 0.1007596189 0.123266496
## 2018-02-28 -0.0179105437 -0.0973179283 -0.1083994547 -0.156141897
## 2018-03-29 -0.0130231459 -0.0166714469 -0.1247204881 -0.020751515
## 2018-04-30 0.0452887771 0.0361427416 0.0508964736 -0.057670227
## 2018-05-31 0.0083747114 0.0150084179 -0.0296364793 0.142032908
## 2018-06-29 0.0527601587 0.0448131677 0.1564375330 0.005876899
## 2018-07-31 0.0455081679 0.0123274824 0.0191480582 0.043445556
## 2018-08-31 0.0663289513 0.0214330003 0.0873862077 0.090503311
## 2018-09-28 0.0074784268 0.0312832216 -0.0789052776 0.054313936
## 2018-10-31 -0.0269697937 -0.1636410492 0.0220832726 -0.182285030
## 2018-11-30 0.0138983928 0.0308949076 0.0011053995 -0.008966810
## 2018-12-31 -0.1269318230 -0.0482855151 -0.0756133088 -0.021529135
## 2019-01-31 0.0522183178 0.0659305592 0.0297353451 0.045385909
## 2019-02-28 0.0216656735 0.0087340265 0.0395356131 0.088803505
## 2019-03-29 0.1016321902 0.0432286726 -0.1758641899 0.040833180
## 2019-04-30 0.0139029013 0.0597263222 0.0468522810 0.037216075
## 2019-05-31 -0.0218347023 -0.0704143065 -0.1168706290 -0.193019852
## 2019-06-28 0.0980459995 0.0981496001 -0.0494261460 0.078623222
## 2019-07-31 0.0421258624 0.0271323922 -0.0256602682 0.010221442
## 2019-08-30 0.0693117305 0.0644244470 0.1194837226 0.101210061
## 2019-09-30 -0.0228188551 0.0239608187 0.0849681041 -0.020166335
## 2019-10-31 0.0329301874 0.0109733212 -0.0452275982 0.019819123
## 2019-11-29 0.0090464381 -0.0618536265 0.1099097778 0.049809218
## 2019-12-31 -0.0198409003 -0.0033309005 0.0586002427 0.020669776
## 2020-01-31 0.0387072285 0.0435472088 -0.0763128565 -0.025327541
## 2020-02-28 -0.0810560986 -0.0460232078 0.0517909033 -0.086855278
## 2020-03-31 0.0140922239 -0.1475357530 0.0683528853 -0.213873465
## 2020-04-30 0.0630700449 0.1632976423 0.0482839738 0.202446795
## 2020-05-29 0.0178914172 0.1225078084 0.0361550582 0.218646448
## 2020-06-30 -0.0171992380 0.0140897870 0.0370134841 0.035939882
## 2020-07-31 0.0731778422 0.0580784134 0.0273909439 0.101003023
## 2020-08-31 0.0657705815 0.0710522620 0.0304747870 0.100722979
## 2020-09-30 0.0208924757 -0.0207923168 -0.0508805236 0.007079018
## 2020-10-30 0.0092729646 -0.0404154682 -0.0514327333 -0.044484440
## 2020-11-30 0.0912040549 0.0393346883 0.0298555780 -0.014526038
## 2020-12-31 -0.0131565994 -0.0379921072 -0.0383000606 0.029654599
## 2021-01-29 -0.0668096764 0.0193876496 0.0827518746 0.042246901
## 2021-02-26 -0.0607610605 -0.0471779685 -0.0633124599 -0.043484992
## 2021-03-31 0.0628754118 0.1731014937 0.1109641320 0.174360831
## 2021-04-30 0.0562818747 0.0585932181 0.0151663585 0.034363724
## 2021-05-28 0.0164723916 -0.0148154667 0.0168955899 -0.007262193
## 2021-06-30 0.0449723740 0.0051675436 0.0353340133 -0.004423804
## 2021-07-30 0.0844262455 0.0287460618 0.0605171735 -0.002464747
## 2021-08-31 0.0582397418 -0.0061432405 0.1279610325 0.056497637
## 2021-09-30 -0.0135717329 0.0114281961 -0.1297213256 -0.005064485
## 2021-10-29 0.0913576346 0.1243885598 -0.0101926520 0.145649859
## 2021-11-30 0.0928770822 0.0747937851 0.0420167712 0.045031853
## 2021-12-31 0.0511727096 0.0394415090 0.0860076409 0.055234980
## 2022-01-31 -0.1167771434 -0.1229952035 -0.0375955794 -0.081990343
## 2022-02-28 0.0290840964 -0.1501033683 0.0756040943 -0.071101665
## 2022-03-31 0.1034616092 -0.0476407703 0.2036384853 -0.089226207
## 2022-04-29 -0.0781046685 0.0035683293 -0.0612787565 -0.018267479
## 2022-05-31 -0.1314591445 0.0077925609 -0.0146443628 -0.012365640
## 2022-06-30 0.0276272942 -0.0924989389 -0.1125812544 -0.111638312
## 2022-07-29 0.1234133306 0.0927978006 -0.0189834029 0.097813475
## 2022-08-31 -0.0361144997 -0.0359893266 0.0373139915 0.013535035
## 2022-09-30 -0.1003082995 -0.0442343362 -0.0914586836 -0.033148894
## 2022-10-31 0.0618563481 0.0706150493 0.0778073892 0.042736909
## 2022-11-30 0.0725755399 0.0959453203 0.0449267394 0.086434244
## 2022-12-30 -0.1665905207 -0.0254133260 -0.0984049457 -0.064667017
## 2023-01-31 0.1130549557 0.0259690585 0.0011208003 0.049361470
## 2023-02-28 -0.0524475945 -0.0890859797 -0.0281465831 -0.012077437
## 2023-03-31 0.0258718498 0.0024153355 0.1348922438 -0.028494596
## 2023-04-28 0.0126989425 0.0181989215 -0.0151023678 0.043540934
## 2023-05-31 0.0185210199 -0.0513716918 -0.0649686325 -0.032768833
## 2023-06-30 0.0510998332 0.0915987662 0.0361785722 0.115255026
## 2023-07-31 0.0405674912 0.0720273323 0.0342985937 0.041979567
## 2023-08-31 -0.0186361058 -0.0042950333 -0.0414240061 -0.016310277
## 2023-09-29 0.0281464824 -0.0890447797 -0.0359923908 -0.103395494
## 2023-10-31 -0.0224100220 -0.0595557029 0.0137595881 -0.080892553
## 2023-11-30 0.0722446763 0.1030589640 -0.0179579552 0.042430556
## 2023-12-29 0.1300906809 0.1002596994 0.0320097557 0.112701713
## 2024-01-31 0.0513780706 0.0183274865 0.0093630065 -0.039595378
## 2024-02-29 0.0696230446 0.0754204665 0.0788965168 0.122886071
## 2024-03-28 -0.0152525968 0.0137881220 0.1411369321 0.056777404
## 2024-04-30 -0.0117659705 -0.1378006064 -0.0311108217 -0.106133963
## 2024-05-31 0.1136274162 0.0088706032 -0.0506172307 -0.029827703
## 2024-06-28 0.0483262343 0.0275965735 -0.0477119509 -0.003757612
## 2024-07-31 -0.0320616920 0.0671785179 0.0875786877 0.112458633
## 2024-08-30 0.0821518280 0.0069784573 -0.0178636463 0.012105161
## 2024-09-30 -0.0065884475 0.0949400962 0.0740541662 0.086122347
## 2024-10-31 -0.0140172841 -0.0286643748 -0.0270644723 -0.029636328
## 2024-11-29 0.1072738571 0.0912959295 0.0963795041 0.039686285
## 2024-12-31 -0.0589213249 -0.0982063760 0.0011454137 -0.098803291
## 2025-01-31 0.0671191599 0.0574211568 0.0079811763 0.056616929
## 2025-02-28 0.0688948236 -0.0380465635 0.0551909038 -0.044829357
## 2025-03-31 -0.1032106050 -0.0726310403 0.0433242018 -0.063981000
## 2025-04-30 0.0502299867 -0.0165070305 0.0646401659 -0.037082076
## 2025-05-30 0.0462110575 0.0214064436 -0.0518588432 0.009659872
## 2025-06-30 -0.0495047089 0.0017052055 0.0500246639 -0.017247977
## 2025-07-31 -0.0521475308 0.0023701160 -0.0229863021 0.012937315
## 2025-08-29 0.0052932558 0.1014972862 -0.0277024224 0.143467499
## 2025-09-30 -0.0189310151 0.0017653944 -0.0063584832 -0.026504885
## 2025-10-31 -0.0140241387 -0.0652642496 -0.0577083908 -0.048965948
## 2025-11-28 0.0023452106 -0.0615800273 0.0610974906 0.018102500
## 2025-12-31 -0.0577322984 -0.0301324750 -0.0740164945 -0.005458519
## 2026-01-30 0.0878613212 0.0848997557 0.0059044243 0.106493697
## 2026-02-27 0.0723416643 0.0162321390 0.0872824495 -0.009367528
## 2026-03-31 -0.0143085153 -0.1396774602 0.0586228526 -0.113088339
## 2026-04-30 0.0180018788 -0.0002738028 -0.0611172645 0.015355702
## 2026-05-29 -0.0576382270 -0.0361062306 -0.0857315345 -0.107927669
## 2026-06-30 -0.0220435224 0.1136903557 -0.1126274040 0.028195481
## 2026-07-31 0.0189891303 -0.0605466366 0.0390268434 -0.053066991
## 2026-08-31 -0.0084398759 -0.0125193138 0.0033680471 -0.015078733
## 2026-09-30 -0.0361914793 -0.1344548258 0.0185955784 -0.105034897
## 2026-10-30 0.1076192535 0.1459674366 0.1300282289 0.132474766
## 2026-11-20 0.0850575984 0.0294526138 0.0303770361 0.054769282
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(0.35, 0.25, 0.25, 0.15))
## # A tibble: 1 × 4
## COST HD KR LOW
## <dbl> <dbl> <dbl> <dbl>
## 1 0.328 0.255 0.25 0.167
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.35, 0.25, 0.25, 0.15)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# add weights
add_column(weight = c(0.35, 0.25, 0.25, 0.15)) %>%
# 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 Volitility 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?
COST is the largest contributor to the portfolio volatility. I would say the portfolio risk is not concentrated in one asset, and it is instead spread out evenly throughout all of my assets according to their weight.