<- c("SP500", "DTWEXAFEGS") # "SP500" does not contain dividends; note: "DTWEXM" discontinued as of Jan 2020
factors_r <- c("DGS10", "BAMLH0A0HYM2") factors_d
Price momentum
One month reversal and 2-12 month momentum are two ends of the spectrum. The general trend indicates that positive acceleration leads to reversals or negative acceleration leads to rebounds. An unsustainable acceleration leading to reversal can reconcile the one-month reversal and 2-12 month momentum. The key is that it implies that acceleration is not sustainable.
<- 20 order
# "Momentum, Acceleration, and Reversal"
<- na.omit(lag(roll_prod(1 + returns_xts, width - order, min_obs = 1) - 1, order)) momentum_xts
Time-series score
Suppose we are looking at \(n\) independent and identically distributed random variables, \(X_{1},X_{2},\ldots,X_{n}\). Since they are iid, each random variable \(X_{i}\) has to have the same mean, which we will call \(\mu\), and variance, which we will call \(\sigma^{2}\):
\[ \begin{aligned} \mathrm{E}\left(X_{i}\right)&=\mu\\ \mathrm{Var}\left(X_{i}\right)&=\sigma^{2} \end{aligned} \]
Let’s suppose we want to look at the average value of our \(n\) random variables:
\[ \begin{aligned} \bar{X}=\frac{X_{1}+X_{2}+\cdots+X_{n}}{n}=\left(\frac{1}{n}\right)\left(X_{1}+X_{2}+\cdots+X_{n}\right) \end{aligned} \]
We want to find the expected value and variance of the average, \(\mathrm{E}\left(\bar{X}\right)\) and \(\mathrm{Var}\left(\bar{X}\right)\).
Expected value
\[ \begin{aligned} \mathrm{E}\left(\bar{X}\right)&=\mathrm{E}\left[\left(\frac{1}{n}\right)\left(X_{1}+X_{2}+\cdots+X_{n}\right)\right]\\ &=\left(\frac{1}{n}\right)\mathrm{E}\left(X_{1}+X_{2}+\cdots+X_{n}\right)\\ &=\left(\frac{1}{n}\right)\left(n\mu\right)\\ &=\mu \end{aligned} \]
Variance
\[ \begin{aligned} \mathrm{Var}\left(\bar{X}\right)&=\mathrm{Var}\left[\left(\frac{1}{n}\right)\left(X_{1}+X_{2}+\cdots+X_{n}\right)\right]\\ &=\left(\frac{1}{n}\right)^{2}\mathrm{Var}\left(X_{1}+X_{2}+\cdots+X_{n}\right)\\ &=\left(\frac{1}{n}\right)^{2}\left(n\sigma^{2}\right)\\ &=\frac{\sigma^{2}}{n} \end{aligned} \]
# volatility scale only
<- na.omit(momentum_xts / roll_sd(momentum_xts, width, center = FALSE, min_obs = 1)) score_xts
# overall_xts <- xts(rowMeans(score_xts), index(score_xts))
# overall_xts <- overall_xts / roll_sd(overall_xts, width, center = FALSE, min_obs = 1)
# colnames(overall_xts) <- "Overall"
# score_xts <- na.omit(merge(overall_xts, score_xts))
Outlier detection
Interquartile range
Outliers are defined as the regression residuals that fall below \(Q_{1}−1.5\times IQR\) or above \(Q_{3}+1.5\times IQR\):
- https://stats.stackexchange.com/a/1153
- https://stats.stackexchange.com/a/108951
- https://robjhyndman.com/hyndsight/tsoutliers/
<- function(z) {
outliers
<- ncol(z)
n_cols <- list()
result_ls
for (j in 1:n_cols) {
<- z[ , j]
y
if (n_cols == 1) {
<- 1:length(y)
x else {
} <- cbind(1:length(y), z[ , -j])
x
}
<- coef(lm(y ~ x))
coef <- coef[1] + x %*% as.matrix(coef[-1])
predict <- y - predict
resid
<- quantile(resid, prob = 0.25)
lower <- quantile(resid, prob = 0.75)
upper <- upper - lower
iqr
<- y[(resid < lower - 1.5 * iqr) | (resid > upper + 1.5 * iqr)]
total
<- append(result_ls, list(total))
result_ls
}
<- do.call(merge, result_ls)
result
return(result)
}
<- outliers(score_xts) outliers_xts
Contour ellipsoid
Granger causality
\[ \begin{aligned} \left(R\hat{\beta}-r\right)^\mathrm{T}\left(R\hat{V}R^\mathrm{T}\right)^{-1}\left(R\hat{\beta}-r\right)\xrightarrow\quad\chi_{Q}^{2} \end{aligned} \]
- https://github.com/cran/lmtest/blob/master/R/waldtest.R
- https://en.wikipedia.org/wiki/Wald_test#Test(s)_on_multiple_parameters
- https://math.stackexchange.com/a/1591946
<- function(x, y, order) {
granger_test
# compute lagged observations
<- lag(x, order)
lag_x <- lag(y, order)
lag_y
# collect series
<- merge(x, y, lag_x, lag_y)
data colnames(data) <- c("x", "y", "lag_x", "lag_y")
# fit full model
<- lm(y ~ lag_y + lag_x, data = data)
fit
<- matrix(c(0, 0, 1), nrow = 1)
R <- fit$coefficients
coef <- 0 # technically a matrix (see Stack Exchange)
r
<- t(R %*% coef - r) %*% solve(R %*% vcov(fit) %*% t(R)) %*% (R %*% coef - r)
wald
<- 1 - pchisq(wald, 1)
result
return(result)
}
<- function(x, y, width, order, p_value) {
roll_lead_lag
<- nrow(x)
n_rows <- names(x)
x_name <- names(y)
y_name <- list()
x_y_ls <- list()
y_x_ls
for (i in width:n_rows) {
<- max(i - width + 1, 1):i
idx
<- granger_test(x[idx], y[idx], order)
x_y <- granger_test(y[idx], x[idx], order)
y_x
<- (x_y < p_value) && (y_x > p_value)
x_y_status <- (x_y > p_value) && (y_x < p_value)
y_x_status
<- append(x_y_ls, list(x_y_status))
x_y_ls <- append(y_x_ls, list(y_x_status))
y_x_ls
}
<- data.frame(do.call(c, x_y_ls), do.call(c, y_x_ls))
result <- xts(result, index(x)[width:n_rows])
result colnames(result) <- c(x_name, y_name)
return(result)
}
<- 0.05 p_value
<- score_xts[ , "SP500"]
score_x_xts <- score_xts[ , "DGS10"] score_y_xts
<- roll_lead_lag(score_x_xts, score_y_xts, width, order, p_value) lead_lag_xts