# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG” from 2012-12-31 to 2017-12-31
symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-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
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062314447 -0.0029358074 0.0366064445 0.052133371 4.992292e-02
## 2013-02-28 0.0058912352 -0.0231050865 -0.0129695790 0.016175735 1.267868e-02
## 2013-03-28 0.0009851786 -0.0102348139 0.0129695790 0.040258026 3.726727e-02
## 2013-04-30 0.0096393156 0.0120847573 0.0489674772 0.001222315 1.903041e-02
## 2013-05-31 -0.0202141304 -0.0494836985 -0.0306553180 0.041976135 2.333540e-02
## 2013-06-28 -0.0157781614 -0.0547282506 -0.0271446453 -0.001402794 -1.343474e-02
## 2013-07-31 0.0026876946 0.0131597901 0.0518604521 0.063541364 5.038651e-02
## 2013-08-30 -0.0082977621 -0.0257054581 -0.0197463254 -0.034743527 -3.045138e-02
## 2013-09-30 0.0111431216 0.0695887396 0.0753384250 0.063873903 3.115587e-02
## 2013-10-31 0.0082926095 0.0408611570 0.0320815933 0.034233867 4.526626e-02
## 2013-11-29 -0.0025098782 -0.0025942099 0.0054496821 0.041661011 2.920724e-02
## 2013-12-31 -0.0055836075 -0.0040741683 0.0215280270 0.012892173 2.559627e-02
## 2014-01-31 0.0152917331 -0.0903226388 -0.0534133204 -0.035775201 -3.588492e-02
## 2014-02-28 0.0037569414 0.0332206284 0.0595050544 0.045257270 4.451074e-02
## 2014-03-31 -0.0014813577 0.0380218532 -0.0046025089 0.013315560 8.261279e-03
## 2014-04-30 0.0081829001 0.0077725282 0.0165293759 -0.023184184 6.927490e-03
## 2014-05-30 0.0117217321 0.0290910804 0.0158282324 0.006205094 2.294125e-02
## 2014-06-30 -0.0005753327 0.0237341897 0.0091655973 0.037718813 2.043407e-02
## 2014-07-31 -0.0025123680 0.0135553791 -0.0263796564 -0.052009482 -1.352856e-02
## 2014-08-29 0.0114307645 0.0279048146 0.0018004100 0.043657934 3.870474e-02
## 2014-09-30 -0.0061672464 -0.0808566437 -0.0395984610 -0.061260759 -1.389195e-02
## 2014-10-31 0.0105842754 0.0140964700 -0.0026548295 0.068874977 2.327766e-02
## 2014-11-28 0.0065491443 -0.0155413060 0.0006251507 0.004773810 2.710114e-02
## 2014-12-31 0.0014748859 -0.0404424143 -0.0407467370 0.025295778 -2.539361e-03
## 2015-01-30 0.0203147836 -0.0068955068 0.0062266885 -0.054628026 -3.007728e-02
## 2015-02-27 -0.0089878307 0.0431360303 0.0614504843 0.056914546 5.468184e-02
## 2015-03-31 0.0037398990 -0.0150864600 -0.0143887809 0.010156422 -1.583022e-02
## 2015-04-30 -0.0032329198 0.0662815357 0.0358165271 -0.018417764 9.786009e-03
## 2015-05-29 -0.0043832358 -0.0419107653 0.0019525568 0.007509947 1.277413e-02
## 2015-06-30 -0.0108254491 -0.0297471701 -0.0316786729 0.004171401 -2.052151e-02
## 2015-07-31 0.0085851245 -0.0651778348 0.0201145268 -0.027375637 2.233812e-02
## 2015-08-31 -0.0033645885 -0.0925122992 -0.0771525052 -0.047268287 -6.288664e-02
## 2015-09-30 0.0080816582 -0.0318250341 -0.0451948440 -0.038464602 -2.584730e-02
## 2015-10-30 0.0006852809 0.0618083383 0.0640259413 0.063589762 8.163515e-02
## 2015-11-30 -0.0038980491 -0.0255603460 -0.0075558689 0.024415008 3.648358e-03
## 2015-12-31 -0.0019192890 -0.0389470954 -0.0235949512 -0.052156904 -1.743350e-02
## 2016-01-29 0.0123299645 -0.0516366428 -0.0567578043 -0.060306861 -5.106882e-02
## 2016-02-29 0.0088315667 -0.0082117316 -0.0339140803 0.020605401 -8.260311e-04
## 2016-03-31 0.0087093526 0.1218790109 0.0637460342 0.089910359 6.509993e-02
## 2016-04-29 0.0025459438 0.0040792910 0.0219748194 0.021044188 3.933497e-03
## 2016-05-31 0.0001360958 -0.0376287589 -0.0008561906 0.004397029 1.686854e-02
## 2016-06-30 0.0191661128 0.0445825552 -0.0244914120 0.008292354 3.469884e-03
## 2016-07-29 0.0054294404 0.0524424851 0.0390002525 0.049348286 3.582208e-02
## 2016-08-31 -0.0021560705 0.0087982188 0.0053267535 0.011261075 1.196632e-03
## 2016-09-30 0.0005158869 0.0248729848 0.0132790432 0.008614744 5.791795e-05
## 2016-10-31 -0.0082047578 -0.0083122584 -0.0224035120 -0.038134683 -1.748892e-02
## 2016-11-30 -0.0259894961 -0.0451617326 -0.0179744554 0.125246397 3.617609e-02
## 2016-12-30 0.0025374568 -0.0025301331 0.0267028261 0.031491513 2.006900e-02
## 2017-01-31 0.0021261847 0.0644313570 0.0323818299 -0.012143914 1.773636e-02
## 2017-02-28 0.0064378176 0.0172580800 0.0118364549 0.013428928 3.853927e-02
## 2017-03-31 -0.0005525506 0.0361887280 0.0318059025 -0.006533295 1.249073e-03
## 2017-04-28 0.0090290028 0.0168665520 0.0239521045 0.005107904 9.877278e-03
## 2017-05-31 0.0068473361 0.0280598440 0.0348103271 -0.022862479 1.401442e-02
## 2017-06-30 -0.0001829040 0.0092239356 0.0029559201 0.029151582 6.354619e-03
## 2017-07-31 0.0033349668 0.0565944167 0.0261878629 0.007481756 2.034574e-02
## 2017-08-31 0.0093688884 0.0232439051 -0.0004484447 -0.027564686 2.913657e-03
## 2017-09-29 -0.0057322923 -0.0004463692 0.0233428366 0.082321616 1.994895e-02
## 2017-10-31 0.0009781554 0.0322785151 0.0166537655 0.005916133 2.329071e-02
## 2017-11-30 -0.0014838960 -0.0038970436 0.0068699320 0.036913275 3.010819e-02
## 2017-12-29 0.0047400570 0.0369254672 0.0133981663 -0.003731260 1.205512e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398278e-05 0.0001042106 4.178322e-05 -7.811911e-05 -9.029489e-06
## EEM 1.042106e-04 0.0017547113 1.039018e-03 6.437738e-04 6.795430e-04
## EFA 4.178322e-05 0.0010390179 1.064237e-03 6.490294e-04 6.975414e-04
## IJS -7.811911e-05 0.0006437738 6.490294e-04 1.565449e-03 8.290241e-04
## SPY -9.029489e-06 0.0006795430 6.975414e-04 8.290241e-04 7.408301e-04
# 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)
sd_portfolio
## [,1]
## [1,] 0.02347492
# 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
## AGG EEM EFA IJS SPY
## [1,] 0.0003874159 0.009257148 0.005815635 0.005684466 0.002330251
rowSums(component_contribution)
## [1] 0.02347492
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AGG 0.017
## 2 EEM 0.394
## 3 EFA 0.248
## 4 IJS 0.242
## 5 SPY 0.099
# 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")
# Custom function
calculate_component_contribution <- function(asset_returns_wide_tbl, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# Standard deviation of portfolio
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
# Component contribution
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.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
gather(key = "asset", value = "contribution") %>%
ggplot(aes(asset, contribution)) +
geom_col(fill = "cornflowerblue") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
labs(title = "Percent Contribution to Portfolio Standard Deviation",
y = "Percent Contribution to Risk",
x = NULL)
# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
gather(key = "asset", value = "contribution") %>%
add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%
ggplot(aes(asset, value, fill = type)) +
geom_col(position = "dodge") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme_tq() +
scale_fill_tq() +
labs(title = "Percent Contribution to Volatility",
y = "percent",
x = "asset")
## 6 Rolling Component Contribution
calculate_comp_contrib_by_window <- function(asset_returns_wide_tbl,
start = 1,
window = 24,
weights) {
# 1 Define start date
start_date <- rownames(asset_returns_wide_tbl)[start]
# 2 Define end date
end_date <- rownames(asset_returns_wide_tbl)[start + window]
# 3 Subset df
df_subset <- asset_returns_wide_tbl %>%
rownames_to_column(var = "date") %>%
filter(date >= start_date & date < end_date) %>%
column_to_rownames(var = "date")
# 4 Calculate component contribution
component_percentages <-df_subset %>%
calculate_component_contribution(w = weights)
# 5 Add end date to df
component_percentages %>%
mutate(date = ymd(end_date)) %>%
select(date, everything())
}
# Check the custom function
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 1, window = 24,
w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 6
## date AGG EEM EFA IJS SPY
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-01-30 0.039 0.372 0.256 0.245 0.088
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 2, window = 24,
w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 6
## date AGG EEM EFA IJS SPY
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-02-27 0.036 0.374 0.249 0.252 0.089
dump(list = c("calculate_component_contribution",
"calculate_comp_contrib_by_window"),
file = "../00_scripts/calculate_comp_contrib_to_portfolio_volatility.R")
# Iterate the custom function
w <- c(0.25,0.25,0.2,0.2,0.1)
window <- 24
rolling_comp_contrib_tbl <- 1:(nrow(asset_returns_wide_tbl) - window) %>%
map_df(.x = ., .f = ~calculate_comp_contrib_by_window(asset_returns_wide_tbl,
start = .x,
weights = w,
window = window))
rolling_comp_contrib_tbl
## # A tibble: 36 × 6
## date AGG EEM EFA IJS SPY
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-01-30 0.039 0.372 0.256 0.245 0.088
## 2 2015-02-27 0.036 0.374 0.249 0.252 0.089
## 3 2015-03-31 0.027 0.37 0.255 0.255 0.092
## 4 2015-04-30 0.027 0.372 0.256 0.252 0.093
## 5 2015-05-29 0.024 0.385 0.254 0.246 0.092
## 6 2015-06-30 0.018 0.383 0.248 0.257 0.094
## 7 2015-07-31 0.014 0.375 0.253 0.261 0.097
## 8 2015-08-31 0.013 0.404 0.237 0.257 0.09
## 9 2015-09-30 0.012 0.407 0.248 0.238 0.094
## 10 2015-10-30 0.003 0.405 0.244 0.243 0.105
## # ℹ 26 more rows
# Figure 10.3 Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%
# Transform data to long form
pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%
# Plot
ggplot(aes(date, contribution, color = asset)) +
geom_line() +
scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
scale_y_continuous(labels = scales::percent_format()) +
annotate(geom = "text",
x = as.Date("2016-07-01"),
y = 0.03,
color = "red", size = 5,
label = str_glue("AGG dips below zero sometimes, indicating
it reduces the portfolio volatility."))
# Figure 10.4 Stacked Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%
# Transform data to long form
pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%
# Plot
ggplot(aes(date, contribution, fill = asset)) +
geom_area() +
scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
scale_y_continuous(labels = scales::percent_format()) +
annotate(geom = "text",
x = as.Date("2016-07-01"),
y = 0.08,
color = "red", size = 5,
label = str_glue("AGG dips below zero sometimes, indicating
it reduces the portfolio volatility."))