~/satyajit

The Kalman filter: from two Gaussians to the error-state filter inside LiDAR-inertial SLAM

mdjsonmcp

2026-06-28 · 39 min · state-estimation · kalman-filter · slam · robotics

A 2:31 narrated explainer, drawn in code. Every number and picture in it is this article's own; the sources are below.

› transcript

A car drives a loop through Karlsruhe. Its IMU logs a hundred times a second. Noisy position fixes arrive once a second. For thirty seconds, the fixes stop. Every filter drifts about forty metres, and the good ones know it. Hi, I'm Rudolf. Four filters, one real drive, and a test that tells you which of them is lying. Every Kalman filter runs the same update. Start from the prediction. Take the innovation, the part of the measurement you did not expect. And move by the gain. It weighs how unsure the prediction is against how unsure the innovation is. Rotations break it. A quaternion lives on a sphere, and adding a correction walks it off. The error-state filter keeps the error in the tangent line at its estimate, then wraps it back on. Here a heading is a point on a circle. The filter predicts one direction. The sensor says the body is seventy degrees further round. The classic EKF adds its correction in a straight line. It leaves the circle, so the code divides by the length and lands short. The error-state filter solves for an angle and wraps it along the circle. It stays a rotation by construction. The iterated filter relinearises at its new guess and solves again. Five passes later it is almost at the truth. That iterated update is the loop inside FAST-LIO. It runs on every LiDAR scan, until it converges. On the real drive, with ranges to beacons and a start thirty metres off, one linear step leaves the EKF eighteen metres out. Iterating gets within about a metre. To check a filter, weigh its errors by its own covariance. That is NEES, and a consistent filter averages the number of states. Cut the process noise a hundredfold and NEES jumps past one hundred sixty. The filter is almost twice as wrong, and sure of itself. A filter folds the past into one Gaussian and forgets it. Run it backwards too, the factor graph's trick, and the error more than halves. When the answer can wait, look back. Keep rotations in an error state. Iterate when the measurement bends. And log the innovation test from day one. The gain weighs two spreads, error states keep rotations honest, and the chi-square tests tell you when a filter lies. Every source is in the full article. I'm Rudolf. Bye!

Why read this

Hightop 30%

KF to EKF, UKF, ESKF and IEKF derived, with NEES/NIS tuning, and all four run in NumPy on a real KITTI drive: error and consistency measured.

  • Runs on a laptop CPU
  • Interactive explanations
  • Evergreen reference

Robotics & embodiedPractitioner guide

How this was scored
Is it new?
0 of 3: Repackaging or news of a known thing
Can I trust it?
2 of 3: Measures key facts from files, code or configs
Can I run it?
3 of 3: Open, permissive, runs on reader hardware with instructions
Will I understand it?
3 of 3: Mechanism carried by interactives built from real code or data
Can I act on it?
2 of 3: A concrete recipe, numbers or comparison
Will it last?
3 of 3: Evergreen fundamentals
Does it affect many?
2 of 3: A widely used model, tool or lab release
Only here?
2 of 3: A teardown or measurement few others did

Score 73 of 100, ranked 97 of 773 rated articles. Each question is answered 0–3 by hand, and a 3 is rare. How articles are scored

I spend a lot of time on odometry and SLAM, and underneath almost all of it is one idea. You have a noisy sensor and a model of how the thing you track moves, and you want a single best estimate that also says how much to trust itself. That is the Kalman filter. It is the optimal recursive estimator for a linear system with Gaussian noise, and every LiDAR-inertial system on this site (FAST-LIO2, FAR-LIO, the iPhone port) runs a descendant of it.

The first version of this page stopped at "and then you linearise". This one goes the rest of the way. It derives the linear filter, then follows the four nonlinear filters a working roboticist meets: the EKF, the UKF, the error-state filter that keeps rotations honest, and the iterated EKF that FAST-LIO uses. It covers how to check that a filter's covariance means anything, and when to drop filtering for a factor graph. Then I wrote all four in NumPy and ran them on 288 seconds of a real KITTI drive.

The loop as geometry, in 66 seconds. Press play: it is narrated and captioned. Every frame is drawn by JavaScript on a 2D canvas at 12fps and voiced with Kokoro-82M on CPU, a drawn explanation and not a recording. The scatter, the gain lever and the sizes of the ellipses are illustrative and measure nothing. The small ellipse in the update shot is the real normalised product of the two on screen, so when all three are slid onto one centre it lands inside both; that containment is the covariance inequality, and exact. The sawtooth band at the end is a real scalar filter run, its width the actual square root of P. The arithmetic you can check is below. (Drawn with the vendored hand-drawn-canvas-animation skill, MIT, Alexey Fateev.)

Two sources of information

You are tracking a state x\mathbf{x}, say the position and velocity of something. You have two sources:

  1. A process model: how the state evolves on its own. For constant velocity, pt=pt−1+v Δtp_{t} = p_{t-1} + v\,\Delta t. You can predict forward, but the prediction drifts and accumulates uncertainty.
  2. A measurement model: a sensor that observes some function of the state, noisily. A position sensor gives z=p+noisez = p + \text{noise}.

Neither is enough alone. The prediction drifts and the sensor jitters. The Kalman filter combines them, and the key fact is that combining two Gaussian estimates yields a third Gaussian that is sharper than either input.

That is the whole update step. If the prediction is N(μ1,σ12)\mathcal{N}(\mu_1, \sigma_1^2) and the measurement is N(μ2,σ22)\mathcal{N}(\mu_2, \sigma_2^2), their normalised product has

μ=μ1+K(μ2−μ1),σ2=(1−K) σ12,K=σ12σ12+σ22.\mu = \mu_1 + K(\mu_2 - \mu_1), \qquad \sigma^2 = (1-K)\,\sigma_1^2, \qquad K = \frac{\sigma_1^2}{\sigma_1^2 + \sigma_2^2}.

KK is the Kalman gain: the fraction of the way you move from the prediction toward the measurement, set by which one is more certain. Trust the sensor (σ2→0\sigma_2 \to 0) and K→1K \to 1; trust the prediction (σ1→0\sigma_1 \to 0) and K→0K \to 0. The rest of this page is that formula generalised to vectors, then bent to fit the parts of the world that are not linear.

Deriving the filter properly

The scalar picture is a good intuition and a weak derivation. Here is the one I actually trust, because every later filter on this page is a variation of it.

The model

The state at step kk is a vector xk\mathbf{x}_k. It evolves linearly with additive Gaussian noise, and a sensor sees a linear function of it:

xk=Fxk−1+Buk+wk,wk∼N(0,Q);zk=Hxk+vk,vk∼N(0,R).\mathbf{x}_k = \mathbf{F}\mathbf{x}_{k-1} + \mathbf{B}\mathbf{u}_k + \mathbf{w}_k, \quad \mathbf{w}_k\sim\mathcal{N}(0,\mathbf{Q}); \qquad \mathbf{z}_k = \mathbf{H}\mathbf{x}_k + \mathbf{v}_k, \quad \mathbf{v}_k\sim\mathcal{N}(0,\mathbf{R}).

F\mathbf{F} is the state transition, Bu\mathbf{B}\mathbf{u} a known input (an IMU reading, a motor command), H\mathbf{H} maps state to measurement, Q\mathbf{Q} is process noise (how much the model is wrong per step) and R\mathbf{R} is measurement noise. The noises are white, independent of each other and of the initial state x0∼N(x^0,P0)\mathbf{x}_0 \sim \mathcal{N}(\hat{\mathbf{x}}_0, \mathbf{P}_0).

Predict: push a Gaussian through a linear map

If xk−1∼N(x^,P)\mathbf{x}_{k-1} \sim \mathcal{N}(\hat{\mathbf{x}}, \mathbf{P}), then Fxk−1\mathbf{F}\mathbf{x}_{k-1} is Gaussian with mean Fx^\mathbf{F}\hat{\mathbf{x}} and covariance E[Fee⊤F⊤]=FPF⊤\mathbb{E}[\mathbf{F}\mathbf{e}\mathbf{e}^{\top}\mathbf{F}^{\top}] = \mathbf{F}\mathbf{P}\mathbf{F}^{\top}, where e\mathbf{e} is the error. Adding independent noise adds covariances. So:

x^−=Fx^+Bu,P−=FPF ⁣⊤+Q.\hat{\mathbf{x}}^- = \mathbf{F}\hat{\mathbf{x}} + \mathbf{B}\mathbf{u}, \qquad \mathbf{P}^- = \mathbf{F}\mathbf{P}\mathbf{F}^{\!\top} + \mathbf{Q}.

Nothing is approximated. A Gaussian stays Gaussian under a linear map, and these two lines are its exact mean and covariance. Every nonlinear filter below breaks exactly this step and differs only in how it patches it.

Update: condition a joint Gaussian

Before the measurement arrives, state and measurement are jointly Gaussian:

[xz]∼N ⁣([x^−Hx^−],[P−P−H⊤HP−HP−H⊤+R]).\begin{bmatrix}\mathbf{x}\\ \mathbf{z}\end{bmatrix} \sim \mathcal{N}\!\left( \begin{bmatrix}\hat{\mathbf{x}}^-\\ \mathbf{H}\hat{\mathbf{x}}^-\end{bmatrix}, \begin{bmatrix}\mathbf{P}^- & \mathbf{P}^-\mathbf{H}^{\top}\\ \mathbf{H}\mathbf{P}^- & \mathbf{H}\mathbf{P}^-\mathbf{H}^{\top}+\mathbf{R}\end{bmatrix} \right).

The off-diagonal block is Cov(x,Hx+v)=P−H⊤\mathrm{Cov}(\mathbf{x}, \mathbf{H}\mathbf{x}+\mathbf{v}) = \mathbf{P}^-\mathbf{H}^{\top}. Conditioning a joint Gaussian on one block is a standard identity (complete the square in the exponent): for [ab]\begin{bmatrix}\mathbf{a}\\ \mathbf{b}\end{bmatrix} with blocks Σaa,Σab,Σbb\boldsymbol{\Sigma}_{aa}, \boldsymbol{\Sigma}_{ab}, \boldsymbol{\Sigma}_{bb},

a∣b∼N(μa+ΣabΣbb−1(b−μb),  Σaa−ΣabΣbb−1Σba).\mathbf{a}\mid\mathbf{b} \sim \mathcal{N}\big(\boldsymbol{\mu}_a + \boldsymbol{\Sigma}_{ab}\boldsymbol{\Sigma}_{bb}^{-1}(\mathbf{b}-\boldsymbol{\mu}_b),\; \boldsymbol{\Sigma}_{aa} - \boldsymbol{\Sigma}_{ab}\boldsymbol{\Sigma}_{bb}^{-1}\boldsymbol{\Sigma}_{ba}\big).

Read the filter straight off it. Name the pieces:

y=z−Hx^−  (innovation),S=HP−H ⁣⊤+R  (its covariance),K=P−H ⁣⊤S−1,\mathbf{y} = \mathbf{z} - \mathbf{H}\hat{\mathbf{x}}^- \;(\text{innovation}), \qquad \mathbf{S} = \mathbf{H}\mathbf{P}^-\mathbf{H}^{\!\top} + \mathbf{R} \;(\text{its covariance}), \qquad \mathbf{K} = \mathbf{P}^-\mathbf{H}^{\!\top}\mathbf{S}^{-1}, x^=x^−+Ky,P=(I−KH)P−.\hat{\mathbf{x}} = \hat{\mathbf{x}}^- + \mathbf{K}\mathbf{y}, \qquad \mathbf{P} = (\mathbf{I} - \mathbf{K}\mathbf{H})\mathbf{P}^-.

Each consequence below gets used later.

The posterior is exact. For a linear-Gaussian system the filter's x^\hat{\mathbf{x}} and P\mathbf{P} are the true conditional mean and covariance given every measurement so far. That is why it is "optimal": no estimator, linear or not, has a lower mean squared error here.

The update is a least-squares problem. The same posterior mean minimises

J(x)=∥x−x^−∥P−2+∥z−Hx∥R2,∥e∥Σ2=e⊤Σ−1e.J(\mathbf{x}) = \lVert \mathbf{x} - \hat{\mathbf{x}}^- \rVert^2_{\mathbf{P}^{-}} + \lVert \mathbf{z} - \mathbf{H}\mathbf{x} \rVert^2_{\mathbf{R}}, \qquad \lVert \mathbf{e}\rVert^2_{\boldsymbol{\Sigma}} = \mathbf{e}^{\top}\boldsymbol{\Sigma}^{-1}\mathbf{e}.

Set the gradient to zero and you get x^=x^−+(P− −1+H⊤R−1H)−1H⊤R−1y\hat{\mathbf{x}} = \hat{\mathbf{x}}^- + (\mathbf{P}^{-\,-1} + \mathbf{H}^{\top}\mathbf{R}^{-1}\mathbf{H})^{-1}\mathbf{H}^{\top}\mathbf{R}^{-1}\mathbf{y}, which equals the gain form above by the matrix inversion lemma. This is the maximum a posteriori (MAP) view: a prior factor and a measurement factor, solved jointly. The iterated EKF and the factor graph are both this cost with the linearity assumption removed.

The gain has two forms with different costs. P−H⊤S−1\mathbf{P}^-\mathbf{H}^{\top}\mathbf{S}^{-1} inverts an m×mm \times m matrix (mm measurements). The information form (P− −1+H⊤R−1H)−1H⊤R−1(\mathbf{P}^{-\,-1} + \mathbf{H}^{\top}\mathbf{R}^{-1}\mathbf{H})^{-1}\mathbf{H}^{\top}\mathbf{R}^{-1} inverts an n×nn \times n one (nn states). With 18 states and a few thousand LiDAR residuals per scan, FAST-LIO uses the second, and that rewrite is most of its speed.

predictx̂=Fx · P̂=FPFᵀ+Qupdatex=x̂+K(z−Hx̂)prior x̂, P̂posterior x, P → next stepmeasurement z
The Kalman loop: predict pushes the estimate and its covariance forward through the motion model (uncertainty grows by Q); update pulls it back toward the measurement by the gain K (uncertainty shrinks). The two steps alternate for every timestep.

Watch it run on a constant-velocity tracker. The state is [position,velocity][\text{position}, \text{velocity}], the sensor sees only position, and the filter has to infer velocity and smooth the noise. Drag RR and QQ to feel the trade-off the gain encodes:

In code

The whole thing is a few lines of linear algebra. Here is the constant-velocity tracker above, in NumPy, runnable as-is:

import numpy as np
 
dt = 1.0
F = np.array([[1, dt],      # constant-velocity transition
              [0, 1]])
H = np.array([[1.0, 0.0]])  # observe position only
Q = 0.5 * np.array([[dt**3/3, dt**2/2],   # process noise (drift)
                    [dt**2/2, dt]])
R = np.array([[49.0]])      # measurement noise (sensor variance)
 
x = np.array([[0.0], [0.0]])      # initial state
P = np.eye(2) * 50.0              # initial uncertainty
 
def step(x, P, z):
    # predict
    x = F @ x
    P = F @ P @ F.T + Q
    # update
    y = z - H @ x                 # innovation
    S = H @ P @ H.T + R           # innovation covariance
    K = P @ H.T @ np.linalg.inv(S)   # Kalman gain
    x = x + K @ y
    P = (np.eye(2) - K @ H) @ P
    return x, P
 
for z in measurements:            # stream of noisy position readings
    x, P = step(x, P, np.array([[z]]))
    # x[0] is the smoothed position estimate, P[0,0] its variance

That Q is not arbitrary. For a state of position and velocity driven by white acceleration of intensity qq, integrating the noise over one step gives exactly q[Δt3/3Δt2/2Δt2/2Δt]q\begin{bmatrix}\Delta t^3/3 & \Delta t^2/2\\ \Delta t^2/2 & \Delta t\end{bmatrix}. It is the most common Q\mathbf{Q} in tracking, and the off-diagonal terms matter: drop them and the filter thinks position and velocity errors are unrelated, which they are not.

Is the filter telling the truth?

A filter returns an estimate and a covariance that claims how wrong the estimate is. The estimate gets all the attention. The covariance is the part that breaks silently, and downstream code (a gating test, a loop-closure check, a planner) trusts it.

NEES: errors measured in the filter's own units

When you know the truth (in simulation, or with a reference system), take the error ek=xk−x^k\mathbf{e}_k = \mathbf{x}_k - \hat{\mathbf{x}}_k and weight it by the claimed covariance:

ϵk=ek⊤Pk−1ek.\epsilon_k = \mathbf{e}_k^{\top}\mathbf{P}_k^{-1}\mathbf{e}_k .

This is the normalised estimation error squared. If the filter is right, ek∼N(0,Pk)\mathbf{e}_k \sim \mathcal{N}(0, \mathbf{P}_k), so ϵk\epsilon_k is a sum of nn squared standard normals: chi-square with nn degrees of freedom, mean nn. A filter whose NEES averages well above nn is overconfident: its errors are bigger than it admits. Below nn, it is underconfident and is throwing information away.

One run gives a noisy, time-correlated NEES, and putting a chi-square band around it with "degrees of freedom = number of steps" is a common mistake that makes the band far too tight. The standard test (Bar-Shalom, Li and Kirubarajan, Estimation with Applications to Tracking and Navigation) runs NN independent Monte Carlo trials and averages per step:

ϵˉk=1N∑i=1Nϵk(i),Nϵˉk∼χNn2.\bar{\epsilon}_k = \frac{1}{N}\sum_{i=1}^{N}\epsilon_k^{(i)}, \qquad N\bar{\epsilon}_k \sim \chi^2_{Nn}.

For N=20N = 20 runs of a 9-dimensional error, the two-sided 95% band on ϵˉk\bar{\epsilon}_k is [χ1802(0.025)/20, χ1802(0.975)/20]=[7.24, 10.95][\chi^2_{180}(0.025)/20,\ \chi^2_{180}(0.975)/20] = [7.24,\ 10.95] (scipy's quantiles). For 16 runs of a 2-D position it is [1.143, 3.093][1.143,\ 3.093].

NIS: the test you can run without the truth

On a real robot you rarely have the truth. You always have the innovations. If the filter is consistent, yk∼N(0,Sk)\mathbf{y}_k \sim \mathcal{N}(0, \mathbf{S}_k), so the normalised innovation squared yk⊤Sk−1yk\mathbf{y}_k^{\top}\mathbf{S}_k^{-1}\mathbf{y}_k is chi-square with mm degrees of freedom. NIS is what you log in production. It cannot see everything (an error in a direction the sensor never observes does not show in the innovation), but a NIS that sits at three times its mean is a filter in trouble, and you can see it live.

Here is a filter you can break. The truth is drawn from the filter's own model (white acceleration of intensity 0.040.04, position noise of variance 44), and 16 seeded runs feed the two strips. The plot shows run one.

In that widget's code, matched settings average a NEES of 2.07 over 16 runs: two degrees of freedom, as the chi-square says. Cut QQ a hundredfold and the average NEES goes to 47.6 while the position error goes from 1.78 to 4.81 units. That is what an overconfident filter looks like in practice: it lags every turn, and its ellipses stay small enough to exclude the truth. Make RR ten times too small and NEES goes to 17.4 with the error barely changed, because a filter that over-trusts its sensor chases noise but stays near the data. Inflating QQ is the subtle one: NEES stays inside the band (1.78) because the filter collapses into following the fixes, and only NIS (1.32, under its band) gives it away.

Tuning Q and R without guessing

Two knobs do all the tuning. R says how noisy the sensor is; Q says how wrong the model is per step. In practice:

I ran that last point on the real drive described below, scaling the whole of Q\mathbf{Q} in the error-state filter. At the tuned value the averaged NEES is 7.67 and the 3-D position error 1.315 m. Divide Q\mathbf{Q} by 100 and NEES jumps to 164.34 while the error almost doubles to 2.358 m. Multiply it by 100 and NEES drops to 4.63 (underconfident) and the error rises to 1.763 m. Both directions cost accuracy. Only one of them lies about it.

When the world isn't linear: the EKF

The linear filter's predict step was exact because a Gaussian through a linear map stays Gaussian. Robotics is full of maps that are not linear: rotations, projections, ranges, anything with a sine in it. Write the model as

xk=f(xk−1,uk)+wk,zk=h(xk)+vk.\mathbf{x}_k = f(\mathbf{x}_{k-1}, \mathbf{u}_k) + \mathbf{w}_k, \qquad \mathbf{z}_k = h(\mathbf{x}_k) + \mathbf{v}_k .

The extended Kalman filter replaces each function by its first-order Taylor expansion at the current estimate. Propagate the mean through the real ff, and the covariance through the Jacobian:

x^−=f(x^,u),F=∂f∂x∣x^,P−=FPF⊤+Q;y=z−h(x^−),H=∂h∂x∣x^−.\hat{\mathbf{x}}^- = f(\hat{\mathbf{x}}, \mathbf{u}), \quad \mathbf{F} = \left.\frac{\partial f}{\partial \mathbf{x}}\right|_{\hat{\mathbf{x}}}, \quad \mathbf{P}^- = \mathbf{F}\mathbf{P}\mathbf{F}^{\top} + \mathbf{Q}; \qquad \mathbf{y} = \mathbf{z} - h(\hat{\mathbf{x}}^-), \quad \mathbf{H} = \left.\frac{\partial h}{\partial \mathbf{x}}\right|_{\hat{\mathbf{x}}^-}.

The update equations are then the linear ones. What goes wrong has names.

Linearisation error. The Jacobian is the slope at one point. When the uncertainty is wide compared with the scale over which ff or hh curves, the tangent is a bad model of the function across the region the state could be in. The mean is biased (a Gaussian pushed through a curve has a shifted mean, and the EKF ignores the shift) and the covariance is usually too small.

Linearising at the wrong point. The Jacobian is evaluated at the estimate, which is wrong by exactly the error you are trying to correct. A good estimate gives a good Jacobian; a bad one gives a bad Jacobian, which gives a worse estimate. This is the EKF's characteristic failure: fine while it is fine, and then it diverges from a bad start and does not come back. The covariance has no idea, because the Jacobian that produced it assumed the estimate was right. EKF-SLAM's famous inconsistency (the filter growing confident about headings it cannot observe) is this effect. Huang, Mourikis and Roumeliotis traced it to Jacobians evaluated at changing estimates; their fix, the first-estimates Jacobian, is now standard in visual-inertial filters.

The unscented filter: push points, not Jacobians

The unscented Kalman filter (Julier and Uhlmann) skips the Jacobians. It picks 2n+12n+1 sigma points that have exactly the estimate's mean and covariance, pushes each one through the real nonlinear function, and fits a Gaussian to where they land:

χ0=x^,χ±i=x^±[(n+λ)P]i,x^−=∑iwi f(χi),P−=∑iwi (f(χi)−x^−)(⋯ )⊤+Q.\boldsymbol{\chi}_0 = \hat{\mathbf{x}}, \quad \boldsymbol{\chi}_{\pm i} = \hat{\mathbf{x}} \pm \big[\sqrt{(n+\lambda)\mathbf{P}}\big]_i, \qquad \hat{\mathbf{x}}^- = \sum_i w_i\, f(\boldsymbol{\chi}_i), \quad \mathbf{P}^- = \sum_i w_i\,(f(\boldsymbol{\chi}_i)-\hat{\mathbf{x}}^-)(\cdots)^{\top} + \mathbf{Q}.

The update builds the innovation covariance and the state-measurement cross-covariance the same way, then uses the usual gain, K=PxzS−1\mathbf{K} = \mathbf{P}_{xz}\mathbf{S}^{-1}. The sigma points sample the function across the spread of the uncertainty instead of at one point, so the transformed mean is right to second order and the covariance accounts for curvature the EKF cannot see. No Jacobians to derive, which is a real saving when hh is ugly.

The cost is 2n+12n+1 evaluations of ff and hh per step and a Cholesky factorisation of P\mathbf{P}. In my NumPy runs below, with 15 error states (31 sigma points) at 100 Hz, the UKF took 10.2 s per 288 s run against 1.3 s for the error-state filter. For a rotation the naive version also breaks: you cannot average quaternions by adding them. My UKF draws its sigma points in the tangent space of the rotation and maps them back with the exponential map, the UKF-M construction of Brossard, Barrau and Bonnabel (arXiv:2002.00878).

Rotations don't add: the error-state filter

Every robot that moves in 3-D has an orientation in its state, and orientation is where the textbook EKF goes wrong in code. A rotation has three degrees of freedom, but every representation of it is either singular somewhere (Euler angles) or redundant (a unit quaternion is four numbers with one constraint; a rotation matrix is nine with six). The group of rotations, SO(3)SO(3), is a curved manifold, not a vector space. "Add a correction" has no meaning on it.

The classic quaternion EKF puts the four numbers qw,qx,qy,qzq_w, q_x, q_y, q_z in the state and treats them as free. Its update adds Ky\mathbf{K}\mathbf{y} to them, which leaves the unit sphere, so the code renormalises afterwards. Its covariance is 4×44 \times 4 for three degrees of freedom, so it is singular in the ideal case and wrong in the radial direction in practice. Nothing in the math knows about the constraint; it is patched on after each step.

Two diagrams of a unit circle standing for the unit-quaternion sphere S3. Left: a quaternion q drawn as a radius, and a red tangent line at the identity carrying a vector theta whose length matches the blue arc to q; labelled q = Exp(theta), theta = Log(q). Right: two quaternions p and q on the circle, with the tangent line at p carrying theta; labelled q = p oplus theta, theta = q ominus p.
The quaternion sphere drawn as a circle. Left: Exp and Log map a vector in the 3-D tangent space to a rotation and back. Right: the ⊕ and ⊖ operators that replace + and − on a manifold; the error-state filter lives in the tangent line at its current estimate (Solà, Quaternion kinematics for the error-state Kalman filter, Figure 11).

The error-state Kalman filter (ESKF) makes the constraint structural. It keeps a pair:

This works better than it sounds. The error is always small (it is reset to zero after every update), so its dynamics are close to linear and the first-order Jacobians are good. The rotation error has exactly three numbers, so the covariance is minimal and non-singular. And the error dynamics are slow, so the heavy covariance step can run at a lower rate than the IMU if needed. This is the filter Solà's notes (arXiv:1711.02508) derive in full, and they are the best single reference on the subject I know.

A curved blue surface labelled M with a point X on it, and a flat red grid tangent to the surface at X labelled T_X M. A red ellipse is drawn in the tangent plane around X and a thick blue closed curve on the surface shows where that ellipse lands when wrapped onto the manifold.
Uncertainty on a manifold is a Gaussian in the tangent space at the estimate (red), wrapped onto the manifold by ⊕ (blue). The ESKF's 15 × 15 covariance is exactly this red ellipse (Solà, Deray and Atchuthan, A micro Lie theory for state estimation in robotics, Figure 10).

The ESKF in equations

With accelerometer reading am\mathbf{a}_m (specific force in the body frame), gyro reading ωm\boldsymbol{\omega}_m, and gravity g\mathbf{g}, the nominal state propagates as

p←p+vΔt+12(R(am−ba)+g)Δt2,v←v+(R(am−ba)+g)Δt,R←R Exp((ωm−bω)Δt).\mathbf{p} \leftarrow \mathbf{p} + \mathbf{v}\Delta t + \tfrac12\big(\mathbf{R}(\mathbf{a}_m-\mathbf{b}_a)+\mathbf{g}\big)\Delta t^2, \quad \mathbf{v} \leftarrow \mathbf{v} + \big(\mathbf{R}(\mathbf{a}_m-\mathbf{b}_a)+\mathbf{g}\big)\Delta t, \quad \mathbf{R} \leftarrow \mathbf{R}\,\mathrm{Exp}\big((\boldsymbol{\omega}_m-\mathbf{b}_\omega)\Delta t\big).

The error state propagates through a 15 × 15 Jacobian whose only non-trivial blocks are

∂δv∂δθ=−R [am−ba]×Δt,∂δv∂δba=−R Δt,∂δθ∂δθ=Exp((ωm−bω)Δt) ⁣⊤,∂δθ∂δbω=−I Δt,\frac{\partial\delta\mathbf{v}}{\partial\delta\boldsymbol{\theta}} = -\mathbf{R}\,[\mathbf{a}_m-\mathbf{b}_a]_\times\Delta t, \quad \frac{\partial\delta\mathbf{v}}{\partial\delta\mathbf{b}_a} = -\mathbf{R}\,\Delta t, \quad \frac{\partial\delta\boldsymbol{\theta}}{\partial\delta\boldsymbol{\theta}} = \mathrm{Exp}\big((\boldsymbol{\omega}_m-\mathbf{b}_\omega)\Delta t\big)^{\!\top}, \quad \frac{\partial\delta\boldsymbol{\theta}}{\partial\delta\mathbf{b}_\omega} = -\mathbf{I}\,\Delta t,

plus ∂δp/∂δv=IΔt\partial\delta\mathbf{p}/\partial\delta\mathbf{v} = \mathbf{I}\Delta t and identity on the diagonal. The first block is the one that couples attitude to position: a tilt error rotates the measured specific force, so gravity leaks into the horizontal acceleration. That coupling is why a GPS position fix can correct roll and pitch at all. In NumPy it is ten lines:

F = np.eye(15)
F[0:3, 3:6] = I3 * dt
F[3:6, 6:9] = -R @ skew(am - ba) * dt
F[3:6, 9:12] = -R * dt
F[6:9, 6:9] = Exp((wm - bg) * dt).T
F[6:9, 12:15] = -I3 * dt
x = propagate(x, am, wm, dt)          # nominal state, full nonlinear
P = F @ P @ F.T + Qd(dt)

An update computes H\mathbf{H} with respect to the error state, solves for δx=Ky\delta\mathbf{x} = \mathbf{K}\mathbf{y} as usual, injects it into the nominal state with ⊞\boxplus (so the rotation is updated by R Exp(δθ)\mathbf{R}\,\mathrm{Exp}(\delta\boldsymbol{\theta}) and stays a rotation), and resets the error to zero. The reset moves the tangent space the covariance lives in, so the covariance is transported with the reset Jacobian G≈I−[12δθ]×\mathbf{G} \approx \mathbf{I} - [\tfrac12\delta\boldsymbol{\theta}]_\times on the rotation block. Most implementations skip it. For small corrections the difference is negligible.

Here is one update done three ways on a body that turns in the plane, where the rotation is a unit complex number (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta): two numbers, one constraint, the 2-D cousin of the quaternion. Step through it, then drag the prior error up.

At the default 70° error the noise-free sensor says the body points at 130°. The classic EKF adds its correction in the plane, lands at ∣q∣=1.166|q| = 1.166, renormalises, and reports 112.2° with a σ of 3.9°: 17.8° wrong and claiming better than 4°. The error-state update moves along the circle and lands at 112.8°. Same linearisation point, almost the same answer here. The difference is that one is a rotation by construction and the other only after a patch. The iterated filter in the last step gets to 128.6° in five passes.

Iterating the update: the IEKF inside FAST-LIO

The EKF linearises hh at the predicted state. If the prediction is far from the truth, the measurement Jacobian is evaluated in the wrong place. The fix follows from the MAP view above. The update is the minimiser of

J(δx)=∥(xj⊟x^−)+Jjδx∥P−2+∥z−h(xj⊞δx)∥R2,J(\delta\mathbf{x}) = \lVert (\mathbf{x}_j \boxminus \hat{\mathbf{x}}^-) + \mathbf{J}_j\delta\mathbf{x} \rVert^2_{\mathbf{P}^-} + \lVert \mathbf{z} - h(\mathbf{x}_j \boxplus \delta\mathbf{x}) \rVert^2_{\mathbf{R}},

a nonlinear least-squares problem. The EKF takes one Gauss-Newton step on it from x0=x^−\mathbf{x}_0 = \hat{\mathbf{x}}^-. The iterated EKF keeps going: relinearise hh at the new estimate xj\mathbf{x}_j, solve the linear problem again (with the prior term still anchored at the prediction, which is the xj⊟x^−\mathbf{x}_j \boxminus \hat{\mathbf{x}}^- term people forget), and repeat until the step is small. Each pass is

δxj=−Jj−1(xj⊟x^−)+Kj(rj+HjJj−1(xj⊟x^−)),xj+1=xj⊞δxj,\delta\mathbf{x}_j = -\mathbf{J}_j^{-1}(\mathbf{x}_j \boxminus \hat{\mathbf{x}}^-) + \mathbf{K}_j\big(\mathbf{r}_j + \mathbf{H}_j\mathbf{J}_j^{-1}(\mathbf{x}_j \boxminus \hat{\mathbf{x}}^-)\big), \qquad \mathbf{x}_{j+1} = \mathbf{x}_j \boxplus \delta\mathbf{x}_j,

with rj=z−h(xj)\mathbf{r}_j = \mathbf{z} - h(\mathbf{x}_j) and Kj\mathbf{K}_j computed from Jj−1P−Jj−⊤\mathbf{J}_j^{-1}\mathbf{P}^-\mathbf{J}_j^{-\top}, where Jj=Jr−1(xj⊟x^−)\mathbf{J}_j = \mathbf{J}_r^{-1}(\mathbf{x}_j \boxminus \hat{\mathbf{x}}^-) is, on its rotation block, the inverse right Jacobian of SO(3)SO(3) that carries the prior covariance onto the tangent space at xj\mathbf{x}_j. When the measurement is linear in the state, the second pass changes nothing and it stops.

FAST-LIO and FAST-LIO2 run exactly this, on an error state over a manifold, with the IMU predicting between LiDAR scans and thousands of point-to-plane residuals in each update. The point-to-plane residual is strongly nonlinear in the pose (a small rotation error moves a point 30 m away by a lot), which is why iterating pays there. The loop in the middle of the overview is the iterated update, and the "converged?" test is the one above.

FAST-LIO2 system overview: IMU and LiDAR inputs feed forward propagation, points accumulation, backward propagation, residual computation, and an iterated state update that loops until converged; on the right an ikd-Tree map handles point-wise insert, box-wise delete, and kNN search.
FAST-LIO2's state estimation is an iterated error-state Kalman filter: forward propagation on the IMU, then residual computation and state update repeated until the 'Converged?' test passes, once per scan (FAST-LIO2 paper, Figure 1).
The point-to-plane measurement model: a scan point (red) and the corresponding plane fit from nearby map points (blue), with the plane normal u_j; the residual is the signed distance from the point to the plane.
The measurement each iteration relinearises: the distance from a scan point to the plane through its nearest map points. Its dependence on the pose is what makes iterating worth it (FAST-LIO2 paper, Figure 2).

Each iteration costs one more residual computation and one more gain. FAST-LIO keeps that cheap with the information-form gain from the derivation, which inverts an 18 × 18 matrix however many points there are. The iterated filter is also where filtering and optimisation meet: run it to convergence and the update is a MAP estimate, solved by Gauss-Newton, over one step's worth of variables.

Four filters on a real drive

Derivations are cheap. I wanted to see how much these differences matter on a real vehicle with a real IMU, so I wrote all four nonlinear filters in NumPy (about 380 lines with the runner, no estimation library) and ran them on a KITTI drive.

The data. KITTI raw drive 2011_09_30_drive_0018, a residential loop in Karlsruhe. The "extract" archive of that drive is 16.8 GB, almost all of it images and LiDAR; I read the zip's central directory over HTTP range requests and pulled only the OXTS files, 28 MB in all. The OXTS RT3003 is a GPS/IMU unit with RTK corrections, logging at 100 Hz. After dropping 12 out-of-order timestamps there are 28,658 samples over 288 s and 2.21 km, top speed 11.7 m/s, with one 1.62 s gap in the log that the filters simply integrate across.

The setup, stated plainly because it decides what the numbers mean:

A gate first. Before touching the real log, I simulated 60 s of IMU and fixes from each filter's own model with known noise. A filter that fails NEES there has a bug. All four passed: averaged NEES between 8.06 and 8.11 against the band of 7.24 to 10.95, and NIS 3.13 for 3-dimensional fixes.

Tuning on the log

The IMU's real noise is unknown, so I tuned the gyro noise density on the error-state filter with accelerometer noise fixed at 0.05 m/s² per sample:

gyro σ per sampleavg NEES (band 7.24–10.95)3-D position RMSEyaw RMSE
0.002 rad/s22.401.454 m3.795°
0.005 rad/s10.781.344 m2.548°
0.01 rad/s7.671.315 m2.148°
0.02 rad/s6.321.337 m2.142°

Too little gyro noise and the filter believes its heading too much: NEES of 22.40, and the worst heading. At 0.005 rad/s the average is inside the band but near its top (10.78); at 0.01 rad/s it sits near the bottom (7.67) with a lower heading error, and doubling again buys nothing. I took 0.01 rad/s: slightly underconfident, which is the side I want a navigation filter on. Only 34% of the time steps fall inside the band even so. Split by what the car is doing, the averaged NEES is below the band for 64% of the samples on straights and 95% of the few while stopped, and climbs in turns (mean 8.22, above the band 12% of the time, against 8% on straights). That reads as a noise model a little too generous for steady driving and about right when the attitude is being excited. A filter that passes on average and fails in stretches is normal on real data, and seeing where it fails is the useful part.

A good start: they all agree

With a small initial error, 1 Hz fixes and no outages:

filter3-D position RMSEvelocity RMSEyaw RMSEavg NEESavg NIS (expect 3)time per run
EKF (quaternion)1.315 m0.378 m/s2.155°7.672.853.2 s
UKF-M1.315 m0.378 m/s2.181°7.702.8510.2 s
ESKF1.315 m0.378 m/s2.148°7.672.851.3 s
IEKF1.315 m0.378 m/s2.148°7.672.851.3 s
linear KF, no IMU2.595 m1.925 m/sn/a4.76 (6 dof)3.070.1 s

Four filters, one answer, to three decimals on position. When the errors are small, the linearisation is good and every method is the same estimator. The IEKF averages two passes per update and stops, because a position fix is linear in the state. The useful comparison in this table is the last row: a linear constant-velocity filter on the same fixes, with no IMU, has twice the position error and five times the velocity error. That gap is what the IMU buys. (The KF row uses the best of the three acceleration intensities I tried; at the other two its NEES was 35.6 and 2.76, outside its 6-dof band of 4.58 to 7.61.)

A map of the KITTI drive in metres east and north: a grey loop for the OXTS truth, dark dots for the noisy 1 Hz fixes scattered around it, a blue line for the error-state filter's estimate following the loop. Over a red highlighted stretch where fixes were withheld for 30 seconds, the blue line drifts off the road into the middle of a block, then jumps back onto the road at the first fix after the gap.
The drive, one seed: OXTS truth, the synthetic 1 Hz fixes, and the error-state filter riding the IMU between them. In red, the 30 s window from t = 120 s with no fixes, as in the stress test below: the estimate dead-reckons off the road and snaps back on the first fix after it. My plot, from my runs on KITTI raw drive 2011_09_30_drive_0018.

A bad start: the differences appear

Then I made it hard. Each filter starts with its heading 60° wrong (and is told so: the prior yaw σ is 60°), and fixes are withheld for 30 s from t = 120 s.

filteryaw error at 10 savg NEES, first 30 savg NEES after 10 s3-D position RMSEyaw RMSE
EKF (quaternion)19.0°36.910.466.453 m8.730°
UKF-M10.8°10.97.556.417 m7.818°
ESKF20.8°44.211.476.699 m8.903°
IEKF20.5°40.311.056.725 m8.823°
A log-scale plot of averaged NEES against time over 288 seconds for four filters, with a shaded green band between about 7 and 11 and a shaded red window from 120 to 150 seconds where fixes are withheld. The lines start high while the heading error is large and settle at different levels; the outage drives some of them up.
Averaged NEES over 20 seeds for the 60° heading error with the 30 s outage, against the 95% band. A line above the band is a filter claiming more certainty than it has. My plot, from my runs.

The position errors are close and large for all four, because the 30 s outage dominates them: with no fixes the filters dead-reckon on the IMU and drift about 40 m (RMS over the seeds, at the moment the fixes return) whichever one you run. They all knew it. Averaged NEES during the outage sits between 4.67 and 5.63, below the band: the covariance grew faster than the error, the honest direction to be wrong in.

The heading is where they part. A 60° error is far outside the region where a first-order Jacobian describes the rotation, and in the first 30 s the EKF and ESKF report NEES of 36.9 and 44.2: four to five times overconfident while they swing the heading back. The UKF-M, whose sigma points sample the rotation at ±1.9σ instead of at one point, gets its heading to 10.8° by the 10 s mark (against 19.0° and 20.8°) and stays near its band throughout (NEES 10.9). The IEKF tracks the ESKF, as it should with linear fixes: its second pass confirms the first.

Push the start to 120° wrong (prior σ 120°, no outage) and only one of them comes back. The EKF, ESKF and IEKF end the run with heading errors of 50.0°, 50.4° and 45.7° RMS and NEES in the thousands; a Jacobian taken at a heading that far off points the correction the wrong way, and the covariance shrinks anyway. The UKF-M gets to 18.0° RMS (17.3° at the 30 s mark) with NEES of 36.96: still overconfident, but recovering. If your system can start with no idea which way it faces, initialise the heading some other way (a magnetometer, a short drive, a coarse search) before you hand it to an EKF of any flavour.

A nonlinear measurement: where iterating pays

A position fix is linear in the state, so it cannot show what the IEKF is for. To give it a measurement that is not, I replaced the fixes with ranges to four fixed anchors 50 m from the start in each diagonal direction (two at 2 m height, two at 15 m), like UWB beacons, with σ 0.5 m at 1 Hz. The ranges are computed from the true positions plus noise; they are as synthetic as the fixes were. Each seed draws the start from a prior with σ 30 m on each horizontal axis and σ 30° in heading (34.2 m from the truth at the median, 34.6° RMS in heading).

After the first update, the EKF and the ESKF are 18.63 m from the truth (RMS over 20 seeds) and report NEES of 663 averaged over the first 10 s: they took one linearised step from a point 30 m off and believe it. The IEKF, iterating the same update (5.81 passes on average), lands 1.13 m from the truth on the first update and its averaged NEES over the first 10 s is 9.6, inside the band. Over the whole run its position RMSE is 2.285 m against 6.499 m for the EKF and 6.237 m for the ESKF, and it is the only filter of the four whose run-long NEES (10.16) is in the band; the EKF's is 120.82 and the ESKF's 248.20.

The UKF was the worst: 41.12 m RMS after the first update and 22.524 m over the run, with NEES in the thousands. That RMS is driven by a few seeds. Per seed, the UKF's first-update error has a median of 11.87 m and a worst case of 95.3 m (the ESKF's: 6.87 m and 51.6 m; the IEKF's: 1.03 m and 2.1 m). The likely cause is its spread: the sigma points sit ±1.9σ, about 58 m, from a start already some 34 m off, while the anchors are 50 m away on each diagonal, so some sigma points land near or past an anchor, where range is as nonlinear as it gets. Narrowing the spread (α from 0.5 to 0.1) brings the first-update RMS down to 8.08 m and the median to 5.0 m, which supports that reading without making the UKF competitive here. A UKF's sampling is a strength when its spread matches the function's scale and a liability when it does not. The IEKF's answer to a bad prior is different in kind: keep relinearising at the best estimate so far until the update converges, which is what FAST-LIO's loop does on every scan.

A log-scale plot of position error in metres against time for the first 30 seconds of the range-only experiment, one line per filter. The iterated filter's error is lowest from the first update; the EKF and ESKF lines start highest and overlap; the UKF sits between them.
Position error, RMS over 20 seeds, for the first 30 s with range-only measurements and a 30 m initial error. My plot, from my runs.

Filters versus factor graphs

Every filter on this page marginalises: after each step it folds the past into one Gaussian and forgets it. Once a linearisation error is baked into P\mathbf{P}, it stays there. A factor graph, the model GTSAM solves, keeps the past as variables and factors (the prior, each IMU step, each measurement) and solves for all of it jointly by nonlinear least squares. It is the same cost as the IEKF's, over the whole trajectory instead of one step, so it can relinearise old states when new information arrives.

In the linear-Gaussian case the batch solution over a whole log equals the Rauch-Tung-Striebel smoother: a Kalman filter forward and one backward pass. So I can measure the value of looking back without leaving NumPy. An error-state RTS pass over the same 20 seeds of the good-start run:

3-D position RMSEvelocity RMSEyaw RMSEavg NEES
ESKF, filtering1.315 m0.378 m/s2.148°7.67
ESKF + RTS, smoothing0.540 m0.079 m/s0.981°9.90

Using future fixes to correct past states cuts position error by 59% and heading error by more than half, and the smoothed covariance stays consistent. The backward pass took 1.4 s against 2.2 s for the forward filter with storage. That is the factor graph's case in two rows, and it is a strong one: whenever the answer can wait (mapping, offline trajectories, anything with loop closures), a smoother or a graph wins on accuracy. This is not GTSAM itself. I did not run it here; the RTS pass is the linearised special case, and a real graph with IMU preintegration and relinearisation would do at least as well.

Filters keep their place anyway:

The modern systems split the difference: a filter or a short sliding window for the real-time state (FAST-LIO2, VINS-Mono's local window), and a pose graph in the background for loop closures. That split is what I would build.

Which filter I'd use

And whichever you pick, log NIS from day one. It costs one line, it needs no ground truth, and it is the only way you will find out your filter has started lying before something downstream does.

What I ran and what I read

What I ran, all on a MacBook CPU: the four filters, the linear KF and the RTS smoother, in my own NumPy code; the simulation gate; the gyro sweep, the Q scaling, the good-start, 60° and 120° heading and range-anchor runs, 20 seeds each; and the three result plots. The two widgets' numbers come from their own code, which I also ran under Node to check them. What I read: the filter derivations follow Solà's quaternion notes and micro Lie theory (both figures are from the arXiv sources, rendered from the authors' PDFs), the FAST-LIO and FAST-LIO2 papers for the iterated update and its figures, Brossard et al. for the UKF-M, and Bar-Shalom, Li and Kirubarajan for the NEES and NIS tests. The data is KITTI raw (Geiger et al.), CC BY-NC-SA 3.0. Not verified: whether the OXTS accelerations carry any internal bias compensation (they dead-reckon cleanly, which suggests they are close), and how the filters compare against GTSAM itself, which I did not run.

Cite this article

For attribution, please use the following reference or BibTeX:

Satyajit Ghana, "The Kalman filter: from two Gaussians to the error-state filter inside LiDAR-inertial SLAM", ai.satyajit.si, June 2026.

bibtex
@misc{ghana2026kalmanfilter,
  author = {Satyajit Ghana},
  title  = {The Kalman filter: from two Gaussians to the error-state filter inside LiDAR-inertial SLAM},
  url    = {https://ai.satyajit.si/articles/kalman-filter},
  year   = {2026}
}
share