# 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", "MSFT", "GOOG")
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_wide_tbl <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns) %>%
column_to_rownames(var = "date")
asset_returns_wide_tbl
## GOOG MSFT TSLA
## 2013-01-31 0.0660632586 0.027328093 0.102078114
## 2013-02-28 0.0584794367 0.020915055 -0.074128640
## 2013-03-28 -0.0087878655 0.028720567 0.084208138
## 2013-04-30 0.0375393189 0.145776623 0.354111531
## 2013-05-31 0.0550324684 0.059941393 0.593716684
## 2013-06-28 0.0104477177 -0.010368793 0.093672163
## 2013-07-31 0.0083478266 -0.081394646 0.223739522
## 2013-08-30 -0.0471073807 0.054854433 0.229971642
## 2013-09-30 0.0336805508 -0.003599417 0.134706620
## 2013-10-31 0.1626137511 0.062037859 -0.189806595
## 2013-11-29 0.0277603690 0.081562181 -0.228409405
## 2013-12-31 0.0560803510 -0.019063273 0.167108541
## 2014-01-31 0.0523736758 0.011428782 0.187261714
## 2014-02-28 0.0289427889 0.019814720 0.299722785
## 2014-03-31 -0.0863761431 0.067617548 -0.160783242
## 2014-04-30 -0.0559562158 -0.014498345 -0.002690122
## 2014-05-30 0.0611852104 0.020307324 -0.000577422
## 2014-06-30 0.0271165183 0.018393808 0.144457224
## 2014-07-31 -0.0064174879 0.034412757 -0.072372676
## 2014-08-29 0.0000000000 0.057485016 0.188794007
## 2014-09-30 0.0100266345 0.020264587 -0.105566477
## 2014-10-31 -0.0321734491 0.012646330 -0.004046477
## 2014-11-28 -0.0313403105 0.024438447 0.011599801
## 2014-12-31 -0.0288908834 -0.028858133 -0.094774519
## 2015-01-30 0.0153077624 -0.139546653 -0.088365289
## 2015-02-27 0.0437064372 0.089036431 -0.001277808
## 2015-03-31 -0.0188002471 -0.075530157 -0.074350051
## 2015-04-30 -0.0169025209 0.179201597 0.180226808
## 2015-05-29 -0.0097808463 -0.030803993 0.103899574
## 2015-06-30 -0.0220412022 -0.059571111 0.067300935
## 2015-07-31 0.1839181544 0.056150685 -0.007896616
## 2015-08-31 -0.0118342473 -0.063950866 -0.066366250
## 2015-09-30 -0.0160275192 0.016860611 -0.002653519
## 2015-10-30 0.1555397941 0.173395437 -0.182659777
## 2015-11-30 0.0437523642 0.038685873 0.106828586
## 2015-12-31 0.0216860731 0.020578207 0.041471519
## 2016-01-29 -0.0212148962 -0.007054545 -0.227360626
## 2016-02-29 -0.0627392605 -0.072343987 0.003810669
## 2016-03-31 0.0654275963 0.082036038 0.179948109
## 2016-04-29 -0.0722726559 -0.102086403 0.046721797
## 2016-05-31 0.0598051735 0.067842353 -0.075597968
## 2016-06-30 -0.0611191121 -0.035138677 -0.050296440
## 2016-07-29 0.1050873292 0.102268307 0.100785334
## 2016-08-31 -0.0022657966 0.019880998 -0.102058091
## 2016-09-30 0.0132615302 0.002433342 -0.038366372
## 2016-10-31 0.0092841056 0.039487724 -0.031364583
## 2016-11-30 -0.0343615135 0.012390811 -0.043041267
## 2016-12-30 0.0180152337 0.030721402 0.120665178
## 2017-01-31 0.0318397939 0.039598403 0.164624916
## 2017-02-28 0.0326200945 -0.004373314 -0.007730364
## 2017-03-31 0.0076842168 0.028960689 0.107278727
## 2017-04-28 0.0880997230 0.038718371 0.120916212
## 2017-05-31 0.0629877923 0.025672744 0.082295892
## 2017-06-30 -0.0599350111 -0.013115426 0.058654468
## 2017-07-31 0.0236741629 0.053250016 -0.111459860
## 2017-08-31 0.0094446934 0.033388937 0.095543446
## 2017-09-29 0.0208389738 -0.003751751 -0.042474144
## 2017-10-31 0.0582525835 0.110342088 -0.028457409
## 2017-11-30 0.0046809400 0.016841042 -0.070862541
## 2017-12-29 0.0241716390 0.016145529 0.008061928
## 2018-01-31 0.1115967900 0.104998062 0.129254571
## 2018-02-28 -0.0573515298 -0.008450753 -0.032266877
## 2018-03-29 -0.0683057993 -0.027022705 -0.253920408
## 2018-04-30 -0.0141136248 0.024353094 0.099254576
## 2018-05-31 0.0643892525 0.059652156 -0.031698139
## 2018-06-29 0.0278664749 -0.002329512 0.186043257
## 2018-07-31 0.0871651755 0.073020762 -0.140021491
## 2018-08-31 0.0007637297 0.061088317 0.011737389
## 2018-09-28 -0.0205011698 0.017997738 -0.130439038
## 2018-10-31 -0.1028991445 -0.068387048 0.242170576
## 2018-11-30 0.0162678540 0.041797762 0.038271580
## 2018-12-31 -0.0552431428 -0.087790347 -0.051761952
## 2019-01-31 0.0750917804 0.027768485 -0.080628789
## 2019-02-28 0.0031748651 0.074511445 0.041032981
## 2019-03-29 0.0465716243 0.051409279 -0.133656413
## 2019-04-30 0.0128463578 0.101963428 -0.159123803
## 2019-05-31 -0.0740704060 -0.050747036 -0.253945372
## 2019-06-28 -0.0208014640 0.079843797 0.188012109
## 2019-07-31 0.1183224942 0.017097083 0.078092373
## 2019-08-30 -0.0237704414 0.014924749 -0.068516948
## 2019-09-30 0.0256755038 0.008451043 0.065449565
## 2019-10-31 0.0331681100 0.030738663 0.268061253
## 2019-11-29 0.0349734427 0.057761764 0.046592176
## 2019-12-31 0.0242707879 0.040901229 0.237359743
## 2020-01-31 0.0701848929 0.076456010 0.441578342
## 2020-02-28 -0.0684586298 -0.046764570 0.026424253
## 2020-03-31 -0.1413300093 -0.026900010 -0.242781458
## 2020-04-30 0.1482721074 0.127800083 0.400209535
## 2020-05-29 0.0578073650 0.025074352 0.065730500
## 2020-06-30 -0.0107722179 0.104863704 0.257108657
## 2020-07-31 0.0478934005 0.007343460 0.281420674
## 2020-08-31 0.0971010982 0.097808951 0.554719320
## 2020-09-30 -0.1061508709 -0.069775542 -0.149762306
## 2020-10-30 0.0980590834 -0.038086049 -0.100371771
## 2020-11-30 0.0826847480 0.058326136 0.380308519
## 2020-12-31 -0.0050446216 0.038264353 0.217730753
## 2021-01-29 0.0467581267 0.041997505 0.117343701
## 2021-02-26 0.1039617314 0.004109536 -0.161038203
## 2021-03-31 0.0154771604 0.014482782 -0.011269836
## 2021-04-30 0.1527899045 0.067286290 0.060292565
## 2021-05-28 0.0005974250 -0.007656568 -0.126372343
## 2021-06-30 0.0385416519 0.081569834 0.083547955
## 2021-07-30 0.0760718346 0.050423349 0.010973846
## 2021-08-31 0.0730045198 0.059769083 0.068224268
## 2021-09-30 -0.0875715395 -0.068406252 0.052632612
## 2021-10-29 0.1066948649 0.162366269 0.362230202
## 2021-11-30 -0.0400332042 -0.001283101 0.027237848
## 2021-12-31 0.0155159766 0.017184293 -0.079968440
## 2022-01-31 -0.0640854435 -0.078334221 -0.120597475
## 2022-02-28 -0.0059683911 -0.037922506 -0.073397011
## 2022-03-31 0.0346685444 0.031364889 0.213504290
## 2022-04-29 -0.1944949638 -0.105212707 -0.213125289
## 2022-05-31 -0.0081002558 -0.018242586 -0.138340062
## 2022-06-30 -0.0417810722 -0.056909631 -0.118657126
## 2022-07-29 0.0643327736 0.089014570 0.280480149
## 2022-08-31 -0.0663691728 -0.068988906 -0.075250271
## 2022-09-30 -0.1268135622 -0.115710548 -0.038313992
## 2022-10-31 -0.0156179604 -0.003311516 -0.153346760
## 2022-11-30 0.0692745722 0.097328941 -0.155866121
## 2022-12-30 -0.1339679747 -0.061923721 -0.457813194
## 2023-01-31 0.1182712812 0.032773669 0.340915767
## 2023-02-28 -0.1007318653 0.008977499 0.171904973
## 2023-03-31 0.1412534063 0.144863314 0.008471140
## 2023-04-28 0.0397753062 0.063692838 -0.233183710
## 2023-05-31 0.1310217643 0.068691231 0.216021889
## 2023-06-30 -0.0196454416 0.036330649 0.249689452
## 2023-07-31 0.0956332795 -0.013659572 0.021391609
## 2023-08-31 0.0313565693 -0.022476312 -0.035588260
## 2023-09-29 -0.0408675029 -0.037330992 -0.030929027
## 2023-10-31 -0.0509540273 0.068420543 -0.219831983
## 2023-11-30 0.0665317006 0.115955042 0.178463656
## 2023-12-29 0.0510207183 -0.007603273 0.034390133
## 2024-01-31 0.0061543397 0.055700720 -0.282704160
## 2024-02-29 -0.0143479204 0.041447449 0.075015304
## 2024-03-28 0.0855198700 0.016971419 -0.138383423
## 2024-04-30 0.0781716797 -0.077540195 0.041724969
## 2024-05-31 0.0550640990 0.065966531 -0.028782134
## 2024-06-28 0.0540905131 0.073855019 0.105427913
## 2024-07-31 -0.0576203452 -0.066128350 0.159378271
## 2024-08-30 -0.0475462855 -0.001095688 -0.080549177
## 2024-09-30 0.0138344637 0.031060679 0.200441406
## 2024-10-31 0.0323671889 -0.057267739 -0.046070548
## 2024-11-19 0.0391227076 0.027764062 0.325578013
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## GOOG MSFT TSLA
## GOOG 0.004225900 0.002267473 0.003475058
## MSFT 0.002267473 0.003516174 0.003142578
## TSLA 0.003475058 0.003142578 0.029250123
w <- c(0.3, 0.3, 0.4)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.08586725
component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
component_contribution
## GOOG MSFT TSLA
## [1,] 0.01166231 0.01045378 0.06375115
rowSums(component_contribution)
## [1] 0.08586725
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 3
## GOOG MSFT TSLA
## <dbl> <dbl> <dbl>
## 1 0.136 0.122 0.742
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
## asset contribution
## <chr> <dbl>
## 1 GOOG 0.136
## 2 MSFT 0.122
## 3 TSLA 0.742
asset_returns_wide_tbl <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns) %>%
column_to_rownames(var = "date")
asset_returns_wide_tbl
## GOOG MSFT TSLA
## 2013-01-31 0.0660632586 0.027328093 0.102078114
## 2013-02-28 0.0584794367 0.020915055 -0.074128640
## 2013-03-28 -0.0087878655 0.028720567 0.084208138
## 2013-04-30 0.0375393189 0.145776623 0.354111531
## 2013-05-31 0.0550324684 0.059941393 0.593716684
## 2013-06-28 0.0104477177 -0.010368793 0.093672163
## 2013-07-31 0.0083478266 -0.081394646 0.223739522
## 2013-08-30 -0.0471073807 0.054854433 0.229971642
## 2013-09-30 0.0336805508 -0.003599417 0.134706620
## 2013-10-31 0.1626137511 0.062037859 -0.189806595
## 2013-11-29 0.0277603690 0.081562181 -0.228409405
## 2013-12-31 0.0560803510 -0.019063273 0.167108541
## 2014-01-31 0.0523736758 0.011428782 0.187261714
## 2014-02-28 0.0289427889 0.019814720 0.299722785
## 2014-03-31 -0.0863761431 0.067617548 -0.160783242
## 2014-04-30 -0.0559562158 -0.014498345 -0.002690122
## 2014-05-30 0.0611852104 0.020307324 -0.000577422
## 2014-06-30 0.0271165183 0.018393808 0.144457224
## 2014-07-31 -0.0064174879 0.034412757 -0.072372676
## 2014-08-29 0.0000000000 0.057485016 0.188794007
## 2014-09-30 0.0100266345 0.020264587 -0.105566477
## 2014-10-31 -0.0321734491 0.012646330 -0.004046477
## 2014-11-28 -0.0313403105 0.024438447 0.011599801
## 2014-12-31 -0.0288908834 -0.028858133 -0.094774519
## 2015-01-30 0.0153077624 -0.139546653 -0.088365289
## 2015-02-27 0.0437064372 0.089036431 -0.001277808
## 2015-03-31 -0.0188002471 -0.075530157 -0.074350051
## 2015-04-30 -0.0169025209 0.179201597 0.180226808
## 2015-05-29 -0.0097808463 -0.030803993 0.103899574
## 2015-06-30 -0.0220412022 -0.059571111 0.067300935
## 2015-07-31 0.1839181544 0.056150685 -0.007896616
## 2015-08-31 -0.0118342473 -0.063950866 -0.066366250
## 2015-09-30 -0.0160275192 0.016860611 -0.002653519
## 2015-10-30 0.1555397941 0.173395437 -0.182659777
## 2015-11-30 0.0437523642 0.038685873 0.106828586
## 2015-12-31 0.0216860731 0.020578207 0.041471519
## 2016-01-29 -0.0212148962 -0.007054545 -0.227360626
## 2016-02-29 -0.0627392605 -0.072343987 0.003810669
## 2016-03-31 0.0654275963 0.082036038 0.179948109
## 2016-04-29 -0.0722726559 -0.102086403 0.046721797
## 2016-05-31 0.0598051735 0.067842353 -0.075597968
## 2016-06-30 -0.0611191121 -0.035138677 -0.050296440
## 2016-07-29 0.1050873292 0.102268307 0.100785334
## 2016-08-31 -0.0022657966 0.019880998 -0.102058091
## 2016-09-30 0.0132615302 0.002433342 -0.038366372
## 2016-10-31 0.0092841056 0.039487724 -0.031364583
## 2016-11-30 -0.0343615135 0.012390811 -0.043041267
## 2016-12-30 0.0180152337 0.030721402 0.120665178
## 2017-01-31 0.0318397939 0.039598403 0.164624916
## 2017-02-28 0.0326200945 -0.004373314 -0.007730364
## 2017-03-31 0.0076842168 0.028960689 0.107278727
## 2017-04-28 0.0880997230 0.038718371 0.120916212
## 2017-05-31 0.0629877923 0.025672744 0.082295892
## 2017-06-30 -0.0599350111 -0.013115426 0.058654468
## 2017-07-31 0.0236741629 0.053250016 -0.111459860
## 2017-08-31 0.0094446934 0.033388937 0.095543446
## 2017-09-29 0.0208389738 -0.003751751 -0.042474144
## 2017-10-31 0.0582525835 0.110342088 -0.028457409
## 2017-11-30 0.0046809400 0.016841042 -0.070862541
## 2017-12-29 0.0241716390 0.016145529 0.008061928
## 2018-01-31 0.1115967900 0.104998062 0.129254571
## 2018-02-28 -0.0573515298 -0.008450753 -0.032266877
## 2018-03-29 -0.0683057993 -0.027022705 -0.253920408
## 2018-04-30 -0.0141136248 0.024353094 0.099254576
## 2018-05-31 0.0643892525 0.059652156 -0.031698139
## 2018-06-29 0.0278664749 -0.002329512 0.186043257
## 2018-07-31 0.0871651755 0.073020762 -0.140021491
## 2018-08-31 0.0007637297 0.061088317 0.011737389
## 2018-09-28 -0.0205011698 0.017997738 -0.130439038
## 2018-10-31 -0.1028991445 -0.068387048 0.242170576
## 2018-11-30 0.0162678540 0.041797762 0.038271580
## 2018-12-31 -0.0552431428 -0.087790347 -0.051761952
## 2019-01-31 0.0750917804 0.027768485 -0.080628789
## 2019-02-28 0.0031748651 0.074511445 0.041032981
## 2019-03-29 0.0465716243 0.051409279 -0.133656413
## 2019-04-30 0.0128463578 0.101963428 -0.159123803
## 2019-05-31 -0.0740704060 -0.050747036 -0.253945372
## 2019-06-28 -0.0208014640 0.079843797 0.188012109
## 2019-07-31 0.1183224942 0.017097083 0.078092373
## 2019-08-30 -0.0237704414 0.014924749 -0.068516948
## 2019-09-30 0.0256755038 0.008451043 0.065449565
## 2019-10-31 0.0331681100 0.030738663 0.268061253
## 2019-11-29 0.0349734427 0.057761764 0.046592176
## 2019-12-31 0.0242707879 0.040901229 0.237359743
## 2020-01-31 0.0701848929 0.076456010 0.441578342
## 2020-02-28 -0.0684586298 -0.046764570 0.026424253
## 2020-03-31 -0.1413300093 -0.026900010 -0.242781458
## 2020-04-30 0.1482721074 0.127800083 0.400209535
## 2020-05-29 0.0578073650 0.025074352 0.065730500
## 2020-06-30 -0.0107722179 0.104863704 0.257108657
## 2020-07-31 0.0478934005 0.007343460 0.281420674
## 2020-08-31 0.0971010982 0.097808951 0.554719320
## 2020-09-30 -0.1061508709 -0.069775542 -0.149762306
## 2020-10-30 0.0980590834 -0.038086049 -0.100371771
## 2020-11-30 0.0826847480 0.058326136 0.380308519
## 2020-12-31 -0.0050446216 0.038264353 0.217730753
## 2021-01-29 0.0467581267 0.041997505 0.117343701
## 2021-02-26 0.1039617314 0.004109536 -0.161038203
## 2021-03-31 0.0154771604 0.014482782 -0.011269836
## 2021-04-30 0.1527899045 0.067286290 0.060292565
## 2021-05-28 0.0005974250 -0.007656568 -0.126372343
## 2021-06-30 0.0385416519 0.081569834 0.083547955
## 2021-07-30 0.0760718346 0.050423349 0.010973846
## 2021-08-31 0.0730045198 0.059769083 0.068224268
## 2021-09-30 -0.0875715395 -0.068406252 0.052632612
## 2021-10-29 0.1066948649 0.162366269 0.362230202
## 2021-11-30 -0.0400332042 -0.001283101 0.027237848
## 2021-12-31 0.0155159766 0.017184293 -0.079968440
## 2022-01-31 -0.0640854435 -0.078334221 -0.120597475
## 2022-02-28 -0.0059683911 -0.037922506 -0.073397011
## 2022-03-31 0.0346685444 0.031364889 0.213504290
## 2022-04-29 -0.1944949638 -0.105212707 -0.213125289
## 2022-05-31 -0.0081002558 -0.018242586 -0.138340062
## 2022-06-30 -0.0417810722 -0.056909631 -0.118657126
## 2022-07-29 0.0643327736 0.089014570 0.280480149
## 2022-08-31 -0.0663691728 -0.068988906 -0.075250271
## 2022-09-30 -0.1268135622 -0.115710548 -0.038313992
## 2022-10-31 -0.0156179604 -0.003311516 -0.153346760
## 2022-11-30 0.0692745722 0.097328941 -0.155866121
## 2022-12-30 -0.1339679747 -0.061923721 -0.457813194
## 2023-01-31 0.1182712812 0.032773669 0.340915767
## 2023-02-28 -0.1007318653 0.008977499 0.171904973
## 2023-03-31 0.1412534063 0.144863314 0.008471140
## 2023-04-28 0.0397753062 0.063692838 -0.233183710
## 2023-05-31 0.1310217643 0.068691231 0.216021889
## 2023-06-30 -0.0196454416 0.036330649 0.249689452
## 2023-07-31 0.0956332795 -0.013659572 0.021391609
## 2023-08-31 0.0313565693 -0.022476312 -0.035588260
## 2023-09-29 -0.0408675029 -0.037330992 -0.030929027
## 2023-10-31 -0.0509540273 0.068420543 -0.219831983
## 2023-11-30 0.0665317006 0.115955042 0.178463656
## 2023-12-29 0.0510207183 -0.007603273 0.034390133
## 2024-01-31 0.0061543397 0.055700720 -0.282704160
## 2024-02-29 -0.0143479204 0.041447449 0.075015304
## 2024-03-28 0.0855198700 0.016971419 -0.138383423
## 2024-04-30 0.0781716797 -0.077540195 0.041724969
## 2024-05-31 0.0550640990 0.065966531 -0.028782134
## 2024-06-28 0.0540905131 0.073855019 0.105427913
## 2024-07-31 -0.0576203452 -0.066128350 0.159378271
## 2024-08-30 -0.0475462855 -0.001095688 -0.080549177
## 2024-09-30 0.0138344637 0.031060679 0.200441406
## 2024-10-31 0.0323671889 -0.057267739 -0.046070548
## 2024-11-19 0.0391227076 0.027764062 0.325578013
calculate_component_contribution <- function(.data, w) {
covariance_matrix <- cov(.data)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
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.3,0.3,0.4))
## # A tibble: 1 × 3
## GOOG MSFT TSLA
## <dbl> <dbl> <dbl>
## 1 0.136 0.122 0.742
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.3,0.3,0.4)) %>%
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution")%>%
ggplot(aes(x = Asset, y = Contribution)) +
geom_col(fill = "magenta") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
labs(title = "Percent Contribution to Portfolio Volatility")
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.3,0.3,0.4)) %>%
gather(key = "asset", value = "contribution") %>%
add_column(weights = c(0.3,0.3,0.4)) %>%
pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%
ggplot(aes(asset, 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 portfolio volatility is TSLA. My portfolio risk is concentrated in that asset because I have given it the most weight and it is the most volatile.