# 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("RTX", "GD", "LMT", "BA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-11-30")
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
## BA GD LMT RTX
## 2013-01-31 -0.019969656 -0.0438219381 -0.060523019 0.0655971441
## 2013-02-28 0.046601642 0.0248767619 0.026184404 0.0394271648
## 2013-03-28 0.110096022 0.0366877199 0.092413359 0.0313101560
## 2013-04-30 0.062752861 0.0558799449 0.026278499 -0.0231712034
## 2013-05-31 0.085096793 0.0415789120 0.076580055 0.0443752111
## 2013-06-28 0.033955625 0.0158274108 0.024547224 -0.0208694231
## 2013-07-31 0.025634908 0.0928255792 0.102109852 0.1274201770
## 2013-08-30 -0.006749613 -0.0247951965 0.028337328 -0.0481828398
## 2013-09-30 0.122816954 0.0500192226 0.041050418 0.0742935535
## 2013-10-31 0.104935071 -0.0036090130 0.044393903 -0.0146683742
## 2013-11-29 0.031968121 0.0564402864 0.069938643 0.0479793294
## 2013-12-31 0.016547643 0.0415634107 0.048161201 0.0261743599
## 2014-01-31 -0.085859784 0.0643884475 0.015021769 0.0019315101
## 2014-02-28 0.034424753 0.0781045195 0.081066974 0.0312078726
## 2014-03-31 -0.026966072 -0.0056759926 0.005774923 -0.0015393015
## 2014-04-30 0.027741553 0.0106992580 0.005498026 0.0126714504
## 2014-05-30 0.052752638 0.0762333356 0.005249042 -0.0130108481
## 2014-06-30 -0.061128297 -0.0133808905 -0.018003882 -0.0066477234
## 2014-07-31 -0.054512691 0.0072196996 0.038088255 -0.0934497106
## 2014-08-29 0.057165146 0.0540085441 0.048803915 0.0321984298
## 2014-09-30 0.004563518 0.0306806411 0.049228405 -0.0222880297
## 2014-10-31 -0.019581021 0.1001818152 0.041736250 0.0131706417
## 2014-11-28 0.078746934 0.0392868158 0.013163877 0.0338432274
## 2014-12-31 -0.033142252 -0.0547172262 0.005258488 0.0437244577
## 2015-01-30 0.111901872 -0.0280595716 -0.022051845 -0.0019143199
## 2015-02-27 0.043169475 0.0409630511 0.067508098 0.0656397934
## 2015-03-31 -0.005117498 -0.0222225400 0.014441652 -0.0394011394
## 2015-04-30 -0.045948658 0.0167706727 -0.084041390 -0.0298787701
## 2015-05-29 -0.013481667 0.0204707279 0.016407584 0.0350909170
## 2015-06-30 -0.012892676 0.0157332456 -0.012296047 -0.0547263610
## 2015-07-31 0.038535837 0.0510427544 0.107992830 -0.1006340188
## 2015-08-31 -0.091881860 -0.0486459221 -0.021673285 -0.0842135298
## 2015-09-30 0.002064087 -0.0291470333 0.030015189 -0.0290164127
## 2015-10-30 0.122869654 0.0791497767 0.058639321 0.1006186138
## 2015-11-30 -0.011688730 -0.0143709604 0.004281713 -0.0178310300
## 2015-12-31 -0.005930304 -0.0641472914 -0.009213673 0.0002080551
## 2016-01-29 -0.185327771 -0.0212618445 -0.028730320 -0.0912693985
## 2016-02-29 -0.007154287 0.0185163663 0.029974927 0.1044061989
## 2016-03-31 0.071505653 -0.0366203517 0.026116855 0.0353839429
## 2016-04-29 0.060077525 0.0731574646 0.047951627 0.0417723333
## 2016-05-31 -0.058196609 0.0095613810 0.023286220 -0.0303708523
## 2016-06-30 0.029062237 -0.0130217129 0.049308162 0.0193971968
## 2016-07-29 0.028765316 0.0534849945 0.018207628 0.0485344007
## 2016-08-31 -0.023751806 0.0356427323 -0.032606791 -0.0053329284
## 2016-09-30 0.017535397 0.0191301769 -0.013466332 -0.0464439409
## 2016-10-31 0.078020458 -0.0239387701 0.027403419 0.0058881971
## 2016-11-30 0.063161004 0.1512275113 0.080665233 0.0587517237
## 2016-12-30 0.033440875 -0.0154596869 -0.059451916 0.0174845161
## 2017-01-31 0.048521251 0.0519429989 0.005546059 0.0004560417
## 2017-02-28 0.106475148 0.0470845784 0.065773077 0.0318548030
## 2017-03-31 -0.018875373 -0.0138461067 0.003818841 -0.0030254528
## 2017-04-28 0.044078221 0.0390823728 0.006889591 0.0586676397
## 2017-05-31 0.022661510 0.0476618702 0.048871491 0.0245137375
## 2017-06-30 0.052531751 -0.0256650099 -0.012600095 0.0068204288
## 2017-07-31 0.203833215 -0.0047587241 0.050981729 -0.0294189660
## 2017-08-31 -0.005668810 0.0252475120 0.050314875 0.0157071714
## 2017-09-29 0.058939426 0.0207906507 0.015917493 -0.0308760939
## 2017-10-31 0.014721322 -0.0087445774 -0.006888309 0.0312102389
## 2017-11-30 0.075774475 0.0203841351 0.041202752 0.0199843148
## 2017-12-29 0.063374882 -0.0180713262 0.006029545 0.0491626664
## 2018-01-31 0.183671608 0.0934534962 0.100098574 0.0786607010
## 2018-02-28 0.026806628 -0.0001349770 -0.001215326 -0.0184388564
## 2018-03-29 -0.099576285 -0.0069922823 -0.042041529 -0.0684944247
## 2018-04-30 0.017174997 -0.0885554810 -0.051896296 -0.0461115112
## 2018-05-31 0.059238784 0.0019850675 -0.013581859 0.0437572706
## 2018-06-29 -0.048433591 -0.0788825102 -0.062679298 0.0016810222
## 2018-07-31 0.060121062 0.0740828067 0.098772085 0.0821876681
## 2018-08-31 -0.033726041 -0.0323560461 -0.011369123 -0.0248872987
## 2018-09-28 0.081506964 0.0568824504 0.076721451 0.0597576919
## 2018-10-31 -0.046901637 -0.1662511716 -0.163253626 -0.1183107215
## 2018-11-30 -0.018483267 0.0689002063 0.029446206 -0.0135463100
## 2018-12-31 -0.072529760 -0.1621784264 -0.137480755 -0.1347511063
## 2019-01-31 0.178749274 0.0907753795 0.101077629 0.1033205143
## 2019-02-28 0.136842656 -0.0055655077 0.073034146 0.0683065058
## 2019-03-29 -0.142782636 -0.0055376026 -0.030351521 0.0252999828
## 2019-04-30 -0.009827545 0.0603067283 0.104817162 0.1011540293
## 2019-05-31 -0.094675263 -0.1055344745 0.021963665 -0.1159525500
## 2019-06-28 0.063511616 0.1227313693 0.071247211 0.0304118762
## 2019-07-31 -0.064771129 0.0279788529 -0.003775722 0.0257784555
## 2019-08-30 0.071211481 0.0282619473 0.064554424 -0.0195406731
## 2019-09-30 0.044006198 -0.0456764031 0.015371599 0.0470921994
## 2019-10-31 -0.112726402 -0.0272128087 -0.034904651 0.0504213612
## 2019-11-29 0.080262991 0.0275581179 0.043494754 0.0375828117
## 2019-12-31 -0.116964018 -0.0301066149 -0.004228667 0.0095267692
## 2020-01-31 -0.023260958 0.0004586031 0.094847853 0.0029339719
## 2020-02-28 -0.139796996 -0.0940622962 -0.139992147 -0.1351575384
## 2020-03-31 -0.612285508 -0.1880871485 -0.087298946 -0.3252634537
## 2020-04-30 -0.055983703 -0.0046368161 0.137880976 0.0877655179
## 2020-05-29 0.033676985 0.1169830401 0.004446676 0.0043342310
## 2020-06-30 0.228545434 0.0177534195 -0.062460573 -0.0459887242
## 2020-07-31 -0.148529138 -0.0109796018 0.037778982 -0.0835651818
## 2020-08-31 0.083852425 0.0176302991 0.035441443 0.0808684614
## 2020-09-30 -0.038927497 -0.0759280103 -0.018047286 -0.0583934385
## 2020-10-30 -0.135001996 -0.0450291897 -0.090461378 -0.0575878354
## 2020-11-30 0.377964849 0.1285791731 0.048562776 0.2851164644
## 2020-12-31 0.015773526 -0.0035549770 -0.027836000 -0.0029323295
## 2021-01-29 -0.097419269 -0.0072510593 -0.098069020 -0.0691825517
## 2021-02-26 0.087796348 0.1083760455 0.033495923 0.0820068489
## 2021-03-31 0.183531487 0.1049566929 0.112300725 0.0707783818
## 2021-04-30 -0.083519890 0.0531081619 0.029493173 0.0744225124
## 2021-05-28 0.052824305 -0.0016834707 0.011034467 0.0696477218
## 2021-06-30 -0.030665422 -0.0087262974 -0.010124454 -0.0390809932
## 2021-07-30 -0.056147276 0.0467848467 -0.017813311 0.0190415392
## 2021-08-31 -0.031304391 0.0215983651 -0.025250749 -0.0195251051
## 2021-09-30 0.002002558 -0.0215983651 -0.041714064 0.0140581448
## 2021-10-29 -0.060491086 0.0396929113 -0.037735911 0.0331801355
## 2021-11-30 -0.045354507 -0.0703853816 0.011244093 -0.0877521346
## 2021-12-31 0.017386519 0.0982068946 0.064168954 0.0615837080
## 2022-01-31 -0.005379045 0.0228936835 0.090641502 0.0468738893
## 2022-02-28 0.025150450 0.1001843982 0.115531355 0.1354378268
## 2022-03-31 -0.069779314 0.0283011866 0.017367863 -0.0359854393
## 2022-04-29 -0.252015932 -0.0142213119 -0.021248064 -0.0428975418
## 2022-05-31 -0.124625816 -0.0503747301 0.024555173 0.0081667445
## 2022-06-30 0.039688986 -0.0106444057 -0.023332444 0.0103541246
## 2022-07-29 0.152916931 0.0242018823 -0.038285274 -0.0306383600
## 2022-08-31 0.005883119 0.0099210615 0.021717956 -0.0320201522
## 2022-09-30 -0.280283595 -0.0760284443 -0.083928197 -0.0920174916
## 2022-10-31 0.162960637 0.1688468090 0.231018463 0.1469698844
## 2022-11-29 0.207200778 0.0056684435 -0.005315292 0.0330001789
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
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(0.35, 0.30, 0.20, 0.15))
## # A tibble: 1 × 4
## BA GD LMT RTX
## <dbl> <dbl> <dbl> <dbl>
## 1 0.525 0.22 0.126 0.128
plot_data_with_weight <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.35, 0.30, 0.20, 0.15)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add Weights
add_column(weight = c(0.35, 0.30, 0.20, 0.15)) %>%
# Transform Data
pivot_longer(cols = c(Contribution, weight), names_to = "type", values_to = "value")
plot_data_with_weight %>%
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 largest contributor to the portfolio volatility is BA or Boeing. The weight of BA is 35% but the contribution is over 50%. I think that the portfolios risk is concentrated on BA. The contribution of to the risk is greater than the weight of the stock. All of the other assets are the opposite.