Bayesian Rolling Regression

See the DLM for stocks page for an introduction to dynamic linear models. Here we can apply the same library but wrapped up in a convenient function called dlm.rolling.regression() which takes only two parameters, two or more independent variables in X and the single dependent variable in y. To test the function and demonstrate how it is used I generate a time-varying regression where I deliberately let the coefficients drift in a random walk over time. Then we can see how well the DLM performs fitting the time-varying regression using a Kalman filter.

Here’s the data generation bit. We have one dependent variable, y, and two dependent variables.

Now we can create a fairly generic function that wraps up the DLM code. This firstly estimates the observable and state variable volatility using an MLE estimate, then performs the filtration. Finally we plot the filtrations and confidence limits.

This is what the result looks like. You can see the thick red, green and blue lines are the true values of the time-varying coefficients. The Kalman filter in the DLM does a good job of dynamically tracking the coefficients as they drift. The true value of the coefficients lies within the error bands almost all of the time.