# 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.0062306458 -0.0029358068 0.0366063597 0.052133686 4.992273e-02
## 2013-02-28 0.0058906897 -0.0231051917 -0.0129695813 0.016175108 1.267819e-02
## 2013-03-28 0.0009845187 -0.0102349242 0.0129695813 0.040258237 3.726842e-02
## 2013-04-30 0.0096396959 0.0120848674 0.0489676534 0.001222713 1.902982e-02
## 2013-05-31 -0.0202136756 -0.0494835772 -0.0306555775 0.041975932 2.333495e-02
## 2013-06-28 -0.0157785449 -0.0547282986 -0.0271444746 -0.001403272 -1.343394e-02
## 2013-07-31 0.0026877351 0.0131597274 0.0518604566 0.063541567 5.038606e-02
## 2013-08-30 -0.0082982001 -0.0257057642 -0.0197462410 -0.034743255 -3.045139e-02
## 2013-09-30 0.0111441336 0.0695888098 0.0753383452 0.063873636 3.115610e-02
## 2013-10-31 0.0082922458 0.0408613832 0.0320815958 0.034233957 4.526616e-02
## 2013-11-29 -0.0025103390 -0.0025939874 0.0054497594 0.041661256 2.920734e-02
## 2013-12-31 -0.0055830765 -0.0040745015 0.0215281022 0.012892171 2.559578e-02
## 2014-01-31 0.0152917137 -0.0903226999 -0.0534131576 -0.035775443 -3.588422e-02
## 2014-02-28 0.0037564623 0.0332209256 0.0595048163 0.045257274 4.451024e-02
## 2014-03-31 -0.0014815272 0.0380215033 -0.0046023586 0.013315328 8.261281e-03
## 2014-04-30 0.0081833373 0.0077726419 0.0165290778 -0.023184192 6.927588e-03
## 2014-05-30 0.0117221899 0.0290911900 0.0158285985 0.006205650 2.294126e-02
## 2014-06-30 -0.0005761542 0.0237339731 0.0091655233 0.037718270 2.043463e-02
## 2014-07-31 -0.0025119903 0.0135554861 -0.0263798006 -0.052009097 -1.352902e-02
## 2014-08-29 0.0114300199 0.0279047119 0.0018004838 0.043657700 3.870492e-02
## 2014-09-30 -0.0061665510 -0.0808566524 -0.0395986117 -0.061260442 -1.389231e-02
## 2014-10-31 0.0105846482 0.0140963618 -0.0026546756 0.068874890 2.327776e-02
## 2014-11-28 0.0065482165 -0.0155411980 0.0006250736 0.004773582 2.710157e-02
## 2014-12-31 0.0014752494 -0.0404421868 -0.0407466568 0.025295634 -2.539882e-03
## 2015-01-30 0.0203151009 -0.0068956229 0.0062264488 -0.054627809 -3.007693e-02
## 2015-02-27 -0.0089882347 0.0431358065 0.0614506437 0.056914698 5.468158e-02
## 2015-03-31 0.0037408799 -0.0150861225 -0.0143888570 0.010156275 -1.583005e-02
## 2015-04-30 -0.0032339836 0.0662815284 0.0358167499 -0.018417691 9.785923e-03
## 2015-05-29 -0.0043832812 -0.0419109825 0.0019525565 0.007509799 1.277405e-02
## 2015-06-30 -0.0108251043 -0.0297466022 -0.0316788193 0.004171549 -2.052142e-02
## 2015-07-31 0.0085849178 -0.0651782308 0.0201143787 -0.027375410 2.233795e-02
## 2015-08-31 -0.0033640951 -0.0925123602 -0.0771522770 -0.047268276 -6.288648e-02
## 2015-09-30 0.0080815647 -0.0318250341 -0.0451947566 -0.038464922 -2.584712e-02
## 2015-10-30 0.0006853730 0.0618084681 0.0640258524 0.063589845 8.163522e-02
## 2015-11-30 -0.0038982067 -0.0255606089 -0.0075559474 0.024415234 3.648189e-03
## 2015-12-31 -0.0019186989 -0.0389470315 -0.0235950322 -0.052157130 -1.743359e-02
## 2016-01-29 0.0123292284 -0.0516367193 -0.0567578091 -0.060306777 -5.106874e-02
## 2016-02-29 0.0088321804 -0.0082114388 -0.0339139058 0.020605151 -8.263023e-04
## 2016-03-31 0.0087083390 0.1218786687 0.0637456126 0.089910298 6.510004e-02
## 2016-04-29 0.0025465787 0.0040791621 0.0219750709 0.021044415 3.933413e-03
## 2016-05-31 0.0001353771 -0.0376283677 -0.0008559461 0.004397103 1.686896e-02
## 2016-06-30 0.0191667837 0.0445825523 -0.0244915750 0.008292354 3.469883e-03
## 2016-07-29 0.0054295468 0.0524422376 0.0390003329 0.049348352 3.582175e-02
## 2016-08-31 -0.0021559208 0.0087984625 0.0053268330 0.011260867 1.196872e-03
## 2016-09-30 0.0005157141 0.0248726882 0.0132790411 0.008614745 5.791794e-05
## 2016-10-31 -0.0082053055 -0.0083119032 -0.0224037503 -0.038134969 -1.748909e-02
## 2016-11-30 -0.0259894584 -0.0451618516 -0.0179744568 0.125246752 3.617593e-02
## 2016-12-30 0.0025381831 -0.0025301955 0.0267029082 0.031491634 2.006931e-02
## 2017-01-31 0.0021258088 0.0644314195 0.0323819846 -0.012144035 1.773658e-02
## 2017-02-28 0.0064376968 0.0172578498 0.0118363766 0.013429170 3.853912e-02
## 2017-03-31 -0.0005527183 0.0361891802 0.0318056038 -0.006533354 1.249217e-03
## 2017-04-28 0.0090290531 0.0168663300 0.0239521098 0.005107843 9.877132e-03
## 2017-05-31 0.0068474965 0.0280600562 0.0348103345 -0.022862600 1.401428e-02
## 2017-06-30 -0.0001823834 0.0092237233 0.0029560600 0.029151702 6.354760e-03
## 2017-07-31 0.0033343542 0.0565944167 0.0261878647 0.007481397 2.034574e-02
## 2017-08-31 0.0093691796 0.0232437110 -0.0004481733 -0.027564631 2.913519e-03
## 2017-09-29 -0.0057320557 -0.0004462722 0.0233426993 0.082321800 1.994908e-02
## 2017-10-31 0.0009779630 0.0322787063 0.0166537644 0.005915795 2.329058e-02
## 2017-11-30 -0.0014838851 -0.0038971377 0.0068698020 0.036913612 3.010806e-02
## 2017-12-29 0.0047397500 0.0369253762 0.0133984228 -0.003731151 1.205500e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398306e-05 0.0001042062 4.178128e-05 -7.811985e-05 -9.031752e-06
## EEM 1.042062e-04 0.0017547105 1.039017e-03 6.437728e-04 6.795411e-04
## EFA 4.178128e-05 0.0010390167 1.064236e-03 6.490303e-04 6.975395e-04
## IJS -7.811985e-05 0.0006437728 6.490303e-04 1.565451e-03 8.290245e-04
## SPY -9.031752e-06 0.0006795411 6.975395e-04 8.290245e-04 7.408272e-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.02347489
# 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.0003873972 0.009257139 0.005815633 0.005684473 0.002330247
rowSums(component_contribution)
## [1] 0.02347489
# 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."))