R Matrix Lab 4 — State Space and Kalman Filtering
R Matrix Lab 4 — State Space and Kalman Filtering
Question
How can a fixed-dimensional state summarise all past information needed for sequential prediction?
Use the linear Gaussian system
where
The filter carries only a state mean and covariance .
The two-stage recursion
Prediction:
Correction:
Example 1 — Local level with a missing observation
The local-level model is
so all matrices are scalar:
Implement a local-level Kalman filter by hand
At , uncertainty grows by but no observation reduces it. At , the larger prior variance gives the new measurement more weight.
Example 2 — AR(2) as a state-space forecast
For
use
The stationary state covariance solves the discrete Lyapunov equation
Vectorisation turns it into one linear solve:
Propagate an AR(2) state mean and forecast covariance
The mean decays toward zero because the companion eigenvalues lie inside the unit circle. Forecast variance rises toward because future innovations accumulate until the initial state is no longer informative.
From hand recursion to R's implementation
Base R exposes lower-level functions KalmanRun, KalmanForecast, KalmanSmooth, and KalmanLike. R's exact arima likelihood builds a state-space representation and evaluates innovations with a Kalman filter.
The hand implementation remains important: software output is interpretable only when you can identify , , , , and the initial covariance.
Exercises
- In Example 1, set . What happens to the gain over time, and why?
- Increase from 0.5 to 5. Predict the direction of change in the gain.
- Replace the missing observation with three consecutive missing values. Track .
- Verify the first two AR(2) point forecasts by scalar recursion.
- Change the AR coefficients so the companion spectral radius exceeds one. Which step of the stationary covariance solve fails conceptually, even if a numerical answer is returned?
- Graduate extension: add measurement error to the AR(2) observation equation and implement filtering rather than forecasting from an exactly observed state.
Checkpoints
- With no state noise, repeated observations make state uncertainty and gain decline toward zero.
- Noisier measurements receive less weight, so the gain decreases.
- Each missing time adds process uncertainty without correction.
- The first forecast is ; insert that value and 1.2 for the second.
- A finite stationary solution to requires a stable transition matrix.
Completion standard
You should be able to derive every line of a Kalman recursion, explain missing-data handling, and connect AR companion propagation with state-space prediction.
Return to the course guide or use the optional Python appendix only after the matrix derivations are secure.
R Matrix Lab 3 — Prediction and Gaussian Likelihood
Solve best-linear-prediction equations, verify Schur-complement uncertainty, and compare conditional, moment, and exact Gaussian AR estimates.
Optional Python Appendix — Empirical Forecasting
A supplementary Pyodide forecasting workflow for students who want to contrast matrix theory with an applied software pipeline.