kalman_filter
Linear Kalman filter for a small state-space model x[k] = F x[k-1] + w (cov Q), z[k] = H x[k] + v (cov R): filtered states and covariance (Joseph form), innovation statistics with the NIS chi-square consistency band, log-likelihood, the steady-state gain and covariance (Riccati recursion to its fixed point), and the RMS error against a truth track when given. Measurements inline (z) or from a csv; a null step is predicted, not updated. States carry the model's own units (declared 1).
- Field
- Dynamics and control
- Version
- 0.1.0
- Runs as
- Compiled (Rust)
- Typical run
- 0.05 s
Compiled from source: run sh packs/install.sh once before admitting it.
Known answers it must pass
r0001scalar random walk q=1, r=2: steady prior P = (q + sqrt(q^2 + 4qr))/2 = 2, gain P/(P+r) = 0.5, posterior P r/(P+r) = 1 (closed-form Riccati fixed point, rel 1e-6)passed
steady_converged= truetruelockedexact matchsteady_prior_cov.0.0{"value": 2.0, "rel_tol": 1e-06}steady_gain.0.0{"value": 0.5, "rel_tol": 1e-06}steady_post_cov.0.0{"value": 1.0, "rel_tol": 1e-06}r0002scalar random walk q=r=1: steady prior P = (1 + sqrt 5)/2 = golden ratio 1.6180339887, gain = 1/phi = 0.6180339887 (closed-form Riccati fixed point, rel 1e-6)passed
steady_prior_cov.0.0{"value": 1.618033988749895, "rel_tol": 1e-06}steady_gain.0.0{"value": 0.6180339887498949, "rel_tol": 1e-06}r0003constant state, q=0, diffuse prior, r=4, z=[1,2,3,4,10]: estimate = running mean 4, P_n = r/n = 0.8; truth 4 gives RMS of running means (-3,-2.5,-2,-1.5,0) = sqrt(4.3) (exact; rel 1e-6 covers the 1e9 prior's ~1e-9 bias)passed
x_final.0{"value": 4.0, "rel_tol": 1e-06}P_final.0.0{"value": 0.8, "rel_tol": 1e-06}rms_error.0{"value": 2.073644135332772, "rel_tol": 1e-06}steady_converged= falsefalselockedexact matchr0004noise-free constant-velocity target z = 3 + 2k (k=1..10), F=[[1,1],[0,1]], q=0, r=1, diffuse prior: velocity 2 and position 23 exactly; covariance = least-squares line fit: var v = r/Sxx = 1/82.5, var x10 = r(1/N + (N-tbar)^2/Sxx) = 0.345454, cov = 4.5/82.5 (exact; rel 1e-6)passed
x_final.0{"value": 23.0, "rel_tol": 1e-06}x_final.1{"value": 2.0, "rel_tol": 1e-06}P_final.1.1{"value": 0.012121212121212121, "rel_tol": 1e-06}P_final.0.0{"value": 0.34545454545454546, "rel_tol": 1e-06}P_final.0.1{"value": 0.05454545454545454, "rel_tol": 1e-06}r0005alpha-beta steady state (Kalata 1984; Bar-Shalom, Li, Kirubarajan 2001, sec. 6.5): white-acceleration CV model, T=1, tracking index lambda = sigma_a T^2/sigma_z = 1 -> alpha = 0.75, beta = 0.5, gain [alpha, beta/T], prior var x = alpha r/(1-alpha) = 3 (closed form; rel 1e-6)passed
steady_converged= truetruelockedexact matchsteady_gain.0.0{"value": 0.75, "rel_tol": 1e-06}steady_gain.1.0{"value": 0.5, "rel_tol": 1e-06}steady_prior_cov.0.0{"value": 3.0, "rel_tol": 1e-06}r0006two hand-worked scalar steps (F=H=Q=R=P0=1, x0=0, z=[3,6]): S=3, v=3, NIS 3; S=8/3, v=4, NIS 6 -> x=4.5, P=5/8, mean NIS 4.5, log-lik -(2 ln 2pi + ln 3 + ln 8/3 + 9)/2; 2-dof chi-square band from the closed form F^-1(p) = -2 ln(1-p): [-ln 0.975, -ln 0.025] (exact; rel 1e-6)passed
x_final.0{"value": 4.5, "rel_tol": 1e-06}P_final.0.0{"value": 0.625, "rel_tol": 1e-06}nis_mean4.5 ± 4.5e-064.5lockedunder 0.01 % of the tolerance usedinnovation_mean.0{"value": 3.5, "rel_tol": 1e-06}log_likelihood-7.3776 ± 7.4e-06-7.3776lockedunder 0.01 % of the tolerance usednis_band_lo0.0253178 ± 2.5e-080.0253178lockedunder 0.01 % of the tolerance usednis_band_hi3.68888 ± 3.7e-063.68888lockedunder 0.01 % of the tolerance usedconsistent= falsefalselockedexact match
Each line is one reading the kernel checked. The bright band is the tolerance; the needle is the value the instrument actually returned when it was admitted. A needle near the centre line used little of its tolerance.
Inputs
| Name | Unit | Default | Meaning |
|---|---|---|---|
F | 1 | State transition matrix, n x n (1 to 12 states), as a list of rows, e.g. [[1, 1], [0, 1]] for constant velocity with a step of 1. Model units. | |
H | 1 | Measurement matrix, m x n (1 to 12 measurements), e.g. [[1, 0]] to measure position only. | |
Q | 1 | Process noise covariance, n x n, symmetric, variances >= 0. Left out: zero (a deterministic state). | |
R | 1 | Measurement noise covariance, m x m, symmetric; positive variances keep the gain defined. | |
P0 | 1 | Prior covariance of x0, n x n. Left out: a diffuse prior, diffuse_variance on every state. | |
x0 | 1 | Prior state, n numbers. Left out: zeros (harmless with the diffuse prior). | |
z | 1 | Measurements, one entry per step: a list of m numbers (a bare number when m = 1), or null for a step without a measurement. Leave out z and csv to get the steady state only. | |
csv | Measurements from a csv with a header row instead of z: columns z (m = 1) or z1..zm, optional truth columns x1_true..xn_true. Empty cell = no measurement. | ||
z_columns | csv column names holding the m measurements, in the order of the rows of H. | ||
truth_columns | csv column names holding the true states, in state order ("" for a state without truth). | ||
truth | 1 | True states inline, one list of n numbers per step (null where unknown); gives the RMS error of the estimates. | |
diffuse_variance | 1 | 1000000000.0 | Prior variance on every state when P0 is left out; far above anything the states can take. |
predict_first | true | true: x0, P0 are at step 0 and the first measurement is at step 1 (predict, then update); false: x0, P0 are at the first measurement. | |
confidence | 1 | 0.95 | Two-sided probability of the NIS chi-square consistency band. |
Readings
| Name | Unit | Meaning |
|---|---|---|
n_states | 1 | |
n_measurements | 1 | |
n_steps | 1 | |
n_updates | 1 | |
prior | "given" or "diffuse" | |
x_final | 1 | Filtered state after the last step (model units). |
P_final | 1 | Its covariance. |
sigma_final | 1 | Square roots of the diagonal of P_final. |
gain_final | 1 | Kalman gain of the last update, n x m. |
innovation_mean | 1 | |
innovation_rms | 1 | |
innovation_lag1_autocorrelation | 1 | Near 0 for a well-tuned filter (white innovations). |
nis_mean | 1 | Mean normalised innovation squared v' S^-1 v; expected value m. |
nis_expected | 1 | |
nis_band_lo | 1 | Lower edge of the chi-square band for the mean NIS (dof = updates x m), divided by the number of updates. |
nis_band_hi | 1 | |
consistent | true when the mean NIS lies inside its band. | |
log_likelihood | 1 | Gaussian log-likelihood of the innovations (nats). |
steady_converged | Whether the Riccati recursion reached a fixed point (relative change <= 1e-13). | |
steady_iterations | 1 | |
steady_gain | 1 | Steady-state Kalman gain, n x m. |
steady_prior_cov | 1 | Steady-state predicted covariance (Riccati fixed point). |
steady_post_cov | 1 | Steady-state updated covariance. |
steady_post_sigma | 1 | |
rms_error | 1 | RMS of estimate - truth per state, over the steps with truth. |
rms_measurement_error | 1 | RMS of z - H truth per measurement (the raw sensor error), for comparison. |
states | ||
warnings |
Try the call
Change a value and the command updates. Nothing runs here; paste it into your terminal or give it to your agent.