# 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("FDX", "UPS", "MSFT")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2023-04-26")
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
## FDX MSFT UPS
## 2013-01-31 1.008258e-01 0.027327829 0.072702275
## 2013-02-28 3.848115e-02 0.020915229 0.048928033
## 2013-03-28 -6.974444e-02 0.028720381 0.038569152
## 2013-04-30 -4.360490e-02 0.145776986 -0.000698945
## 2013-05-31 2.448250e-02 0.059941068 0.007687111
## 2013-06-28 2.452437e-02 -0.010368847 0.006729511
## 2013-07-31 7.257088e-02 -0.081394365 0.003693317
## 2013-08-30 1.274870e-02 0.054854594 -0.007040038
## 2013-09-30 6.235141e-02 -0.003599652 0.065465546
## 2013-10-31 1.380343e-01 0.062037949 0.072496359
## 2013-11-29 5.711609e-02 0.081562093 0.047433148
## 2013-12-31 3.697507e-02 -0.019063286 0.026030364
## 2014-01-31 -7.546252e-02 0.011428878 -0.098426893
## 2014-02-28 7.506482e-05 0.019814957 0.012718951
## 2014-03-31 -4.698964e-03 0.067617054 0.016671000
## 2014-04-30 2.745605e-02 -0.014498455 0.011435647
## 2014-05-30 5.643235e-02 0.020307798 0.059767425
## 2014-06-30 5.030488e-02 0.018393322 -0.011813268
## 2014-07-31 -3.017755e-02 0.034413543 -0.055784708
## 2014-08-29 6.785150e-03 0.057484626 0.009427838
## 2014-09-30 8.910002e-02 0.020264178 0.009814814
## 2014-10-31 3.619081e-02 0.012646184 0.065180657
## 2014-11-28 6.240793e-02 0.024439373 0.052837528
## 2014-12-31 -2.457544e-02 -0.028858152 0.011307562
## 2015-01-30 -2.655010e-02 -0.139546761 -0.117558130
## 2015-02-27 4.548737e-02 0.089035996 0.035959081
## 2015-03-31 -6.621232e-02 -0.075529808 -0.048230243
## 2015-04-30 2.459710e-02 0.179201562 0.036364103
## 2015-05-29 2.129652e-02 -0.030804045 -0.005748103
## 2015-06-30 -1.504432e-02 -0.059571325 -0.023556718
## 2015-07-31 5.967933e-03 0.056151026 0.054713508
## 2015-08-31 -1.294230e-01 -0.063951002 -0.039981204
## 2015-09-30 -4.333563e-02 0.016861056 0.010594163
## 2015-10-30 8.050212e-02 0.173394730 0.042939344
## 2015-11-30 1.583079e-02 0.038685971 0.006893000
## 2015-12-31 -6.042667e-02 0.020578018 -0.068085060
## 2016-01-29 -1.144326e-01 -0.007054279 -0.031993410
## 2016-02-29 2.965846e-02 -0.072344026 0.043279714
## 2016-03-31 1.746884e-01 0.082036185 0.088365601
## 2016-04-29 1.458115e-02 -0.102086642 -0.003799689
## 2016-05-31 -8.483732e-04 0.067842489 -0.011381668
## 2016-06-30 -8.083502e-02 -0.035138645 0.043932747
## 2016-07-29 6.454695e-02 0.102267968 0.003521475
## 2016-08-31 1.854200e-02 0.019880978 0.017365643
## 2016-09-30 5.985891e-02 0.002433549 0.001281041
## 2016-10-31 -2.063113e-03 0.039487972 -0.014738786
## 2016-11-30 9.488248e-02 0.012390693 0.079980583
## 2016-12-30 -2.691627e-02 0.030721560 -0.011103533
## 2017-01-31 1.550776e-02 0.039598132 -0.049256746
## 2017-02-28 2.025748e-02 -0.004373225 -0.023725061
## 2017-03-31 1.325755e-02 0.028960839 0.014456239
## 2017-04-28 -2.832487e-02 0.038718152 0.001489823
## 2017-05-31 2.158934e-02 0.025672915 -0.006034092
## 2017-06-30 1.167570e-01 -0.013115503 0.042673687
## 2017-07-31 -4.373482e-02 0.053249709 -0.002716348
## 2017-08-31 3.006792e-02 0.033388885 0.043649274
## 2017-09-29 5.326779e-02 -0.003751359 0.048889901
## 2017-10-31 1.019026e-03 0.110341983 -0.021547641
## 2017-11-30 2.471313e-02 0.016840932 0.040210893
## 2017-12-29 7.730374e-02 0.016145543 -0.019119382
## 2018-01-31 5.055551e-02 0.104998162 0.066320325
## 2018-02-28 -6.317783e-02 -0.008450883 -0.189940672
## 2018-03-29 -2.381849e-02 -0.027022736 0.002391317
## 2018-04-30 2.910074e-02 0.024353311 0.081085815
## 2018-05-31 7.737133e-03 0.059652135 0.030657090
## 2018-06-29 -9.008254e-02 -0.002329801 -0.089017528
## 2018-07-31 7.958848e-02 0.073020904 0.120967824
## 2018-08-31 -7.839537e-03 0.061088496 0.032175842
## 2018-09-28 -1.039427e-02 0.017997685 -0.051173220
## 2018-10-31 -8.875357e-02 -0.068387221 -0.091514458
## 2018-11-30 3.855026e-02 0.041797705 0.087203582
## 2018-12-31 -3.472432e-01 -0.087790111 -0.167290856
## 2019-01-31 9.591308e-02 0.027768484 0.077602762
## 2019-02-28 1.913228e-02 0.074511474 0.053270940
## 2019-03-29 6.029915e-03 0.051409297 0.013877754
## 2019-04-30 4.341847e-02 0.101963206 -0.050662031
## 2019-05-31 -2.054090e-01 -0.050746857 -0.124252510
## 2019-06-28 6.612005e-02 0.079843829 0.105607894
## 2019-07-31 3.788720e-02 0.017096761 0.145718413
## 2019-08-30 -7.246312e-02 0.014924820 0.001660977
## 2019-09-30 -8.182913e-02 0.008451084 0.009728587
## 2019-10-31 4.755613e-02 0.030738862 -0.039581334
## 2019-11-29 4.727321e-02 0.057761688 0.046596897
## 2019-12-31 -5.258529e-02 0.040901379 -0.022552723
## 2020-01-31 -4.442182e-02 0.076455989 -0.122921822
## 2020-02-28 -2.428302e-02 -0.046764762 -0.124665477
## 2020-03-31 -1.470126e-01 -0.026900196 0.031866284
## 2020-04-30 4.443722e-02 0.127800379 0.013186120
## 2020-05-29 2.945859e-02 0.025074178 0.062407765
## 2020-06-30 7.616668e-02 0.104863877 0.108884498
## 2020-07-31 1.831295e-01 0.007343516 0.250014432
## 2020-08-31 2.665579e-01 0.097808776 0.142732030
## 2020-09-30 1.374785e-01 -0.069775341 0.018229214
## 2020-10-30 3.111862e-02 -0.038086135 -0.058829803
## 2020-11-30 9.937667e-02 0.058326127 0.091206416
## 2020-12-31 -9.656189e-02 0.038264301 -0.015730791
## 2021-01-29 -9.818759e-02 0.041997469 -0.082917125
## 2021-02-26 7.826953e-02 0.004109586 0.024404852
## 2021-03-31 1.124145e-01 0.014482850 0.074221413
## 2021-04-30 2.183421e-02 0.067286296 0.181693726
## 2021-05-28 8.101997e-02 -0.007656523 0.056121347
## 2021-06-30 -5.129634e-02 0.081569709 -0.031381894
## 2021-07-30 -6.358919e-02 0.050423497 -0.083237331
## 2021-08-31 -5.228084e-02 0.059768766 0.027338593
## 2021-09-30 -1.891109e-01 -0.068406103 -0.071669071
## 2021-10-29 7.144332e-02 0.162366274 0.158940268
## 2021-11-30 -2.215163e-02 -0.001283013 -0.068433822
## 2021-12-31 1.188132e-01 0.017184163 0.077429530
## 2022-01-31 -5.067485e-02 -0.078334330 -0.058256645
## 2022-02-28 -1.008694e-01 -0.037922168 0.047038458
## 2022-03-31 4.360638e-02 0.031364809 0.019017836
## 2022-04-29 -1.521071e-01 -0.105212880 -0.175277619
## 2022-05-31 1.222345e-01 -0.018242375 0.021078366
## 2022-06-30 1.449347e-02 -0.056909847 0.001589943
## 2022-07-29 2.775288e-02 0.089014717 0.065466012
## 2022-08-31 -1.004673e-01 -0.068989078 0.005511283
## 2022-09-30 -3.451043e-01 -0.115710390 -0.185730810
## 2022-10-31 7.653932e-02 -0.003311601 0.037841237
## 2022-11-30 1.282925e-01 0.097329063 0.132319029
## 2022-12-30 -4.405578e-02 -0.061923768 -0.087466671
## 2023-01-31 1.126893e-01 0.032773659 0.063462889
## 2023-02-28 4.715292e-02 0.008977476 -0.006133668
## 2023-03-31 1.227521e-01 0.144863349 0.061111233
## 2023-04-25 -1.330535e-02 -0.045704311 -0.095676311
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.5, 0.3, 0.2))
## # A tibble: 1 × 3
## FDX MSFT UPS
## <dbl> <dbl> <dbl>
## 1 0.634 0.185 0.181
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.5, 0.3, 0.2)) %>%
# transform to long from
pivot_longer(cols = everything(),names_to = "Asset", values_to = "Contribution") %>%
# add weights
add_column(weight = c(0.5, 0.3, 0.2)) %>%
# 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 Volitility 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?
Fed EX (FDX) is the largest contributor to portoflio volitility. After looking at the graph I feel as though Fed Ex has to much weight and could pose a risk.