# 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("UAL", "AAL", "DAL", "LUV")
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
## AAL DAL LUV UAL
## 2013-01-31 0.0561700369 0.157155300 0.0905049513 0.0324034442
## 2013-02-28 -0.0613690477 0.026989978 0.0427826480 0.1007536285
## 2013-03-28 0.2339561096 0.145806695 0.1424490257 0.1810103087
## 2013-04-30 -0.0041333663 0.037449232 0.0161889640 0.0090189051
## 2013-05-31 0.0388792121 0.049511938 0.0337312422 0.0049413273
## 2013-06-28 -0.0676930180 0.038130998 -0.0918484876 -0.0367099469
## 2013-07-31 0.1641926331 0.126357208 0.0703880638 0.1077545065
## 2013-08-30 -0.1801533516 -0.070410824 -0.0737709500 -0.2025535329
## 2013-09-30 0.1597923803 0.178682471 0.1280521201 0.0760887441
## 2013-10-31 0.1473462243 0.111783136 0.1677933061 0.1003005718
## 2013-11-29 0.0664710557 0.096249637 0.0765522180 0.1450625678
## 2013-12-31 0.0726775425 -0.053511376 0.0155235087 -0.0368489572
## 2014-01-31 0.2842105035 0.108231818 0.1061561161 0.1920545887
## 2014-02-28 0.0959872613 0.083466796 0.0687065639 -0.0193838905
## 2014-03-31 -0.0089758542 0.042446880 0.0526204773 -0.0073668826
## 2014-04-30 -0.0427020557 0.061015070 0.0234416847 -0.0880100311
## 2014-05-30 0.1355254823 0.081907174 0.0901443732 0.0821672699
## 2014-06-30 0.0673977508 -0.030270603 0.0176526597 -0.0772856208
## 2014-07-31 -0.0980297012 -0.033082532 0.0515167545 0.1218059851
## 2014-08-29 0.0007719221 0.057537592 0.1258420832 0.0259589304
## 2014-09-30 -0.0915106411 -0.090646738 0.0535248036 -0.0173733116
## 2014-10-31 0.1556097392 0.106935847 0.0208063093 0.1210310683
## 2014-11-28 0.1601099995 0.150617211 0.1929956351 0.1479366653
## 2014-12-31 0.0999265214 0.052588948 0.0133098939 0.0884122120
## 2015-01-30 -0.0886571715 -0.038968651 0.0653948608 0.0364050670
## 2015-02-27 -0.0223121824 -0.058770496 -0.0438886212 -0.0623018589
## 2015-03-31 0.0970167125 0.009834605 0.0255764267 0.0312643404
## 2015-04-30 -0.0868597877 -0.007143027 -0.0882026644 -0.1184151776
## 2015-05-29 -0.1307841254 -0.037239897 -0.0905140430 -0.0901511223
## 2015-06-30 -0.0590619737 -0.043816384 -0.1110105927 -0.0293701753
## 2015-07-31 0.0039978736 0.076365534 0.0898281197 0.0618112837
## 2015-08-31 -0.0259971402 -0.009793317 0.0155733448 0.0102330079
## 2015-09-30 -0.0038553805 0.024592494 0.0358617769 -0.0712899842
## 2015-10-30 0.1742193585 0.124914180 0.1962875254 0.1282630996
## 2015-11-30 -0.1113651182 -0.087441198 -0.0088966466 -0.0789793569
## 2015-12-31 0.0260750829 0.087136742 -0.0619209773 0.0277820170
## 2016-01-29 -0.0826631924 -0.134969749 -0.1350584113 -0.1712832395
## 2016-02-29 0.0529917844 0.088596508 0.1089422980 0.1705848994
## 2016-03-31 0.0002436782 0.009079579 0.0675193005 0.0444062423
## 2016-04-29 -0.1673645134 -0.155486974 -0.0042501673 -0.2675060747
## 2016-05-31 -0.0806451241 0.045275951 -0.0489247785 -0.0158419421
## 2016-06-30 -0.1197044972 -0.176448192 -0.0776998853 -0.0941132726
## 2016-07-29 0.2263176839 0.061738095 -0.0577439066 0.1332571969
## 2016-08-31 0.0252737969 -0.047513262 -0.0008035226 0.0723851450
## 2016-09-30 0.0085034728 0.068611855 0.0530677748 0.0400520359
## 2016-10-31 0.1034469140 0.059428549 0.0293914403 0.0692088132
## 2016-11-30 0.1369143056 0.147367533 0.1516867066 0.2039311458
## 2016-12-30 0.0053689698 0.020746330 0.0691294232 0.0554326553
## 2017-01-31 -0.0536746333 -0.040449395 0.0483698520 -0.0336271106
## 2017-02-28 0.0488032399 0.059353079 0.0998012156 0.0500934163
## 2017-03-31 -0.0916498999 -0.082850618 -0.0707205545 -0.0476839733
## 2017-04-28 0.0075366148 -0.011378538 0.0447429137 -0.0061058097
## 2017-05-31 0.1295338363 0.082185577 0.0665709005 0.1264023424
## 2017-06-30 0.0386961612 0.089687829 0.0356195586 -0.0570771768
## 2017-07-31 0.0023819432 -0.085017025 -0.1128267246 -0.1060251838
## 2017-08-31 -0.1178902618 -0.038520962 -0.0602934352 -0.0883017190
## 2017-09-29 0.0596512222 0.021591939 0.0710623024 -0.0175842697
## 2017-10-31 -0.0142086231 0.036848902 -0.0386065203 -0.0402199235
## 2017-11-30 0.0776519747 0.062360653 0.1190612720 0.0795164226
## 2017-12-29 0.0300452196 0.056570188 0.0778645728 0.0624438104
## 2018-01-31 0.0430718173 0.013656488 -0.0736969208 0.0062120916
## 2018-02-28 0.0006317662 -0.046123773 -0.0499092636 -0.0004424272
## 2018-03-29 -0.0431288198 0.016742307 -0.0075857111 0.0244803173
## 2018-04-30 -0.1909033378 -0.048407144 -0.0808720856 -0.0281749938
## 2018-05-31 0.0164453594 0.040370026 -0.0336861940 0.0299007977
## 2018-06-29 -0.1371472201 -0.087129246 -0.0008204536 0.0020098631
## 2018-07-31 0.0407798634 0.100747080 0.1337279274 0.1423835058
## 2018-08-31 0.0261014130 0.071952928 0.0551814454 0.0837098733
## 2018-09-28 0.0207805898 -0.011177121 0.0185862729 0.0185862040
## 2018-10-31 -0.1639575987 -0.055095675 -0.2405072306 -0.0406769193
## 2018-11-30 0.1379968137 0.109960474 0.1063581019 0.1229800178
## 2018-12-31 -0.2237039664 -0.196087242 -0.1582476969 -0.1440159946
## 2019-01-31 0.1079419095 -0.009463675 0.1998095985 0.0414093464
## 2019-02-28 -0.0011988528 0.009994569 -0.0127661017 0.0061686389
## 2019-03-29 -0.1149804276 0.040902664 -0.0735774660 -0.0959025304
## 2019-04-30 0.0734330219 0.120940317 0.0437227940 0.1077892732
## 2019-05-31 -0.2244394985 -0.117826228 -0.1304015571 -0.1348505351
## 2019-06-28 0.1802998345 0.097073978 0.0684434491 0.1199985243
## 2019-07-31 -0.0665646549 0.079281869 0.0146616077 0.0485997836
## 2019-08-30 -0.1445940948 -0.053503201 0.0187754317 -0.0863094242
## 2019-09-30 0.0247760327 -0.004503586 0.0317906183 0.0474846739
## 2019-10-31 0.1084701853 -0.037334416 0.0385011280 0.0271145501
## 2019-11-29 -0.0416692299 0.039688402 0.0265463030 0.0213469958
## 2019-12-31 -0.0020898344 0.020209608 -0.0623528414 -0.0520876951
## 2020-01-31 -0.0663067000 -0.047986715 0.0183558232 -0.1635410518
## 2020-02-28 -0.3391375822 -0.182347777 -0.1742062339 -0.1943184036
## 2020-03-31 -0.4464511525 -0.480507312 -0.2562955207 -0.6689259574
## 2020-04-30 -0.0148762539 -0.096327169 -0.1306069851 -0.0644751101
## 2020-05-29 -0.1343643980 -0.027388149 0.0268364813 -0.0534663599
## 2020-06-30 0.2189442471 0.106747762 0.0627845888 0.2105105805
## 2020-07-31 -0.1615742258 -0.116313567 -0.1012080512 -0.0979719547
## 2020-08-31 0.1600428699 0.211461662 0.1960395545 0.1373482179
## 2020-09-30 -0.0600022279 -0.008790419 -0.0021311898 -0.0353393664
## 2020-10-30 -0.0857546981 0.001960051 0.0527190224 -0.0259451773
## 2020-11-30 0.2252689824 0.272803688 0.1589455439 0.2855385724
## 2020-12-31 0.1098092252 -0.000994365 0.0058096372 -0.0407757337
## 2021-01-29 0.0850542494 -0.057582895 -0.0589900696 -0.0783677685
## 2021-02-26 0.1984975516 0.233417369 0.2798568166 0.2756064178
## 2021-03-31 0.1322172120 0.007067224 0.0491752502 0.0882444914
## 2021-04-30 -0.0956449795 -0.028573357 0.0277794721 -0.0561161921
## 2021-05-28 0.1097706883 0.016068089 -0.0212499722 0.0700951507
## 2021-06-30 -0.1335314263 -0.097283695 -0.1463979785 -0.1096541669
## 2021-07-30 -0.0399187388 -0.080852017 -0.0496193507 -0.1126328011
## 2021-08-31 -0.0218261953 0.013442871 -0.0147559214 -0.0045050575
## 2021-09-30 0.0286722513 0.052269737 0.0326083677 0.0225349969
## 2021-10-29 -0.0664897238 -0.085199500 -0.0841342240 -0.0305220645
## 2021-11-30 -0.0819107804 -0.077830400 -0.0628478874 -0.0878392670
## 2021-12-31 0.0151474735 0.076551710 -0.0357672418 0.0353360917
## 2022-01-31 -0.0866065094 0.015488309 0.0438425063 -0.0207715225
## 2022-02-28 0.0462716410 0.005778295 -0.0216809090 0.0348339621
## 2022-03-31 0.0563529366 -0.008806163 0.0446500722 0.0431975278
## 2022-04-29 0.0280947950 0.083826364 0.0198883968 0.0855363046
## 2022-05-31 -0.0491364989 -0.031636429 -0.0185789543 -0.0585104990
## 2022-06-30 -0.3430974031 -0.364000385 -0.2387465078 -0.2961862566
## 2022-07-29 0.0780995233 0.093205503 0.0538921031 0.0368616450
## 2022-08-31 -0.0539456833 -0.023223656 -0.0379622306 -0.0485045386
## 2022-09-30 -0.0759453764 -0.101897589 -0.1739642961 -0.0734709841
## 2022-10-31 0.1635981059 0.189954575 0.1643817777 0.2808961918
## 2022-11-30 0.0174768513 0.041564257 0.0934328102 0.0249868894
## 2022-12-30 -0.1261338150 -0.073607593 -0.1700199437 -0.1583856695
## 2023-01-31 0.2381250461 0.173866243 0.0656469025 0.2613435048
## 2023-02-28 -0.0099627133 -0.019628840 -0.0631790980 0.0594705547
## 2023-03-31 -0.0800948289 -0.093434004 -0.0261144511 -0.1606188027
## 2023-04-28 -0.0782364052 -0.017622724 -0.0716524832 -0.0102215715
## 2023-05-31 0.0802682199 0.057206907 -0.0139630192 0.0804641583
## 2023-06-30 0.1937579889 0.268927625 0.1977498911 0.1448687733
## 2023-07-31 -0.0686346281 -0.025195826 -0.0582799086 -0.0102583343
## 2023-08-31 -0.1285120589 -0.075871942 -0.0778981340 -0.0864926027
## 2023-09-29 -0.1396600507 -0.147487619 -0.1489857609 -0.1634287266
## 2023-10-31 -0.1387866850 -0.166103753 -0.1969833419 -0.1891533810
## 2023-11-30 0.1086734664 0.167004798 0.1399770078 0.1181321680
## 2023-12-29 0.1001983401 0.085588885 0.1278457523 0.0461276119
## 2024-01-31 0.0350411098 -0.027468061 0.0343746807 0.0029042312
## 2024-02-29 0.0970336545 0.079309661 0.1367462411 0.0946948603
## 2024-03-28 -0.0212705355 0.124411354 -0.1551530166 0.0512053445
## 2024-04-30 -0.1276853300 0.044933094 -0.1180399783 0.0721068861
## 2024-05-31 -0.1610831335 0.020698578 0.0341071035 0.0292984591
## 2024-06-28 -0.0148929671 -0.072752007 0.0701391574 -0.0852459140
## 2024-07-31 -0.0628335521 -0.094320730 -0.0601441374 -0.0689048379
## 2024-08-30 -0.0018815111 -0.012396332 0.0709211773 -0.0308541637
## 2024-09-30 0.0567398191 0.178430757 0.0303577943 0.2590050372
## 2024-10-31 0.1757758544 0.122150650 0.0315588707 0.3159332742
## 2024-11-29 0.0802723624 0.109163083 0.0565768711 0.2129202385
## 2024-12-31 0.1826658361 -0.053423214 0.0434520068 0.0027844770
## 2025-01-31 -0.0296965184 0.106070918 -0.0905328209 0.0861871256
## 2025-02-28 -0.1647463899 -0.109937717 0.0113325135 -0.1206570511
## 2025-03-31 -0.3076240908 -0.321285382 0.0839231799 -0.3064405101
## 2025-04-30 -0.0585533460 -0.046236096 -0.1831557790 -0.0033365281
## 2025-05-30 0.1369176185 0.153409576 0.1771820037 0.1435076545
## 2025-06-24 -0.0035118528 0.023890916 -0.0408639290 -0.0053011080
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(.4, .3, .2, .1))
## # A tibble: 1 × 4
## AAL DAL LUV UAL
## <dbl> <dbl> <dbl> <dbl>
## 1 0.452 0.288 0.151 0.108
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?
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.4, .3, .2, .1)) %>%
# Transform to long form
pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.4, .3, .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)) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_tq() +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight", y = "Percent",
x = NULL)
AAL contributes the most to volatility. Its volatility is over 40%, with its contribution outweighing the set weight. Volatility is concentrated in AAL, being much higher than the next-leading contributor, DAL (below 30%). LUV and UAL seem to be the least volatile.