A filter can be sure and wrong at once. Tell a Kalman filter its target moves 100 times more smoothly than it does: it claims ±0.75 m while its real error is 3.6 m. Its normalized surprise, the NIS, climbs from 1 to 4.7 and gives it away, with no ground truth.
Drag what the filter is told about its target's motion. Watch the teal band shrink while the pink NIS climbs out of the green band. Then we build the toolkit piece by piece: the innovation, NIS, NEES, whiteness, robust costs and a runbook.
You need the Kalman filter (Lesson 5) and a little Python. We build the rest from zero.
Lesson 21 of 22 in Sensor Fusion: Classical to Modern. The consistency tests follow Bar-Shalom, Li and Kirubarajan (2001). Before: Learned and Differentiable Filters. Next: The Sensor Fusion Atlas.
Illustrative toy: a target on a line with random acceleration (true Q from 0.05 m²/s³), a sensor with a 2 m error, one reading a second. The green band is the 95% chi-square band for the mean of 50 NIS values, 0.65 to 1.43. Only the simulation knows the real error.
Chapter 0
See how a filter's error bar shrinks while its real error grows
You built a fuser. It runs, and in your test it draws a smooth track. You ship it. In the field it drives into a wall, and up to the last second it reports a tight, shrinking error bar.
A filter reports two things: its estimate x̂, and its covariance P, its own claim about the size of its error. A filter is consistent when that claim is honest: its errors average zero, and their real spread matches P. It can fail in two directions.
| Failure | P against the real error | What happens |
|---|---|---|
| Overconfident | too small | ignores readings, drifts, reports that all is well |
| Underconfident | too large | wastes good readings, never surprises you |
Overconfidence feeds itself. The gain K comes from P. Tell the filter its target moves smoothly, with the process noise Q too small, and P shrinks. Then K shrinks, and the filter listens less to the readings that would correct it.
Teal is the error the filter claims. Purple is its real error. Pick the lie, then drag how wrong the filter's number is.
Exact steady state of the hero's toy, from the covariance equations: true Q from 0.05 m²/s³, sensor error 2 m, one reading a second. Errors are one standard deviation of position.
With these toy numbers, an honest filter claims ±1.23 m and is right. Told Q ×0.01, it claims ±0.75 m, its gain falls from 0.38 to 0.14, and its real error is 3.65 m. Lie about the sensor noise R instead, and it is overconfident too, but K rises: it twitches instead of going deaf.
The failure: trusting the error bar. Symptom: a smooth track and a shrinking P, then a crash. Measure: you cannot measure it from P. Fix: a test that compares the claim with data, which is this lesson.
Chapter 1
Compare each reading with the filter's own forecast of it
If you cannot trust P, what can you trust? Each tick, the filter predicts the reading before it sees it. Call the predicted state x̂−, the matrix that maps a state to a reading H, and the real reading z. The innovation ν (nu) is the part of the reading the filter did not see coming. Lesson 5 called it y.
Nu equals z minus H times x-hat-minus.
Map the predicted state x-hat-minus into reading units with H. Subtract it from the real reading z. The leftover is nu.
Lesson 5's drone: predicted 110 m, H = 1, reading 115 m. ν = 115 − 110 = 5 m.
The filter also forecasts how big ν should be. Two things make a surprise: the filter's doubt about the state, P−, and the sensor's noise variance, R. Call the forecast variance of the innovation S.
S equals H times P-minus times H-transpose, plus R.
Push the state doubt P-minus through H into reading units. Add the sensor noise R. The sum S is the variance the surprise nu should have.
P− = 13 m², R = 9 m², H = 1: S = 22 m². A normal surprise is about √22 = 4.7 m, so ν = 5 m is one standard deviation: normal.
If the filter is consistent, its innovations have three signatures. Each later test checks one.
| Signature | What to check | If broken |
|---|---|---|
| Zero mean | the average of ν is near 0 | a bias |
| Spread S | ν matches √S in size (Chapter 2) | Q or R is wrong |
| White | one ν does not predict the next (Chapter 4) | the model is wrong |
Each pink bar is one tick's innovation. The shaded band is ±2√S, where 95% of honest surprises fall. Pick a filter.
Illustrative: the hero's target and sensor, 200 ticks. The filter computes ν and S anyway, for its gain.
None of this needs the truth. Log ν and S on every update. Every test in this lesson reads those two arrays.
The failure: a filter with no audit trail. Symptom: after a crash, nobody can say why. Measure: look for ν and S in the log. Fix: log both on every update, from day one.
Chapter 2
Scale each surprise by its forecast and test the average against chi-square
To compare surprises across ticks and sensors, scale each one by its forecast size. The normalized innovation squared (NIS), written ε (epsilon), does that for a reading of any size.
Epsilon equals nu-transpose times S-inverse times nu.
Measure the surprise nu in units of its forecast spread S, and square it. For a reading with one number, epsilon is nu squared over S.
One number: ν = 5 m, S = 22 m², so ε = 25 / 22 = 1.14. A 2-D position: ν = (2, −1) m, S = diag(4, 1) m², so ε = 4/4 + 1/1 = 2.0.
If the filter is honest, ε follows a chi-square distribution with k degrees of freedom, where k is the number of values in one reading. Its average is k. So a healthy filter averages NIS 1 for a range reading, 2 for a 2-D position and 3 for a 3-D point.
One value is noisy: for k = 2, an honest tick lands anywhere from 0.05 to 7.4. So average N ticks. The sum of N honest values is chi-square with N·k degrees of freedom, which gives a band for the mean, ε̄.
Chi-square point-zero-two-five of N k, over N, is at most epsilon-bar, which is at most chi-square point-nine-seven-five of N k, over N.
Average the last N values of epsilon. Look up the two chi-square points for N times k degrees of freedom and divide each by N. If epsilon-bar sits between them, the filter is consistent.
k = 2, N = 50: 100 degrees of freedom give 74.22 and 129.56, so the band is 1.484 to 2.591. A mean NIS of 11 is far above it: overconfident.
Dots are single-tick NIS on a log scale. The pink line is the mean of the last N. Pick a filter, then drag N.
Illustrative: the hero's toy, k = 1, 300 ticks. Bands use the Wilson–Hilferty approximation to chi-square, within 0.1% here.
| Mean NIS | Verdict | Fix (Chapter 5) |
|---|---|---|
| above the band | overconfident | raise Q or R |
| inside the band | consistent | none |
| below the band | underconfident | lower Q or R |
The failure: an alarm on one tick. Symptom: the monitor fires on a single NIS of 6. Measure: the window mean, not one value. Fix: compute the band from your own N and k; never copy 1.48 to 2.59 from another setup.
Chapter 3
Test the whole state against the filter's claim in many simulated runs
NIS sees only what the sensor sees. A GPS reads position, so NIS checks velocity only through its effect on position. To test the whole state you need the true state. You have it in a simulator, or with a ground-truth rig such as motion capture.
Call the true state x and the estimate x̂. The estimation error e is their difference. The normalized estimation error squared (NEES), written η (eta), scales it by the filter's own claim, P.
Eta equals e-transpose times P-inverse times e, where e is x minus x-hat.
Subtract the estimate x-hat from the true state x to get e. Measure e in units of the claimed spread P, and square it.
With made-up numbers for four states (x, y, vx, vy): e = (1.0 m, −0.5 m, 0.2 m/s, 0.1 m/s). Take P = diag(1, 0.25, 0.04, 0.04). Then η = 1 + 1 + 1 + 0.25 = 3.25, near the honest average of 4.
An honest η is chi-square with n degrees of freedom, n the number of states, so it averages n. Run the filter M times with fresh noise, a Monte Carlo test. Average η across the runs at each tick. The band uses M·n degrees of freedom: for n = 4 and M = 50 it is 162.73/50 to 241.06/50, so 3.26 to 4.82.
The purple line is NEES averaged over 50 runs at each tick, on a log scale. The green band is 3.26 to 4.82. Drag what the filter is told about Q.
Illustrative: the hero's toy on two axes (n = 4), 50 runs of 80 ticks, each run starting with an error drawn from the filter's first P.
| NIS | NEES | |
|---|---|---|
| Scales | ν by S | e by P |
| Honest mean | k, the reading size | n, the state size |
| Needs the truth | no | yes |
| Runs | live, on the robot | in simulation, before you ship |
The failure: a hidden state. Symptom: NIS passes in the field, yet the velocity estimate is wrong. Measure: Monte Carlo NEES in simulation. Fix: raise Q on that state, or add a sensor that sees it.
Chapter 4
Check whether one surprise predicts the next
A filter can have surprises of the right size and the wrong shape. If its model misses a turn, it makes the same mistake many ticks in a row. Its surprises are then correlated: today's ν predicts tomorrow's. An honest filter's innovations are white, with no memory from one tick to the next.
Measure memory with the autocorrelation. Call the gap in ticks the lag τ (tau), and the innovation at tick i νi.
Rho of tau equals the sum over i of nu-i times nu-i-plus-tau, divided by the sum over i of nu-i squared.
Subtract the mean from the innovations. Multiply each nu-i by the one tau ticks later and add the products. Divide by the sum of squares, so rho of 0 is 1.
With made-up innovations 1, 2, −1, −2 (mean 0): ρ(1) = (1·2 + 2·(−1) + (−1)·(−2)) / (1 + 4 + 1 + 4) = 2/10 = 0.2.
White noise wobbles too. With N innovations, 95% of white lags fall inside ±1.96/√N. For N = 400 that is ±1.96/20 = ±0.098. One lag outside is chance; a run of them is a pattern.
Bars are ρ(τ) for lags 0 to 30 over 400 innovations. The green band is ±0.098. Pick a filter.
Illustrative: the hero's filter. "Unmodeled turn" tracks a target that swings 15 m each way every 40 s with the honest Q, so the model misses the swing.
| Shape of ρ(τ) | Likely cause |
|---|---|
| One spike at lag 0 | healthy |
| Slow positive tail | the filter under-reacts: gain too low, or a missing state |
| Negative at lag 1 | the filter over-reacts: gain too high |
| A wave, up then down | an unmodeled periodic motion, such as a turn |
The failure: a lagging track that passes NIS. Symptom: the estimate trails each maneuver. Measure: ρ(τ) over the last N ticks. Fix: add the missing state or motion mode.
Chapter 5
Read NIS and the lag-1 correlation together to name Q or R
Every Kalman-family filter has two noise knobs. Q says how far the real motion strays from the model each tick. R says how noisy the sensor is. Too small a value of either makes the filter overconfident, so NIS alone cannot say which one.
The sign of ρ(1) can. If the filter under-reacts, it keeps making the same error, so ρ(1) is positive. If it over-reacts, it overshoots and corrects back, so ρ(1) is negative. NIS gives the size of the claim; ρ(1) gives the size of the gain.
| ρ(1) > 0: under-reacts | ρ(1) < 0: over-reacts | |
|---|---|---|
| NIS above the band | Q too small: raise Q | R too small: raise R |
| NIS below the band | R too large: lower R | Q too large: lower Q |
The dot plots mean NIS (up) against ρ(1) (across). Move it into the green box. The corner labels name the knob.
Illustrative: the hero's toy, three runs of 600 ticks pooled. The box is the 95% band for the mean NIS and ±1.96/√N for ρ(1).
A worked loop, from long runs of the same toy:
Change one knob at a time and re-test. Two rows sit outside the table. A wave in ρ(τ) means a wrong model: fix the model. High NEES with a passing NIS means a state the sensor cannot see.
The failure: the noise hack. Symptom: a tracker lags each turn, so someone raises Q until NIS passes, and the track turns noisy on straight roads. Measure: run whiteness before you touch Q. Fix: model the turn, for example with a coordinated-turn model.
Chapter 6
Cap the pull of a gross outlier, or switch it off
The noise tests assume every reading is honest noise. A gross outlier is not. Examples: a GPS fix bounced off a building, a wrong data association (Lesson 12), a false loop closure (Lesson 17).
Least squares minimizes the sum of squared residuals r, the gaps between each reading and the fit, so one big residual dominates. Fit one value to 10, 11, 9, 12 and 90: the mean is 26.4, though four readings say about 10.
A robust cost grows slower than r² for big residuals. Solve it by IRLS, iteratively reweighted least squares: weight each residual, fit, recompute the weights, repeat. The Huber cost, ½r² inside a threshold δ (delta) and a straight line outside, gives this weight:
W of r equals the smaller of 1 and delta over the absolute value of r.
If the residual r is within delta, keep weight 1. If it is bigger, use weight delta over r, so its pull stops growing.
δ = 3: r = 1 gives w = 1; r = 30 gives 0.1; r = 90 gives 0.033. On the five readings IRLS settles at 11.25, with the 90 at weight 0.038.
Huber never lets go. Dynamic covariance scaling (DCS) scales a constraint by a factor s with a tuning scale Φ (phi), and its cost term by s²:
S equals the smaller of 1 and two phi over phi plus r squared.
If r squared is below phi, s is 1. Above phi, s falls like two phi over r squared, and the weight s squared falls much faster.
Φ = 9: r = 1 gives s = 1. r = 30 gives s = 18/909 = 0.020, so weight s² = 0.0004. On the five readings IRLS settles at 10.50.
Drag the outlier. Yellow is least squares, solid teal is Huber, dashed teal is DCS. Switch fits on and off.
Illustrative: 14 points on the line y = 2 + 1.5x with noise of 0.8, one pushed up by the slider. Both robust fits run 30 rounds of IRLS from the least-squares line.
DCS is the closed-form answer of switchable constraints (Sünderhauf and Protzel, 2012), which give each loop closure an off switch. Agarwal and colleagues (2013) showed the best switch value is s, so DCS needs no extra variables.
Cheaper still, the gate of Lessons 5 and 12 skips a reading whose NIS is above a threshold. For k = 1, a gate at 9 is 3 standard deviations; for k = 2, the 99% point is 9.21.
The failure: one false loop closure. Symptom: the map folds. Measure: the largest normalized residuals after the solve. Fix: gate first, then a robust kernel.
Chapter 7
Run a filter and its monitor side by side, then break it four ways
Here the filter and its monitor run together. The top panel is what the filter shows you. The bottom panel is the test. The read-out also prints the real error, which only the simulator knows.
Top: the estimate's error from the true path, with its claimed ±2σ band. Bottom: NIS per tick and its mean over 50 ticks against the green band. Press Run, then turn the knobs.
Illustrative: the hero's toy, 20 ticks a second of simulated time. Glitches add 25 m to one reading in 40. The gate skips a reading whose NIS is above 9; after 10 skips in a row the filter restarts from the reading.
Run these four tests and check each prediction:
In every broken case the top panel looked as calm as before, or calmer. Only the bottom panel told the truth, from numbers the filter already had.
The failure: a gate on an overconfident filter. Symptom: with Q ×0.01 and the gate on, honest readings get rejected and the error grows. Measure: the share of readings rejected. Fix: fix Q first; a gate is for glitches, not for bad tuning.
Chapter 8
Run the tests in order so each one rules out a class of cause
The call comes: the estimate flew off mid-mission. Do not guess. Pull the log of ν and S and run four tests. Each one rules out a class of cause.
Top: the innovations of one log against ±2√S. Bottom: four tests, green for pass and red for fail. Pick a log.
Illustrative: 400 ticks of the hero's toy. Mean test: |mean ν| under 1.96√(S/N). NIS: the 95% band. White: ρ(1) inside ±0.098 and under 10% of lags 1 to 20 outside it. Spikes: at most 2 ticks with NIS above 9.
Two faults need a second sensor to show. A calibration or time-sync error (Lesson 15) makes innovations near zero at rest and large in motion. Plot |ν| against turn rate: a rising slope points to calibration, not noise.
The failure: tuning before testing. Symptom: Q goes up, NIS passes, the fault stays. Measure: run the six steps first. Fix: turn only the knob a test names.
Chapter 9
Keep the tests, their numbers, the runbook and the core code on one page
A filter's covariance is its claim; the tests check the claim against data. Log ν and S. Watch NIS live and run NEES on the bench. Use whiteness and the sign of ρ(1) to tell a wrong model from a wrong Q or R. Gate glitches and add a robust cost.
| Test | Equation | Worked numbers |
|---|---|---|
| Innovation | ν = z − H x̂− | 115 − 110 = 5 m |
| Its covariance | S = H P− HT + R | 13 + 9 = 22 m² |
| NIS | ε = νT S−1 ν | 25 / 22 = 1.14; honest mean k |
| NIS band | χ²(Nk) quantiles / N | k 2, N 50: 1.484 to 2.591 |
| NEES | η = eT P−1 e | n 4, M 50: 3.26 to 4.82 |
| Whiteness | ρ(τ), band ±1.96/√N | N 400: ±0.098 |
| Huber | w = min(1, δ/|r|) | δ 3, r 30: 0.1 |
| DCS | s = min(1, 2Φ/(Φ + r²)) | Φ 9, r 30: s 0.020, weight 0.0004 |
| Gate | skip if ε > γ | k 1: 9; k 2 at 99%: 9.21 |
| Term | Meaning |
|---|---|
| Consistent | Errors average zero and their spread matches P. |
| Innovation ν | The reading minus the filter's forecast of it. |
| S | The forecast variance of the innovation. |
| NIS | The innovation squared in units of S; honest mean k. |
| NEES | The true error squared in units of P; honest mean n. |
| White | No memory from one innovation to the next. |
| Robust cost | A cost that caps the pull of a big residual. |
| Gate | Skip a reading whose NIS is too large to be honest. |
pythonimport numpy as np from scipy.stats import chi2 def nis(nu, S): # nu^T S^-1 nu; solve is stabler than inv return float(nu @ np.linalg.solve(S, nu)) def band(k, N, conf=0.95): # the band for the mean of N values a = (1 - conf) / 2 return chi2.ppf(a, N * k) / N, chi2.ppf(1 - a, N * k) / N def acf(nu, L=20): # whiteness: rho(0..L) c = nu - nu.mean(); d = c @ c return np.array([c[:len(c) - t] @ c[t:] / d for t in range(L + 1)]) def verdict(eps, nu): # scalar readings, k = 1 lo, hi = band(1, len(eps)); m = np.mean(eps); r1 = acf(nu, 1)[1] if lo <= m <= hi: return "consistent" if m > hi: return "raise Q" if r1 > 0 else "raise R" return "lower R" if r1 > 0 else "lower Q" def huber_w(r, d=3.0): return np.minimum(1.0, d / np.maximum(np.abs(r), 1e-12)) def dcs_w(r, phi=9.0): return np.minimum(1.0, 2 * phi / (phi + r * r)) ** 2 band(2, 50) # (1.484, 2.591)
| System | Tool | What it catches |
|---|---|---|
| PX4 EKF2 autopilot | innovation test ratio: NIS over the gate squared, reject above 1 | bad GPS, barometer or magnetometer readings |
| Car multi-object trackers | chi-square gate and per-track NIS | wrong associations, diverging tracks |
| g2o, GTSAM, Ceres | Huber, Cauchy and DCS robust kernels | false loop closures |
| Navigation validation | Monte Carlo NEES | overconfidence before deployment |
Now press Present or Teach and explain, from memory, why NIS climbs when Q is too small and which way ρ(1) leans. Then go back to the hero, drag Q to ×0.01, and read the NIS before you read the band.