# 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("NVDA", "MSFT", "TSLA", "AMD")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2024-06-24")
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
## AMD MSFT NVDA TSLA
## 2013-01-31 0.080042631 0.027328140 0.000000000 0.102078114
## 2013-02-28 -0.043228694 0.020914748 0.038221638 -0.074128640
## 2013-03-28 0.023810626 0.028720544 0.013338473 0.084208138
## 2013-04-30 0.100643521 0.145776685 0.070706518 0.354111531
## 2013-05-31 0.349557500 0.059941424 0.054651647 0.593716684
## 2013-06-28 0.019802609 -0.010368475 -0.030166672 0.093672163
## 2013-07-31 -0.079021973 -0.081394714 0.028091702 0.223739522
## 2013-08-30 -0.142285017 0.054854545 0.026269924 0.229971642
## 2013-09-30 0.152839195 -0.003599743 0.053460626 0.134706620
## 2013-10-31 -0.131658393 0.062037930 -0.024066347 -0.189806595
## 2013-11-29 0.086012929 0.081562332 0.032034957 -0.228409405
## 2013-12-31 0.061270767 -0.019063465 0.026566803 0.167108541
## 2014-01-31 -0.120694197 0.011429005 -0.020176837 0.187261714
## 2014-02-28 0.078471606 0.019814700 0.162107429 0.299722785
## 2014-03-31 0.077759411 0.067617272 -0.025904067 -0.160783242
## 2014-04-30 0.019753709 -0.014498323 0.030788240 -0.002690122
## 2014-05-30 -0.022250646 0.020307610 0.032886765 -0.000577422
## 2014-06-30 0.046406386 0.018393254 -0.024508429 0.144457224
## 2014-07-31 -0.069163352 0.034413273 -0.057729831 -0.072372676
## 2014-08-29 0.064378658 0.057485178 0.110060177 0.188794007
## 2014-09-30 -0.201203738 0.020264417 -0.052782660 -0.105566477
## 2014-10-31 -0.197092916 0.012645820 0.057399186 -0.004046477
## 2014-11-28 -0.003577818 0.024439082 0.074852373 0.011599801
## 2014-12-31 -0.043963081 -0.028858042 -0.044863790 -0.094774519
## 2015-01-30 -0.038172628 -0.139546984 -0.043318911 -0.088365289
## 2015-02-27 0.190716820 0.089036290 0.142699056 -0.001277808
## 2015-03-31 -0.148805873 -0.075529835 -0.052582251 -0.074350051
## 2015-04-30 -0.170452010 0.179201546 0.058908748 0.180226808
## 2015-05-29 0.008810621 -0.030803793 0.001459857 0.103899574
## 2015-06-30 0.051293347 -0.059571313 -0.095717138 0.067300935
## 2015-07-31 -0.217948801 0.056150602 -0.007988106 -0.007896616
## 2015-08-31 -0.064193162 -0.063950746 0.123595437 -0.066366250
## 2015-09-30 -0.051002506 0.016860711 0.092150870 -0.002653519
## 2015-10-30 0.209091727 0.173394894 0.140555674 -0.182659777
## 2015-11-30 0.107245540 0.038686179 0.115404956 0.106828586
## 2015-12-31 0.195650415 0.020577982 0.038347215 0.041471519
## 2016-01-29 -0.265854608 -0.007054322 -0.118048303 -0.227360626
## 2016-02-29 -0.027651504 -0.072344108 0.071923572 0.003810669
## 2016-03-31 0.286513083 0.082036541 0.127654846 0.179948109
## 2016-04-29 0.219628629 -0.102086925 -0.002810731 0.046721797
## 2016-05-31 0.252565652 0.067842329 0.276388533 -0.075597968
## 2016-06-30 0.117539811 -0.035138342 0.006187785 -0.050296440
## 2016-07-29 0.288654408 0.102267934 0.194444076 0.100785334
## 2016-08-31 0.075772552 0.019880851 0.073468916 -0.102058091
## 2016-09-30 -0.068510397 0.002433661 0.110693810 -0.038366372
## 2016-10-31 0.045269423 0.039487601 0.037805045 -0.031364583
## 2016-11-30 0.208935186 0.012390994 0.260525340 -0.043041267
## 2016-12-30 0.241162087 0.030721627 0.146435639 0.120665178
## 2017-01-31 -0.089419301 0.039598006 0.022602211 0.164624916
## 2017-02-28 0.332469208 -0.004373182 -0.071875087 -0.007730364
## 2017-03-31 0.006204787 0.028960656 0.070843835 0.107278727
## 2017-04-28 -0.089826957 0.038718291 -0.043434290 0.120916212
## 2017-05-31 -0.172743565 0.025672836 0.326022326 0.082295892
## 2017-06-30 0.109106841 -0.013115062 0.001453729 0.058654468
## 2017-07-31 0.086677465 0.053249786 0.117044802 -0.111459860
## 2017-08-31 -0.045855434 0.033388955 0.042639710 0.095543446
## 2017-09-29 -0.019418086 -0.003751961 0.053600823 -0.042474144
## 2017-10-31 -0.148545524 0.110342156 0.145700326 -0.028457409
## 2017-11-30 -0.009140779 0.016841111 -0.029244711 -0.070862541
## 2017-12-29 -0.057644734 0.016145579 -0.036583514 0.008061928
## 2018-01-31 0.290111036 0.104998052 0.239240354 0.129254571
## 2018-02-28 -0.126279741 -0.008450808 -0.014958848 -0.032266877
## 2018-03-29 -0.186458876 -0.027022804 -0.043969366 -0.253920408
## 2018-04-30 0.079353598 0.024353113 -0.029312649 0.099254576
## 2018-05-31 0.232656934 0.059652267 0.115145163 -0.031698139
## 2018-06-29 0.087800110 -0.002329768 -0.062544768 0.186043257
## 2018-07-31 0.201155761 0.073020915 0.033048653 -0.140021491
## 2018-08-31 0.317113754 0.061088402 0.137075494 0.011737389
## 2018-09-28 0.204779675 0.017997510 0.001210380 -0.130439038
## 2018-10-31 -0.528461644 -0.068387097 -0.287373431 0.242170576
## 2018-11-30 0.156736193 0.041798001 -0.253667113 0.038271580
## 2018-12-31 -0.143100857 -0.087790625 -0.202283670 -0.051761952
## 2019-01-31 0.279386699 0.027768869 0.073974250 -0.080628789
## 2019-02-28 -0.036716646 0.074511259 0.071593897 0.041032981
## 2019-03-29 0.081186244 0.051409264 0.151869779 -0.133656413
## 2019-04-30 0.079439632 0.101963388 0.007987876 -0.159123803
## 2019-05-31 -0.007994204 -0.050746912 -0.288679947 -0.253945372
## 2019-06-28 0.102547402 0.079843677 0.192591631 0.188012109
## 2019-07-31 0.002630713 0.017096995 0.026972510 0.078092373
## 2019-08-30 0.032312988 0.014924704 -0.006208304 -0.068516948
## 2019-09-30 -0.081448072 0.008451044 0.038414536 0.065449565
## 2019-10-31 0.157348653 0.030739153 0.143946986 0.268061253
## 2019-11-29 0.143100874 0.057761450 0.076031728 0.046592176
## 2019-12-31 0.158192829 0.040901384 0.082162827 0.237359743
## 2020-01-31 0.024554311 0.076455956 0.004790859 0.441578342
## 2020-02-28 -0.032874943 -0.046764912 0.133626936 0.026424253
## 2020-03-31 0.000000000 -0.026900207 -0.024248349 -0.242781458
## 2020-04-30 0.141443063 0.127800617 0.103279464 0.400209535
## 2020-05-29 0.026557731 0.025074131 0.194461918 0.065730500
## 2020-06-30 -0.022367226 0.104863934 0.068216413 0.257108657
## 2020-07-31 0.386468079 0.007343402 0.111189501 0.281420674
## 2020-08-31 0.159505216 0.097808886 0.231105475 0.554719320
## 2020-09-30 -0.102282260 -0.069775287 0.011895549 -0.149762306
## 2020-10-30 -0.085249927 -0.038086127 -0.076501232 -0.100371771
## 2020-11-30 0.207589583 0.058326085 0.066921537 0.380308519
## 2020-12-31 -0.010305505 0.038264400 -0.025900028 0.217730753
## 2021-01-29 -0.068478958 0.041997470 -0.005010623 0.117343701
## 2021-02-26 -0.013282561 0.004109382 0.054293175 -0.161038203
## 2021-03-31 -0.073771271 0.014482934 -0.026723395 -0.011269836
## 2021-04-30 0.038975739 0.067286239 0.117298031 0.060292565
## 2021-05-28 -0.019048206 -0.007656595 0.079071150 -0.126372343
## 2021-06-30 0.159523669 0.081569607 0.208332116 0.083547955
## 2021-07-30 0.122680138 0.050423677 -0.025494210 0.010973846
## 2021-08-31 0.041774537 0.059768825 0.138204256 0.068224268
## 2021-09-30 -0.073246845 -0.068406124 -0.077484855 0.052632612
## 2021-10-29 0.155648945 0.162366311 0.210396084 0.362230202
## 2021-11-30 0.275527434 -0.001283055 0.245338542 0.027237848
## 2021-12-31 -0.095815465 0.017184243 -0.105149811 -0.079968440
## 2022-01-31 -0.230729542 -0.078334353 -0.183267225 -0.120597475
## 2022-02-28 0.076555710 -0.037922248 -0.004133154 -0.073397011
## 2022-03-31 -0.120482480 0.031364756 0.112575749 0.213504290
## 2022-04-29 -0.245712033 -0.105212625 -0.386065597 -0.213125289
## 2022-05-31 0.174849100 -0.018242815 0.006716877 -0.138340062
## 2022-06-30 -0.286700804 -0.056909474 -0.208219114 -0.118657126
## 2022-07-29 0.211383814 0.089014539 0.180792144 0.280480149
## 2022-08-31 -0.107161630 -0.068989111 -0.185089196 -0.075250271
## 2022-09-30 -0.292287949 -0.115710273 -0.217576827 -0.038313992
## 2022-10-31 -0.053488672 -0.003311622 0.106044023 -0.153346760
## 2022-11-30 0.256609830 0.097329043 0.226461913 -0.155866121
## 2022-12-30 -0.181111434 -0.061923720 -0.146693749 -0.457813194
## 2023-01-31 0.148643655 0.032773642 0.290330387 0.340915767
## 2023-02-28 0.044631101 0.008977423 0.172531382 0.171904973
## 2023-03-31 0.220952299 0.144863386 0.179536580 0.008471140
## 2023-04-28 -0.092284450 0.063692768 -0.001008526 -0.233183710
## 2023-05-31 0.279677610 0.068691305 0.310008254 0.216021889
## 2023-06-30 -0.037054001 0.036330672 0.111729704 0.249689452
## 2023-07-31 0.004292397 -0.013659548 0.099530583 0.021391609
## 2023-08-31 -0.078906991 -0.022476429 0.054674115 -0.035588260
## 2023-09-29 -0.027814218 -0.037330893 -0.126218698 -0.030929027
## 2023-10-31 -0.042923335 0.068420561 -0.064546193 -0.219831983
## 2023-11-30 0.207055468 0.115954969 0.137050216 0.178463656
## 2023-12-29 0.196105829 -0.007603225 0.057262968 0.034390133
## 2024-01-31 0.128899206 0.055700698 0.217059049 -0.282704160
## 2024-02-29 0.138134928 0.041447426 0.251388505 0.075015304
## 2024-03-28 -0.064576574 0.016971445 0.132939772 -0.138383423
## 2024-04-30 -0.130678165 -0.077540252 -0.044746588 0.041724969
## 2024-05-31 0.052397554 0.065966581 0.238127628 -0.028782134
## 2024-06-21 -0.034562904 0.080166836 0.143739179 0.027307868
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMD MSFT NVDA TSLA
## AMD 0.023742713 0.004015212 0.01056383 0.007378926
## MSFT 0.004015212 0.003544100 0.00416640 0.003217386
## NVDA 0.010563828 0.004166400 0.01538079 0.006814540
## TSLA 0.007378926 0.003217386 0.00681454 0.029197448
# 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(.30, .30, .15, .15)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.08226545
# 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
## AMD MSFT NVDA TSLA
## [1,] 0.04018257 0.01230904 0.01412811 0.01564574
rowSums(component_contribution)
## [1] 0.08226545
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 4
## AMD MSFT NVDA TSLA
## <dbl> <dbl> <dbl> <dbl>
## 1 0.488 0.15 0.172 0.19
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
## asset contribution
## <chr> <dbl>
## 1 AMD 0.488
## 2 MSFT 0.15
## 3 NVDA 0.172
## 4 TSLA 0.19
# 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
## AMD MSFT NVDA TSLA
## 2013-01-31 0.080042631 0.027328140 0.000000000 0.102078114
## 2013-02-28 -0.043228694 0.020914748 0.038221638 -0.074128640
## 2013-03-28 0.023810626 0.028720544 0.013338473 0.084208138
## 2013-04-30 0.100643521 0.145776685 0.070706518 0.354111531
## 2013-05-31 0.349557500 0.059941424 0.054651647 0.593716684
## 2013-06-28 0.019802609 -0.010368475 -0.030166672 0.093672163
## 2013-07-31 -0.079021973 -0.081394714 0.028091702 0.223739522
## 2013-08-30 -0.142285017 0.054854545 0.026269924 0.229971642
## 2013-09-30 0.152839195 -0.003599743 0.053460626 0.134706620
## 2013-10-31 -0.131658393 0.062037930 -0.024066347 -0.189806595
## 2013-11-29 0.086012929 0.081562332 0.032034957 -0.228409405
## 2013-12-31 0.061270767 -0.019063465 0.026566803 0.167108541
## 2014-01-31 -0.120694197 0.011429005 -0.020176837 0.187261714
## 2014-02-28 0.078471606 0.019814700 0.162107429 0.299722785
## 2014-03-31 0.077759411 0.067617272 -0.025904067 -0.160783242
## 2014-04-30 0.019753709 -0.014498323 0.030788240 -0.002690122
## 2014-05-30 -0.022250646 0.020307610 0.032886765 -0.000577422
## 2014-06-30 0.046406386 0.018393254 -0.024508429 0.144457224
## 2014-07-31 -0.069163352 0.034413273 -0.057729831 -0.072372676
## 2014-08-29 0.064378658 0.057485178 0.110060177 0.188794007
## 2014-09-30 -0.201203738 0.020264417 -0.052782660 -0.105566477
## 2014-10-31 -0.197092916 0.012645820 0.057399186 -0.004046477
## 2014-11-28 -0.003577818 0.024439082 0.074852373 0.011599801
## 2014-12-31 -0.043963081 -0.028858042 -0.044863790 -0.094774519
## 2015-01-30 -0.038172628 -0.139546984 -0.043318911 -0.088365289
## 2015-02-27 0.190716820 0.089036290 0.142699056 -0.001277808
## 2015-03-31 -0.148805873 -0.075529835 -0.052582251 -0.074350051
## 2015-04-30 -0.170452010 0.179201546 0.058908748 0.180226808
## 2015-05-29 0.008810621 -0.030803793 0.001459857 0.103899574
## 2015-06-30 0.051293347 -0.059571313 -0.095717138 0.067300935
## 2015-07-31 -0.217948801 0.056150602 -0.007988106 -0.007896616
## 2015-08-31 -0.064193162 -0.063950746 0.123595437 -0.066366250
## 2015-09-30 -0.051002506 0.016860711 0.092150870 -0.002653519
## 2015-10-30 0.209091727 0.173394894 0.140555674 -0.182659777
## 2015-11-30 0.107245540 0.038686179 0.115404956 0.106828586
## 2015-12-31 0.195650415 0.020577982 0.038347215 0.041471519
## 2016-01-29 -0.265854608 -0.007054322 -0.118048303 -0.227360626
## 2016-02-29 -0.027651504 -0.072344108 0.071923572 0.003810669
## 2016-03-31 0.286513083 0.082036541 0.127654846 0.179948109
## 2016-04-29 0.219628629 -0.102086925 -0.002810731 0.046721797
## 2016-05-31 0.252565652 0.067842329 0.276388533 -0.075597968
## 2016-06-30 0.117539811 -0.035138342 0.006187785 -0.050296440
## 2016-07-29 0.288654408 0.102267934 0.194444076 0.100785334
## 2016-08-31 0.075772552 0.019880851 0.073468916 -0.102058091
## 2016-09-30 -0.068510397 0.002433661 0.110693810 -0.038366372
## 2016-10-31 0.045269423 0.039487601 0.037805045 -0.031364583
## 2016-11-30 0.208935186 0.012390994 0.260525340 -0.043041267
## 2016-12-30 0.241162087 0.030721627 0.146435639 0.120665178
## 2017-01-31 -0.089419301 0.039598006 0.022602211 0.164624916
## 2017-02-28 0.332469208 -0.004373182 -0.071875087 -0.007730364
## 2017-03-31 0.006204787 0.028960656 0.070843835 0.107278727
## 2017-04-28 -0.089826957 0.038718291 -0.043434290 0.120916212
## 2017-05-31 -0.172743565 0.025672836 0.326022326 0.082295892
## 2017-06-30 0.109106841 -0.013115062 0.001453729 0.058654468
## 2017-07-31 0.086677465 0.053249786 0.117044802 -0.111459860
## 2017-08-31 -0.045855434 0.033388955 0.042639710 0.095543446
## 2017-09-29 -0.019418086 -0.003751961 0.053600823 -0.042474144
## 2017-10-31 -0.148545524 0.110342156 0.145700326 -0.028457409
## 2017-11-30 -0.009140779 0.016841111 -0.029244711 -0.070862541
## 2017-12-29 -0.057644734 0.016145579 -0.036583514 0.008061928
## 2018-01-31 0.290111036 0.104998052 0.239240354 0.129254571
## 2018-02-28 -0.126279741 -0.008450808 -0.014958848 -0.032266877
## 2018-03-29 -0.186458876 -0.027022804 -0.043969366 -0.253920408
## 2018-04-30 0.079353598 0.024353113 -0.029312649 0.099254576
## 2018-05-31 0.232656934 0.059652267 0.115145163 -0.031698139
## 2018-06-29 0.087800110 -0.002329768 -0.062544768 0.186043257
## 2018-07-31 0.201155761 0.073020915 0.033048653 -0.140021491
## 2018-08-31 0.317113754 0.061088402 0.137075494 0.011737389
## 2018-09-28 0.204779675 0.017997510 0.001210380 -0.130439038
## 2018-10-31 -0.528461644 -0.068387097 -0.287373431 0.242170576
## 2018-11-30 0.156736193 0.041798001 -0.253667113 0.038271580
## 2018-12-31 -0.143100857 -0.087790625 -0.202283670 -0.051761952
## 2019-01-31 0.279386699 0.027768869 0.073974250 -0.080628789
## 2019-02-28 -0.036716646 0.074511259 0.071593897 0.041032981
## 2019-03-29 0.081186244 0.051409264 0.151869779 -0.133656413
## 2019-04-30 0.079439632 0.101963388 0.007987876 -0.159123803
## 2019-05-31 -0.007994204 -0.050746912 -0.288679947 -0.253945372
## 2019-06-28 0.102547402 0.079843677 0.192591631 0.188012109
## 2019-07-31 0.002630713 0.017096995 0.026972510 0.078092373
## 2019-08-30 0.032312988 0.014924704 -0.006208304 -0.068516948
## 2019-09-30 -0.081448072 0.008451044 0.038414536 0.065449565
## 2019-10-31 0.157348653 0.030739153 0.143946986 0.268061253
## 2019-11-29 0.143100874 0.057761450 0.076031728 0.046592176
## 2019-12-31 0.158192829 0.040901384 0.082162827 0.237359743
## 2020-01-31 0.024554311 0.076455956 0.004790859 0.441578342
## 2020-02-28 -0.032874943 -0.046764912 0.133626936 0.026424253
## 2020-03-31 0.000000000 -0.026900207 -0.024248349 -0.242781458
## 2020-04-30 0.141443063 0.127800617 0.103279464 0.400209535
## 2020-05-29 0.026557731 0.025074131 0.194461918 0.065730500
## 2020-06-30 -0.022367226 0.104863934 0.068216413 0.257108657
## 2020-07-31 0.386468079 0.007343402 0.111189501 0.281420674
## 2020-08-31 0.159505216 0.097808886 0.231105475 0.554719320
## 2020-09-30 -0.102282260 -0.069775287 0.011895549 -0.149762306
## 2020-10-30 -0.085249927 -0.038086127 -0.076501232 -0.100371771
## 2020-11-30 0.207589583 0.058326085 0.066921537 0.380308519
## 2020-12-31 -0.010305505 0.038264400 -0.025900028 0.217730753
## 2021-01-29 -0.068478958 0.041997470 -0.005010623 0.117343701
## 2021-02-26 -0.013282561 0.004109382 0.054293175 -0.161038203
## 2021-03-31 -0.073771271 0.014482934 -0.026723395 -0.011269836
## 2021-04-30 0.038975739 0.067286239 0.117298031 0.060292565
## 2021-05-28 -0.019048206 -0.007656595 0.079071150 -0.126372343
## 2021-06-30 0.159523669 0.081569607 0.208332116 0.083547955
## 2021-07-30 0.122680138 0.050423677 -0.025494210 0.010973846
## 2021-08-31 0.041774537 0.059768825 0.138204256 0.068224268
## 2021-09-30 -0.073246845 -0.068406124 -0.077484855 0.052632612
## 2021-10-29 0.155648945 0.162366311 0.210396084 0.362230202
## 2021-11-30 0.275527434 -0.001283055 0.245338542 0.027237848
## 2021-12-31 -0.095815465 0.017184243 -0.105149811 -0.079968440
## 2022-01-31 -0.230729542 -0.078334353 -0.183267225 -0.120597475
## 2022-02-28 0.076555710 -0.037922248 -0.004133154 -0.073397011
## 2022-03-31 -0.120482480 0.031364756 0.112575749 0.213504290
## 2022-04-29 -0.245712033 -0.105212625 -0.386065597 -0.213125289
## 2022-05-31 0.174849100 -0.018242815 0.006716877 -0.138340062
## 2022-06-30 -0.286700804 -0.056909474 -0.208219114 -0.118657126
## 2022-07-29 0.211383814 0.089014539 0.180792144 0.280480149
## 2022-08-31 -0.107161630 -0.068989111 -0.185089196 -0.075250271
## 2022-09-30 -0.292287949 -0.115710273 -0.217576827 -0.038313992
## 2022-10-31 -0.053488672 -0.003311622 0.106044023 -0.153346760
## 2022-11-30 0.256609830 0.097329043 0.226461913 -0.155866121
## 2022-12-30 -0.181111434 -0.061923720 -0.146693749 -0.457813194
## 2023-01-31 0.148643655 0.032773642 0.290330387 0.340915767
## 2023-02-28 0.044631101 0.008977423 0.172531382 0.171904973
## 2023-03-31 0.220952299 0.144863386 0.179536580 0.008471140
## 2023-04-28 -0.092284450 0.063692768 -0.001008526 -0.233183710
## 2023-05-31 0.279677610 0.068691305 0.310008254 0.216021889
## 2023-06-30 -0.037054001 0.036330672 0.111729704 0.249689452
## 2023-07-31 0.004292397 -0.013659548 0.099530583 0.021391609
## 2023-08-31 -0.078906991 -0.022476429 0.054674115 -0.035588260
## 2023-09-29 -0.027814218 -0.037330893 -0.126218698 -0.030929027
## 2023-10-31 -0.042923335 0.068420561 -0.064546193 -0.219831983
## 2023-11-30 0.207055468 0.115954969 0.137050216 0.178463656
## 2023-12-29 0.196105829 -0.007603225 0.057262968 0.034390133
## 2024-01-31 0.128899206 0.055700698 0.217059049 -0.282704160
## 2024-02-29 0.138134928 0.041447426 0.251388505 0.075015304
## 2024-03-28 -0.064576574 0.016971445 0.132939772 -0.138383423
## 2024-04-30 -0.130678165 -0.077540252 -0.044746588 0.041724969
## 2024-05-31 0.052397554 0.065966581 0.238127628 -0.028782134
## 2024-06-21 -0.034562904 0.080166836 0.143739179 0.027307868
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
# 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(.30, .30, .15, .15))
## # A tibble: 1 × 4
## AMD MSFT NVDA TSLA
## <dbl> <dbl> <dbl> <dbl>
## 1 0.488 0.15 0.172 0.19
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.30, .30, .15, .15)) %>%
# 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 = "Precent Contribution to Portfolio Volatility")
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.30, .30, .15, .15)) %>%
# transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weight
add_column(weight = c(.30, .30, .15, .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 = "Precent Contribution to Portfolio Volatility and Weight",
x = "Precent",
y = 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 asset in my portfolio that is the largest contributor to portfolio volatility is AMD, as represented by the highest bar in the “Contribution” category.
Given that AMD contributes substantially more to my portfolio’s volatility than its weight, it might appear that my portfolio risk is concentrated in one single asset. This concentration might pose a danger because AMD’s performance has a significant impact on the portfolio’s overall volatility. I think It may be useful to diversify my portfolio in order to more fairly distribute risk across assets.