R Matrix Laboratory

R Matrix Lab 4 — State Space and Kalman Filtering

Implement Kalman prediction and correction from matrix equations, handle missing observations, and express AR forecasts as state propagation.

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

αt=Tαt1+Rηt,Yt=Zαt+εt,\boldsymbol\alpha_t=T\boldsymbol\alpha_{t-1}+R\boldsymbol\eta_t, \qquad Y_t=Z\boldsymbol\alpha_t+\varepsilon_t,

where

Var(ηt)=Q,Var(εt)=H.\operatorname{Var}(\boldsymbol\eta_t)=Q,\qquad \operatorname{Var}(\varepsilon_t)=H.

The filter carries only a state mean at\mathbf a_t and covariance PtP_t.

The two-stage recursion

Prediction:

att1=Tat1,Ptt1=TPt1T+RQR.\mathbf a_{t\mid t-1}=T\mathbf a_{t-1},\qquad P_{t\mid t-1}=TP_{t-1}T^\top+RQR^\top.

Correction:

vt=YtZatt1,Ft=ZPtt1Z+H,\mathbf v_t=Y_t-Z\mathbf a_{t\mid t-1},\qquad F_t=ZP_{t\mid t-1}Z^\top+H,Kt=Ptt1ZFt1,K_t=P_{t\mid t-1}Z^\top F_t^{-1},at=att1+Ktvt,Pt=Ptt1KtFtKt.\mathbf a_t=\mathbf a_{t\mid t-1}+K_t\mathbf v_t,\qquad P_t=P_{t\mid t-1}-K_tF_tK_t^\top.

Example 1 — Local level with a missing observation

The local-level model is

αt=αt1+ηt,Yt=αt+εt,\alpha_t=\alpha_{t-1}+\eta_t,\qquad Y_t=\alpha_t+\varepsilon_t,

so all matrices are scalar:

T=R=Z=1,Q=q,H=h.T=R=Z=1,\qquad Q=q,\qquad H=h.
R

Implement a local-level Kalman filter by hand

Idle

At t=4t=4, uncertainty grows by qq but no observation reduces it. At t=5t=5, the larger prior variance gives the new measurement more weight.

Example 2 — AR(2) as a state-space forecast

For

Xt=0.65Xt10.20Xt2+εt,X_t=0.65X_{t-1}-0.20X_{t-2}+\varepsilon_t,

use

T=[0.650.2010],R=[10],Z=[10].T= \begin{bmatrix} 0.65&-0.20\\ 1&0 \end{bmatrix}, \qquad R= \begin{bmatrix}1\\0\end{bmatrix}, \qquad Z=\begin{bmatrix}1&0\end{bmatrix}.

The stationary state covariance solves the discrete Lyapunov equation

P=TPT+σε2RR.P=TPT^\top+\sigma_\varepsilon^2RR^\top.

Vectorisation turns it into one linear solve:

vec(P)=(ITT)1vec(σε2RR).\operatorname{vec}(P) = \left(I-T\otimes T\right)^{-1} \operatorname{vec}(\sigma_\varepsilon^2RR^\top).
R

Propagate an AR(2) state mean and forecast covariance

Idle

The mean decays toward zero because the companion eigenvalues lie inside the unit circle. Forecast variance rises toward P11=γ(0)P_{11}=\gamma(0) 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 TT, ZZ, RQRRQR^\top, HH, and the initial covariance.

Exercises

  1. In Example 1, set q=0q=0. What happens to the gain over time, and why?
  2. Increase hh from 0.5 to 5. Predict the direction of change in the gain.
  3. Replace the missing t=4t=4 observation with three consecutive missing values. Track Ptt1P_{t\mid t-1}.
  4. Verify the first two AR(2) point forecasts by scalar recursion.
  5. 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?
  6. Graduate extension: add measurement error H>0H>0 to the AR(2) observation equation and implement filtering rather than forecasting from an exactly observed state.
Checkpoints
  1. With no state noise, repeated observations make state uncertainty and gain decline toward zero.
  2. Noisier measurements receive less weight, so the gain decreases.
  3. Each missing time adds process uncertainty without correction.
  4. The first forecast is 0.65(1.2)0.20(0.4)=0.700.65(1.2)-0.20(0.4)=0.70; insert that value and 1.2 for the second.
  5. A finite stationary solution to P=TPT+QP=TPT^\top+Q 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.

Copyright © 2026