A Kalman filter requires four parameter matrices — the state-transition matrix
The Expectation-Maximization (EM) algorithm for linear Gaussian state-space models (Shumway & Stoffer, 1982; 2000) treats the latent state sequence
This implementation follows the Shumway-Stoffer formulation and operates exclusively in float because typical dynamics (forces, velocities, torques) exceed the
with
EM alternates between:
-
E-step: compute expected sufficient statistics of
$x_{1:T}$ given current parameters and all observations, using the RTS smoother. - M-step: analytically maximize the expected complete-data log-likelihood over parameters.
Run KalmanSmoother::Smooth() to obtain, for each
| Quantity | Notation | Description |
|---|---|---|
smoothedMeans[t] |
Smoothed state mean | |
smoothedCovariances[t] |
Smoothed state covariance | |
lagCrossCovariances[t] |
Lag-1 cross-covariance (index 0 undefined) |
From the smoother output, accumulate six matrices:
The symmetrisation and ridge term
Matrix "division" — e.g. SolveSystem(B, A^T)^T (Gaussian elimination) to avoid explicit matrix inversion.
After each EM iteration, compute the log-likelihood
for a user-supplied tolerance
| Case | Time per iteration | Space (stack) | Notes |
|---|---|---|---|
| All | Dominated by RTS smoother forward/backward |
All storage is stack-allocated inside KalmanSmoother (owned by ExpectationMaximization) and in local M-step temporaries.
Consider
Iteration 1:
- E-step via RTS smoother: obtain $\hat{x}{0|3}, \hat{x}{1|3}, \hat{x}_{2|3}$ and corresponding covariances and lag cross-covariances.
- Accumulate scalars:
-
$A = P_{2,1|3} + P_{3,2|3}$ (indices 2 and 3 in the 1-indexed convention). (In the implementation indices 1..T-1 are used.) -
$B, C, D, W, \Sigma_{zz}$ as sums over the smoothed moments.
-
- M-step:
$F \leftarrow A/B$ ,$Q \leftarrow (C - F\cdot A)/(T-1)$ , etc. - Evaluate new log-likelihood
$\ell^{(1)}$ .
Repeat until
- Local optima: EM converges to a local maximum of the likelihood, not necessarily the global maximum. The quality of the solution depends heavily on the initial parameter guess.
-
Short sequences (
$T \ll N$ ): With fewer observations than state dimensions, some sufficient-statistic matrices become rank-deficient andSolveSystemmay produce inaccurate results. Prefer$T \gg N$ . -
$Q$ or$R$ collapse: Without the$\varepsilon I$ regularisation, a maximally tight model (zero-noise) can collapse$Q$ or$R$ to zero, making the likelihood degenerate. If this happens, increase the regularisation or constrain the parameters. -
Float only:
ExpectationMaximizationhas no template type parameter. Dynamics values typically exceed the$[-1, 1]$ range of Q15/Q31. Scale your state variables into$[-1, 1]$ and use a float wrapper if fixed-point EM is required. -
Re-entrant smoother:
ExpectationMaximizationowns aKalmanSmootherinstance. The smoother state is overwritten on every call toRun(). Do not share anExpectationMaximizationinstance across threads.
- EM for EKF/UKF: Replace the RTS smoother with an extended or unscented smoother for nonlinear models. The M-step remains identical.
-
Constrained EM: Fix some parameters (e.g.,
$H$ is known) by simply not updating them in the M-step. - Online (incremental) EM: Accumulate sufficient statistics incrementally to handle streaming data.
-
Structural constraints on
$F$ : If$F$ has a known parametric form (e.g., a companion matrix), the M-step can be modified to enforce the structure.
-
System identification: Learn the dynamics
$F$ and noise statistics$Q$ ,$R$ of an unknown linear system from input-output data. -
Sensor calibration: Identify measurement noise covariance
$R$ from reference measurements. -
Hyperparameter tuning for KF: Replace hand-tuned
$Q$ ,$R$ with data-driven estimates before deploying a real-time Kalman filter. - Time-series model selection: Compare log-likelihoods of different state-space structures to select model order.
filters::KalmanSmoother: Provides the complete E-step.ExpectationMaximizationowns aKalmanSmootherinstance internally.filters::KalmanFilter: After EM converges, pass the resultingEmParameterstoKalmanFilter::SetStateTransition(),SetProcessNoise(), andSetMeasurementNoise()for real-time deployment.solvers::GaussianElimination: Used internally (viaKalmanSmoother) for numerically stable matrix solves in both the smoother and the M-step.
- Shumway, R. H., & Stoffer, D. S. (1982). An approach to time series smoothing and forecasting using the EM algorithm. Journal of Time Series Analysis, 3(4), 253–264.
- Shumway, R. H., & Stoffer, D. S. (2000). Time Series Analysis and Its Applications. Springer. (Section 6.3)
- Dempster, A. P., Laird, N. M., & Rubin, D. B. (1977). Maximum likelihood from incomplete data via the EM algorithm. Journal of the Royal Statistical Society, Series B, 39(1), 1–38.
- Ghahramani, Z., & Hinton, G. E. (1996). Parameter estimation for linear dynamical systems. Technical Report CRG-TR-96-2, University of Toronto.