Time series analysis: Lab 3

Recursive least squares and the Kalman filter for parameters that change over time, tested on simulated data, then on a year of Svedala temperatures, where a fixed winter model still predicts best.

PythonNumPySciPyMatplotlibJupyter 2026

The third MASM17 lab drops the assumption that a model's parameters stay put. It estimates them recursively, one sample at a time, first with recursive least squares and then with a Kalman filter, on simulated data where the true parameter path is known. Then it asks whether any of this helps on a year of temperatures from Svedala. Mostly it does not, and the reasons are the useful part.

Forgetting old data

The first data set is 500 samples of an AR(2) process in which $a_1$ climbs from −1.5 to 1.5 over 300 samples while $a_2$ stays at 0.7. Recursive least squares follows a change like that only if it discounts old data. Its forgetting factor $\lambda$ gives the filter a memory of roughly $1/(1 - \lambda)$ samples.

With $\lambda = 1$ nothing is forgotten, and the estimate of $a_1$ ends at 0.08 when the truth is 1.5. It drags the constant parameter down with it: $\hat a_2$ ends at −0.33. With $\lambda = 0.95$, a memory of 20 samples, the estimate follows the ramp a short step behind. With $\lambda = 0.9$ it follows faster and is noisier.

Choosing $\lambda$ by the sum of squared one-step prediction errors gives 0.941. The curve is flat around its minimum, so anything from 0.92 to 0.96 does about as well.

The Kalman filter

Recursive least squares has one $\lambda$ for every parameter, so it cannot be told that $a_1$ moves and $a_2$ does not. The Kalman filter can. The parameters become the state, each a random walk, and the data enters through the observation equation:

$$x_t = \begin{bmatrix} a_1 \\ a_2 \end{bmatrix}, \qquad x_t = x_{t-1} + e_t, \qquad y_t = \begin{bmatrix} -y_{t-1} & -y_{t-2} \end{bmatrix} x_t + w_t.$$

The covariance of $e_t$ says how far each parameter may move per step. Setting it to 0.004 for $a_1$ and to zero for $a_2$ gives a filter that tracks the ramp and lets $\hat a_2$ settle, ending at 0.693.

Recursive least squares Kalman filter
Sum of squared prediction errors 631.0 606.7
Mean squared error of $\hat a_1$ 0.031 0.028

Recursive least squares runs here with its best forgetting factor, 0.941. The gain is modest but consistent. What matters in the tuning is the ratio of the two noise variances: a large one gives a fast, nervous filter, a small one a slow, smooth one. Of twelve combinations tried, none beat the starting values.

Predicting with the filter

Prediction with a Kalman filter does not use the polynomial division of the previous lab. It steps the state forward instead. The two-step prediction of an AR(2) needs $y_{t+1}$, which has not arrived yet, so its one-step prediction takes its place:

$$\hat y_{t+2 \mid t} = \begin{bmatrix} -\hat y_{t+1 \mid t} & -y_t \end{bmatrix} \hat x_{t \mid t}.$$

On 10,000 simulated samples with noise variance 0.01, the squared one-step errors over the last 100 samples sum to 0.993, against 1 in theory. The two-step errors sum to 1.694, against $1 + 0.8^2 = 1.64$. The states converge to −0.812 and 0.212, where the truth is −0.8 and 0.2. An ordinary least squares fit of the same data gives −0.809 and 0.213, so the small gap comes from the data and not from the filter.

A parameter that cannot be seen yet

The fourth exercise hides a level and a gain in the same measurement. A quality variable drifts as a random walk, $x_t = x_{t-1} + e_t$, and is observed as

$$y_t = x_t + b\,u_t + v_t,$$

where $u_t$ is a known input that switches between +1 and −1 as a slow Markov chain. Both $x_t$ and $b = 20$ are unknown, so both go into the state.

The filter ends at $\hat b = 19.83$. How it gets there is the interesting part. While the input sits at +1, the measurement is $x_t + b$, and nothing in the data can say how to split that sum. The filter divides it evenly and holds $\hat b$ near 10. At sample 6 the input switches for the first time, and the estimate jumps to 19.6 in a single step. A parameter can only be estimated once its input has moved.

The filter also passes its own consistency check: the one-step prediction error has variance 7.02, and the filter expects 6.59.

A year of temperatures

The real data is the temperature at Svedala every four hours through 1994, 2190 samples, so a season of 6 is one day. Building a model on January to March, one term at a time, leads to the structure the lab suggests:

$$\nabla_6\,(1 + a_1z^{-1} + a_2z^{-2} + a_3z^{-3})\,y_t = (1 + c_6z^{-6})\,e_t.$$

An AR(1) on the differenced data leaves a residual variance of 1.66 and a spike at lag 6. Adding $c_6$ brings it to 1.00, and widening to AR(3) to 0.96, with every parameter significant.

Winter Summer Whole year
$a_1$ −1.032 −0.831 −0.930
$a_2$ 0.284 0.205 0.228
$a_3$ −0.165 −0.200 −0.160
$c_6$ −0.910 −0.802 −0.846
Residual variance 0.96 2.68 1.81

Between winter and summer $a_1$ and $c_6$ shift by about three standard errors, and the noise variance almost triples. That looks like a case for letting the parameters vary, so the Kalman filter is started from the winter estimates and run through the year, with the last 401 samples, late October to December, kept for validation.

Prediction error variance 1 step 2 steps 3 steps
Naive, the last measured value 1.299 2.783 3.983
Kalman filter, all parameters free 1.263 2.800 4.007
Kalman filter, $c_6$ fixed 1.115 2.458 3.417
Fixed winter model 1.110 2.455 3.451

The adaptive filter loses. With all four parameters free it barely beats the naive predictor one step ahead and falls behind it at two and three steps. The fixed winter model, which never sees the summer, does best at one and two steps, and its residual on the validation data passes all four whiteness tests.

The parameter plot shows what went wrong. Through spring and summer $\hat c_6$ drifts from −0.91 to about −0.6, further than the summer fit itself would justify, and it has not returned when the validation period starts. Holding $c_6$ fixed, and letting the AR parameters move ten times more slowly, brings the filter level with the winter model. That choice was made after looking at the validation data, so its figures are a little flattering.

There are good reasons for the fixed model to hold up. The validation period is late autumn, with weather much like the winter the model was fitted on. The daily difference already removes both the daily cycle and the slow change in level over the year. What changes most between seasons is the noise variance, and that does not enter the predictor at all. Every parameter left free adds estimation noise of its own. In the middle of the year a very slow filter does edge ahead, 2.39 against 2.48, but the gain is small.

What this involved

A Kalman filter written from the lab's outline and extended three ways: to two unknown states with a known input, to an ARMA model whose noise terms are replaced by the filter's own prediction errors, and to predictions two and three steps ahead. Recursive least squares and the fixed-model estimates came from the course's Python package. Each filter was checked on simulated data with known parameters before it was run on the temperatures.


Recursive least squares estimates of $a_1$. With $\lambda = 1$ every sample counts equally and the estimate ends at 0.08, far from 1.5. With $\lambda = 0.95$ it follows the ramp a short step behind.
Recursive least squares estimates of $a_1$. With $\lambda = 1$ every sample counts equally and the estimate ends at 0.08, far from 1.5. With $\lambda = 0.95$ it follows the ramp a short step behind.
Estimates of the constant $a_2$. Recursive least squares forgets at one rate for both parameters, so $\hat a_2$ stays noisy. The Kalman filter is told that $a_2$ does not move.
Estimates of the constant $a_2$. Recursive least squares forgets at one rate for both parameters, so $\hat a_2$ stays noisy. The Kalman filter is told that $a_2$ does not move.
The first 40 estimates of the gain $b$. While the input sits at +1 the filter cannot separate $b$ from the level and splits the sum evenly. One switch of the input is enough.
The first 40 estimates of the gain $b$. While the input sits at +1 the filter cannot separate $b$ from the level and splits the sum evenly. One switch of the input is enough.
The Kalman estimate of $c_6$ through 1994, started at the winter value. It has moved to about −0.6 by October and has not returned when the validation period, shaded, begins.
The Kalman estimate of $c_6$ through 1994, started at the winter value. It has moved to about −0.6 by October and has not returned when the validation period, shaded, begins.
One-step prediction error variance on the last 401 samples. The filter with every parameter free barely beats the naive predictor; with $c_6$ held fixed it matches the winter model.
One-step prediction error variance on the last 401 samples. The filter with every parameter free barely beats the naive predictor; with $c_6$ held fixed it matches the winter model.