# 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("TSLA", "AMZN", "WMT", "RGR")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2024-11-20")
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 RGR TSLA WMT
## 2013-01-31 0.0566799395 0.1117935320 0.102078114 0.0248960021
## 2013-02-28 -0.0046435024 0.0729115637 -0.074128640 0.0117958128
## 2013-03-28 0.0083654162 -0.0665755483 0.084208138 0.0620732354
## 2013-04-30 -0.0487507497 0.0105884266 0.354111531 0.0378936698
## 2013-05-31 0.0588686246 -0.0099514023 0.593716684 -0.0317797673
## 2013-06-28 0.0310507506 -0.0455733145 0.093672163 -0.0046877656
## 2013-07-31 0.0813355350 0.0572395083 0.223739522 0.0452743819
## 2013-08-30 -0.0695574090 0.0408137253 0.229971642 -0.0596999311
## 2013-09-30 0.1067688897 0.1789102283 0.134706620 0.0133389542
## 2013-10-31 0.1521839116 0.0434309888 -0.189806595 0.0370292626
## 2013-11-29 0.0781496860 0.1701087752 -0.228409405 0.0540189089
## 2013-12-31 0.0130490386 -0.0512045123 0.167108541 -0.0232518323
## 2014-01-31 -0.1059765119 0.0412760533 0.187261714 -0.0523038069
## 2014-02-28 0.0094619003 -0.1781553850 0.299722785 0.0002674148
## 2014-03-31 -0.0737086161 -0.0551840738 -0.160783242 0.0293260740
## 2014-04-30 -0.1007565303 0.0733307909 -0.002690122 0.0420196932
## 2014-05-30 0.0273091844 -0.0524004589 -0.000577422 -0.0314089994
## 2014-06-30 0.0383836202 -0.0269174138 0.144457224 -0.0223928742
## 2014-07-31 -0.0369768154 -0.1664845566 -0.072372676 -0.0200477946
## 2014-08-29 0.0799468404 0.0178873499 0.188794007 0.0323259225
## 2014-09-30 -0.0502010184 -0.0347155876 -0.105566477 0.0127656719
## 2014-10-31 -0.0540982347 -0.1554521529 -0.004046477 -0.0026186512
## 2014-11-28 0.1031187277 -0.0863214261 0.011599801 0.1378163689
## 2014-12-31 -0.0872368614 -0.0952315788 -0.094774519 -0.0135740769
## 2015-01-30 0.1330922557 0.1541097992 -0.088365289 -0.0105350165
## 2015-02-27 0.0697992426 0.2516441192 -0.001277808 -0.0124329844
## 2015-03-31 -0.0214295755 -0.0426223026 -0.074350051 -0.0142312614
## 2015-04-30 0.1253212736 0.0992772052 0.180226808 -0.0524139063
## 2015-05-29 0.0175090293 -0.0139700330 0.103899574 -0.0433510840
## 2015-06-30 0.0112589814 0.0667574056 0.067300935 -0.0460135682
## 2015-07-31 0.2111621090 0.0437628870 -0.007896616 0.0146949025
## 2015-08-31 -0.0443525782 0.0524472035 -0.066366250 -0.0993583525
## 2015-09-30 -0.0019516837 -0.0691178680 -0.002653519 0.0016977161
## 2015-10-30 0.2010808743 -0.0302712317 -0.182659777 -0.1246695070
## 2015-11-30 0.0602956777 -0.0837941416 0.106828586 0.0275687794
## 2015-12-31 0.0165440008 0.1346583945 0.041471519 0.0492991432
## 2016-01-29 -0.1410054620 -0.0128316188 -0.227360626 0.0793148143
## 2016-02-29 -0.0605352209 0.1779224671 0.003810669 -0.0003018849
## 2016-03-31 0.0717834363 -0.0231000162 0.179948109 0.0392708251
## 2016-04-29 0.1053453760 -0.0657285748 0.046721797 -0.0239370453
## 2016-05-31 0.0915002899 0.0409793165 -0.075597968 0.0641207395
## 2016-06-30 -0.0099694639 -0.0340941553 -0.050296440 0.0311570160
## 2016-07-29 0.0586021229 0.0604683058 0.100785334 -0.0006849940
## 2016-08-31 0.0135476418 -0.0965154556 -0.102058091 -0.0143680938
## 2016-09-30 0.0848953908 -0.0593202246 -0.038366372 0.0094733988
## 2016-10-31 -0.0583893058 0.0627407372 -0.031364583 -0.0295505913
## 2016-11-30 -0.0509721927 -0.1715813736 -0.043041267 0.0058384913
## 2016-12-30 -0.0009330556 0.0249773684 0.120665178 -0.0116433465
## 2017-01-31 0.0936394059 0.0009485147 0.164624916 -0.0350399061
## 2017-02-28 0.0258446800 -0.0565450570 -0.007730364 0.0608889529
## 2017-03-31 0.0479423007 0.0799573447 0.107278727 0.0234091209
## 2017-04-28 0.0424566944 0.1212008660 0.120916212 0.0421086215
## 2017-05-31 0.0725778018 0.0600099803 0.082295892 0.0511563470
## 2017-06-30 -0.0271286156 -0.0246336760 0.058654468 -0.0378578417
## 2017-07-31 0.0202278808 -0.0760282315 -0.111459860 0.0553877759
## 2017-08-31 -0.0072953953 -0.2246875106 0.095543446 -0.0180255760
## 2017-09-29 -0.0198260355 0.1211738268 -0.042474144 0.0008962784
## 2017-10-31 0.1395154056 -0.0424753741 -0.028457409 0.1109628688
## 2017-11-30 0.0626577318 0.1041034232 -0.070862541 0.1076143827
## 2017-12-29 -0.0062057845 0.0198919331 0.008061928 0.0207683675
## 2018-01-31 0.2156265497 -0.0533213456 0.129254571 0.0764922219
## 2018-02-28 0.0415536279 -0.2069858485 -0.032266877 -0.1691627737
## 2018-03-29 -0.0440034760 0.2030202249 -0.253920408 -0.0056773312
## 2018-04-30 0.0788803060 0.0510551152 0.099254576 -0.0057486790
## 2018-05-31 0.0397392430 0.1084524886 -0.031698139 -0.0629870503
## 2018-06-29 0.0421636787 -0.0896123896 0.186043257 0.0369859977
## 2018-07-31 0.0446635734 -0.0326708412 -0.140021491 0.0409479421
## 2018-08-31 0.1243079079 0.1944195575 0.011737389 0.0774629213
## 2018-09-28 -0.0048359814 0.0535442430 -0.130439038 -0.0205519541
## 2018-10-31 -0.2258869989 -0.1507048725 0.242170576 0.0656293407
## 2018-11-30 0.0560700324 -0.0986986852 0.038271580 -0.0265764804
## 2018-12-31 -0.1180514843 -0.0073011455 -0.051761952 -0.0417363462
## 2019-01-31 0.1348080312 0.0233990848 -0.080628789 0.0283646139
## 2019-02-28 -0.0469930640 0.0443401511 0.041032981 0.0324430856
## 2019-03-29 0.0824420184 -0.0659509101 -0.133656413 -0.0094924859
## 2019-04-30 0.0786806224 0.0545038051 -0.159123803 0.0530142725
## 2019-05-31 -0.0818753491 -0.1135102949 -0.253945372 -0.0084087905
## 2019-06-28 0.0646557767 0.0916276561 0.188012109 0.0854577850
## 2019-07-31 -0.0142806686 0.0364070754 0.078092373 -0.0009960787
## 2019-08-30 -0.0496880810 -0.3171598787 -0.068516948 0.0394578978
## 2019-09-30 -0.0229951159 0.0181229549 0.065449565 0.0379542679
## 2019-10-31 0.0232034080 0.0914712805 0.268061253 -0.0120373481
## 2019-11-29 0.0134958216 -0.0020940519 0.046592176 0.0154858409
## 2019-12-31 0.0257863501 0.0319748429 0.237359743 0.0023740895
## 2020-01-31 0.0834803026 0.0515909586 0.441578342 -0.0372906808
## 2020-02-28 -0.0642332026 -0.0303425196 0.026424253 -0.0613232536
## 2020-03-31 0.0344213022 0.0623448048 -0.242781458 0.0581101251
## 2020-04-30 0.2381504762 0.0439990589 0.400209535 0.0674665334
## 2020-05-29 -0.0128673719 0.1645791718 0.065730500 0.0248288888
## 2020-06-30 0.1218341331 0.1981301429 0.257108657 -0.0351089263
## 2020-07-31 0.1372488933 0.0682733217 0.281420674 0.0772517309
## 2020-08-31 0.0866005735 -0.0743263222 0.554719320 0.0745885244
## 2020-09-30 -0.0916533253 -0.1472126516 -0.149762306 0.0076052159
## 2020-10-30 -0.0364089187 0.0891077115 -0.100371771 -0.0083254673
## 2020-11-30 0.0425228214 -0.0794433371 0.380308519 0.0963905454
## 2020-12-31 0.0276719582 0.0606630077 0.217730753 -0.0545613483
## 2021-01-29 -0.0156985929 -0.0266309065 0.117343701 -0.0257180631
## 2021-02-26 -0.0359675607 0.0736116794 -0.161038203 -0.0782173623
## 2021-03-31 0.0003717151 -0.0217062161 -0.011269836 0.0486518191
## 2021-04-30 0.1139202399 -0.0172510097 0.060292565 0.0295950986
## 2021-05-28 -0.0730764715 0.2067926140 -0.126372343 0.0189582790
## 2021-06-30 0.0651836137 0.1308994614 0.083547955 -0.0071364767
## 2021-07-30 -0.0332696301 -0.1906690120 0.010973846 0.0107910541
## 2021-08-31 0.0421339363 0.0623582222 0.068224268 0.0418681095
## 2021-09-30 -0.0550034409 -0.0580541138 0.052632612 -0.0606838645
## 2021-10-29 0.0262547651 0.0668399787 0.362230202 0.0695572674
## 2021-11-30 0.0391473463 -0.0847210096 0.027237848 -0.0606287305
## 2021-12-31 -0.0505062029 -0.0525493368 -0.079968440 0.0324795390
## 2022-01-31 -0.1085098142 -0.0116823267 -0.120597475 -0.0343095070
## 2022-02-28 0.0263230079 0.0742248982 -0.073397011 -0.0338249542
## 2022-03-31 0.0596239190 -0.0268808881 0.213504290 0.1008101750
## 2022-04-29 -0.2711856801 -0.0213408259 -0.213125289 0.0269633133
## 2022-05-31 -0.0333130879 0.0065882479 -0.138340062 -0.1698040171
## 2022-06-30 -0.1238178226 -0.0644895002 -0.118657126 -0.0563676819
## 2022-07-29 0.2394860591 0.0382231360 0.280480149 0.0826081356
## 2022-08-31 -0.0625299224 -0.2275969350 -0.075250271 0.0081251868
## 2022-09-30 -0.1149865758 -0.0283402914 -0.038313992 -0.0217358657
## 2022-10-31 -0.0981105335 0.0999708519 -0.153346760 0.0929241006
## 2022-11-30 -0.0593198454 -0.0141227456 -0.155866121 0.0684915668
## 2022-12-30 -0.1391406409 0.0080978855 -0.457813194 -0.0685298754
## 2023-01-31 0.2051735029 0.1169485807 0.340915767 0.0145629297
## 2023-02-28 -0.0902516639 0.0237920448 0.171904973 -0.0121679518
## 2023-03-31 0.0918019370 -0.0068650984 0.008471140 0.0408371296
## 2023-04-28 0.0206963031 0.0020869803 -0.233183710 0.0235920102
## 2023-05-31 0.1340765703 -0.1041115272 0.216021889 -0.0237416277
## 2023-06-30 0.0779864104 0.0267906556 0.249689452 0.0678437111
## 2023-07-31 0.0251489708 -0.0001888581 0.021391609 0.0169069122
## 2023-08-31 0.0318772770 -0.0196253438 -0.035588260 0.0206048123
## 2023-09-29 -0.0821945627 0.0104145463 -0.030929027 -0.0166184916
## 2023-10-31 0.0458940197 0.0601279969 -0.219831983 0.0215261781
## 2023-11-30 0.0931972815 -0.2265672628 0.178463656 -0.0483954709
## 2023-12-29 0.0392628771 0.0333327742 0.034390133 0.0162176849
## 2024-01-31 0.0212288659 -0.0401805080 -0.282704160 0.0470820679
## 2024-02-29 0.1300782611 -0.0078178897 0.075015304 0.0620582315
## 2024-03-28 0.0202729146 0.0683525383 -0.138383423 0.0296685031
## 2024-04-30 -0.0302797899 0.0017319138 0.041724969 -0.0137217860
## 2024-05-31 0.0081949152 -0.0357690012 -0.028782134 0.1060150534
## 2024-06-28 0.0910037984 -0.0648384397 0.105427913 0.0292221364
## 2024-07-31 -0.0329830511 0.0798024930 0.159378271 0.0136415209
## 2024-08-30 -0.0464130352 -0.0639510689 -0.080549177 0.1207589765
## 2024-09-30 0.0429307038 -0.0105013175 0.200441406 0.0445699361
## 2024-10-31 0.0003755644 -0.0582881199 -0.046070548 0.0147513058
## 2024-11-19 0.0932108617 -0.0240913280 0.325578013 0.0551905300
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMZN RGR TSLA WMT
## AMZN 0.0071603669 0.0015549150 0.0052094637 0.0008307772
## RGR 0.0015549150 0.0092376689 0.0002915389 0.0003232800
## TSLA 0.0052094637 0.0002915389 0.0292501230 0.0009392995
## WMT 0.0008307772 0.0003232800 0.0009392995 0.0026338260
# 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.25, 0.2, 0.2, 0.1)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.05296391
# 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 RGR TSLA WMT
## [1,] 0.01522756 0.008786731 0.02758342 0.0013662
rowSums(component_contribution)
## [1] 0.05296391
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 4
## AMZN RGR TSLA WMT
## <dbl> <dbl> <dbl> <dbl>
## 1 0.288 0.166 0.521 0.026
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
## asset contribution
## <chr> <dbl>
## 1 AMZN 0.288
## 2 RGR 0.166
## 3 TSLA 0.521
## 4 WMT 0.026
# 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 RGR TSLA WMT
## 2013-01-31 0.0566799395 0.1117935320 0.102078114 0.0248960021
## 2013-02-28 -0.0046435024 0.0729115637 -0.074128640 0.0117958128
## 2013-03-28 0.0083654162 -0.0665755483 0.084208138 0.0620732354
## 2013-04-30 -0.0487507497 0.0105884266 0.354111531 0.0378936698
## 2013-05-31 0.0588686246 -0.0099514023 0.593716684 -0.0317797673
## 2013-06-28 0.0310507506 -0.0455733145 0.093672163 -0.0046877656
## 2013-07-31 0.0813355350 0.0572395083 0.223739522 0.0452743819
## 2013-08-30 -0.0695574090 0.0408137253 0.229971642 -0.0596999311
## 2013-09-30 0.1067688897 0.1789102283 0.134706620 0.0133389542
## 2013-10-31 0.1521839116 0.0434309888 -0.189806595 0.0370292626
## 2013-11-29 0.0781496860 0.1701087752 -0.228409405 0.0540189089
## 2013-12-31 0.0130490386 -0.0512045123 0.167108541 -0.0232518323
## 2014-01-31 -0.1059765119 0.0412760533 0.187261714 -0.0523038069
## 2014-02-28 0.0094619003 -0.1781553850 0.299722785 0.0002674148
## 2014-03-31 -0.0737086161 -0.0551840738 -0.160783242 0.0293260740
## 2014-04-30 -0.1007565303 0.0733307909 -0.002690122 0.0420196932
## 2014-05-30 0.0273091844 -0.0524004589 -0.000577422 -0.0314089994
## 2014-06-30 0.0383836202 -0.0269174138 0.144457224 -0.0223928742
## 2014-07-31 -0.0369768154 -0.1664845566 -0.072372676 -0.0200477946
## 2014-08-29 0.0799468404 0.0178873499 0.188794007 0.0323259225
## 2014-09-30 -0.0502010184 -0.0347155876 -0.105566477 0.0127656719
## 2014-10-31 -0.0540982347 -0.1554521529 -0.004046477 -0.0026186512
## 2014-11-28 0.1031187277 -0.0863214261 0.011599801 0.1378163689
## 2014-12-31 -0.0872368614 -0.0952315788 -0.094774519 -0.0135740769
## 2015-01-30 0.1330922557 0.1541097992 -0.088365289 -0.0105350165
## 2015-02-27 0.0697992426 0.2516441192 -0.001277808 -0.0124329844
## 2015-03-31 -0.0214295755 -0.0426223026 -0.074350051 -0.0142312614
## 2015-04-30 0.1253212736 0.0992772052 0.180226808 -0.0524139063
## 2015-05-29 0.0175090293 -0.0139700330 0.103899574 -0.0433510840
## 2015-06-30 0.0112589814 0.0667574056 0.067300935 -0.0460135682
## 2015-07-31 0.2111621090 0.0437628870 -0.007896616 0.0146949025
## 2015-08-31 -0.0443525782 0.0524472035 -0.066366250 -0.0993583525
## 2015-09-30 -0.0019516837 -0.0691178680 -0.002653519 0.0016977161
## 2015-10-30 0.2010808743 -0.0302712317 -0.182659777 -0.1246695070
## 2015-11-30 0.0602956777 -0.0837941416 0.106828586 0.0275687794
## 2015-12-31 0.0165440008 0.1346583945 0.041471519 0.0492991432
## 2016-01-29 -0.1410054620 -0.0128316188 -0.227360626 0.0793148143
## 2016-02-29 -0.0605352209 0.1779224671 0.003810669 -0.0003018849
## 2016-03-31 0.0717834363 -0.0231000162 0.179948109 0.0392708251
## 2016-04-29 0.1053453760 -0.0657285748 0.046721797 -0.0239370453
## 2016-05-31 0.0915002899 0.0409793165 -0.075597968 0.0641207395
## 2016-06-30 -0.0099694639 -0.0340941553 -0.050296440 0.0311570160
## 2016-07-29 0.0586021229 0.0604683058 0.100785334 -0.0006849940
## 2016-08-31 0.0135476418 -0.0965154556 -0.102058091 -0.0143680938
## 2016-09-30 0.0848953908 -0.0593202246 -0.038366372 0.0094733988
## 2016-10-31 -0.0583893058 0.0627407372 -0.031364583 -0.0295505913
## 2016-11-30 -0.0509721927 -0.1715813736 -0.043041267 0.0058384913
## 2016-12-30 -0.0009330556 0.0249773684 0.120665178 -0.0116433465
## 2017-01-31 0.0936394059 0.0009485147 0.164624916 -0.0350399061
## 2017-02-28 0.0258446800 -0.0565450570 -0.007730364 0.0608889529
## 2017-03-31 0.0479423007 0.0799573447 0.107278727 0.0234091209
## 2017-04-28 0.0424566944 0.1212008660 0.120916212 0.0421086215
## 2017-05-31 0.0725778018 0.0600099803 0.082295892 0.0511563470
## 2017-06-30 -0.0271286156 -0.0246336760 0.058654468 -0.0378578417
## 2017-07-31 0.0202278808 -0.0760282315 -0.111459860 0.0553877759
## 2017-08-31 -0.0072953953 -0.2246875106 0.095543446 -0.0180255760
## 2017-09-29 -0.0198260355 0.1211738268 -0.042474144 0.0008962784
## 2017-10-31 0.1395154056 -0.0424753741 -0.028457409 0.1109628688
## 2017-11-30 0.0626577318 0.1041034232 -0.070862541 0.1076143827
## 2017-12-29 -0.0062057845 0.0198919331 0.008061928 0.0207683675
## 2018-01-31 0.2156265497 -0.0533213456 0.129254571 0.0764922219
## 2018-02-28 0.0415536279 -0.2069858485 -0.032266877 -0.1691627737
## 2018-03-29 -0.0440034760 0.2030202249 -0.253920408 -0.0056773312
## 2018-04-30 0.0788803060 0.0510551152 0.099254576 -0.0057486790
## 2018-05-31 0.0397392430 0.1084524886 -0.031698139 -0.0629870503
## 2018-06-29 0.0421636787 -0.0896123896 0.186043257 0.0369859977
## 2018-07-31 0.0446635734 -0.0326708412 -0.140021491 0.0409479421
## 2018-08-31 0.1243079079 0.1944195575 0.011737389 0.0774629213
## 2018-09-28 -0.0048359814 0.0535442430 -0.130439038 -0.0205519541
## 2018-10-31 -0.2258869989 -0.1507048725 0.242170576 0.0656293407
## 2018-11-30 0.0560700324 -0.0986986852 0.038271580 -0.0265764804
## 2018-12-31 -0.1180514843 -0.0073011455 -0.051761952 -0.0417363462
## 2019-01-31 0.1348080312 0.0233990848 -0.080628789 0.0283646139
## 2019-02-28 -0.0469930640 0.0443401511 0.041032981 0.0324430856
## 2019-03-29 0.0824420184 -0.0659509101 -0.133656413 -0.0094924859
## 2019-04-30 0.0786806224 0.0545038051 -0.159123803 0.0530142725
## 2019-05-31 -0.0818753491 -0.1135102949 -0.253945372 -0.0084087905
## 2019-06-28 0.0646557767 0.0916276561 0.188012109 0.0854577850
## 2019-07-31 -0.0142806686 0.0364070754 0.078092373 -0.0009960787
## 2019-08-30 -0.0496880810 -0.3171598787 -0.068516948 0.0394578978
## 2019-09-30 -0.0229951159 0.0181229549 0.065449565 0.0379542679
## 2019-10-31 0.0232034080 0.0914712805 0.268061253 -0.0120373481
## 2019-11-29 0.0134958216 -0.0020940519 0.046592176 0.0154858409
## 2019-12-31 0.0257863501 0.0319748429 0.237359743 0.0023740895
## 2020-01-31 0.0834803026 0.0515909586 0.441578342 -0.0372906808
## 2020-02-28 -0.0642332026 -0.0303425196 0.026424253 -0.0613232536
## 2020-03-31 0.0344213022 0.0623448048 -0.242781458 0.0581101251
## 2020-04-30 0.2381504762 0.0439990589 0.400209535 0.0674665334
## 2020-05-29 -0.0128673719 0.1645791718 0.065730500 0.0248288888
## 2020-06-30 0.1218341331 0.1981301429 0.257108657 -0.0351089263
## 2020-07-31 0.1372488933 0.0682733217 0.281420674 0.0772517309
## 2020-08-31 0.0866005735 -0.0743263222 0.554719320 0.0745885244
## 2020-09-30 -0.0916533253 -0.1472126516 -0.149762306 0.0076052159
## 2020-10-30 -0.0364089187 0.0891077115 -0.100371771 -0.0083254673
## 2020-11-30 0.0425228214 -0.0794433371 0.380308519 0.0963905454
## 2020-12-31 0.0276719582 0.0606630077 0.217730753 -0.0545613483
## 2021-01-29 -0.0156985929 -0.0266309065 0.117343701 -0.0257180631
## 2021-02-26 -0.0359675607 0.0736116794 -0.161038203 -0.0782173623
## 2021-03-31 0.0003717151 -0.0217062161 -0.011269836 0.0486518191
## 2021-04-30 0.1139202399 -0.0172510097 0.060292565 0.0295950986
## 2021-05-28 -0.0730764715 0.2067926140 -0.126372343 0.0189582790
## 2021-06-30 0.0651836137 0.1308994614 0.083547955 -0.0071364767
## 2021-07-30 -0.0332696301 -0.1906690120 0.010973846 0.0107910541
## 2021-08-31 0.0421339363 0.0623582222 0.068224268 0.0418681095
## 2021-09-30 -0.0550034409 -0.0580541138 0.052632612 -0.0606838645
## 2021-10-29 0.0262547651 0.0668399787 0.362230202 0.0695572674
## 2021-11-30 0.0391473463 -0.0847210096 0.027237848 -0.0606287305
## 2021-12-31 -0.0505062029 -0.0525493368 -0.079968440 0.0324795390
## 2022-01-31 -0.1085098142 -0.0116823267 -0.120597475 -0.0343095070
## 2022-02-28 0.0263230079 0.0742248982 -0.073397011 -0.0338249542
## 2022-03-31 0.0596239190 -0.0268808881 0.213504290 0.1008101750
## 2022-04-29 -0.2711856801 -0.0213408259 -0.213125289 0.0269633133
## 2022-05-31 -0.0333130879 0.0065882479 -0.138340062 -0.1698040171
## 2022-06-30 -0.1238178226 -0.0644895002 -0.118657126 -0.0563676819
## 2022-07-29 0.2394860591 0.0382231360 0.280480149 0.0826081356
## 2022-08-31 -0.0625299224 -0.2275969350 -0.075250271 0.0081251868
## 2022-09-30 -0.1149865758 -0.0283402914 -0.038313992 -0.0217358657
## 2022-10-31 -0.0981105335 0.0999708519 -0.153346760 0.0929241006
## 2022-11-30 -0.0593198454 -0.0141227456 -0.155866121 0.0684915668
## 2022-12-30 -0.1391406409 0.0080978855 -0.457813194 -0.0685298754
## 2023-01-31 0.2051735029 0.1169485807 0.340915767 0.0145629297
## 2023-02-28 -0.0902516639 0.0237920448 0.171904973 -0.0121679518
## 2023-03-31 0.0918019370 -0.0068650984 0.008471140 0.0408371296
## 2023-04-28 0.0206963031 0.0020869803 -0.233183710 0.0235920102
## 2023-05-31 0.1340765703 -0.1041115272 0.216021889 -0.0237416277
## 2023-06-30 0.0779864104 0.0267906556 0.249689452 0.0678437111
## 2023-07-31 0.0251489708 -0.0001888581 0.021391609 0.0169069122
## 2023-08-31 0.0318772770 -0.0196253438 -0.035588260 0.0206048123
## 2023-09-29 -0.0821945627 0.0104145463 -0.030929027 -0.0166184916
## 2023-10-31 0.0458940197 0.0601279969 -0.219831983 0.0215261781
## 2023-11-30 0.0931972815 -0.2265672628 0.178463656 -0.0483954709
## 2023-12-29 0.0392628771 0.0333327742 0.034390133 0.0162176849
## 2024-01-31 0.0212288659 -0.0401805080 -0.282704160 0.0470820679
## 2024-02-29 0.1300782611 -0.0078178897 0.075015304 0.0620582315
## 2024-03-28 0.0202729146 0.0683525383 -0.138383423 0.0296685031
## 2024-04-30 -0.0302797899 0.0017319138 0.041724969 -0.0137217860
## 2024-05-31 0.0081949152 -0.0357690012 -0.028782134 0.1060150534
## 2024-06-28 0.0910037984 -0.0648384397 0.105427913 0.0292221364
## 2024-07-31 -0.0329830511 0.0798024930 0.159378271 0.0136415209
## 2024-08-30 -0.0464130352 -0.0639510689 -0.080549177 0.1207589765
## 2024-09-30 0.0429307038 -0.0105013175 0.200441406 0.0445699361
## 2024-10-31 0.0003755644 -0.0582881199 -0.046070548 0.0147513058
## 2024-11-19 0.0932108617 -0.0240913280 0.325578013 0.0551905300
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(.25, .25, .2, .3))
## # A tibble: 1 × 4
## AMZN RGR TSLA WMT
## <dbl> <dbl> <dbl> <dbl>
## 1 0.251 0.206 0.434 0.11
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .3)) %>%
# Transform to long form
pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.25, .25, .2, .3)) %>%
# 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)) +
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?