Project documentation

Fixed-size C++23 state estimation with a thin Python interface.

RTS smoother + Gaussian confidence bands

The Rauch-Tung-Striebel smoother computes the posterior mean at every step conditioned on all measurements — not just the past. Given the forward filter's filtered states x_filt[t] and predicted states x_pred[t], the backward pass produces x_smooth[t] that's at least as good as the filtered estimate everywhere, and substantially better in the middle of the trajectory where future data is now available.

kf.smooth(measurements, ...)

result = kf.smooth(
    measurements,
    delta_t=None,                 # only on templated; runtime Dyn rejects
    measurement_variance=None,    # only on templated; runtime Dyn rejects
    record_covariances=False,
    eps=1e-15,
)
result["filtered_states"]   # (N, nx) forward pass
result["smoothed_states"]   # (N, nx) backward pass
result["filtered_covariances"]  # (N, nx, nx)  if record_covariances=True
result["smoothed_covariances"]  # (N, nx, nx)  if record_covariances=True
result["ok"]                # False if any P_pred was singular

The algorithm

Forward pass (the usual filter loop):

for t = 0..N-1:
    x_pred[t], P_pred[t] = predict(x_filt[t-1], P_filt[t-1])
    x_filt[t], P_filt[t] = correct(x_pred[t], P_pred[t], z[t])

Backward pass (starting from x_smooth[N-1] = x_filt[N-1]):

for t = N-2..0:
    C[t]        = P_filt[t] · Fᵀ · inv(P_pred[t+1])
    x_smooth[t] = x_filt[t] + C[t] · (x_smooth[t+1] − x_pred[t+1])
    P_smooth[t] = P_filt[t] + C[t] · (P_smooth[t+1] − P_pred[t+1]) · C[t]ᵀ

P_smooth[t] is a strict information improvement over P_filt[t] whenever P_pred[t+1] is full rank.

smooth_with_bands(kf, measurements, levels=(0.95,))

Runs kf.smooth(record_covariances=True) and adds per-step Gaussian confidence bands at each level you pass:

result = kalman.smooth_with_bands(
    kf, measurements,
    levels=[0.75, 0.90, 0.95, 0.99, 0.999],
)
result["levels"]       # (L,) clamped coverage levels actually used
result["z_scores"]     # (L,) inv_cdf(0.5 * (1 + level))
result["bands_upper"]  # (N, nx, L) — smoothed mean + z · sqrt(diag P)
result["bands_lower"]  # (N, nx, L) — smoothed mean − z · sqrt(diag P)

bands_upper[:, i, k] is the trajectory of the upper band for state component i at level levels[k].

The bands assume a Gaussian posterior at each step (the smoother's model) and are computed from the marginal smoothed variance P_smoothed[t, i, i]. Cross-time covariance isn't captured; if you need joint bands across time, use record_covariances=True and combine them yourself.

Levels are clamped to [1e-6, 1 − 1e-9] so level=0.999999 gives finite z-scores. level ≤ 0 or ≥ 1 raises ValueError.

Templated vs runtime Dyn

smooth works on every LinearKalmanFilter(nx, nz). The templated path supports per-tick delta_t and per-tick measurement_variance overrides for kinematic shapes (nx == 2*nz or nx == 3*nz). The runtime _DynLinearKalmanFilter.smooth runs the full forward+RTS but raises ValueError if delta_t or measurement_variance is set — those features still need a runtime kinematic specialization.

Cost

Smoothing N ticks costs 2× the forward filter cost plus a backward pass that does one (nx × nx) matrix invert and a few small matmuls per tick. For (nx=2, nz=1) over 10k ticks: ~750 µs total wall-clock on a 2024-era laptop; for (nx=8, nz=8) over 1k ticks: ~12 ms.

What it doesn't do