# 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.0062310958 -0.0029354636 0.0366062100 0.052132991 4.992311e-02
## 2013-02-28 0.0058907089 -0.0231051224 -0.0129694779 0.016175669 1.267798e-02
## 2013-03-28 0.0009850716 -0.0102350107 0.0129694779 0.040257790 3.726821e-02
## 2013-04-30 0.0096393419 0.0120847911 0.0489677181 0.001222770 1.902994e-02
## 2013-05-31 -0.0202130761 -0.0494832585 -0.0306557752 0.041976671 2.333538e-02
## 2013-06-28 -0.0157793790 -0.0547285037 -0.0271440195 -0.001403139 -1.343434e-02
## 2013-07-31 0.0026880034 0.0131596987 0.0518601598 0.063541070 5.038599e-02
## 2013-08-30 -0.0082980040 -0.0257054850 -0.0197463751 -0.034743283 -3.045125e-02
## 2013-09-30 0.0111440802 0.0695888322 0.0753384162 0.063873739 3.115566e-02
## 2013-10-31 0.0082917494 0.0408612922 0.0320817979 0.034234022 4.526680e-02
## 2013-11-29 -0.0025094722 -0.0025941412 0.0054497801 0.041661092 2.920693e-02
## 2013-12-31 -0.0055836574 -0.0040742469 0.0215278959 0.012892288 2.559627e-02
## 2014-01-31 0.0152923667 -0.0903225381 -0.0534132206 -0.035775487 -3.588464e-02
## 2014-02-28 0.0037562706 0.0332203487 0.0595048493 0.045257655 4.451020e-02
## 2014-03-31 -0.0014807160 0.0380217573 -0.0046024121 0.013314877 8.261416e-03
## 2014-04-30 0.0081821644 0.0077726260 0.0165291408 -0.023184040 6.927568e-03
## 2014-05-30 0.0117214569 0.0290911914 0.0158285772 0.006205303 2.294137e-02
## 2014-06-30 -0.0005750547 0.0237340534 0.0091654826 0.037718799 2.043459e-02
## 2014-07-31 -0.0025124761 0.0135552466 -0.0263799323 -0.052009605 -1.352883e-02
## 2014-08-29 0.0114309532 0.0279050056 0.0018004228 0.043658128 3.870455e-02
## 2014-09-30 -0.0061670580 -0.0808569014 -0.0395984174 -0.061260697 -1.389256e-02
## 2014-10-31 0.0105840153 0.0140965659 -0.0026546525 0.068875056 2.327807e-02
## 2014-11-28 0.0065489709 -0.0155412963 0.0006252304 0.004773331 2.710131e-02
## 2014-12-31 0.0014747384 -0.0404422264 -0.0407467275 0.025296135 -2.539750e-03
## 2015-01-30 0.0203158024 -0.0068957966 0.0062265867 -0.054628201 -3.007717e-02
## 2015-02-27 -0.0089887149 0.0431363135 0.0614505303 0.056914643 5.468217e-02
## 2015-03-31 0.0037405985 -0.0150862503 -0.0143890184 0.010156463 -1.583019e-02
## 2015-04-30 -0.0032328528 0.0662814074 0.0358169657 -0.018417592 9.785692e-03
## 2015-05-29 -0.0043838241 -0.0419112382 0.0019523458 0.007509648 1.277439e-02
## 2015-06-30 -0.0108257184 -0.0297464970 -0.0316785964 0.004171596 -2.052127e-02
## 2015-07-31 0.0085847509 -0.0651779380 0.0201144044 -0.027375440 2.233772e-02
## 2015-08-31 -0.0033635521 -0.0925123800 -0.0771526510 -0.047268502 -6.288651e-02
## 2015-09-30 0.0080812008 -0.0318252684 -0.0451948180 -0.038464635 -2.584704e-02
## 2015-10-30 0.0006856494 0.0618084413 0.0640260593 0.063589794 8.163478e-02
## 2015-11-30 -0.0038981617 -0.0255606062 -0.0075557984 0.024415337 3.648705e-03
## 2015-12-31 -0.0019192489 -0.0389469654 -0.0235951109 -0.052157303 -1.743400e-02
## 2016-01-29 0.0123306196 -0.0516367506 -0.0567577314 -0.060306957 -5.106840e-02
## 2016-02-29 0.0088311387 -0.0082113794 -0.0339139897 0.020605462 -8.264855e-04
## 2016-03-31 0.0087081795 0.1218790346 0.0637456024 0.089910253 6.510051e-02
## 2016-04-29 0.0025468031 0.0040792818 0.0219751003 0.021044161 3.933307e-03
## 2016-05-31 0.0001360014 -0.0376287763 -0.0008558782 0.004397116 1.686830e-02
## 2016-06-30 0.0191662416 0.0445824046 -0.0244915341 0.008292253 3.469969e-03
## 2016-07-29 0.0054294454 0.0524420934 0.0390000281 0.049348598 3.582231e-02
## 2016-08-31 -0.0021561793 0.0087988491 0.0053270279 0.011260969 1.196251e-03
## 2016-09-30 0.0005162247 0.0248727475 0.0132791575 0.008614599 5.837817e-05
## 2016-10-31 -0.0082050594 -0.0083121901 -0.0224038311 -0.038134793 -1.748921e-02
## 2016-11-30 -0.0259901287 -0.0451619574 -0.0179743144 0.125246628 3.617606e-02
## 2016-12-30 0.0025383305 -0.0025299462 0.0267027760 0.031491550 2.006917e-02
## 2017-01-31 0.0021262473 0.0644310950 0.0323819564 -0.012143722 1.773638e-02
## 2017-02-28 0.0064377595 0.0172581393 0.0118362339 0.013428745 3.853934e-02
## 2017-03-31 -0.0005533038 0.0361891001 0.0318057158 -0.006533142 1.248867e-03
## 2017-04-28 0.0090295114 0.0168662855 0.0239522923 0.005107958 9.877674e-03
## 2017-05-31 0.0068474362 0.0280599865 0.0348101875 -0.022862999 1.401423e-02
## 2017-06-30 -0.0001826117 0.0092236570 0.0029560702 0.029152122 6.354546e-03
## 2017-07-31 0.0033340475 0.0565945476 0.0261878661 0.007481190 2.034593e-02
## 2017-08-31 0.0093695836 0.0232437740 -0.0004482934 -0.027564741 2.913239e-03
## 2017-09-29 -0.0057321697 -0.0004462958 0.0233425686 0.082321743 1.994908e-02
## 2017-10-31 0.0009779225 0.0322786711 0.0166537998 0.005916546 2.329068e-02
## 2017-11-30 -0.0014840591 -0.0038970489 0.0068700385 0.036913278 3.010801e-02
## 2017-12-29 0.0047404538 0.0369253030 0.0133982702 -0.003731688 1.205525e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398443e-05 0.0001042068 4.178057e-05 -7.812301e-05 -9.033394e-06
## EEM 1.042068e-04 0.0017547119 1.039018e-03 6.437749e-04 6.795447e-04
## EFA 4.178057e-05 0.0010390176 1.064236e-03 6.490294e-04 6.975405e-04
## IJS -7.812301e-05 0.0006437749 6.490294e-04 1.565454e-03 8.290271e-04
## SPY -9.033394e-06 0.0006795447 6.975405e-04 8.290271e-04 7.408300e-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.0234749
# 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.0003873922 0.00925715 0.005815631 0.005684475 0.002330252
rowSums(component_contribution)
## [1] 0.0234749
# 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."))