Repository navigation
feat: add em estimator and kf smoother #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||||||||||
| # Expectation-Maximization for Kalman Filter Parameters | ||||||||||||||
|
|
||||||||||||||
| ## Overview & Motivation | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| ## Mathematical Theory | ||||||||||||||
|
|
||||||||||||||
| ### State-Space Model | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| x_{t+1} = F x_t + w_t, \quad w_t \sim \mathcal{N}(0, Q) | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| z_t = H x_t + v_t, \quad v_t \sim \mathcal{N}(0, R) | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| with $x_0 \sim \mathcal{N}(\mu_0, P_0)$ and $T$ observations $z_0, \ldots, z_{T-1}$. | ||||||||||||||
|
|
||||||||||||||
| ### EM Overview | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| ### E-Step (RTS Smoother) | ||||||||||||||
|
|
||||||||||||||
| Run `KalmanSmoother::Smooth()` to obtain, for each $t$: | ||||||||||||||
|
|
||||||||||||||
| | Quantity | Notation | Description | | ||||||||||||||
| |----------|----------|-------------| | ||||||||||||||
| | $\hat{x}_{t\|T}$ | `smoothedMeans[t]` | Smoothed state mean | | ||||||||||||||
| | $P_{t\|T}$ | `smoothedCovariances[t]` | Smoothed state covariance | | ||||||||||||||
| | $P_{t,t-1\|T}$ | `lagCrossCovariances[t]` | Lag-1 cross-covariance (index 0 undefined) | | ||||||||||||||
|
|
||||||||||||||
| ### Sufficient Statistics | ||||||||||||||
|
|
||||||||||||||
| From the smoother output, accumulate six matrices: | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| A = \sum_{t=1}^{T-1} P_{t,t-1|T} | ||||||||||||||
| \qquad | ||||||||||||||
| B = \sum_{t=0}^{T-2} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| C = \sum_{t=1}^{T-1} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} | ||||||||||||||
| \qquad | ||||||||||||||
| D = \sum_{t=0}^{T-1} \hat{x}_{t|T}\hat{x}_{t|T}^\top + P_{t|T} | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| W = \sum_{t=0}^{T-1} z_t \hat{x}_{t|T}^\top | ||||||||||||||
| \qquad | ||||||||||||||
| \Sigma_{zz} = \sum_{t=0}^{T-1} z_t z_t^\top | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| ### M-Step (Closed-Form Updates) | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| F \leftarrow A B^{-1} | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| Q \leftarrow \frac{C - F A^\top}{T - 1}, \quad Q \leftarrow \tfrac{1}{2}(Q + Q^\top) + \varepsilon I | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| H \leftarrow W D^{-1} | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| R \leftarrow \frac{\Sigma_{zz} - H W^\top}{T}, \quad R \leftarrow \tfrac{1}{2}(R + R^\top) + \varepsilon I | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| \mu_0 \leftarrow \hat{x}_{0|T}, \quad P_0 \leftarrow P_{0|T} | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| The symmetrisation and ridge term $\varepsilon I$ ($\varepsilon = 10^{-6}$) enforce that $Q$ and $R$ remain symmetric positive definite, preventing numerical collapse. | ||||||||||||||
|
|
||||||||||||||
| Matrix "division" — e.g. $A B^{-1}$ — is implemented as `SolveSystem(B, A^T)^T` (Gaussian elimination) to avoid explicit matrix inversion. | ||||||||||||||
|
|
||||||||||||||
| ### Convergence | ||||||||||||||
|
|
||||||||||||||
| After each EM iteration, compute the log-likelihood $\ell(\theta)$ from the RTS smoother. Convergence is declared when | ||||||||||||||
|
|
||||||||||||||
| $$ | ||||||||||||||
| |\ell^{(k)} - \ell^{(k-1)}| < \delta | ||||||||||||||
| $$ | ||||||||||||||
|
|
||||||||||||||
| for a user-supplied tolerance $\delta$. | ||||||||||||||
|
|
||||||||||||||
| ## Complexity Analysis | ||||||||||||||
|
|
||||||||||||||
| | Case | Time per iteration | Space (stack) | Notes | | ||||||||||||||
| |-------|-----------------------|------------------------|-------| | ||||||||||||||
| | All | $O(T \cdot N^3)$ | $O(T \cdot N^2)$ | Dominated by RTS smoother forward/backward | | ||||||||||||||
|
Comment on lines
+102
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MegaLinter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| All storage is stack-allocated inside `KalmanSmoother` (owned by `ExpectationMaximization`) and in local M-step temporaries. | ||||||||||||||
|
|
||||||||||||||
| ## Step-by-Step Walkthrough | ||||||||||||||
|
|
||||||||||||||
| 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$. | ||||||||||||||
|
|
||||||||||||||
| **Iteration 1:** | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
| 2. 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. | ||||||||||||||
| 3. M-step: $F \leftarrow A/B$, $Q \leftarrow (C - F\cdot A)/(T-1)$, etc. | ||||||||||||||
| 4. Evaluate new log-likelihood $\ell^{(1)}$. | ||||||||||||||
|
|
||||||||||||||
| Repeat until $|\ell^{(k)} - \ell^{(k-1)}| < \delta$ or maximum iterations reached. | ||||||||||||||
|
|
||||||||||||||
| ## Pitfalls & Edge Cases | ||||||||||||||
|
|
||||||||||||||
| - **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 and `SolveSystem` may 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**: `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. | ||||||||||||||
| - **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. | ||||||||||||||
|
|
||||||||||||||
| ## Variants & Generalizations | ||||||||||||||
|
|
||||||||||||||
| - **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. | ||||||||||||||
|
|
||||||||||||||
| ## Applications | ||||||||||||||
|
|
||||||||||||||
| - **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. | ||||||||||||||
|
|
||||||||||||||
| ## Connections to Other Algorithms | ||||||||||||||
|
|
||||||||||||||
| - **`filters::KalmanSmoother`**: Provides the complete E-step. `ExpectationMaximization` owns a `KalmanSmoother` instance internally. | ||||||||||||||
| - **`filters::KalmanFilter`**: After EM converges, pass the resulting `EmParameters` to `KalmanFilter::SetStateTransition()`, `SetProcessNoise()`, and `SetMeasurementNoise()` for real-time deployment. | ||||||||||||||
| - **`solvers::GaussianElimination`**: Used internally (via `KalmanSmoother`) for numerically stable matrix solves in both the smoother and the M-step. | ||||||||||||||
|
|
||||||||||||||
| ## References & Further Reading | ||||||||||||||
|
|
||||||||||||||
| - 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. | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,10 +4,11 @@ Statistical estimation algorithms for fitting models to observed data and making | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Offline Estimators (Batch) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| | Algorithm | Description | | ||||||||||||||||||||||
| |------------------------------------------|-------------------------------------------------------------------------| | ||||||||||||||||||||||
| | [Linear Regression](LinearRegression.md) | Ordinary least-squares regression using the normal equation | | ||||||||||||||||||||||
| | [Yule-Walker](YuleWalker.md) | Autoregressive model parameter estimation via the Yule-Walker equations | | ||||||||||||||||||||||
| | Algorithm | Description | | ||||||||||||||||||||||
| |-------------------------------------------------------------------|-------------------------------------------------------------------------| | ||||||||||||||||||||||
| | [Linear Regression](LinearRegression.md) | Ordinary least-squares regression using the normal equation | | ||||||||||||||||||||||
| | [Yule-Walker](YuleWalker.md) | Autoregressive model parameter estimation via the Yule-Walker equations | | ||||||||||||||||||||||
| | [Expectation-Maximization](ExpectationMaximization.md) | EM algorithm for Kalman filter parameter identification (Shumway-Stoffer) | | ||||||||||||||||||||||
|
Comment on lines
+7
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MegaLinter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Online Estimators (Streaming) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MegaLinter] reported by reviewdog 🐶 numerical-toolbox-cpp/doc/filters/README.md Lines 11 to 15 in ef5cfb0
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||||||||||||||
| # Kalman Smoother (RTS Smoother) | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Overview & Motivation | ||||||||||||||||||
|
|
||||||||||||||||||
| The Kalman filter is a **causal** estimator: its state estimate at time $t$ uses only observations up to and including time $t$. When the full observation sequence is available (offline/batch setting), the **Rauch-Tung-Striebel (RTS) smoother** refines every filtered estimate by incorporating future measurements. The result is the minimum-mean-square-error (MMSE) state estimate given the *entire* sequence $z_{1:T}$. | ||||||||||||||||||
|
|
||||||||||||||||||
| The RTS smoother is the prerequisite for the Expectation-Maximization (EM) algorithm for Kalman Filter parameter identification: the E-step requires smoothed means, smoothed covariances, and the **lag-1 cross-covariance** $P_{t,t-1|T}$ in order to form the sufficient statistics used by the M-step. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Mathematical Theory | ||||||||||||||||||
|
|
||||||||||||||||||
| ### State-Space Model | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| x_{t+1} = F x_t + w_t, \quad w_t \sim \mathcal{N}(0, Q) | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| z_t = H x_t + v_t, \quad v_t \sim \mathcal{N}(0, R) | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| with $x_0 \sim \mathcal{N}(\mu_0, P_0)$. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Forward Pass (Kalman Filter) | ||||||||||||||||||
|
|
||||||||||||||||||
| For $t = 0, 1, \ldots, T-1$: | ||||||||||||||||||
|
|
||||||||||||||||||
| **Initialise** (t = 0 only): | ||||||||||||||||||
|
|
||||||||||||||||||
| $$\hat{x}_{0|-} = \mu_0, \quad P_{0|-} = P_0$$ | ||||||||||||||||||
|
|
||||||||||||||||||
| **Update:** | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| S_t = H P_{t|-} H^\top + R | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| K_t = P_{t|-} H^\top S_t^{-1} | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| \hat{x}_{t|t} = \hat{x}_{t|-} + K_t (z_t - H \hat{x}_{t|-}) | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| P_{t|t} = (I - K_t H) P_{t|-} (I - K_t H)^\top + K_t R K_t^\top \quad \text{(Joseph form)} | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| **Log-likelihood contribution:** | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| \ell_t = -\tfrac{1}{2}\bigl(\log\det S_t + (z_t - H\hat{x}_{t|-})^\top S_t^{-1} (z_t - H\hat{x}_{t|-}) + m\log 2\pi\bigr) | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| where $m$ = `MeasurementSize`. The total is $\ell = \sum_{t=0}^{T-1} \ell_t$. | ||||||||||||||||||
|
|
||||||||||||||||||
| **Predict** (for $t < T-1$): | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| \hat{x}_{t+1|-} = F \hat{x}_{t|t}, \quad P_{t+1|-} = F P_{t|t} F^\top + Q | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Backward Pass (RTS Smoother) | ||||||||||||||||||
|
|
||||||||||||||||||
| Initialise at $t = T-1$: | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| \hat{x}_{T-1|T} = \hat{x}_{T-1|T-1}, \quad P_{T-1|T} = P_{T-1|T-1} | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| For $t = T-2, T-3, \ldots, 0$: | ||||||||||||||||||
|
|
||||||||||||||||||
| **Smoother gain:** | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| G_t = P_{t|t} F^\top P_{t+1|t}^{-1} | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| **Smoothed mean and covariance:** | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| \hat{x}_{t|T} = \hat{x}_{t|t} + G_t (\hat{x}_{t+1|T} - \hat{x}_{t+1|t}) | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| P_{t|T} = P_{t|t} + G_t (P_{t+1|T} - P_{t+1|t}) G_t^\top | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Lag-1 Cross-Covariance | ||||||||||||||||||
|
|
||||||||||||||||||
| The EM M-step requires $P_{t,t-1|T} = \mathrm{Cov}(x_t, x_{t-1} \mid z_{1:T})$. | ||||||||||||||||||
|
|
||||||||||||||||||
| **Initialisation** (Shumway & Stoffer, 1982): | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| P_{T-1, T-2|T} = (I - K_{T-1} H) F P_{T-2|T-2} | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| **Recursion** for $t = T-3, \ldots, 0$: | ||||||||||||||||||
|
|
||||||||||||||||||
| $$ | ||||||||||||||||||
| P_{t+1, t|T} = P_{t+1|t+1} G_t^\top + G_{t+1} (P_{t+2, t+1|T} - F P_{t+1|t+1}) G_t^\top | ||||||||||||||||||
| $$ | ||||||||||||||||||
|
|
||||||||||||||||||
| Note: $P_{0,-1|T}$ is undefined; index 0 of `lagCrossCovariances` is always zero. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Complexity Analysis | ||||||||||||||||||
|
|
||||||||||||||||||
| | Case | Time | Space | Notes | | ||||||||||||||||||
| |-------|----------------|--------------------------------|-------------------------------| | ||||||||||||||||||
| | All | $O(T \cdot N^3)$ | $O(T \cdot N^2)$ stack | Dominant cost: $N\times N$ solves at each step | | ||||||||||||||||||
|
Comment on lines
+109
to
+111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MegaLinter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| All storage is stack-allocated via `std::array`. There is no heap usage. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Step-by-Step Walkthrough | ||||||||||||||||||
|
|
||||||||||||||||||
| Consider a 1-D constant-position model ($N=1$, $M=1$, $T=3$) with $F=1$, $H=1$, $Q=0.1$, $R=1$, $\mu_0=0$, $P_0=1$, and observations $z = [0.5, 0.3, 0.8]$. | ||||||||||||||||||
|
|
||||||||||||||||||
| **Forward pass:** | ||||||||||||||||||
|
|
||||||||||||||||||
| | $t$ | $P_{t\|-}$ | $S_t$ | $K_t$ | $\hat{x}_{t\|t}$ | $P_{t\|t}$ | | ||||||||||||||||||
| |-----|-----------|-------|--------|------------------|-----------| | ||||||||||||||||||
| | 0 | 1.0 | 2.0 | 0.500 | 0.250 | 0.500 | | ||||||||||||||||||
| | 1 | 0.600 | 1.600 | 0.375 | 0.363 | 0.375 | | ||||||||||||||||||
| | 2 | 0.475 | 1.475 | 0.322 | 0.620 | 0.322 | | ||||||||||||||||||
|
Comment on lines
+122
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MegaLinter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| **Backward pass ($t=1$ then $t=0$):** | ||||||||||||||||||
|
|
||||||||||||||||||
| $t=1$: $G_1 = 0.500/0.600 = 0.833$; $\hat{x}_{1|3} = 0.363 + 0.833(0.620 - 0.363) \approx 0.577$ | ||||||||||||||||||
|
|
||||||||||||||||||
| $t=0$: $G_0 = 0.375/0.375 = 1.000$; $\hat{x}_{0|3} = 0.250 + 1.000(0.577 - 0.363) \approx 0.464$ | ||||||||||||||||||
|
|
||||||||||||||||||
| Notice how the smoothed $\hat{x}_{0|3}=0.464$ incorporates all three observations, whereas the filtered $\hat{x}_{0|0}=0.250$ only used the first. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Pitfalls & Edge Cases | ||||||||||||||||||
|
|
||||||||||||||||||
| - **Near-singular $P_{t+1|t}$**: The smoother gain is computed via `SolveSystem` (Gaussian elimination with partial pivoting) rather than explicit matrix inversion. If the predicted covariance becomes near-singular (possible when $Q \approx 0$), numerical accuracy degrades. Ensure $Q$ has positive diagonal entries. | ||||||||||||||||||
| - **Joseph-form update**: The standard update $P = (I-KH)P$ is numerically unstable for finite-precision arithmetic. This implementation uses the Joseph form to maintain positive semi-definiteness. | ||||||||||||||||||
| - **`lagCrossCovariances[0]` is always zero**: The lag-1 cross-covariance for index 0 ($P_{0,-1|T}$) is undefined. The first valid entry is index 1 ($P_{1,0|T}$). | ||||||||||||||||||
| - **Unsigned loop guard**: The backward loop uses `std::size_t`; the break guard `if (t == 0) break` prevents unsigned wrap-around. | ||||||||||||||||||
| - **Stack usage**: For large $N$ or $T$, the five internal `std::array` members dominate stack usage. Choose `MaxSteps` conservatively on resource-constrained targets. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Variants & Generalizations | ||||||||||||||||||
|
|
||||||||||||||||||
| - **Square-root RTS smoother**: Propagates Cholesky factors instead of full covariance matrices for improved numerical conditioning. | ||||||||||||||||||
| - **Information filter smoother**: Operates in the dual (information) domain; preferable when observations are dense relative to state transitions. | ||||||||||||||||||
| - **Extended / Unscented smoother**: Replace the linear forward pass with EKF or UKF; the backward pass equations remain identical in form. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Applications | ||||||||||||||||||
|
|
||||||||||||||||||
| - **EM parameter identification**: Used as the E-step by `ExpectationMaximization` to compute the sufficient statistics $\{A, B, C, D, W, \Sigma_{zz}\}$ for the M-step. | ||||||||||||||||||
| - **Offline trajectory estimation**: Position/velocity smoothing in GNSS post-processing and inertial navigation. | ||||||||||||||||||
| - **Batch signal denoising**: Any application where the full observation sequence is available before estimation begins. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Connections to Other Algorithms | ||||||||||||||||||
|
|
||||||||||||||||||
| - **`filters::KalmanFilter`**: The forward pass of `KalmanSmoother` replicates the KF update equations. The smoother is applied *after* the filter, not *instead of* it. | ||||||||||||||||||
| - **`estimators::ExpectationMaximization`**: The smoother is owned and invoked by `ExpectationMaximization` as its E-step. Users who only need EM should interact with `ExpectationMaximization` directly. | ||||||||||||||||||
| - **`solvers::GaussianElimination`**: Used for all matrix "division" operations (smoother gain, Kalman gain) to avoid explicit matrix inversion. | ||||||||||||||||||
| - **`solvers::CholeskyDecomposition`**: Used to compute $\log \det S_t$ for the log-likelihood. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## References & Further Reading | ||||||||||||||||||
|
|
||||||||||||||||||
| - Rauch, H. E., Tung, F., & Striebel, C. T. (1965). Maximum likelihood estimates of linear dynamic systems. *AIAA Journal*, 3(8), 1445–1450. | ||||||||||||||||||
| - 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. (Chapter 6) | ||||||||||||||||||
| - Särkkä, S. (2013). *Bayesian Filtering and Smoothing*. Cambridge University Press. (Chapter 8) | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include "numerical/estimators/offline/ExpectationMaximization.hpp" | ||
|
|
||
| namespace estimators | ||
| { | ||
| template class ExpectationMaximization<2, 1, 10>; | ||
| template class ExpectationMaximization<4, 2, 20>; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MegaLinter] reported by reviewdog 🐶