|
| 1 | +# Expectation-Maximization for Kalman Filter Parameters |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +A Kalman filter requires four parameter matrices — the state-transition matrix $F$, measurement matrix $H$, process-noise covariance $Q$, and measurement-noise covariance $R$ — to be known *a priori*. In many real systems these parameters are unknown or only approximately known. |
| 6 | + |
| 7 | +The **Expectation-Maximization (EM) algorithm** for linear Gaussian state-space models (Shumway & Stoffer, 1982; 2000) treats the latent state sequence $x_{1:T}$ as missing data and iteratively finds the maximum-likelihood parameter estimate from a batch of observations $z_{1:T}$. Each iteration of EM is guaranteed to be non-decreasing in log-likelihood, providing a principled convergence criterion. |
| 8 | + |
| 9 | +This implementation follows the Shumway-Stoffer formulation and operates exclusively in `float` because typical dynamics (forces, velocities, torques) exceed the $[-1, 1]$ range of Q15/Q31 fixed-point formats. |
| 10 | + |
| 11 | +## Mathematical Theory |
| 12 | + |
| 13 | +### State-Space Model |
| 14 | + |
| 15 | +$$ |
| 16 | +x_{t+1} = F x_t + w_t, \quad w_t \sim \mathcal{N}(0, Q) |
| 17 | +$$ |
| 18 | + |
| 19 | +$$ |
| 20 | +z_t = H x_t + v_t, \quad v_t \sim \mathcal{N}(0, R) |
| 21 | +$$ |
| 22 | + |
| 23 | +with $x_0 \sim \mathcal{N}(\mu_0, P_0)$ and $T$ observations $z_0, \ldots, z_{T-1}$. |
| 24 | + |
| 25 | +### EM Overview |
| 26 | + |
| 27 | +EM alternates between: |
| 28 | + |
| 29 | +- **E-step**: compute expected sufficient statistics of $x_{1:T}$ given current parameters and all observations, using the RTS smoother. |
| 30 | +- **M-step**: analytically maximize the expected complete-data log-likelihood over parameters. |
| 31 | + |
| 32 | +### E-Step (RTS Smoother) |
| 33 | + |
| 34 | +Run `KalmanSmoother::Smooth()` to obtain, for each $t$: |
| 35 | + |
| 36 | +| Quantity | Notation | Description | |
| 37 | +|----------|----------|-------------| |
| 38 | +| $\hat{x}_{t\|T}$ | `smoothedMeans[t]` | Smoothed state mean | |
| 39 | +| $P_{t\|T}$ | `smoothedCovariances[t]` | Smoothed state covariance | |
| 40 | +| $P_{t,t-1\|T}$ | `lagCrossCovariances[t]` | Lag-1 cross-covariance (index 0 undefined) | |
| 41 | + |
| 42 | +### Sufficient Statistics |
| 43 | + |
| 44 | +From the smoother output, accumulate six matrices: |
| 45 | + |
| 46 | +$$ |
| 47 | +A = \sum_{t=1}^{T-1} P_{t,t-1|T} |
| 48 | +\qquad |
| 49 | +B = \sum_{t=0}^{T-2} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} |
| 50 | +$$ |
| 51 | + |
| 52 | +$$ |
| 53 | +C = \sum_{t=1}^{T-1} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} |
| 54 | +\qquad |
| 55 | +D = \sum_{t=0}^{T-1} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} |
| 56 | +$$ |
| 57 | + |
| 58 | +$$ |
| 59 | +W = \sum_{t=0}^{T-1} z_t \hat{x}_{t|T}^\top |
| 60 | +\qquad |
| 61 | +\Sigma_{zz} = \sum_{t=0}^{T-1} z_t z_t^\top |
| 62 | +$$ |
| 63 | + |
| 64 | +### M-Step (Closed-Form Updates) |
| 65 | + |
| 66 | +$$ |
| 67 | +F \leftarrow A B^{-1} |
| 68 | +$$ |
| 69 | + |
| 70 | +$$ |
| 71 | +Q \leftarrow \frac{C - F A^\top}{T - 1}, \quad Q \leftarrow \tfrac{1}{2}(Q + Q^\top) + \varepsilon I |
| 72 | +$$ |
| 73 | + |
| 74 | +$$ |
| 75 | +H \leftarrow W D^{-1} |
| 76 | +$$ |
| 77 | + |
| 78 | +$$ |
| 79 | +R \leftarrow \frac{\Sigma_{zz} - H W^\top}{T}, \quad R \leftarrow \tfrac{1}{2}(R + R^\top) + \varepsilon I |
| 80 | +$$ |
| 81 | + |
| 82 | +$$ |
| 83 | +\mu_0 \leftarrow \hat{x}_{0|T}, \quad P_0 \leftarrow P_{0|T} |
| 84 | +$$ |
| 85 | + |
| 86 | +The symmetrisation and ridge term $\varepsilon I$ ($\varepsilon = 10^{-6}$) enforce that $Q$ and $R$ remain symmetric positive definite, preventing numerical collapse. |
| 87 | + |
| 88 | +Matrix "division" — e.g. $A B^{-1}$ — is implemented as `SolveSystem(B, A^T)^T` (Gaussian elimination) to avoid explicit matrix inversion. |
| 89 | + |
| 90 | +### Convergence |
| 91 | + |
| 92 | +After each EM iteration, compute the log-likelihood $\ell(\theta)$ from the RTS smoother. Convergence is declared when |
| 93 | + |
| 94 | +$$ |
| 95 | +|\ell^{(k)} - \ell^{(k-1)}| < \delta |
| 96 | +$$ |
| 97 | + |
| 98 | +for a user-supplied tolerance $\delta$. |
| 99 | + |
| 100 | +## Complexity Analysis |
| 101 | + |
| 102 | +| Case | Time per iteration | Space (stack) | Notes | |
| 103 | +|-------|-----------------------|------------------------|-------| |
| 104 | +| All | $O(T \cdot N^3)$ | $O(T \cdot N^2)$ | Dominated by RTS smoother forward/backward | |
| 105 | + |
| 106 | +All storage is stack-allocated inside `KalmanSmoother` (owned by `ExpectationMaximization`) and in local M-step temporaries. |
| 107 | + |
| 108 | +## Step-by-Step Walkthrough |
| 109 | + |
| 110 | +Consider $N=1$, $M=1$, $T=3$, observations $z=[0.5, 0.3, 0.8]$, initial guess $F=1, H=1, Q=0.5, R=1, \mu_0=0, P_0=1$. |
| 111 | + |
| 112 | +**Iteration 1:** |
| 113 | + |
| 114 | +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. |
| 115 | +2. Accumulate scalars: |
| 116 | + - $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.)* |
| 117 | + - $B, C, D, W, \Sigma_{zz}$ as sums over the smoothed moments. |
| 118 | +3. M-step: $F \leftarrow A/B$, $Q \leftarrow (C - F\cdot A)/(T-1)$, etc. |
| 119 | +4. Evaluate new log-likelihood $\ell^{(1)}$. |
| 120 | + |
| 121 | +Repeat until $|\ell^{(k)} - \ell^{(k-1)}| < \delta$ or maximum iterations reached. |
| 122 | + |
| 123 | +## Pitfalls & Edge Cases |
| 124 | + |
| 125 | +- **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. |
| 126 | +- **Short sequences ($T \ll N$)**: With fewer observations than state dimensions, some sufficient-statistic matrices become rank-deficient and `SolveSystem` may produce inaccurate results. Prefer $T \gg N$. |
| 127 | +- **$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. |
| 128 | +- **Float only**: `ExpectationMaximization` has 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. |
| 129 | +- **Re-entrant smoother**: `ExpectationMaximization` owns a `KalmanSmoother` instance. The smoother state is overwritten on every call to `Run()`. Do not share an `ExpectationMaximization` instance across threads. |
| 130 | + |
| 131 | +## Variants & Generalizations |
| 132 | + |
| 133 | +- **EM for EKF/UKF**: Replace the RTS smoother with an extended or unscented smoother for nonlinear models. The M-step remains identical. |
| 134 | +- **Constrained EM**: Fix some parameters (e.g., $H$ is known) by simply not updating them in the M-step. |
| 135 | +- **Online (incremental) EM**: Accumulate sufficient statistics incrementally to handle streaming data. |
| 136 | +- **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. |
| 137 | + |
| 138 | +## Applications |
| 139 | + |
| 140 | +- **System identification**: Learn the dynamics $F$ and noise statistics $Q$, $R$ of an unknown linear system from input-output data. |
| 141 | +- **Sensor calibration**: Identify measurement noise covariance $R$ from reference measurements. |
| 142 | +- **Hyperparameter tuning for KF**: Replace hand-tuned $Q$, $R$ with data-driven estimates before deploying a real-time Kalman filter. |
| 143 | +- **Time-series model selection**: Compare log-likelihoods of different state-space structures to select model order. |
| 144 | + |
| 145 | +## Connections to Other Algorithms |
| 146 | + |
| 147 | +- **`filters::KalmanSmoother`**: Provides the complete E-step. `ExpectationMaximization` owns a `KalmanSmoother` instance internally. |
| 148 | +- **`filters::KalmanFilter`**: After EM converges, pass the resulting `EmParameters` to `KalmanFilter::SetStateTransition()`, `SetProcessNoise()`, and `SetMeasurementNoise()` for real-time deployment. |
| 149 | +- **`solvers::GaussianElimination`**: Used internally (via `KalmanSmoother`) for numerically stable matrix solves in both the smoother and the M-step. |
| 150 | + |
| 151 | +## References & Further Reading |
| 152 | + |
| 153 | +- 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. |
| 154 | +- Shumway, R. H., & Stoffer, D. S. (2000). *Time Series Analysis and Its Applications*. Springer. (Section 6.3) |
| 155 | +- 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. |
| 156 | +- Ghahramani, Z., & Hinton, G. E. (1996). Parameter estimation for linear dynamical systems. Technical Report CRG-TR-96-2, University of Toronto. |
0 commit comments