# 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", "AMZN", "MSFT", "NFLX")
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
## AMZN MSFT NFLX TSLA
## 2013-01-31 0.0566799395 0.027328315 0.5792178072 0.102078114
## 2013-02-28 -0.0046435024 0.020914998 0.1294683790 -0.074128640
## 2013-03-28 0.0083654162 0.028720293 0.0063600708 0.084208138
## 2013-04-30 -0.0487507497 0.145777036 0.1323750202 0.354111531
## 2013-05-31 0.0588686246 0.059941404 0.0460381788 0.593716684
## 2013-06-28 0.0310507506 -0.010369139 -0.0693560587 0.093672163
## 2013-07-31 0.0813355350 -0.081394598 0.1468488828 0.223739522
## 2013-08-30 -0.0695574090 0.054854625 0.1495238153 0.229971642
## 2013-09-30 0.1067688897 -0.003599537 0.0853633452 0.134706620
## 2013-10-31 0.1521839116 0.062037926 0.0420204523 -0.189806595
## 2013-11-29 0.0781496860 0.081562088 0.1260456325 -0.228409405
## 2013-12-31 0.0130490386 -0.019062861 0.0064580908 0.167108541
## 2014-01-31 -0.1059765119 0.011428339 0.1059769064 0.187261714
## 2014-02-28 0.0094619003 0.019814940 0.0849673396 0.299722785
## 2014-03-31 -0.0737086161 0.067617492 -0.2357726219 -0.160783242
## 2014-04-30 -0.1007565303 -0.014498320 -0.0890406532 -0.002690122
## 2014-05-30 0.0273091844 0.020307058 0.2603988640 -0.000577422
## 2014-06-30 0.0383836202 0.018393798 0.0530627854 0.144457224
## 2014-07-31 -0.0369768154 0.034413266 -0.0414273713 -0.072372676
## 2014-08-29 0.0799468404 0.057484677 0.1221472352 0.188794007
## 2014-09-30 -0.0502010184 0.020264711 -0.0569909207 -0.105566477
## 2014-10-31 -0.0540982347 0.012646010 -0.1386421470 -0.004046477
## 2014-11-28 0.1031187277 0.024438708 -0.1250817171 0.011599801
## 2014-12-31 -0.0872368614 -0.028858143 -0.0144727330 -0.094774519
## 2015-01-30 0.1330922557 -0.139546698 0.2571875124 -0.088365289
## 2015-02-27 0.0697992426 0.089036190 0.0722680160 -0.001277808
## 2015-03-31 -0.0214295755 -0.075529627 -0.1307827695 -0.074350051
## 2015-04-30 0.1253212736 0.179201077 0.2893246523 0.180226808
## 2015-05-29 0.0175090293 -0.030803618 0.1145793430 0.103899574
## 2015-06-30 0.0112589814 -0.059571522 0.0513461801 0.067300935
## 2015-07-31 0.2111621090 0.056151091 0.1972314899 -0.007896616
## 2015-08-31 -0.0443525782 -0.063951039 0.0062789186 -0.066366250
## 2015-09-30 -0.0019516837 0.016861204 -0.1079428532 -0.002653519
## 2015-10-30 0.2010808743 0.173394600 0.0483934447 -0.182659777
## 2015-11-30 0.0602956777 0.038686179 0.1292201596 0.106828586
## 2015-12-31 0.0165440008 0.020577749 -0.0753374900 0.041471519
## 2016-01-29 -0.1410054620 -0.007054089 -0.2194783218 -0.227360626
## 2016-02-29 -0.0605352209 -0.072344108 0.0169505524 0.003810669
## 2016-03-31 0.0717834363 0.082036309 0.0902267645 0.179948109
## 2016-04-29 0.1053453760 -0.102086864 -0.1270822750 0.046721797
## 2016-05-31 0.0915002899 0.067842821 0.1304025548 -0.075597968
## 2016-06-30 -0.0099694639 -0.035138912 -0.1144250835 -0.050296440
## 2016-07-29 0.0586021229 0.102268183 -0.0025174134 0.100785334
## 2016-08-31 0.0135476418 0.019880851 0.0657364022 -0.102058091
## 2016-09-30 0.0848953908 0.002433661 0.0112246699 -0.038366372
## 2016-10-31 -0.0583893058 0.039487672 0.2367091536 -0.031364583
## 2016-11-30 -0.0509721927 0.012390993 -0.0650992833 -0.043041267
## 2016-12-30 -0.0009330556 0.030721423 0.0564934501 0.120665178
## 2017-01-31 0.0936394059 0.039598271 0.1280336976 0.164624916
## 2017-02-28 0.0258446800 -0.004373311 0.0100410836 -0.007730364
## 2017-03-31 0.0479423007 0.028960719 0.0391854826 0.107278727
## 2017-04-28 0.0424566944 0.038718045 0.0292677767 0.120916212
## 2017-05-31 0.0725778018 0.025673256 0.0689841760 0.082295892
## 2017-06-30 -0.0271286156 -0.013115359 -0.0874853720 0.058654468
## 2017-07-31 0.0202278808 0.053249618 0.1954425989 -0.111459860
## 2017-08-31 -0.0072953953 0.033389072 -0.0390093333 0.095543446
## 2017-09-29 -0.0198260355 -0.003751518 0.0373014045 -0.042474144
## 2017-10-31 0.1395154056 0.110341626 0.0798771971 -0.028457409
## 2017-11-30 0.0626577318 0.016841407 -0.0461006656 -0.070862541
## 2017-12-29 -0.0062057845 0.016145386 0.0230816211 0.008061928
## 2018-01-31 0.2156265497 0.104998062 0.3422453562 0.129254571
## 2018-02-28 0.0415536279 -0.008450722 0.0750958659 -0.032266877
## 2018-03-29 -0.0440034760 -0.027022714 0.0135328404 -0.253920408
## 2018-04-30 0.0788803060 0.024353198 0.0563153192 0.099254576
## 2018-05-31 0.0397392430 0.059652092 0.1180177495 -0.031698139
## 2018-06-29 0.0421636787 -0.002329850 0.1073124981 0.186043257
## 2018-07-31 0.0446635734 0.073020998 -0.1483892939 -0.140021491
## 2018-08-31 0.1243079079 0.061088402 0.0857955874 0.011737389
## 2018-09-28 -0.0048359814 0.017997793 0.0173903689 -0.130439038
## 2018-10-31 -0.2258869989 -0.068387153 -0.2149050726 0.242170576
## 2018-11-30 0.0560700324 0.041797773 -0.0532520010 0.038271580
## 2018-12-31 -0.1180514843 -0.087790466 -0.0667287380 -0.051761952
## 2019-01-31 0.1348080312 0.027768710 0.2377564149 -0.080628789
## 2019-02-28 -0.0469930640 0.074511331 0.0533383470 0.041032981
## 2019-03-29 0.0824420184 0.051409328 -0.0043097723 -0.133656413
## 2019-04-30 0.0786806224 0.101963314 0.0384589024 -0.159123803
## 2019-05-31 -0.0818753491 -0.050746909 -0.0764149871 -0.253945372
## 2019-06-28 0.0646557767 0.079843673 0.0676869856 0.188012109
## 2019-07-31 -0.0142806686 0.017097053 -0.1286120899 0.078092373
## 2019-08-30 -0.0496880810 0.014924818 -0.0948922673 -0.068516948
## 2019-09-30 -0.0229951159 0.008450928 -0.0931610198 0.065449565
## 2019-10-31 0.0232034080 0.030739261 0.0713417349 0.268061253
## 2019-11-29 0.0134958216 0.057761228 0.0905829220 0.046592176
## 2019-12-31 0.0257863501 0.040901082 0.0279227940 0.237359743
## 2020-01-31 0.0834803026 0.076456352 0.0643897455 0.441578342
## 2020-02-28 -0.0642332026 -0.046765006 0.0670726963 0.026424253
## 2020-03-31 0.0344213022 -0.026900106 0.0173805330 -0.242781458
## 2020-04-30 0.2381504762 0.127800428 0.1116390480 0.400209535
## 2020-05-29 -0.0128673719 0.025074305 -0.0002858456 0.065730500
## 2020-06-30 0.1218341331 0.104863692 0.0807736724 0.257108657
## 2020-07-31 0.1372488933 0.007343711 0.0717317246 0.281420674
## 2020-08-31 0.0866005735 0.097808802 0.0799293979 0.554719320
## 2020-09-30 -0.0916533253 -0.069775432 -0.0573783743 -0.149762306
## 2020-10-30 -0.0364089187 -0.038086052 -0.0497966282 -0.100371771
## 2020-11-30 0.0425228214 0.058326011 0.0309615020 0.380308519
## 2020-12-31 0.0276719582 0.038264473 0.0970870744 0.217730753
## 2021-01-29 -0.0156985929 0.041997470 -0.0155437114 0.117343701
## 2021-02-26 -0.0359675607 0.004109449 0.0120608627 -0.161038203
## 2021-03-31 0.0003717151 0.014482799 -0.0324212105 -0.011269836
## 2021-04-30 0.1139202399 0.067286368 -0.0158244352 0.060292565
## 2021-05-28 -0.0730764715 -0.007656595 -0.0209791877 -0.126372343
## 2021-06-30 0.0651836137 0.081569659 0.0492816132 0.083547955
## 2021-07-30 -0.0332696301 0.050423671 -0.0203491777 0.010973846
## 2021-08-31 0.0421339363 0.059768611 0.0950695060 0.068224268
## 2021-09-30 -0.0550034409 -0.068406021 0.0698019211 0.052632612
## 2021-10-29 0.0262547651 0.162366217 0.1231245456 0.362230202
## 2021-11-30 0.0391473463 -0.001283055 -0.0727082022 0.027237848
## 2021-12-31 -0.0505062029 0.017184430 -0.0634444870 -0.079968440
## 2022-01-31 -0.1085098142 -0.078334646 -0.3438762168 -0.120597475
## 2022-02-28 0.0263230079 -0.037921943 -0.0794420544 -0.073397011
## 2022-03-31 0.0596239190 0.031364753 -0.0518377235 0.213504290
## 2022-04-29 -0.2711856801 -0.105212726 -0.6769150653 -0.213125289
## 2022-05-31 -0.0333130879 -0.018242701 0.0365177336 -0.138340062
## 2022-06-30 -0.1238178226 -0.056909528 -0.1213919403 -0.118657126
## 2022-07-29 0.2394860591 0.089014589 0.2516130190 0.280480149
## 2022-08-31 -0.0625299224 -0.068989103 -0.0059760083 -0.075250271
## 2022-09-30 -0.1149865758 -0.115710392 0.0517762908 -0.038313992
## 2022-10-31 -0.0981105335 -0.003311622 0.2148866613 -0.153346760
## 2022-11-30 -0.0593198454 0.097329043 0.0457051938 -0.155866121
## 2022-12-30 -0.1391406409 -0.061923784 -0.0354794590 -0.457813194
## 2023-01-31 0.2051735029 0.032773706 0.1823328028 0.340915767
## 2023-02-28 -0.0902516639 0.008977484 -0.0939461074 0.171904973
## 2023-03-31 0.0918019370 0.144863324 0.0699795803 0.008471140
## 2023-04-28 0.0206963031 0.063692868 -0.0460542977 -0.233183710
## 2023-05-31 0.1340765703 0.068691205 0.1805874132 0.216021889
## 2023-06-30 0.0779864104 0.036330672 0.1084198198 0.249689452
## 2023-07-31 0.0251489708 -0.013659548 -0.0034566451 0.021391609
## 2023-08-31 0.0318772770 -0.022476429 -0.0121241583 -0.035588260
## 2023-09-29 -0.0821945627 -0.037330893 -0.1384714676 -0.030929027
## 2023-10-31 0.0458940197 0.068420470 0.0864351944 -0.219831983
## 2023-11-30 0.0931972815 0.115955060 0.1408733862 0.178463656
## 2023-12-29 0.0392628771 -0.007603225 0.0268736650 0.034390133
## 2024-01-31 0.0212288659 0.055700698 0.1472315458 -0.282704160
## 2024-02-29 0.1300782611 0.041447426 0.0665352476 0.075015304
## 2024-03-28 0.0202729146 0.016971445 0.0072878392 -0.138383423
## 2024-04-30 -0.0302797899 -0.077540252 -0.0979910641 0.041724969
## 2024-05-31 0.0081949152 0.065966581 0.1529149558 -0.028782134
## 2024-06-28 0.0910037984 0.073855023 0.0505386840 0.105427913
## 2024-07-01 0.0202337392 0.021645672 -0.0018836178 0.058779834
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(0.25, 0.25, 0.3, 0.2))
## # A tibble: 1 × 4
## AMZN MSFT NFLX TSLA
## <dbl> <dbl> <dbl> <dbl>
## 1 0.2 0.116 0.395 0.289
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .3, .2)) %>%
# 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 = "Percent Contribution to Portfolio Volatility")
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 risk is definitely higher with Netflix and the others are lower by more then 10%. The risk is concentrated with Netflix with Tesla being the closest behind and Amazon and Microsoft being the safest by far.