# 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("GOOG", "GME", "NVDA", "V")
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"))
# 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
## GME GOOG NVDA V
## 2013-01-31 -0.078316817 0.0660632586 0.000000000 0.040910974
## 2013-02-28 0.077120536 0.0584794367 0.038221820 0.006719313
## 2013-03-28 0.120893825 -0.0087879622 0.013338909 0.068219709
## 2013-04-30 0.221354595 0.0375394155 0.070705912 -0.008158685
## 2013-05-31 -0.042899901 0.0550324684 0.054652070 0.057703126
## 2013-06-28 0.237039251 0.0104477177 -0.030167017 0.025549562
## 2013-07-31 0.154660585 0.0083479130 0.028091693 -0.031913204
## 2013-08-30 0.028696326 -0.0471075578 0.026270342 -0.012897152
## 2013-09-30 -0.011215692 0.0336806414 0.053460583 0.091330775
## 2013-10-31 0.099056802 0.1626137511 -0.024066506 0.028730636
## 2013-11-29 -0.127659584 0.0277602965 0.032035389 0.035961533
## 2013-12-31 0.026432406 0.0560803550 0.026566512 0.090266301
## 2014-01-31 -0.339766393 0.0523737443 -0.020176809 -0.033100049
## 2014-02-28 0.061915224 0.0289427889 0.162107353 0.049434476
## 2014-03-31 0.105355250 -0.0863761431 -0.025904064 -0.045639394
## 2014-04-30 -0.035160952 -0.0559561430 0.030788450 -0.063346917
## 2014-05-30 -0.047216234 0.0611852061 0.032886692 0.060451890
## 2014-06-30 0.075687007 0.0271165165 -0.024508387 -0.019364242
## 2014-07-31 0.036394244 -0.0064175546 -0.057729824 0.001422602
## 2014-08-29 0.013138632 0.0000000000 0.110059972 0.009034589
## 2014-09-30 -0.023982151 0.0100265681 -0.052782568 0.003991379
## 2014-10-31 0.037164976 -0.0321733827 0.057399105 0.123552003
## 2014-11-28 -0.115478907 -0.0313402397 0.074851972 0.069040166
## 2014-12-31 -0.112112758 -0.0288909542 -0.044863214 0.015411756
## 2015-01-30 0.042004647 0.0153077624 -0.043318941 -0.028196929
## 2015-02-27 0.047641301 0.0437064372 0.142699035 0.064167600
## 2015-03-31 0.035540190 -0.0188003170 -0.052582373 -0.036292654
## 2015-04-30 0.015163787 -0.0169024509 0.058908740 0.009736857
## 2015-05-29 0.118993223 -0.0097808463 0.001459760 0.040794589
## 2015-06-30 -0.002231242 -0.0220411287 -0.095717050 -0.022529192
## 2015-07-31 0.065105713 0.1839180810 -0.007988052 0.115082377
## 2015-08-31 -0.076341959 -0.0118342473 0.123595673 -0.053477922
## 2015-09-30 -0.021683868 -0.0160275192 0.092151117 -0.023269622
## 2015-10-30 0.111481466 0.1555397941 0.140555158 0.107683049
## 2015-11-30 -0.273957433 0.0437523642 0.115404733 0.020060290
## 2015-12-31 -0.212242680 0.0216861739 0.038347330 -0.018651417
## 2016-01-29 -0.067491123 -0.0212149969 -0.118048363 -0.040257649
## 2016-02-29 0.162022868 -0.0627391508 0.071923933 -0.026626016
## 2016-03-31 0.040474919 0.0654275894 0.127654284 0.054961191
## 2016-04-29 0.033165826 -0.0722726482 -0.002810288 0.009888372
## 2016-05-31 -0.119690411 0.0598050631 0.276388289 0.023539478
## 2016-06-30 -0.077571409 -0.0611192226 0.006188004 -0.062328658
## 2016-07-29 0.152214078 0.1050874397 0.194443967 0.050990581
## 2016-08-31 -0.086336007 -0.0022657966 0.073469214 0.037597777
## 2016-09-30 -0.015455949 0.0132614319 0.110693486 0.022005857
## 2016-10-31 -0.137318266 0.0092841065 0.037805158 -0.002299733
## 2016-11-30 0.041084175 -0.0343614160 0.260525434 -0.062864850
## 2016-12-30 0.022823983 0.0180152337 0.146435310 0.009012378
## 2017-01-31 -0.030957439 0.0318397939 0.022602178 0.058375296
## 2017-02-28 -0.002043599 0.0326201874 -0.071875004 0.063215972
## 2017-03-31 -0.065169448 0.0076841239 0.070843930 0.010520072
## 2017-04-28 0.006189341 0.0880996386 -0.043434291 0.026099157
## 2017-05-31 -0.024538445 0.0629878767 0.326022269 0.044793832
## 2017-06-30 -0.007325390 -0.0599350111 0.001453853 -0.015343357
## 2017-07-31 0.003695213 0.0236740807 0.117044925 0.059808784
## 2017-08-31 -0.159080530 0.0094447756 0.042638950 0.040613605
## 2017-09-29 0.130272559 0.0208389738 0.053601328 0.016478597
## 2017-10-31 -0.100210872 0.0582525835 0.145700750 0.044055045
## 2017-11-30 0.022864475 0.0046809400 -0.029245128 0.025224623
## 2017-12-29 -0.043603694 0.0241716390 -0.036583518 0.012621125
## 2018-01-31 -0.065616247 0.1115967900 0.239240781 0.085760776
## 2018-02-28 -0.068950327 -0.0573515298 -0.014959417 -0.008698841
## 2018-03-29 -0.193658461 -0.0683057993 -0.043968981 -0.027376561
## 2018-04-30 0.078456645 -0.0141136248 -0.029312706 0.058921669
## 2018-05-31 -0.033522723 0.0643892525 0.115144994 0.031421647
## 2018-06-29 0.125248730 0.0278664064 -0.062544526 0.013147582
## 2018-07-31 -0.011042298 0.0871652441 0.033048678 0.031875834
## 2018-08-31 -0.082416485 0.0007637297 0.137075326 0.073104888
## 2018-09-28 0.163276878 -0.0205011057 0.001210552 0.021551106
## 2018-10-31 -0.044868592 -0.1028992086 -0.287373838 -0.085068923
## 2018-11-30 -0.066549767 0.0162678540 -0.253667537 0.029410564
## 2018-12-31 -0.051018086 -0.0552431428 -0.202282840 -0.071435618
## 2019-01-31 -0.106946524 0.0750918489 0.073974121 0.023001466
## 2019-02-28 0.031252541 0.0031747966 0.071593955 0.094417712
## 2019-03-29 -0.107764703 0.0465715591 0.151869575 0.053050597
## 2019-04-30 -0.160899150 0.0128464230 0.007987603 0.051411707
## 2019-05-31 -0.132046087 -0.0740704753 -0.288679537 -0.017495995
## 2019-06-28 -0.326234612 -0.0208013947 0.192591548 0.073014019
## 2019-07-31 -0.307996680 0.1183224942 0.026972288 0.025317484
## 2019-08-30 -0.012515796 -0.0237704414 -0.006208153 0.017160476
## 2019-09-30 0.329611755 0.0256755038 0.038414793 -0.049949723
## 2019-10-31 -0.014598785 0.0331681707 0.143946905 0.039050990
## 2019-11-29 0.153099721 0.0349733820 0.076031539 0.032773877
## 2019-12-31 -0.041874109 0.0242707879 0.082163076 0.018206167
## 2020-01-31 -0.459532339 0.0701848929 0.004790619 0.057244140
## 2020-02-28 -0.064538525 -0.0684586298 0.133626954 -0.089019469
## 2020-03-31 -0.028170850 -0.1413300093 -0.024248201 -0.120537782
## 2020-04-30 0.492952566 0.1482719939 0.103279410 0.103670620
## 2020-05-29 -0.344532575 0.0578074784 0.194461679 0.090082540
## 2020-06-30 0.066691424 -0.0107722179 0.068216534 -0.010659037
## 2020-07-31 -0.079083085 0.0478934005 0.111189779 -0.014443563
## 2020-08-31 0.510326663 0.0971010982 0.231105222 0.108922742
## 2020-09-30 0.423269740 -0.1061509749 0.011895855 -0.058371882
## 2020-10-30 0.026126349 0.0980591875 -0.076501688 -0.095755084
## 2020-11-30 0.458476066 0.0826847480 0.066921884 0.147866148
## 2020-12-31 0.128992161 -0.0050447089 -0.025899855 0.039065243
## 2021-01-29 2.847842905 0.0467582140 -0.005010978 -0.123853330
## 2021-02-26 -1.161404664 0.1039617314 0.054293241 0.095990701
## 2021-03-31 0.623655775 0.0154771604 -0.026723279 -0.003112115
## 2021-04-30 -0.089380118 0.1527899045 0.117298133 0.098127115
## 2021-05-28 0.245981206 0.0005973616 0.079070876 -0.025716908
## 2021-06-30 -0.036047378 0.0385416543 0.208331984 0.028280945
## 2021-07-30 -0.284480605 0.0760718956 -0.025493779 0.052364026
## 2021-08-31 0.303446001 0.0730045198 0.138204093 -0.071394702
## 2021-09-30 -0.218127305 -0.0875715395 -0.077485105 -0.028108792
## 2021-10-29 0.044801037 0.1066949681 0.210396516 -0.050549105
## 2021-11-30 0.066916416 -0.0400333073 0.245338515 -0.087070014
## 2021-12-31 -0.279341610 0.0155159766 -0.105149858 0.111888180
## 2022-01-31 -0.309138462 -0.0640854435 -0.183267241 0.042726843
## 2022-02-28 0.124239263 -0.0059685045 -0.004133273 -0.043827636
## 2022-03-31 0.300530946 0.0346686578 0.112575897 0.025807210
## 2022-04-29 -0.286602107 -0.1944949638 -0.386065775 -0.039738482
## 2022-05-31 -0.002642024 -0.0081002558 0.006717041 -0.002606477
## 2022-06-30 -0.019754486 -0.0417810023 -0.208219297 -0.074742642
## 2022-07-29 0.106471843 0.0643327037 0.180792265 0.074459818
## 2022-08-31 -0.171850229 -0.0663691027 -0.185089433 -0.063488889
## 2022-09-30 -0.130742003 -0.1268136323 -0.217576666 -0.112031192
## 2022-10-31 0.119152762 -0.0156178797 0.106044073 0.153676040
## 2022-11-30 -0.077074098 0.0692744160 0.226461816 0.048638600
## 2022-12-30 -0.350534803 -0.1339678993 -0.146693626 -0.043513755
## 2023-01-31 0.169509694 0.1182712047 0.290330288 0.102608376
## 2023-02-28 -0.128644337 -0.1007318734 0.172531461 -0.043680446
## 2023-03-31 0.179891887 0.1412534910 0.179536548 0.024788070
## 2023-04-28 -0.176776549 0.0397752355 -0.001008475 0.031736146
## 2023-05-31 0.220548090 0.1310218349 0.310008292 -0.049632843
## 2023-06-30 0.008281653 -0.0196454416 0.111729625 0.071784927
## 2023-07-31 -0.088324294 0.0956332795 0.099530518 0.001052185
## 2023-08-31 -0.179622575 0.0313565693 0.054674262 0.034772906
## 2023-09-29 -0.119536608 -0.0408675029 -0.126218700 -0.065907100
## 2023-10-31 -0.178440794 -0.0509540273 -0.064546276 0.021888266
## 2023-11-30 0.055098661 0.0665317006 0.137050155 0.089945867
## 2023-12-29 0.186322731 0.0510207183 0.057263031 0.014196787
## 2024-01-31 -0.208561358 0.0061543397 0.217059054 0.048396628
## 2024-02-29 0.002807084 -0.0143479204 0.251388463 0.035613435
## 2024-03-28 -0.130832061 0.0855198700 0.132939782 -0.012675627
## 2024-04-30 -0.121283587 0.0781716797 -0.044746618 -0.038238042
## 2024-05-31 0.735518880 0.0550640990 0.238127666 0.016080348
## 2024-06-28 0.064835630 0.0540905131 0.119508665 -0.037354995
## 2024-07-31 -0.085355857 -0.0576203452 -0.054220172 0.012118243
## 2024-08-30 0.032547894 -0.0475462855 0.019883127 0.041488747
## 2024-09-30 -0.021144251 0.0138344637 0.017277918 -0.005151144
## 2024-10-31 -0.033255114 0.0323671889 0.089122612 0.052774205
## 2024-11-19 0.217537218 0.0391227076 0.101957629 0.075058153
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, .25, .25))
## # A tibble: 1 × 4
## GME GOOG NVDA V
## <dbl> <dbl> <dbl> <dbl>
## 1 0.71 0.085 0.176 0.029
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.25, 0.25)) %>%
# Transform to long from
pivot_longer(cols = everything(), names_to = "asset", values_to = "Contribution") %>%
#add weights
add_column(weight = c(0.25, 0.25, 0.25, 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 asset with the largest contribution to the portfolio volatility is GME (GameStop). The graph I created shows that their volatility is slightly above 70%, which is well above the other assets in this portfolio. I think the risk is concentrated in one asset, GME, because they have a higher contribution to the portfolio by a lot, meaning the portfolio risk may be heavily concentrated in GME. More specifically, GME has a disproportionately large impact on the overall volatility of this portfolio. This may mean that too much price movement in GME may affect the overall risk and performance of this entire portfolio.