# 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", "MSFT", "HD", "WMT")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-11-30")
prices
## # A tibble: 9,988 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AMZN 2012-12-31 12.2 12.6 12.1 12.5 68380000 12.5
## 2 AMZN 2013-01-02 12.8 12.9 12.7 12.9 65420000 12.9
## 3 AMZN 2013-01-03 12.9 13.0 12.8 12.9 55018000 12.9
## 4 AMZN 2013-01-04 12.9 13.0 12.8 13.0 37484000 13.0
## 5 AMZN 2013-01-07 13.1 13.5 13.1 13.4 98200000 13.4
## 6 AMZN 2013-01-08 13.4 13.4 13.2 13.3 60214000 13.3
## 7 AMZN 2013-01-09 13.4 13.5 13.3 13.3 45312000 13.3
## 8 AMZN 2013-01-10 13.4 13.4 13.1 13.3 57268000 13.3
## 9 AMZN 2013-01-11 13.3 13.4 13.2 13.4 48266000 13.4
## 10 AMZN 2013-01-14 13.4 13.7 13.4 13.6 85500000 13.6
## # … with 9,978 more rows
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: 476 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AMZN 2013-01-31 0.0567
## 2 AMZN 2013-02-28 -0.00464
## 3 AMZN 2013-03-28 0.00837
## 4 AMZN 2013-04-30 -0.0488
## 5 AMZN 2013-05-31 0.0589
## 6 AMZN 2013-06-28 0.0311
## 7 AMZN 2013-07-31 0.0813
## 8 AMZN 2013-08-30 -0.0696
## 9 AMZN 2013-09-30 0.107
## 10 AMZN 2013-10-31 0.152
## # … with 466 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
## AMZN HD MSFT WMT
## 2013-01-31 0.0566799640 0.0787859351 0.027327946 0.0248963751
## 2013-02-28 -0.0046435329 0.0233354638 0.020915524 0.0117959006
## 2013-03-28 0.0083654117 0.0239971337 0.028720185 0.0620731511
## 2013-04-30 -0.0487507638 0.0498954036 0.145777070 0.0378936132
## 2013-05-31 0.0588686422 0.0698920561 0.059941086 -0.0317798065
## 2013-06-28 0.0310507858 -0.0103002441 -0.010368741 -0.0046877928
## 2013-07-31 0.0813355112 0.0199370832 -0.081394844 0.0452746211
## 2013-08-30 -0.0695574024 -0.0591630457 0.054854662 -0.0597001052
## 2013-09-30 0.1067688764 0.0233423914 -0.003599339 0.0133391417
## 2013-10-31 0.1521839130 0.0265395857 0.062037644 0.0370290088
## 2013-11-29 0.0781496951 0.0350694176 0.081562329 0.0540190909
## 2013-12-31 0.0130490358 0.0253913661 -0.019063311 -0.0232520715
## 2014-01-31 -0.1059765070 -0.0690017285 0.011428608 -0.0523039108
## 2014-02-28 0.0094619111 0.0652295603 0.019814922 0.0002675567
## 2014-03-31 -0.0737086127 -0.0302570985 0.067617132 0.0293262461
## 2014-04-30 -0.1007565625 0.0047905155 -0.014497947 0.0420198454
## 2014-05-30 0.0273092148 0.0090150281 0.020307413 -0.0314092358
## 2014-06-30 0.0383835737 0.0148884622 0.018393640 -0.0223929697
## 2014-07-31 -0.0369767889 -0.0013596655 0.034412912 -0.0200475740
## 2014-08-29 0.0799468534 0.1453658565 0.057484967 0.0323257942
## 2014-09-30 -0.0502010221 -0.0139635202 0.020264347 0.0127657707
## 2014-10-31 -0.0540982353 0.0610988276 0.012646342 -0.0026188517
## 2014-11-28 0.1031187000 0.0190947559 0.024438838 0.1378160783
## 2014-12-31 -0.0872368443 0.0592868769 -0.028858292 -0.0135735468
## 2015-01-30 0.1330922758 -0.0052531698 -0.139546347 -0.0105350785
## 2015-02-27 0.0697991955 0.0943345582 0.089035747 -0.0124330150
## 2015-03-31 -0.0214295288 -0.0048516941 -0.075529585 -0.0142312504
## 2015-04-30 0.1253212631 -0.0601298101 0.179201281 -0.0524133621
## 2015-05-29 0.0175090073 0.0406649653 -0.030803999 -0.0433513626
## 2015-06-30 0.0112589801 0.0026913033 -0.059571387 -0.0460138284
## 2015-07-31 0.2111621241 0.0517295394 0.056151346 0.0146949230
## 2015-08-31 -0.0443525737 -0.0048824469 -0.063951060 -0.0993583301
## 2015-09-30 -0.0019516780 -0.0032848495 0.016860689 0.0016978457
## 2015-10-30 0.2010808557 0.0681901178 0.173395132 -0.1246696684
## 2015-11-30 0.0602956898 0.0795695605 0.038686222 0.0275687753
## 2015-12-31 0.0165439780 -0.0078329472 0.020577889 0.0492992352
## 2016-01-29 -0.1410054619 -0.0503185974 -0.007054510 0.0793144714
## 2016-02-29 -0.0605352242 -0.0131267893 -0.072344166 -0.0003011973
## 2016-03-31 0.0717834457 0.0778121822 0.082036390 0.0392702592
## 2016-04-29 0.1053453885 0.0034415821 -0.102086550 -0.0239371790
## 2016-05-31 0.0915002937 -0.0081431574 0.067842532 0.0641208094
## 2016-06-30 -0.0099694796 -0.0341052657 -0.035138826 0.0311571570
## 2016-07-29 0.0586021200 0.0793862306 0.102268176 -0.0006848384
## 2016-08-31 0.0135476463 -0.0251416775 0.019880925 -0.0143682961
## 2016-09-30 0.0848953859 -0.0414059025 0.002433700 0.0094734268
## 2016-10-31 -0.0583892995 -0.0532259220 0.039487517 -0.0295504129
## 2016-11-30 -0.0509721788 0.0641009482 0.012390934 0.0058383834
## 2016-12-30 -0.0009330597 0.0355285138 0.030721548 -0.0116436368
## 2017-01-31 0.0936394046 0.0257688134 0.039598291 -0.0350397856
## 2017-02-28 0.0258446771 0.0519073275 -0.004373203 0.0608891389
## 2017-03-31 0.0479423059 0.0192299531 0.028960376 0.0234092672
## 2017-04-28 0.0424566809 0.0612214113 0.038718637 0.0421087426
## 2017-05-31 0.0725778079 -0.0109689652 0.025672663 0.0511561498
## 2017-06-30 -0.0271286060 -0.0007169619 -0.013115549 -0.0378581004
## 2017-07-31 0.0202278723 -0.0250833726 0.053249877 0.0553878186
## 2017-08-31 -0.0072953921 0.0076989500 0.033389270 -0.0180253803
## 2017-09-29 -0.0198260414 0.0874119372 -0.003751696 0.0008963573
## 2017-10-31 0.1395154081 0.0134816048 0.110341923 0.1109627299
## 2017-11-30 0.0626577388 0.0863478981 0.016840979 0.1076144166
## 2017-12-29 -0.0062057977 0.0525910960 0.016145686 0.0207684968
## 2018-01-31 0.2156265512 0.0582598892 0.104998014 0.0764919297
## 2018-02-28 0.0415536373 -0.0973178983 -0.008450576 -0.1691626278
## 2018-03-29 -0.0440034786 -0.0166715388 -0.027023030 -0.0056772628
## 2018-04-30 0.0788802990 0.0361431169 0.024353300 -0.0057488960
## 2018-05-31 0.0397392491 0.0150081495 0.059651954 -0.0629872101
## 2018-06-29 0.0421636779 0.0448131779 -0.002329832 0.0369863730
## 2018-07-31 0.0446635758 0.0123273668 0.073021174 0.0409480727
## 2018-08-31 0.1243079035 0.0214330045 0.061088248 0.0774625094
## 2018-09-28 -0.0048359762 0.0312831120 0.017997817 -0.0205519103
## 2018-10-31 -0.2258870091 -0.1636409972 -0.068387264 0.0656295403
## 2018-11-30 0.0560700362 0.0308949028 0.041797919 -0.0265764790
## 2018-12-31 -0.1180514840 -0.0482854831 -0.087790414 -0.0417363774
## 2019-01-31 0.1348080379 0.0659304558 0.027768702 0.0283646790
## 2019-02-28 -0.0469930675 0.0087342282 0.074511213 0.0324429213
## 2019-03-29 0.0824420113 0.0432288572 0.051409442 -0.0094925515
## 2019-04-30 0.0786806236 0.0597260898 0.101963216 0.0530144131
## 2019-05-31 -0.0818753437 -0.0704141318 -0.050746851 -0.0084088107
## 2019-06-28 0.0646557723 0.0981493479 0.079843771 0.0854574574
## 2019-07-31 -0.0142806642 0.0271327464 0.017096730 -0.0009959740
## 2019-08-30 -0.0496880811 0.0644242343 0.014924943 0.0394579787
## 2019-09-30 -0.0229951136 0.0239609329 0.008450903 0.0379541740
## 2019-10-31 0.0232034025 0.0109731623 0.030739249 -0.0120370114
## 2019-11-29 0.0134958246 -0.0618538632 0.057761620 0.0154857896
## 2019-12-31 0.0257863460 -0.0033308619 0.040901072 0.0023739170
## 2020-01-31 0.0834803057 0.0435474794 0.076456133 -0.0372906561
## 2020-02-28 -0.0642332025 -0.0460232239 -0.046765147 -0.0613234360
## 2020-03-31 0.0344213016 -0.1475361035 -0.026899867 0.0581106729
## 2020-04-30 0.2381504772 0.1632980198 0.127800401 0.0674660910
## 2020-05-29 -0.0128673704 0.1225077594 0.025074235 0.0248288042
## 2020-06-30 0.1218341292 0.0140897637 0.104863650 -0.0351086201
## 2020-07-31 0.1372488981 0.0580783640 0.007343664 0.0772517146
## 2020-08-31 0.0866005697 0.0710525449 0.097808938 0.0745885739
## 2020-09-30 -0.0916533239 -0.0207926596 -0.069775649 0.0076049044
## 2020-10-30 -0.0364089200 -0.0404156240 -0.038085818 -0.0083253780
## 2020-11-30 0.0425228235 0.0393349483 0.058325886 0.0963905829
## 2020-12-31 0.0276719595 -0.0379923144 0.038264431 -0.0545615554
## 2021-01-29 -0.0156985927 0.0193877173 0.041997553 -0.0257176320
## 2021-02-26 -0.0359675611 -0.0471778972 0.004109538 -0.0782177690
## 2021-03-31 0.0003717143 0.1731014175 0.014482679 0.0486518646
## 2021-04-30 0.1139202371 0.0585934165 0.067286432 0.0295951582
## 2021-05-28 -0.0730764730 -0.0148155873 -0.007656639 0.0189584089
## 2021-06-30 0.0651836186 0.0051673938 0.081569638 -0.0071364181
## 2021-07-30 -0.0332696344 0.0287466214 0.050423708 0.0107911734
## 2021-08-31 0.0421339383 -0.0061434159 0.059768766 0.0418678651
## 2021-09-30 -0.0550034402 0.0114278684 -0.068406208 -0.0606837125
## 2021-10-29 0.0262547660 0.1243888350 0.162366302 0.0695572290
## 2021-11-30 0.0391473427 0.0747937113 -0.001282880 -0.0606289782
## 2021-12-31 -0.0505062023 0.0394415138 0.017184182 0.0324795267
## 2022-01-31 -0.1085098100 -0.1229953660 -0.078334587 -0.0343091453
## 2022-02-28 0.0263230081 -0.1501032384 -0.037922007 -0.0338249566
## 2022-03-31 0.0596239187 -0.0476407627 0.031364736 0.1008100516
## 2022-04-29 -0.2711856809 0.0035682923 -0.105212719 0.0269634076
## 2022-05-31 -0.0333130912 0.0077925135 -0.018242512 -0.1698041918
## 2022-06-30 -0.1238178220 -0.0924988388 -0.056909700 -0.0563676086
## 2022-07-29 0.2394860603 0.0927979479 0.089014610 0.0826081523
## 2022-08-31 -0.0625299199 -0.0359896461 -0.068989117 0.0081250881
## 2022-09-30 -0.1149865786 -0.0442343382 -0.115710400 -0.0217358872
## 2022-10-31 -0.0981105379 0.0706151060 -0.003311557 0.0929242509
## 2022-11-29 -0.1029338971 0.0648170308 0.037529615 0.0720935111
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMZN HD MSFT WMT
## AMZN 0.0074641054 0.0021180678 0.0027861087 0.0007980405
## HD 0.0021180678 0.0034430660 0.0015253364 0.0009274973
## MSFT 0.0027861087 0.0015253364 0.0035400902 0.0007248449
## WMT 0.0007980405 0.0009274973 0.0007248449 0.0027751091
# 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.30, 0.30, 0.15, 0.25)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.04813749
# 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 HD MSFT WMT
## [1,] 0.02176315 0.01326834 0.00624978 0.006856222
rowSums(component_contribution)
## [1] 0.04813749
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 4
## AMZN HD MSFT WMT
## <dbl> <dbl> <dbl> <dbl>
## 1 0.452 0.276 0.13 0.142
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
## asset contribution
## <chr> <dbl>
## 1 AMZN 0.452
## 2 HD 0.276
## 3 MSFT 0.13
## 4 WMT 0.142
# 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 HD MSFT WMT
## 2013-01-31 0.0566799640 0.0787859351 0.027327946 0.0248963751
## 2013-02-28 -0.0046435329 0.0233354638 0.020915524 0.0117959006
## 2013-03-28 0.0083654117 0.0239971337 0.028720185 0.0620731511
## 2013-04-30 -0.0487507638 0.0498954036 0.145777070 0.0378936132
## 2013-05-31 0.0588686422 0.0698920561 0.059941086 -0.0317798065
## 2013-06-28 0.0310507858 -0.0103002441 -0.010368741 -0.0046877928
## 2013-07-31 0.0813355112 0.0199370832 -0.081394844 0.0452746211
## 2013-08-30 -0.0695574024 -0.0591630457 0.054854662 -0.0597001052
## 2013-09-30 0.1067688764 0.0233423914 -0.003599339 0.0133391417
## 2013-10-31 0.1521839130 0.0265395857 0.062037644 0.0370290088
## 2013-11-29 0.0781496951 0.0350694176 0.081562329 0.0540190909
## 2013-12-31 0.0130490358 0.0253913661 -0.019063311 -0.0232520715
## 2014-01-31 -0.1059765070 -0.0690017285 0.011428608 -0.0523039108
## 2014-02-28 0.0094619111 0.0652295603 0.019814922 0.0002675567
## 2014-03-31 -0.0737086127 -0.0302570985 0.067617132 0.0293262461
## 2014-04-30 -0.1007565625 0.0047905155 -0.014497947 0.0420198454
## 2014-05-30 0.0273092148 0.0090150281 0.020307413 -0.0314092358
## 2014-06-30 0.0383835737 0.0148884622 0.018393640 -0.0223929697
## 2014-07-31 -0.0369767889 -0.0013596655 0.034412912 -0.0200475740
## 2014-08-29 0.0799468534 0.1453658565 0.057484967 0.0323257942
## 2014-09-30 -0.0502010221 -0.0139635202 0.020264347 0.0127657707
## 2014-10-31 -0.0540982353 0.0610988276 0.012646342 -0.0026188517
## 2014-11-28 0.1031187000 0.0190947559 0.024438838 0.1378160783
## 2014-12-31 -0.0872368443 0.0592868769 -0.028858292 -0.0135735468
## 2015-01-30 0.1330922758 -0.0052531698 -0.139546347 -0.0105350785
## 2015-02-27 0.0697991955 0.0943345582 0.089035747 -0.0124330150
## 2015-03-31 -0.0214295288 -0.0048516941 -0.075529585 -0.0142312504
## 2015-04-30 0.1253212631 -0.0601298101 0.179201281 -0.0524133621
## 2015-05-29 0.0175090073 0.0406649653 -0.030803999 -0.0433513626
## 2015-06-30 0.0112589801 0.0026913033 -0.059571387 -0.0460138284
## 2015-07-31 0.2111621241 0.0517295394 0.056151346 0.0146949230
## 2015-08-31 -0.0443525737 -0.0048824469 -0.063951060 -0.0993583301
## 2015-09-30 -0.0019516780 -0.0032848495 0.016860689 0.0016978457
## 2015-10-30 0.2010808557 0.0681901178 0.173395132 -0.1246696684
## 2015-11-30 0.0602956898 0.0795695605 0.038686222 0.0275687753
## 2015-12-31 0.0165439780 -0.0078329472 0.020577889 0.0492992352
## 2016-01-29 -0.1410054619 -0.0503185974 -0.007054510 0.0793144714
## 2016-02-29 -0.0605352242 -0.0131267893 -0.072344166 -0.0003011973
## 2016-03-31 0.0717834457 0.0778121822 0.082036390 0.0392702592
## 2016-04-29 0.1053453885 0.0034415821 -0.102086550 -0.0239371790
## 2016-05-31 0.0915002937 -0.0081431574 0.067842532 0.0641208094
## 2016-06-30 -0.0099694796 -0.0341052657 -0.035138826 0.0311571570
## 2016-07-29 0.0586021200 0.0793862306 0.102268176 -0.0006848384
## 2016-08-31 0.0135476463 -0.0251416775 0.019880925 -0.0143682961
## 2016-09-30 0.0848953859 -0.0414059025 0.002433700 0.0094734268
## 2016-10-31 -0.0583892995 -0.0532259220 0.039487517 -0.0295504129
## 2016-11-30 -0.0509721788 0.0641009482 0.012390934 0.0058383834
## 2016-12-30 -0.0009330597 0.0355285138 0.030721548 -0.0116436368
## 2017-01-31 0.0936394046 0.0257688134 0.039598291 -0.0350397856
## 2017-02-28 0.0258446771 0.0519073275 -0.004373203 0.0608891389
## 2017-03-31 0.0479423059 0.0192299531 0.028960376 0.0234092672
## 2017-04-28 0.0424566809 0.0612214113 0.038718637 0.0421087426
## 2017-05-31 0.0725778079 -0.0109689652 0.025672663 0.0511561498
## 2017-06-30 -0.0271286060 -0.0007169619 -0.013115549 -0.0378581004
## 2017-07-31 0.0202278723 -0.0250833726 0.053249877 0.0553878186
## 2017-08-31 -0.0072953921 0.0076989500 0.033389270 -0.0180253803
## 2017-09-29 -0.0198260414 0.0874119372 -0.003751696 0.0008963573
## 2017-10-31 0.1395154081 0.0134816048 0.110341923 0.1109627299
## 2017-11-30 0.0626577388 0.0863478981 0.016840979 0.1076144166
## 2017-12-29 -0.0062057977 0.0525910960 0.016145686 0.0207684968
## 2018-01-31 0.2156265512 0.0582598892 0.104998014 0.0764919297
## 2018-02-28 0.0415536373 -0.0973178983 -0.008450576 -0.1691626278
## 2018-03-29 -0.0440034786 -0.0166715388 -0.027023030 -0.0056772628
## 2018-04-30 0.0788802990 0.0361431169 0.024353300 -0.0057488960
## 2018-05-31 0.0397392491 0.0150081495 0.059651954 -0.0629872101
## 2018-06-29 0.0421636779 0.0448131779 -0.002329832 0.0369863730
## 2018-07-31 0.0446635758 0.0123273668 0.073021174 0.0409480727
## 2018-08-31 0.1243079035 0.0214330045 0.061088248 0.0774625094
## 2018-09-28 -0.0048359762 0.0312831120 0.017997817 -0.0205519103
## 2018-10-31 -0.2258870091 -0.1636409972 -0.068387264 0.0656295403
## 2018-11-30 0.0560700362 0.0308949028 0.041797919 -0.0265764790
## 2018-12-31 -0.1180514840 -0.0482854831 -0.087790414 -0.0417363774
## 2019-01-31 0.1348080379 0.0659304558 0.027768702 0.0283646790
## 2019-02-28 -0.0469930675 0.0087342282 0.074511213 0.0324429213
## 2019-03-29 0.0824420113 0.0432288572 0.051409442 -0.0094925515
## 2019-04-30 0.0786806236 0.0597260898 0.101963216 0.0530144131
## 2019-05-31 -0.0818753437 -0.0704141318 -0.050746851 -0.0084088107
## 2019-06-28 0.0646557723 0.0981493479 0.079843771 0.0854574574
## 2019-07-31 -0.0142806642 0.0271327464 0.017096730 -0.0009959740
## 2019-08-30 -0.0496880811 0.0644242343 0.014924943 0.0394579787
## 2019-09-30 -0.0229951136 0.0239609329 0.008450903 0.0379541740
## 2019-10-31 0.0232034025 0.0109731623 0.030739249 -0.0120370114
## 2019-11-29 0.0134958246 -0.0618538632 0.057761620 0.0154857896
## 2019-12-31 0.0257863460 -0.0033308619 0.040901072 0.0023739170
## 2020-01-31 0.0834803057 0.0435474794 0.076456133 -0.0372906561
## 2020-02-28 -0.0642332025 -0.0460232239 -0.046765147 -0.0613234360
## 2020-03-31 0.0344213016 -0.1475361035 -0.026899867 0.0581106729
## 2020-04-30 0.2381504772 0.1632980198 0.127800401 0.0674660910
## 2020-05-29 -0.0128673704 0.1225077594 0.025074235 0.0248288042
## 2020-06-30 0.1218341292 0.0140897637 0.104863650 -0.0351086201
## 2020-07-31 0.1372488981 0.0580783640 0.007343664 0.0772517146
## 2020-08-31 0.0866005697 0.0710525449 0.097808938 0.0745885739
## 2020-09-30 -0.0916533239 -0.0207926596 -0.069775649 0.0076049044
## 2020-10-30 -0.0364089200 -0.0404156240 -0.038085818 -0.0083253780
## 2020-11-30 0.0425228235 0.0393349483 0.058325886 0.0963905829
## 2020-12-31 0.0276719595 -0.0379923144 0.038264431 -0.0545615554
## 2021-01-29 -0.0156985927 0.0193877173 0.041997553 -0.0257176320
## 2021-02-26 -0.0359675611 -0.0471778972 0.004109538 -0.0782177690
## 2021-03-31 0.0003717143 0.1731014175 0.014482679 0.0486518646
## 2021-04-30 0.1139202371 0.0585934165 0.067286432 0.0295951582
## 2021-05-28 -0.0730764730 -0.0148155873 -0.007656639 0.0189584089
## 2021-06-30 0.0651836186 0.0051673938 0.081569638 -0.0071364181
## 2021-07-30 -0.0332696344 0.0287466214 0.050423708 0.0107911734
## 2021-08-31 0.0421339383 -0.0061434159 0.059768766 0.0418678651
## 2021-09-30 -0.0550034402 0.0114278684 -0.068406208 -0.0606837125
## 2021-10-29 0.0262547660 0.1243888350 0.162366302 0.0695572290
## 2021-11-30 0.0391473427 0.0747937113 -0.001282880 -0.0606289782
## 2021-12-31 -0.0505062023 0.0394415138 0.017184182 0.0324795267
## 2022-01-31 -0.1085098100 -0.1229953660 -0.078334587 -0.0343091453
## 2022-02-28 0.0263230081 -0.1501032384 -0.037922007 -0.0338249566
## 2022-03-31 0.0596239187 -0.0476407627 0.031364736 0.1008100516
## 2022-04-29 -0.2711856809 0.0035682923 -0.105212719 0.0269634076
## 2022-05-31 -0.0333130912 0.0077925135 -0.018242512 -0.1698041918
## 2022-06-30 -0.1238178220 -0.0924988388 -0.056909700 -0.0563676086
## 2022-07-29 0.2394860603 0.0927979479 0.089014610 0.0826081523
## 2022-08-31 -0.0625299199 -0.0359896461 -0.068989117 0.0081250881
## 2022-09-30 -0.1149865786 -0.0442343382 -0.115710400 -0.0217358872
## 2022-10-31 -0.0981105379 0.0706151060 -0.003311557 0.0929242509
## 2022-11-29 -0.1029338971 0.0648170308 0.037529615 0.0720935111
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.30, 0.30, 0.15, 0.25))
## # A tibble: 1 × 4
## AMZN HD MSFT WMT
## <dbl> <dbl> <dbl> <dbl>
## 1 0.452 0.276 0.13 0.142
Column Chart of Component contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.30, 0.30, 0.15, 0.25)) %>%
# 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")
Column Chart of Contribution and Weight
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.30, 0.30, 0.15, 0.25)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(0.30, 0.30, 0.15, 0.25)) %>%
# 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 volatility is Amazon, with a weight of 30% and a percent contribution of 50%. I think my portfolio risk is concentrated in any one asset because Amazon has the highest weight and the highest contribution. If amazon were to tank, I have other assets that will balance it out but my return would still decrease more than increase if something were to happen that negatively impacts Amazon.