# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
Choose your stocks from 2012-12-31 to present.
symbols <- c("GE", "GILD", "MSCI", "SBUX", "ROL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-12-01")
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
## GE GILD MSCI ROL SBUX
## 2013-01-31 0.0596434695 0.0715761823 0.085019553 0.114753594 0.045383720
## 2013-02-28 0.0494738041 0.0796333126 -0.018244984 -0.004930403 -0.019139167
## 2013-03-28 -0.0043160583 0.1359277718 0.023860405 0.001630417 0.037571478
## 2013-04-30 -0.0365595549 0.0341468140 0.004998234 -0.009412558 0.066073766
## 2013-05-31 0.0451729121 0.0730920886 0.032884152 0.041609191 0.040474103
## 2013-06-28 0.0023646253 -0.0607280546 -0.057525749 0.025020926 0.036848171
## 2013-07-31 0.0496317164 0.1803038954 0.052119172 -0.015564336 0.084553393
## 2013-08-30 -0.0517902771 -0.0185751991 0.067832382 -0.026292243 -0.008016991
## 2013-09-30 0.0395693431 0.0422345326 0.070750476 0.068696674 0.087519463
## 2013-10-31 0.0900068538 0.1241428281 0.012588636 0.041741911 0.051650483
## 2013-11-29 0.0196978424 0.0497399210 0.085067484 0.024239357 0.008261097
## 2013-12-31 0.0581697127 0.0038685973 -0.015208484 0.073980686 -0.038415403
## 2014-01-31 -0.1092128883 0.0712986638 -0.023138459 -0.049747752 -0.097327871
## 2014-02-28 0.0221377815 0.0261883010 0.022909503 0.039278372 0.001524581
## 2014-03-31 0.0163557243 -0.1556011666 -0.015911617 0.012645663 0.033535185
## 2014-04-30 0.0378975739 0.1022648775 -0.059375983 -0.005304846 -0.038338449
## 2014-05-30 -0.0037258204 0.0340674060 0.062625053 0.022950306 0.040101583
## 2014-06-30 -0.0110056608 0.0207173593 0.060460927 -0.022087451 0.054986180
## 2014-07-31 -0.0439501645 0.0991293826 -0.013172351 -0.057982179 0.003869392
## 2014-08-29 0.0324689830 0.1611634765 0.019477363 0.053293173 0.005031593
## 2014-09-30 -0.0055436529 -0.0103734959 0.018893136 -0.015923861 -0.030667238
## 2014-10-31 0.0073886020 0.0508237365 -0.003642119 0.084760116 0.001324586
## 2014-11-28 0.0260051237 -0.1101337475 0.035785906 0.026878351 0.076232922
## 2014-12-31 -0.0377784992 -0.0623083897 -0.019207332 0.017370738 0.010290737
## 2015-01-30 -0.0561577872 0.1062832703 0.126179338 -0.001511763 0.064652869
## 2015-02-27 0.0934025708 -0.0124787131 0.044812543 0.018275821 0.069516032
## 2015-03-31 -0.0464653376 -0.0535680370 0.088629020 0.100743352 0.012859747
## 2015-04-30 0.0875488086 0.0239640244 -0.001959263 0.002826572 0.046020554
## 2015-05-29 0.0069916457 0.1106495138 0.016894323 0.004067306 0.050063331
## 2015-06-30 -0.0175345418 0.0455666235 -0.008090452 0.139306485 0.031448030
## 2015-07-31 -0.0178474920 0.0066398760 0.102008243 0.016339726 0.077313135
## 2015-08-31 -0.0502852437 -0.1148706607 -0.115438291 -0.035299092 -0.054435476
## 2015-09-30 0.0248968356 -0.0637924405 -0.017670049 -0.038332894 0.038193727
## 2015-10-30 0.1368959649 0.0964298901 0.119388769 -0.001862652 0.096045611
## 2015-11-30 0.0346622791 -0.0202725342 0.048830281 0.019055072 -0.015806325
## 2015-12-31 0.0470705283 -0.0417536859 0.028261889 -0.047133727 -0.022402672
## 2016-01-29 -0.0680761527 -0.1981592288 -0.046685545 0.061759707 0.012251897
## 2016-02-29 0.0093476044 0.0499368454 0.027324554 0.003097725 -0.039776560
## 2016-03-31 0.0870398771 0.0562959251 0.049249190 -0.015004840 0.025274931
## 2016-04-29 -0.0332618129 -0.0405454312 0.024798014 -0.009260986 -0.059881563
## 2016-05-31 -0.0170551288 -0.0131224241 0.052302459 0.059789140 -0.020626503
## 2016-06-30 0.0480647028 -0.0370970840 -0.034035849 0.029469825 0.039823107
## 2016-07-29 -0.0108592251 -0.0485087076 0.109449813 -0.037950764 0.016150536
## 2016-08-31 0.0032062610 -0.0138106455 0.049587400 0.014912350 -0.028377652
## 2016-09-30 -0.0454754170 0.0154359866 -0.071039652 0.027000303 -0.037877287
## 2016-10-31 -0.0177114958 -0.0719133678 -0.045703694 0.051259338 -0.019961501
## 2016-11-30 0.0554768919 0.0009502942 -0.014026316 0.048160184 0.092935705
## 2016-12-30 0.0344397566 -0.0223501562 -0.000253843 0.050078715 -0.043182489
## 2017-01-31 -0.0620102093 0.0116620362 0.049164876 0.042880288 -0.005418327
## 2017-02-28 0.0116414394 -0.0275675715 0.136712350 0.039408690 0.033938745
## 2017-03-31 -0.0003354117 -0.0293337362 0.027116056 0.015470306 0.026376691
## 2017-04-28 -0.0275576245 0.0092328682 0.031697307 0.044767979 0.028199504
## 2017-05-31 -0.0571379216 -0.0548696914 0.016814297 0.106512696 0.061537506
## 2017-06-30 -0.0052053131 0.0948967215 0.012309555 -0.056352910 -0.086997173
## 2017-07-31 -0.0532244859 0.0723401925 0.056257058 0.064215982 -0.077159935
## 2017-08-31 -0.0422709111 0.0954414008 0.054129641 0.025383746 0.020674906
## 2017-09-29 -0.0052437122 -0.0264288912 0.019783878 0.038215783 -0.021185294
## 2017-10-31 -0.1818257029 -0.0777411510 0.003927248 -0.049538494 0.020820779
## 2017-11-30 -0.0973457494 -0.0024041845 0.095301623 0.058843116 0.058198376
## 2017-12-29 -0.0401340881 -0.0360833366 -0.016925590 0.003876059 -0.006768071
## 2018-01-31 -0.0761821170 0.1567792323 0.095568467 0.058637636 -0.010854341
## 2018-02-28 -0.1280322567 -0.0624086947 0.018926442 0.021616751 0.010501052
## 2018-03-29 -0.0456764851 -0.0362961381 0.054654659 0.015005005 0.013740608
## 2018-04-30 0.0428375189 -0.0428193113 0.002405191 -0.050437819 -0.005543110
## 2018-05-31 0.0007107486 -0.0692104132 0.084010985 0.028245527 -0.010541193
## 2018-06-29 -0.0251139773 0.0578061409 0.017439253 0.054923364 -0.148490466
## 2018-07-31 0.0014682626 0.0941029434 0.004583596 0.043905962 0.069961317
## 2018-08-31 -0.0519498954 -0.0273522610 0.084614949 0.091927107 0.026968865
## 2018-09-28 -0.1269196782 0.0270903069 -0.015936799 0.010101932 0.061493684
## 2018-10-31 -0.1113818343 -0.1243777790 -0.165297849 -0.024857282 0.024847065
## 2018-11-30 -0.2976325264 0.0536812908 0.047575462 0.074106581 0.140772150
## 2018-12-31 0.0106648012 -0.1314658830 -0.063465415 -0.160226305 -0.035391098
## 2019-01-31 0.2942653654 0.1126716864 0.144032075 0.031090574 0.056450532
## 2019-02-28 0.0616060147 -0.0739432239 0.084754349 0.065755355 0.035843545
## 2019-03-29 -0.0382005630 0.0094743146 0.073659388 0.048237547 0.056446624
## 2019-04-30 0.0178578101 0.0004613478 0.125287366 -0.073516516 0.043948474
## 2019-05-31 -0.0744865082 -0.0438439845 -0.021485774 -0.025995469 -0.016446744
## 2019-06-28 0.1073815630 0.0913361678 0.081911487 -0.046304543 0.097268369
## 2019-07-31 -0.0047732618 -0.0306606423 -0.049577577 -0.067460640 0.121817720
## 2019-08-30 -0.2363888565 -0.0306857138 0.035114223 -0.018493176 0.023339237
## 2019-09-30 0.0814027556 0.0067757146 -0.074662171 0.037683723 -0.088066478
## 2019-10-31 0.1100475562 0.0051931566 0.074363608 0.112059663 -0.044636755
## 2019-11-29 0.1215613955 0.0539268220 0.102582303 -0.057090726 0.015222209
## 2019-12-31 -0.0089022333 -0.0248506024 -0.003904282 -0.077999143 0.028729961
## 2020-01-31 0.1093846417 -0.0277753410 0.101635429 0.134925045 -0.035778109
## 2020-02-28 -0.1347943885 0.0930062057 0.035223063 -0.010474105 -0.073792941
## 2020-03-31 -0.3140203861 0.0842419861 -0.022177716 -0.035339337 -0.176498736
## 2020-04-30 -0.1549906539 0.1165339265 0.123672071 0.101479300 0.154585135
## 2020-05-29 -0.0344088107 -0.0762900511 0.007669778 0.045936645 0.021923266
## 2020-06-30 0.0403089746 -0.0026385843 0.015000043 0.014016220 -0.058071607
## 2020-07-31 -0.1179659991 -0.1012675788 0.118933963 0.211994070 0.039173686
## 2020-08-31 0.0435201567 -0.0408040510 -0.005032764 0.052461526 0.104138694
## 2020-09-30 -0.0158510739 -0.0443851012 -0.045182656 -0.017379102 0.017020248
## 2020-10-30 0.1748028436 -0.0831202722 -0.019643446 0.065357204 0.012031481
## 2020-11-30 0.3162458991 0.0424235461 0.159232328 -0.008318783 0.124427828
## 2020-12-31 0.0600407018 -0.0292790330 0.086764843 0.024615752 0.087470278
## 2021-01-29 -0.0111733753 0.1186601011 -0.121861645 -0.081280509 -0.099891681
## 2021-02-26 0.1605506660 -0.0661658716 0.049253390 -0.080277458 0.113907173
## 2021-03-31 0.0467135429 0.0625863030 0.011417738 0.036991882 0.011412812
## 2021-04-30 -0.0007620029 -0.0181112502 0.147196287 0.079819260 0.046665566
## 2021-05-28 0.0691961405 0.0407526252 -0.035292818 -0.087286688 -0.001371345
## 2021-06-30 -0.0428508771 0.0511331912 0.129925434 0.003221519 -0.018344234
## 2021-07-30 -0.0386265986 -0.0083120882 0.111502058 0.114007485 0.082536982
## 2021-08-31 0.0173203349 0.0636778342 0.064455529 0.017373732 -0.029110901
## 2021-09-30 -0.0220600964 -0.0310619609 -0.042229579 -0.096775667 -0.063058697
## 2021-10-29 0.0177012777 -0.0738107841 0.088872695 -0.002834499 -0.039195313
## 2021-11-30 -0.0989498363 0.0605522102 -0.053165921 -0.051893652 0.037405586
## 2021-12-31 -0.0046140982 0.0621172485 -0.026987214 0.027561368 0.064715152
## 2022-01-31 0.0001058190 -0.0556444703 -0.133501159 -0.103381227 -0.173689974
## 2022-02-28 0.0108428512 -0.1284689962 -0.064460490 0.059389116 -0.063666653
## 2022-03-31 -0.0419942411 -0.0031874014 0.002369132 0.071543438 -0.008973476
## 2022-04-29 -0.2048687715 -0.0018520226 -0.177125145 -0.044036712 -0.197853254
## 2022-05-31 0.0489497466 0.0887932933 0.051563270 0.058609314 0.057440723
## 2022-06-30 -0.2055131015 -0.0355933891 -0.070714140 -0.015345551 -0.027246970
## 2022-07-29 0.1491347717 -0.0338960371 0.155186530 0.099415121 0.104207927
## 2022-08-31 -0.0063794377 0.0603982012 -0.066572838 -0.130547808 -0.002728313
## 2022-09-30 -0.1695469356 -0.0171686626 -0.063049665 0.026886618 0.002257528
## 2022-10-31 0.2285882144 0.2404671010 0.105796643 0.193409473 0.027276930
## 2022-11-30 0.0997284779 0.1128142227 0.082517618 -0.036653271 0.171478081
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## GE GILD MSCI ROL SBUX
## GE 0.0090952999 0.0010940844 0.002013042 0.0004372444 0.0016255421
## GILD 0.0010940844 0.0055761261 0.001247834 0.0002206646 0.0004109835
## MSCI 0.0020130425 0.0012478342 0.004524904 0.0016182804 0.0020206589
## ROL 0.0004372444 0.0002206646 0.001618280 0.0036493854 0.0008120992
## SBUX 0.0016255421 0.0004109835 0.002020659 0.0008120992 0.0040638689
# 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.25, 0.2, 0.2, 0.1)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.04645226
# Component contribution
# Similar to the formula for sd_portfolio
# Mathematical trick to summarize the same, sd_portfolio, by asset instead of a single number
calculate_component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
calculate_component_contribution
## GE GILD MSCI ROL SBUX
## [1,] 0.01722175 0.01077639 0.009669807 0.005593787 0.003190523
rowSums(calculate_component_contribution)
## [1] 0.04645226
# Component contribution in percentage
component_percentages <- (calculate_component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## GE GILD MSCI ROL SBUX
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.371 0.232 0.208 0.12 0.069
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 GE 0.371
## 2 GILD 0.232
## 3 MSCI 0.208
## 4 ROL 0.12
## 5 SBUX 0.069
# 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
## GE GILD MSCI ROL SBUX
## 2013-01-31 0.0596434695 0.0715761823 0.085019553 0.114753594 0.045383720
## 2013-02-28 0.0494738041 0.0796333126 -0.018244984 -0.004930403 -0.019139167
## 2013-03-28 -0.0043160583 0.1359277718 0.023860405 0.001630417 0.037571478
## 2013-04-30 -0.0365595549 0.0341468140 0.004998234 -0.009412558 0.066073766
## 2013-05-31 0.0451729121 0.0730920886 0.032884152 0.041609191 0.040474103
## 2013-06-28 0.0023646253 -0.0607280546 -0.057525749 0.025020926 0.036848171
## 2013-07-31 0.0496317164 0.1803038954 0.052119172 -0.015564336 0.084553393
## 2013-08-30 -0.0517902771 -0.0185751991 0.067832382 -0.026292243 -0.008016991
## 2013-09-30 0.0395693431 0.0422345326 0.070750476 0.068696674 0.087519463
## 2013-10-31 0.0900068538 0.1241428281 0.012588636 0.041741911 0.051650483
## 2013-11-29 0.0196978424 0.0497399210 0.085067484 0.024239357 0.008261097
## 2013-12-31 0.0581697127 0.0038685973 -0.015208484 0.073980686 -0.038415403
## 2014-01-31 -0.1092128883 0.0712986638 -0.023138459 -0.049747752 -0.097327871
## 2014-02-28 0.0221377815 0.0261883010 0.022909503 0.039278372 0.001524581
## 2014-03-31 0.0163557243 -0.1556011666 -0.015911617 0.012645663 0.033535185
## 2014-04-30 0.0378975739 0.1022648775 -0.059375983 -0.005304846 -0.038338449
## 2014-05-30 -0.0037258204 0.0340674060 0.062625053 0.022950306 0.040101583
## 2014-06-30 -0.0110056608 0.0207173593 0.060460927 -0.022087451 0.054986180
## 2014-07-31 -0.0439501645 0.0991293826 -0.013172351 -0.057982179 0.003869392
## 2014-08-29 0.0324689830 0.1611634765 0.019477363 0.053293173 0.005031593
## 2014-09-30 -0.0055436529 -0.0103734959 0.018893136 -0.015923861 -0.030667238
## 2014-10-31 0.0073886020 0.0508237365 -0.003642119 0.084760116 0.001324586
## 2014-11-28 0.0260051237 -0.1101337475 0.035785906 0.026878351 0.076232922
## 2014-12-31 -0.0377784992 -0.0623083897 -0.019207332 0.017370738 0.010290737
## 2015-01-30 -0.0561577872 0.1062832703 0.126179338 -0.001511763 0.064652869
## 2015-02-27 0.0934025708 -0.0124787131 0.044812543 0.018275821 0.069516032
## 2015-03-31 -0.0464653376 -0.0535680370 0.088629020 0.100743352 0.012859747
## 2015-04-30 0.0875488086 0.0239640244 -0.001959263 0.002826572 0.046020554
## 2015-05-29 0.0069916457 0.1106495138 0.016894323 0.004067306 0.050063331
## 2015-06-30 -0.0175345418 0.0455666235 -0.008090452 0.139306485 0.031448030
## 2015-07-31 -0.0178474920 0.0066398760 0.102008243 0.016339726 0.077313135
## 2015-08-31 -0.0502852437 -0.1148706607 -0.115438291 -0.035299092 -0.054435476
## 2015-09-30 0.0248968356 -0.0637924405 -0.017670049 -0.038332894 0.038193727
## 2015-10-30 0.1368959649 0.0964298901 0.119388769 -0.001862652 0.096045611
## 2015-11-30 0.0346622791 -0.0202725342 0.048830281 0.019055072 -0.015806325
## 2015-12-31 0.0470705283 -0.0417536859 0.028261889 -0.047133727 -0.022402672
## 2016-01-29 -0.0680761527 -0.1981592288 -0.046685545 0.061759707 0.012251897
## 2016-02-29 0.0093476044 0.0499368454 0.027324554 0.003097725 -0.039776560
## 2016-03-31 0.0870398771 0.0562959251 0.049249190 -0.015004840 0.025274931
## 2016-04-29 -0.0332618129 -0.0405454312 0.024798014 -0.009260986 -0.059881563
## 2016-05-31 -0.0170551288 -0.0131224241 0.052302459 0.059789140 -0.020626503
## 2016-06-30 0.0480647028 -0.0370970840 -0.034035849 0.029469825 0.039823107
## 2016-07-29 -0.0108592251 -0.0485087076 0.109449813 -0.037950764 0.016150536
## 2016-08-31 0.0032062610 -0.0138106455 0.049587400 0.014912350 -0.028377652
## 2016-09-30 -0.0454754170 0.0154359866 -0.071039652 0.027000303 -0.037877287
## 2016-10-31 -0.0177114958 -0.0719133678 -0.045703694 0.051259338 -0.019961501
## 2016-11-30 0.0554768919 0.0009502942 -0.014026316 0.048160184 0.092935705
## 2016-12-30 0.0344397566 -0.0223501562 -0.000253843 0.050078715 -0.043182489
## 2017-01-31 -0.0620102093 0.0116620362 0.049164876 0.042880288 -0.005418327
## 2017-02-28 0.0116414394 -0.0275675715 0.136712350 0.039408690 0.033938745
## 2017-03-31 -0.0003354117 -0.0293337362 0.027116056 0.015470306 0.026376691
## 2017-04-28 -0.0275576245 0.0092328682 0.031697307 0.044767979 0.028199504
## 2017-05-31 -0.0571379216 -0.0548696914 0.016814297 0.106512696 0.061537506
## 2017-06-30 -0.0052053131 0.0948967215 0.012309555 -0.056352910 -0.086997173
## 2017-07-31 -0.0532244859 0.0723401925 0.056257058 0.064215982 -0.077159935
## 2017-08-31 -0.0422709111 0.0954414008 0.054129641 0.025383746 0.020674906
## 2017-09-29 -0.0052437122 -0.0264288912 0.019783878 0.038215783 -0.021185294
## 2017-10-31 -0.1818257029 -0.0777411510 0.003927248 -0.049538494 0.020820779
## 2017-11-30 -0.0973457494 -0.0024041845 0.095301623 0.058843116 0.058198376
## 2017-12-29 -0.0401340881 -0.0360833366 -0.016925590 0.003876059 -0.006768071
## 2018-01-31 -0.0761821170 0.1567792323 0.095568467 0.058637636 -0.010854341
## 2018-02-28 -0.1280322567 -0.0624086947 0.018926442 0.021616751 0.010501052
## 2018-03-29 -0.0456764851 -0.0362961381 0.054654659 0.015005005 0.013740608
## 2018-04-30 0.0428375189 -0.0428193113 0.002405191 -0.050437819 -0.005543110
## 2018-05-31 0.0007107486 -0.0692104132 0.084010985 0.028245527 -0.010541193
## 2018-06-29 -0.0251139773 0.0578061409 0.017439253 0.054923364 -0.148490466
## 2018-07-31 0.0014682626 0.0941029434 0.004583596 0.043905962 0.069961317
## 2018-08-31 -0.0519498954 -0.0273522610 0.084614949 0.091927107 0.026968865
## 2018-09-28 -0.1269196782 0.0270903069 -0.015936799 0.010101932 0.061493684
## 2018-10-31 -0.1113818343 -0.1243777790 -0.165297849 -0.024857282 0.024847065
## 2018-11-30 -0.2976325264 0.0536812908 0.047575462 0.074106581 0.140772150
## 2018-12-31 0.0106648012 -0.1314658830 -0.063465415 -0.160226305 -0.035391098
## 2019-01-31 0.2942653654 0.1126716864 0.144032075 0.031090574 0.056450532
## 2019-02-28 0.0616060147 -0.0739432239 0.084754349 0.065755355 0.035843545
## 2019-03-29 -0.0382005630 0.0094743146 0.073659388 0.048237547 0.056446624
## 2019-04-30 0.0178578101 0.0004613478 0.125287366 -0.073516516 0.043948474
## 2019-05-31 -0.0744865082 -0.0438439845 -0.021485774 -0.025995469 -0.016446744
## 2019-06-28 0.1073815630 0.0913361678 0.081911487 -0.046304543 0.097268369
## 2019-07-31 -0.0047732618 -0.0306606423 -0.049577577 -0.067460640 0.121817720
## 2019-08-30 -0.2363888565 -0.0306857138 0.035114223 -0.018493176 0.023339237
## 2019-09-30 0.0814027556 0.0067757146 -0.074662171 0.037683723 -0.088066478
## 2019-10-31 0.1100475562 0.0051931566 0.074363608 0.112059663 -0.044636755
## 2019-11-29 0.1215613955 0.0539268220 0.102582303 -0.057090726 0.015222209
## 2019-12-31 -0.0089022333 -0.0248506024 -0.003904282 -0.077999143 0.028729961
## 2020-01-31 0.1093846417 -0.0277753410 0.101635429 0.134925045 -0.035778109
## 2020-02-28 -0.1347943885 0.0930062057 0.035223063 -0.010474105 -0.073792941
## 2020-03-31 -0.3140203861 0.0842419861 -0.022177716 -0.035339337 -0.176498736
## 2020-04-30 -0.1549906539 0.1165339265 0.123672071 0.101479300 0.154585135
## 2020-05-29 -0.0344088107 -0.0762900511 0.007669778 0.045936645 0.021923266
## 2020-06-30 0.0403089746 -0.0026385843 0.015000043 0.014016220 -0.058071607
## 2020-07-31 -0.1179659991 -0.1012675788 0.118933963 0.211994070 0.039173686
## 2020-08-31 0.0435201567 -0.0408040510 -0.005032764 0.052461526 0.104138694
## 2020-09-30 -0.0158510739 -0.0443851012 -0.045182656 -0.017379102 0.017020248
## 2020-10-30 0.1748028436 -0.0831202722 -0.019643446 0.065357204 0.012031481
## 2020-11-30 0.3162458991 0.0424235461 0.159232328 -0.008318783 0.124427828
## 2020-12-31 0.0600407018 -0.0292790330 0.086764843 0.024615752 0.087470278
## 2021-01-29 -0.0111733753 0.1186601011 -0.121861645 -0.081280509 -0.099891681
## 2021-02-26 0.1605506660 -0.0661658716 0.049253390 -0.080277458 0.113907173
## 2021-03-31 0.0467135429 0.0625863030 0.011417738 0.036991882 0.011412812
## 2021-04-30 -0.0007620029 -0.0181112502 0.147196287 0.079819260 0.046665566
## 2021-05-28 0.0691961405 0.0407526252 -0.035292818 -0.087286688 -0.001371345
## 2021-06-30 -0.0428508771 0.0511331912 0.129925434 0.003221519 -0.018344234
## 2021-07-30 -0.0386265986 -0.0083120882 0.111502058 0.114007485 0.082536982
## 2021-08-31 0.0173203349 0.0636778342 0.064455529 0.017373732 -0.029110901
## 2021-09-30 -0.0220600964 -0.0310619609 -0.042229579 -0.096775667 -0.063058697
## 2021-10-29 0.0177012777 -0.0738107841 0.088872695 -0.002834499 -0.039195313
## 2021-11-30 -0.0989498363 0.0605522102 -0.053165921 -0.051893652 0.037405586
## 2021-12-31 -0.0046140982 0.0621172485 -0.026987214 0.027561368 0.064715152
## 2022-01-31 0.0001058190 -0.0556444703 -0.133501159 -0.103381227 -0.173689974
## 2022-02-28 0.0108428512 -0.1284689962 -0.064460490 0.059389116 -0.063666653
## 2022-03-31 -0.0419942411 -0.0031874014 0.002369132 0.071543438 -0.008973476
## 2022-04-29 -0.2048687715 -0.0018520226 -0.177125145 -0.044036712 -0.197853254
## 2022-05-31 0.0489497466 0.0887932933 0.051563270 0.058609314 0.057440723
## 2022-06-30 -0.2055131015 -0.0355933891 -0.070714140 -0.015345551 -0.027246970
## 2022-07-29 0.1491347717 -0.0338960371 0.155186530 0.099415121 0.104207927
## 2022-08-31 -0.0063794377 0.0603982012 -0.066572838 -0.130547808 -0.002728313
## 2022-09-30 -0.1695469356 -0.0171686626 -0.063049665 0.026886618 0.002257528
## 2022-10-31 0.2285882144 0.2404671010 0.105796643 0.193409473 0.027276930
## 2022-11-30 0.0997284779 0.1128142227 0.082517618 -0.036653271 0.171478081
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,.2,.1))
## # A tibble: 1 × 5
## GE GILD MSCI ROL SBUX
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.371 0.232 0.208 0.12 0.069
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25,.25,.2,.2,.1)) %>%
#transform to long
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
#add weight
add_column(weight = c(.25,.25,.2,.2,.1)) %>%
#transform to long
pivot_longer(cols = c(Contribution, weight), names_to = "type", values_to = "value")
plot_data %>%
ggplot() +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + scale_fill_tq() +
theme(plot.title = element_text(hjust = .5)) +
theme_tq() +
labs(title = "percent contribution to the 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 competitor to risk is definitely in GE. Given the slightly higher concentration of the asset in my portfolio it brings a large portion of the portfolio risk.