(GLM-rolling-regression)=

Rolling Regression

:::{post} Jan 28, 2023 :tags: generalized linear model, regression :category: intermediate :author: Thomas Wiecki :::

  • Pairs trading is a famous technique in algorithmic trading that plays two stocks against each other.
  • For this to work, stocks must be correlated (cointegrated).
  • One common example is the price of gold (GLD) and the price of gold mining operations (GFI).

Lets load the prices of GFI and GLD.

Plotting the prices over time suggests a strong correlation. However, the correlation seems to change over time.

A naive approach would be to estimate a linear model and ignore the time domain.

The posterior predictive plot shows how bad the fit is.

Rolling regression

Next, we will build an improved model that will allow for changes in the regression coefficients over time. Specifically, we will assume that intercept and slope follow a random-walk through time. That idea is similar to the {doc}case_studies/stochastic_volatility.

αtN(αt1,σα2)\alpha_t \sim \mathcal{N}(\alpha_{t-1}, \sigma_\alpha^2) βtN(βt1,σβ2)\beta_t \sim \mathcal{N}(\beta_{t-1}, \sigma_\beta^2)

First, lets define the hyper-priors for $\sigma_\alpha^2$ and $\sigma_\beta^2$. This parameter can be interpreted as the volatility in the regression coefficients.

Perform the regression given coefficients and data and link to the data via the likelihood.

Inference. Despite this being quite a complex model, NUTS handles it wells.

Increasing the tree-depth does indeed help but it makes sampling very slow. The results look identical with this run, however.

Analysis of results

As can be seen below, $\alpha$, the intercept, changes over time.

As does the slope.

The posterior predictive plot shows that we capture the change in regression over time much better. Note that we should have used returns instead of prices. The model would still work the same, but the visualisations would not be quite as clear.

Authors

Watermark

:::{include} ../page_footer.md :::