Path: blob/master/10_bayesian_machine_learning/04_rolling_regression.ipynb
2923 views
Rolling Linear Regression
We introduced pairs trading based on cointegration in Chapter 9.
An important implementation step involved the estimation of the hedge ratio to determine the relative size of offsetting positions.
In this notebook, we will explore how to compute this ratio using rolling Bayesian linear regression.
Simple Linear Regression Demo
Artificial Data
Model Definition
HMC Inference
Posterior Parameter Distributions
Linear Regression for Pairs Trading
Based on Thomas Wiecki's rolling regression example.
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.
First, lets define the hyper-priors for and . 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, , the intercept, changes over time.
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.