# 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.0062310020 -0.0029354627 0.0366061158 0.052132985 4.992363e-02
## 2013-02-28 0.0058910786 -0.0231051147 -0.0129695699 0.016175562 1.267810e-02
## 2013-03-28 0.0009845154 -0.0102353502 0.0129695699 0.040258093 3.726796e-02
## 2013-04-30 0.0096397087 0.0120849040 0.0489676317 0.001222264 1.902988e-02
## 2013-05-31 -0.0202142852 -0.0494833715 -0.0306556888 0.041976486 2.333538e-02
## 2013-06-28 -0.0157780614 -0.0547285037 -0.0271441110 -0.001403043 -1.343458e-02
## 2013-07-31 0.0026880022 0.0131598224 0.0518600775 0.063541543 5.038611e-02
## 2013-08-30 -0.0082987649 -0.0257054818 -0.0197462899 -0.034743465 -3.045103e-02
## 2013-09-30 0.0111442724 0.0695885868 0.0753387516 0.063873562 3.115588e-02
## 2013-10-31 0.0082921252 0.0408614106 0.0320816309 0.034234027 4.526647e-02
## 2013-11-29 -0.0025099414 -0.0025942551 0.0054493836 0.041661263 2.920683e-02
## 2013-12-31 -0.0055828077 -0.0040743618 0.0215281353 0.012891964 2.559648e-02
## 2014-01-31 0.0152912395 -0.0903227476 -0.0534132249 -0.035774912 -3.588474e-02
## 2014-02-28 0.0037570143 0.0332204842 0.0595050852 0.045256843 4.451059e-02
## 2014-03-31 -0.0014816444 0.0380220602 -0.0046025662 0.013315517 8.260921e-03
## 2014-04-30 0.0081832752 0.0077727418 0.0165292931 -0.023184038 6.927568e-03
## 2014-05-30 0.0117218158 0.0290908508 0.0158284250 0.006205383 2.294128e-02
## 2014-06-30 -0.0005762386 0.0237341685 0.0091656312 0.037718638 2.043459e-02
## 2014-07-31 -0.0025121117 0.0135557896 -0.0263800046 -0.052009686 -1.352845e-02
## 2014-08-29 0.0114309532 0.0279045724 0.0018006510 0.043657819 3.870446e-02
## 2014-09-30 -0.0061674214 -0.0808570156 -0.0395987218 -0.061260058 -1.389238e-02
## 2014-10-31 0.0105842888 0.0140967927 -0.0026546525 0.068874574 2.327833e-02
## 2014-11-28 0.0065492394 -0.0155412945 0.0006252304 0.004773563 2.710112e-02
## 2014-12-31 0.0014742922 -0.0404424598 -0.0407467275 0.025295759 -2.540013e-03
## 2015-01-30 0.0203157204 -0.0068952579 0.0062264224 -0.054627746 -3.007708e-02
## 2015-02-27 -0.0089880127 0.0431357790 0.0614506945 0.056914639 5.468190e-02
## 2015-03-31 0.0037403336 -0.0150862520 -0.0143887050 0.010156388 -1.583010e-02
## 2015-04-30 -0.0032331169 0.0662814149 0.0358165767 -0.018417743 9.785692e-03
## 2015-05-29 -0.0043836479 -0.0419111291 0.0019525723 0.007509799 1.277448e-02
## 2015-06-30 -0.0108255395 -0.0297466142 -0.0316789031 0.004171372 -2.052144e-02
## 2015-07-31 0.0085846607 -0.0651777583 0.0201144075 -0.027375369 2.233789e-02
## 2015-08-31 -0.0033633738 -0.0925125798 -0.0771522510 -0.047268188 -6.288651e-02
## 2015-09-30 0.0080807571 -0.0318252728 -0.0451949791 -0.038464796 -2.584714e-02
## 2015-10-30 0.0006860025 0.0618085830 0.0640258921 0.063589715 8.163512e-02
## 2015-11-30 -0.0038987810 -0.0255606062 -0.0075557175 0.024415263 3.648190e-03
## 2015-12-31 -0.0019187172 -0.0389470364 -0.0235950275 -0.052157069 -1.743357e-02
## 2016-01-29 0.0123300926 -0.0516367544 -0.0567578149 -0.060306780 -5.106902e-02
## 2016-02-29 0.0088313164 -0.0082114554 -0.0339142639 0.020605373 -8.260276e-04
## 2016-03-31 0.0087087847 0.1218789852 0.0637460481 0.089910315 6.510024e-02
## 2016-04-29 0.0025464585 0.0040791497 0.0219748450 0.021044081 3.933565e-03
## 2016-05-31 0.0001354001 -0.0376283749 -0.0008559622 0.004396966 1.686838e-02
## 2016-06-30 0.0191665059 0.0445825336 -0.0244914523 0.008292476 3.469717e-03
## 2016-07-29 0.0054301178 0.0524419580 0.0390001141 0.049348375 3.582215e-02
## 2016-08-31 -0.0021561786 0.0087982897 0.0053268633 0.011261109 1.196655e-03
## 2016-09-30 0.0005158047 0.0248730626 0.0132792408 0.008614390 5.829742e-05
## 2016-10-31 -0.0082052294 -0.0083119473 -0.0224037499 -0.038134507 -1.748921e-02
## 2016-11-30 -0.0259896141 -0.0451620824 -0.0179745681 0.125246222 3.617638e-02
## 2016-12-30 0.0025381566 -0.0025298182 0.0267029474 0.031491556 2.006869e-02
## 2017-01-31 0.0021263336 0.0644313913 0.0323818793 -0.012143786 1.773646e-02
## 2017-02-28 0.0064375865 0.0172576608 0.0118363933 0.013428995 3.853919e-02
## 2017-03-31 -0.0005530459 0.0361891043 0.0318057158 -0.006533080 1.249380e-03
## 2017-04-28 0.0090294239 0.0168665113 0.0239524413 0.005107773 9.877090e-03
## 2017-05-31 0.0068471812 0.0280599834 0.0348103264 -0.022862687 1.401416e-02
## 2017-06-30 -0.0001823579 0.0092234403 0.0029557824 0.029152117 6.354832e-03
## 2017-07-31 0.0033345530 0.0565947574 0.0261878661 0.007481007 2.034579e-02
## 2017-08-31 0.0093687417 0.0232436720 -0.0004485731 -0.027564491 2.913448e-03
## 2017-09-29 -0.0057320866 -0.0004461961 0.0233427800 0.082322183 1.994901e-02
## 2017-10-31 0.0009779226 0.0322784749 0.0166538010 0.005915628 2.329082e-02
## 2017-11-30 -0.0014841433 -0.0038968556 0.0068701057 0.036913397 3.010813e-02
## 2017-12-29 0.0047400361 0.0369253929 0.0133983361 -0.003731246 1.205499e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398419e-05 0.0001042126 4.178641e-05 -7.811849e-05 -9.029860e-06
## EEM 1.042126e-04 0.0017547133 1.039019e-03 6.437702e-04 6.795435e-04
## EFA 4.178641e-05 0.0010390189 1.064239e-03 6.490283e-04 6.975419e-04
## IJS -7.811849e-05 0.0006437702 6.490283e-04 1.565446e-03 8.290252e-04
## SPY -9.029860e-06 0.0006795435 6.975419e-04 8.290252e-04 7.408310e-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.02347493
# 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.0003874325 0.00925715 0.005815642 0.005684451 0.002330252
rowSums(component_contribution)
## [1] 0.02347493
# 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
# 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."))