# 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("NVDA", "AAPL", "NFLX", "MSFT", "TSLA")
prices <- tq_get (x = symbols,
from = "2012-12-31",
to = "2022-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"))
# 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
## AAPL MSFT NFLX NVDA TSLA
## 2013-01-31 -1.555893e-01 0.027328524 0.5792178072 0.000000000 0.102078114
## 2013-02-28 -2.561129e-02 0.020914966 0.1294683790 0.038221840 -0.074128640
## 2013-03-28 2.850586e-03 0.028720541 0.0063600708 0.013338515 0.084208138
## 2013-04-30 2.709613e-04 0.145776686 0.1323750202 0.070706294 0.354111531
## 2013-05-31 2.217174e-02 0.059941480 0.0460381788 0.054651843 0.593716684
## 2013-06-28 -1.258957e-01 -0.010368631 -0.0693560587 -0.030167153 0.093672163
## 2013-07-31 1.321024e-01 -0.081395007 0.1468488828 0.028091817 0.223739522
## 2013-08-30 8.044271e-02 0.054854553 0.1495238153 0.026270320 0.229971642
## 2013-09-30 -2.172377e-02 -0.003599452 0.0853633452 0.053460384 0.134706620
## 2013-10-31 9.201583e-02 0.062037685 0.0420204523 -0.024066221 -0.189806595
## 2013-11-29 6.770796e-02 0.081562538 0.1260456325 0.032034858 -0.228409405
## 2013-12-31 8.862563e-03 -0.019062967 0.0064580908 0.026566796 0.167108541
## 2014-01-31 -1.139495e-01 0.011428204 0.1059769064 -0.020176965 0.187261714
## 2014-02-28 5.591775e-02 0.019814846 0.0849673396 0.162107573 0.299722785
## 2014-03-31 1.975680e-02 0.067617411 -0.2357726219 -0.025904157 -0.160783242
## 2014-04-30 9.476133e-02 -0.014498465 -0.0890406532 0.030788929 -0.002690122
## 2014-05-30 7.576508e-02 0.020308126 0.2603988640 0.032886354 -0.000577422
## 2014-06-30 2.728592e-02 0.018393431 0.0530627854 -0.024508560 0.144457224
## 2014-07-31 2.832654e-02 0.034412803 -0.0414273713 -0.057729658 -0.072372676
## 2014-08-29 7.465149e-02 0.057485041 0.1221472352 0.110060128 0.188794007
## 2014-09-30 -1.722062e-02 0.020264358 -0.0569909207 -0.052782669 -0.105566477
## 2014-10-31 6.948929e-02 0.012646087 -0.1386421470 0.057399355 -0.004046477
## 2014-11-28 1.007307e-01 0.024438813 -0.1250817171 0.074852020 0.011599801
## 2014-12-31 -7.460633e-02 -0.028858053 -0.0144727330 -0.044863708 -0.094774519
## 2015-01-30 5.961169e-02 -0.139546920 0.2571875124 -0.043318635 -0.088365289
## 2015-02-27 9.601607e-02 0.089036532 0.0722680160 0.142698427 -0.001277808
## 2015-03-31 -3.187430e-02 -0.075530134 -0.1307827695 -0.052582143 -0.074350051
## 2015-04-30 5.769806e-03 0.179201597 0.2893246523 0.058908642 0.180226808
## 2015-05-29 4.434109e-02 -0.030804045 0.1145793430 0.001459982 0.103899574
## 2015-06-30 -3.793816e-02 -0.059571321 0.0513461801 -0.095716967 0.067300935
## 2015-07-31 -3.348105e-02 0.056151114 0.1972314899 -0.007988145 -0.007896616
## 2015-08-31 -6.848879e-02 -0.063950695 0.0062789186 0.123595751 -0.066366250
## 2015-09-30 -2.205751e-02 0.016860851 -0.1079428532 0.092150530 -0.002653519
## 2015-10-30 8.011209e-02 0.173394884 0.0483934447 0.140555383 -0.182659777
## 2015-11-30 -5.821181e-03 0.038685892 0.1292201596 0.115405144 0.106828586
## 2015-12-31 -1.167903e-01 0.020578011 -0.0753374900 0.038347582 0.041471519
## 2016-01-29 -7.822341e-02 -0.007054433 -0.2194783218 -0.118048683 -0.227360626
## 2016-02-29 -1.288471e-03 -0.072344198 0.0169505524 0.071923687 0.003810669
## 2016-03-31 1.197461e-01 0.082036509 0.0902267645 0.127654726 0.179948109
## 2016-04-29 -1.507309e-01 -0.102086450 -0.1270822750 -0.002810242 0.046721797
## 2016-05-31 6.931393e-02 0.067842154 0.1304025548 0.276387965 -0.075597968
## 2016-06-30 -4.359638e-02 -0.035138655 -0.1144250835 0.006188151 -0.050296440
## 2016-07-29 8.623530e-02 0.102268273 -0.0025174134 0.194443995 0.100785334
## 2016-08-31 2.337643e-02 0.019880761 0.0657364022 0.073468927 -0.102058091
## 2016-09-30 6.344812e-02 0.002433333 0.0112246699 0.110693599 -0.038366372
## 2016-10-31 4.325284e-03 0.039488047 0.2367091536 0.037805019 -0.031364583
## 2016-11-30 -2.183798e-02 0.012391042 -0.0650992833 0.260525327 -0.043041267
## 2016-12-30 4.684085e-02 0.030721084 0.0564934501 0.146435784 0.120665178
## 2017-01-31 4.664177e-02 0.039598387 0.1280336976 0.022602090 0.164624916
## 2017-02-28 1.255555e-01 -0.004373407 0.0100410836 -0.071874660 -0.007730364
## 2017-03-31 4.754123e-02 0.028960960 0.0391854826 0.070843558 0.107278727
## 2017-04-28 -6.938045e-05 0.038718457 0.0292677767 -0.043434217 0.120916212
## 2017-05-31 6.560698e-02 0.025672683 0.0689841760 0.326022291 0.082295892
## 2017-06-30 -5.891558e-02 -0.013115271 -0.0874853720 0.001453744 0.058654468
## 2017-07-31 3.218029e-02 0.053249704 0.1954425989 0.117044717 -0.111459860
## 2017-08-31 1.016529e-01 0.033388984 -0.0390093333 0.042639406 0.095543446
## 2017-09-29 -6.213453e-02 -0.003751686 0.0373014045 0.053601206 -0.042474144
## 2017-10-31 9.240383e-02 0.110341787 0.0798771971 0.145700499 -0.028457409
## 2017-11-30 2.007484e-02 0.016841314 -0.0461006656 -0.029244929 -0.070862541
## 2017-12-29 -1.536301e-02 0.016145543 0.0230816211 -0.036583589 0.008061928
## 2018-01-31 -1.069351e-02 0.104998060 0.3422453562 0.239240699 0.129254571
## 2018-02-28 6.596104e-02 -0.008450795 0.0750958659 -0.014959317 -0.032266877
## 2018-03-29 -5.980401e-02 -0.027022736 0.0135328404 -0.043969271 -0.253920408
## 2018-04-30 -1.513383e-02 0.024353303 0.0563153192 -0.029312423 0.099254576
## 2018-05-31 1.267420e-01 0.059651963 0.1180177495 0.115145101 -0.031698139
## 2018-06-29 -9.462836e-03 -0.002329633 0.1073124981 -0.062544660 0.186043257
## 2018-07-31 2.759898e-02 0.073020891 -0.1483892939 0.033048512 -0.140021491
## 2018-08-31 1.826732e-01 0.061088198 0.0857955874 0.137075532 0.011737389
## 2018-09-28 -8.337448e-03 0.017997903 0.0173903689 0.001210659 -0.130439038
## 2018-10-31 -3.095188e-02 -0.068387523 -0.2149050726 -0.287373697 0.242170576
## 2018-11-30 -1.999123e-01 0.041798151 -0.0532520010 -0.253667447 0.038271580
## 2018-12-31 -1.240887e-01 -0.087790407 -0.0667287380 -0.202283348 -0.051761952
## 2019-01-31 5.368690e-02 0.027768793 0.2377564149 0.073974236 -0.080628789
## 2019-02-28 4.380305e-02 0.074511241 0.0533383470 0.071593863 0.041032981
## 2019-03-29 9.260246e-02 0.051409365 -0.0043097723 0.151869824 -0.133656413
## 2019-04-30 5.490103e-02 0.101963140 0.0384589024 0.007987960 -0.159123803
## 2019-05-31 -1.326323e-01 -0.050746792 -0.0764149871 -0.288679976 -0.253945372
## 2019-06-28 1.226770e-01 0.079843643 0.0676869856 0.192591351 0.188012109
## 2019-07-31 7.361699e-02 0.017097109 -0.1286120899 0.026972596 0.078092373
## 2019-08-30 -1.659815e-02 0.014924703 -0.0948922673 -0.006207970 -0.068516948
## 2019-09-30 7.042272e-02 0.008451202 -0.0931610198 0.038414627 0.065449565
## 2019-10-31 1.049764e-01 0.030738523 0.0713417349 0.143946666 0.268061253
## 2019-11-29 7.469363e-02 0.057762002 0.0905829220 0.076031727 0.046592176
## 2019-12-31 9.420418e-02 0.040900975 0.0279227940 0.082162922 0.237359743
## 2020-01-31 5.260155e-02 0.076455913 0.0643897455 0.004790844 0.441578342
## 2020-02-28 -1.218301e-01 -0.046764586 0.0670726963 0.133626993 0.026424253
## 2020-03-31 -7.231433e-02 -0.026900095 0.0173805330 -0.024248390 -0.242781458
## 2020-04-30 1.444238e-01 0.127800202 0.1116390480 0.103279266 0.400209535
## 2020-05-29 8.166713e-02 0.025074439 -0.0002858456 0.194461979 0.065730500
## 2020-06-30 1.374865e-01 0.104863712 0.0807736724 0.068216720 0.257108657
## 2020-07-31 1.528340e-01 0.007343442 0.0717317246 0.111189415 0.281420674
## 2020-08-31 1.960350e-01 0.097808998 0.0799293979 0.231105086 0.554719320
## 2020-09-30 -1.081716e-01 -0.069775489 -0.0573783743 0.011895777 -0.149762306
## 2020-10-30 -6.188830e-02 -0.038086058 -0.0497966282 -0.076501321 -0.100371771
## 2020-11-30 9.120473e-02 0.058326125 0.0309615020 0.066921508 0.380308519
## 2020-12-31 1.084721e-01 0.038264301 0.0970870744 -0.025899727 0.217730753
## 2021-01-29 -5.516766e-03 0.041997603 -0.0155437114 -0.005010880 0.117343701
## 2021-02-26 -8.306871e-02 0.004109385 0.0120608627 0.054293218 -0.161038203
## 2021-03-31 7.312696e-03 0.014482785 -0.0324212105 -0.026723395 -0.011269836
## 2021-04-30 7.345287e-02 0.067286430 -0.0158244352 0.117298151 0.060292565
## 2021-05-28 -5.181659e-02 -0.007656588 -0.0209791877 0.079071104 -0.126372343
## 2021-06-30 9.450007e-02 0.081569775 0.0492816132 0.208332000 0.083547955
## 2021-07-30 6.295825e-02 0.050423604 -0.0203491777 -0.025494078 0.010973846
## 2021-08-31 4.161147e-02 0.059768657 0.0950695060 0.138204221 0.068224268
## 2021-09-30 -7.046176e-02 -0.068406211 0.0698019211 -0.077484765 0.052632612
## 2021-10-29 5.700139e-02 0.162366383 0.1231245456 0.210396094 0.362230202
## 2021-11-30 9.991926e-02 -0.001282922 -0.0727082022 0.245338519 0.027237848
## 2021-12-31 7.160290e-02 0.017184162 -0.0634444870 -0.105149789 -0.079968440
## 2022-01-31 -1.583684e-02 -0.078334522 -0.3438762168 -0.183267223 -0.120597475
## 2022-02-28 -5.558243e-02 -0.037921966 -0.0794420544 -0.004133318 -0.073397011
## 2022-03-31 5.588254e-02 0.031364708 -0.0518377235 0.112575864 0.213504290
## 2022-04-29 -1.021777e-01 -0.105212772 -0.6769150653 -0.386065663 -0.213125289
## 2022-05-31 -5.603708e-02 -0.018242595 0.0365177336 0.006717065 -0.138340062
## 2022-06-30 -8.493689e-02 -0.056909617 -0.1213919403 -0.208219389 -0.118657126
## 2022-07-29 1.728044e-01 0.089014597 0.2516130190 0.180792315 0.280480149
## 2022-08-31 -3.170532e-02 -0.068989078 -0.0059760083 -0.185089353 -0.075250271
## 2022-09-30 -1.289443e-01 -0.115710387 0.0517762908 -0.217576748 -0.038313992
## 2022-10-31 1.039558e-01 -0.003311605 0.2148866613 0.106044007 -0.153346760
## 2022-11-30 -3.358521e-02 0.097329066 0.0457051938 0.226461996 -0.155866121
## 2022-12-30 -1.304191e-01 -0.061923768 -0.0354794590 -0.146693641 -0.457813194
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
## AAPL MSFT NFLX NVDA TSLA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.195 0.147 0.26 0.259 0.14
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
# Transform to Long Form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add Weights
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(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?
Netflix and Nvidia are my largest contributors when it comes to my portfolio volatility. The two of them combined take up nearly 50% of my portfolio volatility. Besides Netflix, my portfolio for the most part is tech related. In my eyes my biggest risk has to deal with the tech market. We saw how the tech market can be impacted by big global events such as Covid-19 for example. That shutdown and slowed down production for tech companies for awhile and it could be a worry when it comes to my portfolios risk probability.