# 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.0062307246 -0.0029359050 0.0366063983 0.052133650 4.992311e-02
## 2013-02-28 0.0058909864 -0.0231050118 -0.0129692940 0.016175454 1.267854e-02
## 2013-03-28 0.0009850712 -0.0102351250 0.0129692940 0.040257887 3.726796e-02
## 2013-04-30 0.0096392466 0.0120849054 0.0489675453 0.001222567 1.902965e-02
## 2013-05-31 -0.0202140999 -0.0494832585 -0.0306553350 0.041976187 2.333561e-02
## 2013-06-28 -0.0157778698 -0.0547283783 -0.0271444699 -0.001403043 -1.343457e-02
## 2013-07-31 0.0026869584 0.0131596971 0.0518603429 0.063541640 5.038600e-02
## 2013-08-30 -0.0082976240 -0.0257058626 -0.0197464637 -0.034743465 -3.045148e-02
## 2013-09-30 0.0111438900 0.0695887308 0.0753385871 0.063873739 3.115666e-02
## 2013-10-31 0.0082918439 0.0408616474 0.0320815564 0.034234107 4.526603e-02
## 2013-11-29 -0.0025102242 -0.0025941412 0.0054498601 0.041661089 2.920714e-02
## 2013-12-31 -0.0055820547 -0.0040742469 0.0215278976 0.012891963 2.559577e-02
## 2014-01-31 0.0152915159 -0.0903229765 -0.0534132249 -0.035775161 -3.588434e-02
## 2014-02-28 0.0037565489 0.0332206659 0.0595050852 0.045257331 4.450991e-02
## 2014-03-31 -0.0014817373 0.0380217618 -0.0046025662 0.013315275 8.261714e-03
## 2014-04-30 0.0081831838 0.0077726269 0.0165292931 -0.023184198 6.927471e-03
## 2014-05-30 0.0117221821 0.0290914195 0.0158284999 0.006205464 2.294138e-02
## 2014-06-30 -0.0005763296 0.0237338312 0.0091654077 0.037718406 2.043441e-02
## 2014-07-31 -0.0025119288 0.0135557896 -0.0263797798 -0.052009209 -1.352826e-02
## 2014-08-29 0.0114308598 0.0279045724 0.0018003464 0.043657809 3.870473e-02
## 2014-09-30 -0.0061671477 -0.0808567872 -0.0395985727 -0.061260541 -1.389256e-02
## 2014-10-31 0.0105840144 0.0140964517 -0.0026546528 0.068875056 2.327761e-02
## 2014-11-28 0.0065491489 -0.0155412963 0.0006252305 0.004773408 2.710193e-02
## 2014-12-31 0.0014749163 -0.0404421073 -0.0407466482 0.025295983 -2.540013e-03
## 2015-01-30 0.0203144827 -0.0068957958 0.0062263403 -0.054627808 -3.007726e-02
## 2015-02-27 -0.0089871357 0.0431359639 0.0614506994 0.056914325 5.468224e-02
## 2015-03-31 0.0037404202 -0.0150861372 -0.0143886278 0.010156463 -1.583053e-02
## 2015-04-30 -0.0032336442 0.0662814149 0.0358165767 -0.018417516 9.785865e-03
## 2015-05-29 -0.0043839138 -0.0419110153 0.0019524968 0.007509497 1.277405e-02
## 2015-06-30 -0.0108253644 -0.0297467280 -0.0316785940 0.004171671 -2.052093e-02
## 2015-07-31 0.0085851945 -0.0651781961 0.0201144792 -0.027375440 2.233797e-02
## 2015-08-31 -0.0033641737 -0.0925120733 -0.0771525563 -0.047268422 -6.288694e-02
## 2015-09-30 0.0080811139 -0.0318249873 -0.0451949791 -0.038464883 -2.584723e-02
## 2015-10-30 0.0006858260 0.0618080957 0.0640258921 0.063590118 8.163506e-02
## 2015-11-30 -0.0038982506 -0.0255604047 -0.0075558806 0.024415104 3.648705e-03
## 2015-12-31 -0.0019187167 -0.0389471047 -0.0235949479 -0.052156984 -1.743348e-02
## 2016-01-29 0.0123298263 -0.0516366796 -0.0567577314 -0.060307028 -5.106873e-02
## 2016-02-29 0.0088314901 -0.0082116056 -0.0339138983 0.020605123 -8.264853e-04
## 2016-03-31 0.0087089555 0.1218789271 0.0637454253 0.089910421 6.510032e-02
## 2016-04-29 0.0025456844 0.0040791502 0.0219751022 0.021044537 3.933307e-03
## 2016-05-31 0.0001364310 -0.0376282419 -0.0008560461 0.004396740 1.686863e-02
## 2016-06-30 0.0191660731 0.0445824676 -0.0244913683 0.008292327 3.469632e-03
## 2016-07-29 0.0054296140 0.0524420867 0.0390001969 0.049348382 3.582199e-02
## 2016-08-31 -0.0021560113 0.0087983513 0.0053267805 0.011261041 1.197140e-03
## 2016-09-30 0.0005160567 0.0248731807 0.0132794033 0.008614669 5.765146e-05
## 2016-10-31 -0.0082054827 -0.0083126171 -0.0224040785 -0.038134649 -1.748880e-02
## 2016-11-30 -0.0259895317 -0.0451615271 -0.0179743174 0.125246357 3.617582e-02
## 2016-12-30 0.0025378968 -0.0025300741 0.0267031098 0.031491800 2.006925e-02
## 2017-01-31 0.0021261614 0.0644314594 0.0323817120 -0.012144156 1.773608e-02
## 2017-02-28 0.0064379336 0.0172578390 0.0118363136 0.013428811 3.853950e-02
## 2017-03-31 -0.0005528741 0.0361888723 0.0318057158 -0.006532525 1.249233e-03
## 2017-04-28 0.0090292535 0.0168667372 0.0239523668 0.005107402 9.877091e-03
## 2017-05-31 0.0068476900 0.0280597625 0.0348102570 -0.022862689 1.401452e-02
## 2017-06-30 -0.0001830348 0.0092235491 0.0029559263 0.029151568 6.354617e-03
## 2017-07-31 0.0033349759 0.0565949613 0.0261876564 0.007481861 2.034579e-02
## 2017-08-31 0.0093689081 0.0232434682 -0.0004482935 -0.027564734 2.913517e-03
## 2017-09-29 -0.0057323373 -0.0004461961 0.0233427783 0.082321608 1.994901e-02
## 2017-10-31 0.0009780905 0.0322784749 0.0166536655 0.005916431 2.329055e-02
## 2017-11-30 -0.0014838908 -0.0038968556 0.0068702395 0.036912948 3.010813e-02
## 2017-12-29 0.0047397831 0.0369252995 0.0133981377 -0.003731357 1.205506e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398192e-05 0.0001042089 4.178418e-05 -7.811946e-05 -9.029085e-06
## EEM 1.042089e-04 0.0017547106 1.039016e-03 6.437761e-04 6.795438e-04
## EFA 4.178418e-05 0.0010390156 1.064236e-03 6.490320e-04 6.975413e-04
## IJS -7.811946e-05 0.0006437761 6.490320e-04 1.565449e-03 8.290253e-04
## SPY -9.029085e-06 0.0006795438 6.975413e-04 8.290253e-04 7.408304e-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.0003874106 0.009257142 0.005815635 0.005684476 0.002330253
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())
}