# 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("SBUX", "AAPL", "VZ", "T", "TSLA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2023-03-22")
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 SBUX T TSLA VZ
## 2013-01-31 -1.555891e-01 0.045383758 0.0485142194 0.102078031 0.0194176904
## 2013-02-28 -2.561125e-02 -0.019138960 0.0316856609 -0.074128613 0.0648105983
## 2013-03-28 2.850440e-03 0.037571595 0.0214883479 0.084208141 0.0547798389
## 2013-04-30 2.710890e-04 0.066073525 0.0365642182 0.354111527 0.1028850223
## 2013-05-31 2.217181e-02 0.040473707 -0.0682111886 0.593716693 -0.1061642338
## 2013-06-28 -1.258959e-01 0.036848558 0.0116491839 0.093672182 0.0376483623
## 2013-07-31 1.321025e-01 0.084553140 0.0130895108 0.223739545 -0.0071421026
## 2013-08-30 8.044306e-02 -0.008016879 -0.0416848011 0.229971572 -0.0433680429
## 2013-09-30 -2.172370e-02 0.087519500 -0.0002957968 0.134706682 -0.0150986860
## 2013-10-31 9.201534e-02 0.051650527 0.0856859481 -0.189806650 0.0903406762
## 2013-11-29 6.770818e-02 0.008261582 -0.0277289697 -0.228409431 -0.0177775601
## 2013-12-31 8.862128e-03 -0.038415731 -0.0014212795 0.167108548 -0.0097207765
## 2014-01-31 -1.139490e-01 -0.097328119 -0.0361716123 0.187261770 -0.0122468553
## 2014-02-28 5.591805e-02 0.001524743 -0.0426116284 0.299722757 -0.0092050359
## 2014-03-31 1.975637e-02 0.033534797 0.0938000494 -0.160783192 -0.0002107844
## 2014-04-30 9.476088e-02 -0.038338096 0.0351144960 -0.002690159 -0.0067403620
## 2014-05-30 7.576539e-02 0.040101402 -0.0064637005 -0.000577395 0.0668363851
## 2014-06-30 2.728619e-02 0.054986910 -0.0031058420 0.144457218 -0.0208322246
## 2014-07-31 2.832654e-02 0.003869005 0.0235364675 -0.072372711 0.0407076370
## 2014-08-29 7.465139e-02 0.005031511 -0.0178601449 0.188794049 -0.0119708645
## 2014-09-30 -1.722059e-02 -0.030667371 0.0079771433 -0.105566506 0.0034061960
## 2014-10-31 6.948913e-02 0.001324468 0.0059733826 -0.004046457 0.0163201669
## 2014-11-28 1.007305e-01 0.076233576 0.0153806387 0.011599756 0.0067435274
## 2014-12-31 -7.460614e-02 0.010290485 -0.0519184742 -0.094774520 -0.0782983366
## 2015-01-30 5.961198e-02 0.064652336 -0.0014542872 -0.088365243 -0.0113776787
## 2015-02-27 9.601565e-02 0.069516235 0.0486165532 -0.001277805 0.0786450561
## 2015-03-31 -3.187423e-02 0.012859880 -0.0568519671 -0.074350086 -0.0167212781
## 2015-04-30 5.769585e-03 0.046020862 0.0781084660 0.180226844 0.0477696828
## 2015-05-29 4.434158e-02 0.050062807 -0.0028910884 0.103899535 -0.0200250787
## 2015-06-30 -3.793816e-02 0.031447794 0.0279779278 0.067300966 -0.0589443689
## 2015-07-31 -3.348118e-02 0.077313630 -0.0046543061 -0.007896618 0.0155626878
## 2015-08-31 -6.848893e-02 -0.054435438 -0.0453418600 -0.066366266 -0.0168108671
## 2015-09-30 -2.205759e-02 0.038193749 -0.0188512354 -0.002653542 -0.0558678429
## 2015-10-30 8.011215e-02 0.096045806 0.0470034614 -0.182659742 0.0875067785
## 2015-11-30 -5.821023e-03 -0.015806796 0.0047633601 0.106828579 -0.0309784909
## 2015-12-31 -1.167902e-01 -0.022402333 0.0217403033 0.041471546 0.0167996147
## 2016-01-29 -7.822363e-02 0.012251827 0.0653804515 -0.227360646 0.0902353829
## 2016-02-29 -1.288306e-03 -0.039776470 0.0243813707 0.003810669 0.0150946564
## 2016-03-31 1.197460e-01 0.025274938 0.0583456124 0.179948112 0.0639470489
## 2016-04-29 -1.507312e-01 -0.059881664 0.0073738629 0.046721799 -0.0493156801
## 2016-05-31 6.931435e-02 -0.020626600 0.0084648320 -0.075597977 -0.0007856406
## 2016-06-30 -4.359664e-02 0.039823319 0.0986715131 -0.050296472 0.0926278112
## 2016-07-29 8.623541e-02 0.016150248 0.0166183275 0.100785361 0.0023145917
## 2016-08-31 2.337657e-02 -0.028377750 -0.0572805941 -0.102058076 -0.0571903387
## 2016-09-30 6.344843e-02 -0.037877076 -0.0066266474 -0.038366402 -0.0067110091
## 2016-10-31 4.324839e-03 -0.019961518 -0.0827324756 -0.031364578 -0.0662370179
## 2016-11-30 -2.183752e-02 0.092935380 0.0488030640 -0.043041257 0.0367392847
## 2016-12-30 4.684067e-02 -0.043182173 0.0961806378 0.120665160 0.0674148740
## 2017-01-31 4.664164e-02 -0.005418212 0.0065903900 0.164624944 -0.0747770450
## 2017-02-28 1.255555e-01 0.033938698 -0.0088149796 -0.007730394 0.0125712835
## 2017-03-31 4.754148e-02 0.026376581 -0.0057594568 0.107278734 -0.0178903108
## 2017-04-28 -6.984301e-05 0.028199441 -0.0315583868 0.120916240 -0.0481612086
## 2017-05-31 6.560731e-02 0.061537502 -0.0281492293 0.082295867 0.0157753487
## 2017-06-30 -5.891613e-02 -0.086997155 -0.0209816869 0.058654469 -0.0433800074
## 2017-07-31 3.218063e-02 -0.077159670 0.0502712373 -0.111459838 0.0934513240
## 2017-08-31 1.016533e-01 0.020674771 -0.0402877287 0.095543417 -0.0089241403
## 2017-09-29 -6.213502e-02 -0.021185427 0.0446373581 -0.042474117 0.0311949277
## 2017-10-31 9.240374e-02 0.020820940 -0.1353418873 -0.028457431 -0.0213566049
## 2017-11-30 2.007514e-02 0.058198293 0.0780062670 -0.070862536 0.0611777068
## 2017-12-29 -1.536330e-02 -0.006768019 0.0664607885 0.008061927 0.0393036714
## 2018-01-31 -1.069352e-02 -0.010854281 -0.0200144877 0.129254601 0.0326349497
## 2018-02-28 6.596088e-02 0.010500985 -0.0311890216 -0.032266877 -0.1245099400
## 2018-03-29 -5.980345e-02 0.013740433 -0.0180683472 -0.253920406 0.0016745675
## 2018-04-30 -1.513403e-02 -0.005542800 -0.0676197244 0.099254538 0.0439978491
## 2018-05-31 1.267419e-01 -0.010541311 -0.0116887958 -0.031698143 -0.0346354907
## 2018-06-29 -9.462681e-03 -0.148490372 -0.0065188804 0.186043287 0.0539014602
## 2018-07-31 2.759882e-02 0.069961278 0.0160956515 -0.140021525 0.0376197084
## 2018-08-31 1.826732e-01 0.026968781 -0.0009387452 0.011737398 0.0515159242
## 2018-09-28 -8.337370e-03 0.061493574 0.0500715579 -0.130439016 -0.0181890232
## 2018-10-31 -3.095190e-02 0.024847155 -0.0707212185 0.242170573 0.0780255612
## 2018-11-30 -1.999124e-01 0.140772167 0.0180883034 0.038271593 0.0547032465
## 2018-12-31 -1.240883e-01 -0.035391310 -0.0903925702 -0.051761993 -0.0700594153
## 2019-01-31 5.368669e-02 0.056450628 0.0737120565 -0.080628787 -0.0104665202
## 2019-02-28 4.380286e-02 0.035843574 0.0346553765 0.041033018 0.0332230928
## 2019-03-29 9.260262e-02 0.056446834 0.0076825084 -0.133656450 0.0380917553
## 2019-04-30 5.490080e-02 0.043948435 0.0082301876 -0.159123800 -0.0231091879
## 2019-05-31 -1.326321e-01 -0.016446883 -0.0123495310 -0.253945344 -0.0509346237
## 2019-06-28 1.226769e-01 0.097268357 0.0914976858 0.188012089 0.0498849768
## 2019-07-31 7.361715e-02 0.121817682 0.0358979132 0.078092368 -0.0226281947
## 2019-08-30 -1.659830e-02 0.023339132 0.0349190706 -0.068516906 0.0509675828
## 2019-09-30 7.042260e-02 -0.088066255 0.0706176815 0.065449564 0.0371289995
## 2019-10-31 1.049766e-01 -0.044637148 0.0352119075 0.268061223 0.0121720400
## 2019-11-29 7.469373e-02 0.015222317 -0.0292625848 0.046592166 -0.0038108599
## 2019-12-31 9.420396e-02 0.028730058 0.0444752398 0.237359751 0.0190733586
## 2020-01-31 5.260196e-02 -0.035777937 -0.0204332162 0.441578346 -0.0220892807
## 2020-02-28 -1.218307e-01 -0.073793028 -0.0659214647 0.026424249 -0.0930248024
## 2020-03-31 -7.231399e-02 -0.176498943 -0.1891592615 -0.242781463 -0.0079712339
## 2020-04-30 1.444239e-01 0.154585574 0.0674420653 0.400209547 0.0776407182
## 2020-05-29 8.166664e-02 0.021922987 0.0127180277 0.065730498 -0.0012192602
## 2020-06-30 1.374867e-01 -0.058071524 -0.0206260349 0.257108653 -0.0400015649
## 2020-07-31 1.528342e-01 0.039173637 0.0011257197 0.281420680 0.0529100822
## 2020-08-31 1.960346e-01 0.104138825 0.0077457022 0.554719315 0.0306663162
## 2020-09-30 -1.081713e-01 0.017020091 -0.0445891330 -0.149762308 0.0037050181
## 2020-10-30 -6.188816e-02 0.012031581 -0.0294818479 -0.100371771 -0.0323414084
## 2020-11-30 9.120466e-02 0.124427787 0.0620606233 0.380308523 0.0582787557
## 2020-12-31 1.084717e-01 0.087470465 0.0003477459 0.217730754 -0.0278634279
## 2021-01-29 -5.516412e-03 -0.099891600 0.0187569484 0.117343701 -0.0598653848
## 2021-02-26 -8.306884e-02 0.113906888 -0.0261870484 -0.161038203 0.0099956156
## 2021-03-31 7.312886e-03 0.011412737 0.0818889434 -0.011269836 0.0502528804
## 2021-04-30 7.345281e-02 0.046665701 0.0594801428 0.060292565 0.0044909768
## 2021-05-28 -5.181659e-02 -0.001371344 -0.0651117595 -0.126372344 -0.0227519646
## 2021-06-30 9.449995e-02 -0.018344272 -0.0223339558 0.083547956 -0.0081764639
## 2021-07-30 6.295836e-02 0.082536970 -0.0016061746 0.010973846 0.0066994570
## 2021-08-31 4.161137e-02 -0.029110888 -0.0227159878 0.068224264 -0.0140822659
## 2021-09-30 -7.046188e-02 -0.063058610 -0.0150653907 0.052632613 -0.0181640791
## 2021-10-29 5.700151e-02 -0.039195376 -0.0414520849 0.362230204 -0.0072597270
## 2021-11-30 9.991927e-02 0.037405635 -0.1011466321 0.027237847 -0.0526948193
## 2021-12-31 7.160291e-02 0.064715212 0.0746708217 -0.079968439 0.0330657770
## 2022-01-31 -1.583684e-02 -0.173689956 0.0626544966 -0.120597477 0.0361241933
## 2022-02-28 -5.558235e-02 -0.063666672 -0.0736253840 -0.073397011 0.0082319779
## 2022-03-31 5.588254e-02 -0.008973562 -0.0025360541 0.213504290 -0.0522057476
## 2022-04-29 -1.021777e-01 -0.197853137 0.0694982463 -0.213125290 -0.0834035218
## 2022-05-31 -5.603707e-02 0.057440631 0.1211943210 -0.138340063 0.1023538453
## 2022-06-30 -8.493690e-02 -0.027246888 -0.0156217421 -0.118657123 -0.0105842309
## 2022-07-29 1.728044e-01 0.104207807 -0.0965919317 0.280480148 -0.0816456027
## 2022-08-31 -3.170532e-02 -0.002728261 -0.0683084442 -0.075250273 -0.0996277792
## 2022-09-30 -1.289443e-01 0.002257479 -0.1340202328 -0.038313991 -0.0963390621
## 2022-10-31 1.039558e-01 0.027276937 0.1902102104 -0.153346761 0.0007842793
## 2022-11-30 -3.358521e-02 0.171478126 0.0559997151 -0.155866119 0.0421803815
## 2022-12-30 -1.304191e-01 -0.029793700 -0.0461742820 -0.457813197 0.0107172396
## 2023-01-31 1.048293e-01 0.095493455 0.1155059544 0.340915768 0.0692114785
## 2023-02-28 2.291839e-02 -0.061778363 -0.0743723203 0.171904975 -0.0687007683
## 2023-03-21 7.744581e-02 -0.015299070 -0.0197603243 -0.040323868 -0.0279573469
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# 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(0.25, 0.25, 0.2, 0.2, 0.1)
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)
}
aasset_reutrns_wide_tbl %>% calculate_component_contribution(w = c(.25, .25, .2, .2, 1))
## # A tibble: 1 × 5
## AAPL SBUX T TSLA VZ
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.261 0.158 0.074 0.48 0.027
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, 1)) %>%
# Transform to long from
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(aaccuracy = 1)) +
scale_fill_tq() +
theme(plot.title = element_text(hjust = 0.5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight",
y = "Precent",
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 stock with the largest contribution to the portfolio volatility is TSLA, at almost 50%. I do think my portfolio risk is concentrated in TSLA.