# 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.0062305166 -0.0029352588 0.0366065071 0.052133216 4.992303e-02
## 2013-02-28 0.0058910579 -0.0231055750 -0.0129695690 0.016175560 1.267824e-02
## 2013-03-28 0.0009852973 -0.0102349980 0.0129695690 0.040258399 3.726757e-02
## 2013-04-30 0.0096387154 0.0120852419 0.0489676215 0.001222024 1.903059e-02
## 2013-05-31 -0.0202140875 -0.0494835668 -0.0306555928 0.041976497 2.333525e-02
## 2013-06-28 -0.0157779366 -0.0547287165 -0.0271445305 -0.001403100 -1.343455e-02
## 2013-07-31 0.0026877033 0.0131598630 0.0518602211 0.063541619 5.038606e-02
## 2013-08-30 -0.0082982754 -0.0257056421 -0.0197463369 -0.034743858 -3.045099e-02
## 2013-09-30 0.0111442302 0.0695888981 0.0753387089 0.063873820 3.115546e-02
## 2013-10-31 0.0082914098 0.0408611197 0.0320816599 0.034234257 4.526663e-02
## 2013-11-29 -0.0025093273 -0.0025939594 0.0054497637 0.041660909 2.920742e-02
## 2013-12-31 -0.0055837289 -0.0040742390 0.0215280075 0.012892298 2.559605e-02
## 2014-01-31 0.0152920826 -0.0903226541 -0.0534131839 -0.035775481 -3.588497e-02
## 2014-02-28 0.0037568430 0.0332204819 0.0595049187 0.045257762 4.451076e-02
## 2014-03-31 -0.0014815557 0.0380214590 -0.0046026528 0.013314827 8.261000e-03
## 2014-04-30 0.0081832614 0.0077729723 0.0165294236 -0.023184114 6.927457e-03
## 2014-05-30 0.0117217429 0.0290913076 0.0158284448 0.006205287 2.294156e-02
## 2014-06-30 -0.0005756507 0.0237339394 0.0091654929 0.037718896 2.043445e-02
## 2014-07-31 -0.0025124399 0.0135557970 -0.0263798899 -0.052009416 -1.352841e-02
## 2014-08-29 0.0114309997 0.0279042740 0.0018003983 0.043657863 3.870467e-02
## 2014-09-30 -0.0061673521 -0.0808567794 -0.0395984613 -0.061260648 -1.389246e-02
## 2014-10-31 0.0105845494 0.0140965730 -0.0026546882 0.068875053 2.327789e-02
## 2014-11-28 0.0065491729 -0.0155412082 0.0006252476 0.004773730 2.710131e-02
## 2014-12-31 0.0014745074 -0.0404422965 -0.0407466441 0.025295752 -2.539736e-03
## 2015-01-30 0.0203152655 -0.0068956968 0.0062263657 -0.054627881 -3.007719e-02
## 2015-02-27 -0.0089882091 0.0431359678 0.0614505880 0.056914372 5.468214e-02
## 2015-03-31 0.0037405326 -0.0150862447 -0.0143887132 0.010156466 -1.583041e-02
## 2015-04-30 -0.0032328674 0.0662815315 0.0358166571 -0.018417957 9.785698e-03
## 2015-05-29 -0.0043841772 -0.0419111554 0.0019525556 0.007510246 1.277408e-02
## 2015-06-30 -0.0108258815 -0.0297463480 -0.0316788424 0.004171359 -2.052098e-02
## 2015-07-31 0.0085852418 -0.0651784554 0.0201145742 -0.027375502 2.233776e-02
## 2015-08-31 -0.0033635062 -0.0925122749 -0.0771526685 -0.047268437 -6.288636e-02
## 2015-09-30 0.0080809759 -0.0318250337 -0.0451948535 -0.038464590 -2.584744e-02
## 2015-10-30 0.0006859119 0.0618085611 0.0640260841 0.063589605 8.163499e-02
## 2015-11-30 -0.0038986261 -0.0255604014 -0.0075558957 0.024415353 3.648681e-03
## 2015-12-31 -0.0019190851 -0.0389471313 -0.0235948798 -0.052157006 -1.743359e-02
## 2016-01-29 0.0123299301 -0.0516369228 -0.0567579639 -0.060306811 -5.106896e-02
## 2016-02-29 0.0088316794 -0.0082113836 -0.0339140194 0.020605556 -8.263365e-04
## 2016-03-31 0.0087085088 0.1218790192 0.0637458506 0.089909722 6.510026e-02
## 2016-04-29 0.0025464761 0.0040791042 0.0219750163 0.021044478 3.933603e-03
## 2016-05-31 0.0001354205 -0.0376284639 -0.0008559985 0.004397180 1.686842e-02
## 2016-06-30 0.0191668944 0.0445824922 -0.0244914364 0.008292020 3.469846e-03
## 2016-07-29 0.0054296821 0.0524419021 0.0390000644 0.049348435 3.582192e-02
## 2016-08-31 -0.0021556546 0.0087984721 0.0053269298 0.011261061 1.196892e-03
## 2016-09-30 0.0005158676 0.0248731720 0.0132791624 0.008614807 5.786201e-05
## 2016-10-31 -0.0082054510 -0.0083124521 -0.0224037498 -0.038134812 -1.748865e-02
## 2016-11-30 -0.0259897956 -0.0451616328 -0.0179744875 0.125246587 3.617583e-02
## 2016-12-30 0.0025378765 -0.0025299175 0.0267028177 0.031491358 2.006937e-02
## 2017-01-31 0.0021263070 0.0644312901 0.0323818322 -0.012143643 1.773609e-02
## 2017-02-28 0.0064378705 0.0172579587 0.0118365146 0.013428895 3.853926e-02
## 2017-03-31 -0.0005535750 0.0361889731 0.0318059096 -0.006533184 1.249048e-03
## 2017-04-28 0.0090298828 0.0168662927 0.0239520183 0.005107827 9.877598e-03
## 2017-05-31 0.0068467419 0.0280600145 0.0348103694 -0.022862609 1.401391e-02
## 2017-06-30 -0.0001816853 0.0092237714 0.0029558517 0.029151576 6.354939e-03
## 2017-07-31 0.0033338778 0.0565947381 0.0261878079 0.007481590 2.034573e-02
## 2017-08-31 0.0093689851 0.0232436070 -0.0004482871 -0.027564712 2.913498e-03
## 2017-09-29 -0.0057320049 -0.0004464642 0.0233426720 0.082321737 1.994893e-02
## 2017-10-31 0.0009784387 0.0322786031 0.0166537929 0.005915866 2.329095e-02
## 2017-11-30 -0.0014844091 -0.0038968199 0.0068698934 0.036913717 3.010782e-02
## 2017-12-29 0.0047402506 0.0369254159 0.0133984131 -0.003731302 1.205514e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398440e-05 0.0001042091 4.178287e-05 -7.811834e-05 -9.030805e-06
## EEM 1.042091e-04 0.0017547132 1.039019e-03 6.437727e-04 6.795441e-04
## EFA 4.178287e-05 0.0010390190 1.064239e-03 6.490299e-04 6.975420e-04
## IJS -7.811834e-05 0.0006437727 6.490299e-04 1.565451e-03 8.290253e-04
## SPY -9.030805e-06 0.0006795441 6.975420e-04 8.290253e-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.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.0003874155 0.009257148 0.005815639 0.005684469 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")
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."))