# 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"))
Refresh your memory on covariance with this video. Click this link Refresh your memory on matrix multiplication. Click this link
# 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.0062306354 -0.0029356990 0.0366061802 0.052133055 4.992337e-02
## 2013-02-28 0.0058906050 -0.0231051892 -0.0129693147 0.016175947 1.267788e-02
## 2013-03-28 0.0009851788 -0.0102352574 0.0129693147 0.040257930 3.726836e-02
## 2013-04-30 0.0096395834 0.0120849802 0.0489679093 0.001222514 1.902971e-02
## 2013-05-31 -0.0202140381 -0.0494835828 -0.0306557453 0.041976230 2.333518e-02
## 2013-06-28 -0.0157779746 -0.0547282441 -0.0271443858 -0.001402794 -1.343371e-02
## 2013-07-31 0.0026874185 0.0131597885 0.0518603677 0.063541269 5.038550e-02
## 2013-08-30 -0.0082982236 -0.0257057642 -0.0197464131 -0.034743248 -3.045106e-02
## 2013-09-30 0.0111440401 0.0695888098 0.0753387568 0.063873277 3.115577e-02
## 2013-10-31 0.0082918795 0.0408614940 0.0320813564 0.034234215 4.526690e-02
## 2013-11-29 -0.0025099696 -0.0025940982 0.0054499131 0.041661092 2.920713e-02
## 2013-12-31 -0.0055827863 -0.0040747246 0.0215280237 0.012892093 2.559577e-02
## 2014-01-31 0.0152913646 -0.0903224768 -0.0534133122 -0.035775119 -3.588462e-02
## 2014-02-28 0.0037566717 0.0332208075 0.0595049704 0.045257424 4.451054e-02
## 2014-03-31 -0.0014814476 0.0380216214 -0.0046025837 0.013315013 8.261280e-03
## 2014-04-30 0.0081834361 0.0077727547 0.0165293020 -0.023184271 6.927395e-03
## 2014-05-30 0.0117216399 0.0290909676 0.0158285246 0.006205413 2.294107e-02
## 2014-06-30 -0.0005754207 0.0237340827 0.0091653791 0.037718511 2.043482e-02
## 2014-07-31 -0.0025128092 0.0135555916 -0.0263797304 -0.052009021 -1.352856e-02
## 2014-08-29 0.0114312914 0.0279045037 0.0018004840 0.043657854 3.870446e-02
## 2014-09-30 -0.0061675963 -0.0808564384 -0.0395986916 -0.061260595 -1.389240e-02
## 2014-10-31 0.0105841894 0.0140962505 -0.0026547530 0.068875118 2.327803e-02
## 2014-11-28 0.0065488863 -0.0155411980 0.0006253818 0.004773125 2.710140e-02
## 2014-12-31 0.0014752315 -0.0404421868 -0.0407465734 0.025296009 -2.539534e-03
## 2015-01-30 0.0203152923 -0.0068957397 0.0062262884 -0.054627878 -3.007728e-02
## 2015-02-27 -0.0089880827 0.0431359233 0.0614507187 0.056914620 5.468176e-02
## 2015-03-31 0.0037398134 -0.0150860088 -0.0143889319 0.010156495 -1.583039e-02
## 2015-04-30 -0.0032329195 0.0662813084 0.0358168233 -0.018417835 9.786096e-03
## 2015-05-29 -0.0043840059 -0.0419109871 0.0019526296 0.007509872 1.277422e-02
## 2015-06-30 -0.0108251969 -0.0297464913 -0.0316789658 0.004171475 -2.052151e-02
## 2015-07-31 0.0085847851 -0.0651783527 0.0201146750 -0.027375634 2.233812e-02
## 2015-08-31 -0.0033640746 -0.0925123720 -0.0771524133 -0.047268442 -6.288683e-02
## 2015-09-30 0.0080818310 -0.0318249694 -0.0451950004 -0.038464522 -2.584703e-02
## 2015-10-30 0.0006851957 0.0618082776 0.0640258576 0.063589762 8.163532e-02
## 2015-11-30 -0.0038977928 -0.0255600164 -0.0075558689 0.024415310 3.648189e-03
## 2015-12-31 -0.0019189456 -0.0389475029 -0.0235951132 -0.052157126 -1.743367e-02
## 2016-01-29 0.0123299593 -0.0516365809 -0.0567578138 -0.060306603 -5.106865e-02
## 2016-02-29 0.0088312268 -0.0082115123 -0.0339138201 0.020604815 -8.263023e-04
## 2016-03-31 0.0087092685 0.1218790025 0.0637456126 0.089910683 6.510012e-02
## 2016-04-29 0.0025457777 0.0040792907 0.0219751523 0.021044039 3.933666e-03
## 2016-05-31 0.0001360959 -0.0376285546 -0.0008560276 0.004397251 1.686846e-02
## 2016-06-30 0.0191666050 0.0445822861 -0.0244915750 0.008291988 3.469884e-03
## 2016-07-29 0.0054290333 0.0524421188 0.0390002525 0.049348436 3.582176e-02
## 2016-08-31 -0.0021559082 0.0087985247 0.0053268334 0.011261076 1.196792e-03
## 2016-09-30 0.0005162116 0.0248729273 0.0132790421 0.008614813 5.839594e-05
## 2016-10-31 -0.0082057368 -0.0083121403 -0.0224035909 -0.038134825 -1.748924e-02
## 2016-11-30 -0.0259890911 -0.0451617326 -0.0179744554 0.125246539 3.617593e-02
## 2016-12-30 0.0025377087 -0.0025300082 0.0267027462 0.031491755 2.006931e-02
## 2017-01-31 0.0021262682 0.0644314663 0.0323819098 -0.012144156 1.773643e-02
## 2017-02-28 0.0064376503 0.0172576156 0.0118365313 0.013428988 3.853898e-02
## 2017-03-31 -0.0005529663 0.0361889582 0.0318056779 -0.006533295 1.249290e-03
## 2017-04-28 0.0090293361 0.0168664429 0.0239523250 0.005108025 9.877348e-03
## 2017-05-31 0.0068475821 0.0280600593 0.0348099754 -0.022862723 1.401406e-02
## 2017-06-30 -0.0001826585 0.0092237243 0.0029559906 0.029151764 6.354761e-03
## 2017-07-31 0.0033340681 0.0565945218 0.0261881396 0.007481456 2.034581e-02
## 2017-08-31 0.0093693778 0.0232437110 -0.0004486483 -0.027564507 2.913588e-03
## 2017-09-29 -0.0057322923 -0.0004461751 0.0233429723 0.082321616 1.994922e-02
## 2017-10-31 0.0009781554 0.0322786091 0.0166537655 0.005916133 2.329038e-02
## 2017-11-30 -0.0014839773 -0.0038969489 0.0068699320 0.036913275 3.010832e-02
## 2017-12-29 0.0047403002 0.0369252784 0.0133982941 -0.003731042 1.205506e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398315e-05 0.0001042104 4.178361e-05 -7.811787e-05 -9.030195e-06
## EEM 1.042104e-04 0.0017547093 1.039017e-03 6.437726e-04 6.795422e-04
## EFA 4.178361e-05 0.0010390171 1.064239e-03 6.490294e-04 6.975411e-04
## IJS -7.811787e-05 0.0006437726 6.490294e-04 1.565448e-03 8.290245e-04
## SPY -9.030195e-06 0.0006795422 6.975411e-04 8.290245e-04 7.408293e-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.02347491
# 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.000387419 0.009257139 0.005815637 0.005684466 0.00233025
rowSums(component_contribution)
## [1] 0.02347491
# 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")
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.096
## 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."))