# 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.0062311938 -0.0029354005 0.0366061757 0.052133140 4.992314e-02
## 2013-02-28 0.0058911491 -0.0231051707 -0.0129694841 0.016175423 1.267823e-02
## 2013-03-28 0.0009850600 -0.0102351118 0.0129694841 0.040257926 3.726805e-02
## 2013-04-30 0.0096395452 0.0120844531 0.0489676089 0.001222811 1.903027e-02
## 2013-05-31 -0.0202147132 -0.0494829885 -0.0306553972 0.041976121 2.333516e-02
## 2013-06-28 -0.0157779825 -0.0547287588 -0.0271444801 -0.001402864 -1.343409e-02
## 2013-07-31 0.0026880092 0.0131599404 0.0518604577 0.063541279 5.038562e-02
## 2013-08-30 -0.0082980899 -0.0257057495 -0.0197463849 -0.034743219 -3.045140e-02
## 2013-09-30 0.0111430458 0.0695890421 0.0753384185 0.063873332 3.115592e-02
## 2013-10-31 0.0082924924 0.0408612799 0.0320817099 0.034234133 4.526659e-02
## 2013-11-29 -0.0025101909 -0.0025939865 0.0054496983 0.041661224 2.920703e-02
## 2013-12-31 -0.0055824907 -0.0040744160 0.0215280708 0.012892071 2.559606e-02
## 2014-01-31 0.0152919823 -0.0903226112 -0.0534132794 -0.035775219 -3.588450e-02
## 2014-02-28 0.0037564801 0.0332206760 0.0595048974 0.045257165 4.451039e-02
## 2014-03-31 -0.0014818075 0.0380216156 -0.0046025452 0.013315259 8.261373e-03
## 2014-04-30 0.0081832315 0.0077724429 0.0165294212 -0.023184144 6.927501e-03
## 2014-05-30 0.0117213295 0.0290915367 0.0158285027 0.006205132 2.294127e-02
## 2014-06-30 -0.0005754430 0.0237339224 0.0091654340 0.037718805 2.043469e-02
## 2014-07-31 -0.0025121061 0.0135554617 -0.0263796497 -0.052009220 -1.352885e-02
## 2014-08-29 0.0114310011 0.0279047061 0.0018002465 0.043657637 3.870485e-02
## 2014-09-30 -0.0061675134 -0.0808571142 -0.0395985369 -0.061260338 -1.389248e-02
## 2014-10-31 0.0105844346 0.0140968857 -0.0026548920 0.068874865 2.327772e-02
## 2014-11-28 0.0065488638 -0.0155413186 0.0006254735 0.004773506 2.710136e-02
## 2014-12-31 0.0014751226 -0.0404424506 -0.0407467494 0.025296250 -2.539643e-03
## 2015-01-30 0.0203153498 -0.0068956277 0.0062265852 -0.054628333 -3.007711e-02
## 2015-02-27 -0.0089885630 0.0431361393 0.0614504436 0.056914669 5.468210e-02
## 2015-03-31 0.0037406263 -0.0150862103 -0.0143887705 0.010156480 -1.583028e-02
## 2015-04-30 -0.0032331841 0.0662813466 0.0358165769 -0.018417951 9.785810e-03
## 2015-05-29 -0.0043838434 -0.0419110152 0.0019526163 0.007510182 1.277398e-02
## 2015-06-30 -0.0108251101 -0.0297465425 -0.0316786710 0.004171345 -2.052115e-02
## 2015-07-31 0.0085842899 -0.0651781389 0.0201145100 -0.027375435 2.233794e-02
## 2015-08-31 -0.0033634289 -0.0925124218 -0.0771525728 -0.047268393 -6.288659e-02
## 2015-09-30 0.0080814495 -0.0318248618 -0.0451948693 -0.038464791 -2.584745e-02
## 2015-10-30 0.0006849612 0.0618082679 0.0640259285 0.063589751 8.163513e-02
## 2015-11-30 -0.0038981426 -0.0255606021 -0.0075556977 0.024415486 3.648467e-03
## 2015-12-31 -0.0019186900 -0.0389470505 -0.0235951354 -0.052157205 -1.743354e-02
## 2016-01-29 0.0123295872 -0.0516368011 -0.0567578101 -0.060306891 -5.106880e-02
## 2016-02-29 0.0088316939 -0.0082114634 -0.0339139805 0.020605377 -8.263092e-04
## 2016-03-31 0.0087089954 0.1218792209 0.0637455915 0.089910149 6.510027e-02
## 2016-04-29 0.0025461389 0.0040791547 0.0219751237 0.021044247 3.933629e-03
## 2016-05-31 0.0001358254 -0.0376287047 -0.0008558911 0.004397031 1.686845e-02
## 2016-06-30 0.0191667225 0.0445825728 -0.0244916419 0.008292459 3.469836e-03
## 2016-07-29 0.0054294202 0.0524423075 0.0390003649 0.049348466 3.582180e-02
## 2016-08-31 -0.0021562507 0.0087983145 0.0053267590 0.011260877 1.196924e-03
## 2016-09-30 0.0005160896 0.0248729397 0.0132791048 0.008614714 5.827379e-05
## 2016-10-31 -0.0082050359 -0.0083121374 -0.0224036424 -0.038134761 -1.748925e-02
## 2016-11-30 -0.0259902514 -0.0451618458 -0.0179744466 0.125246396 3.617591e-02
## 2016-12-30 0.0025384370 -0.0025300917 0.0267028055 0.031491761 2.006913e-02
## 2017-01-31 0.0021260125 0.0644313576 0.0323818587 -0.012143891 1.773659e-02
## 2017-02-28 0.0064377295 0.0172578934 0.0118364607 0.013429120 3.853909e-02
## 2017-03-31 -0.0005528650 0.0361891145 0.0318057513 -0.006533536 1.249459e-03
## 2017-04-28 0.0090298144 0.0168661696 0.0239521597 0.005107755 9.877151e-03
## 2017-05-31 0.0068470303 0.0280600769 0.0348101995 -0.022862610 1.401406e-02
## 2017-06-30 -0.0001827512 0.0092238315 0.0029558687 0.029151928 6.354856e-03
## 2017-07-31 0.0033344185 0.0565945909 0.0261880413 0.007481322 2.034571e-02
## 2017-08-31 0.0093692946 0.0232437794 -0.0004483141 -0.027564251 2.913398e-03
## 2017-09-29 -0.0057321049 -0.0004465575 0.0233427888 0.082321566 1.994915e-02
## 2017-10-31 0.0009776767 0.0322786886 0.0166536389 0.005915704 2.329073e-02
## 2017-11-30 -0.0014838472 -0.0038969510 0.0068699364 0.036913485 3.010806e-02
## 2017-12-29 0.0047401013 0.0369253399 0.0133984342 -0.003730945 1.205483e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398455e-05 0.0001042080 4.178028e-05 -7.812255e-05 -9.033612e-06
## EEM 1.042080e-04 0.0017547152 1.039018e-03 6.437736e-04 6.795449e-04
## EFA 4.178028e-05 0.0010390175 1.064237e-03 6.490275e-04 6.975414e-04
## IJS -7.812255e-05 0.0006437736 6.490275e-04 1.565448e-03 8.290249e-04
## SPY -9.033612e-06 0.0006795449 6.975414e-04 8.290249e-04 7.408295e-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.000387396 0.009257161 0.005815629 0.005684458 0.002330251
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())
}