Skip to main content
Open In Colab In the one-dimensional Kalman filter the belief stays Gaussian at every step. The predict step adds two Gaussians and the update step multiplies two Gaussians, and both results are Gaussian again. That holds because the motion model and the measurement model are linear. However, most real systems are not linear. Both state transition model and measurement model must accommodate non-linearities: when a turn motion that moves the robot along an arc and when a range sensor measures distance through a non-linear function such as a square root.
A range sensor that sees a landmark at (mx,my)(m_x, m_y) from a robot at (x,y)(x, y) returns the Euclidean distancer=(mx−x)2+(my−y)2r = \sqrt{(m_x - x)^2 + (m_y - y)^2}plus noise. This is the landmark measurement model of Probabilistic Robotics (Thrun, Burgard and Fox, Section 6.6.2, Equation 6.41). The square root makes rr a nonlinear function of the robot’s position, so a Gaussian belief about the position does not give a Gaussian prediction of the range. The EKF localization section uses this model and its Jacobian. The beam model gets the expected range by ray casting through the map instead.
The extended Kalman filter (EKF) handles such models by linearizing them around the current estimate. Before we see this linearization step, lets first understand what happens to to Gaussian when it goes through a non-linearity.

A Gaussian through a U-shaped function

Take a belief x∼N(μ,σ2)x \sim \mathcal{N}(\mu, \sigma^2) and pass it through the obviously non-linear U-shaped function y=g(x)=(x−μ)2,y = g(x) = (x - \mu)^2, whose lowest point sits exactly at the mean of the Gaussian. The two halves of the input fold onto the same output: x=μ+dx = \mu + d and x=μ−dx = \mu - d both give y=d2y = d^2. This function produces positive numbers and the majority of the probability mass is near zero, where the input is most likely. To find the distribution of yy, draw many samples of xx, apply gg to each one, and look at the histogram of the results.
Output from cell 3 Output from cell 3 The output looks nothing like a Gaussian. All of its mass sits at y≥0y \ge 0, and its density grows without bound as y→0y \to 0. For this gg the exact density is a scaled chi-squared distribution with one degree of freedom: fY(y)=12πσ2yexp⁡(−y2σ2),y>0.f_Y(y) = \frac{1}{\sqrt{2\pi\sigma^2 y}}\exp\left(-\frac{y}{2\sigma^2}\right), \qquad y > 0. Its mean is σ2\sigma^2 and its variance is 2σ42\sigma^4. The dashed curve is the Gaussian with that mean and variance. It puts about 24% of its probability on negative values of yy, which gg can never produce, and it misses the peak at zero. A filter that forced the belief back into a Gaussian after this step would carry the dashed curve forward, with the wrong shape and much of its mass in the wrong place.

Linearizing the function

The EKF keeps the belief Gaussian by replacing the nonlinear function with its tangent line at the mean of the belief. A first-order Taylor expansion of gg around a point mm gives g(x)≈g(m)+g′(m) (x−m).g(x) \approx g(m) + g'(m)\,(x - m). The right-hand side is linear in xx. If the input is x∼N(m,σ2)x \sim \mathcal{N}(m, \sigma^2), the output of the tangent line is Gaussian again: y∼N(g(m),  g′(m)2 σ2).y \sim \mathcal{N}\left(g(m),\; g'(m)^2\,\sigma^2\right). The slope g′(m)g'(m) stretches or shrinks the spread of the belief. With a vector state the slope becomes a Jacobian matrix GG, and the variance g′(m)2σ2g'(m)^2\sigma^2 becomes the covariance G Σ G⊤G\,\Sigma\,G^\top.
The Jacobian of a vector function g:Rn→Rm\mathbf{g}: \mathbb{R}^n \to \mathbb{R}^m is the m×nm \times n matrix of its first partial derivatives, with entries Gij=∂gi/∂xjG_{ij} = \partial g_i / \partial x_j. Row ii is the gradient of the output gig_i, and the whole matrix is the best linear approximation of g\mathbf{g} near the point where it is evaluated. We have seen Jacobians in backpropagation in deep networks and the references therein.
The code below compares this approximation with the sampled output in two cases. In the first, the belief is centered at x=6x = 6, far enough up the right arm that it no longer reaches the bottom of the U. In the second, it is centered at the lowest point of gg, x=μ=2x = \mu = 2, as before.
Output from cell 5 On the right arm, across most of the belief, gg is close to its tangent line, and the linearized Gaussian N(16,64)\mathcal{N}(16, 64) sits close to the sampled output, whose mean is about 17 and variance about 66. The small gap comes from the curvature of gg, which the first-order expansion does not capture. Next to a large slope it is a small fraction of the total. At the lowest point the picture changes. The tangent line is flat. The slope is g′(μ)=0g'(\mu) = 0, so the linearized output has zero variance and puts all of its mass at y=0y = 0. The true output has mean σ2\sigma^2 and variance 2σ42\sigma^4. The tangent line sees only the slope at one point, and here the slope says nothing about how quickly the function rises on either side. The first-order expansion does not capture the curvature of gg, and here the curvature is all there is.
Write x=m+ex = m + e with e∼N(0,σ2)e \sim \mathcal{N}(0, \sigma^2). For this gg the expansion ends at the second-order term and is exact:g(m+e)=g(m)+g′(m) e+12 g′′(m) e2,g′′(m)=2.g(m + e) = g(m) + g'(m)\,e + \tfrac{1}{2}\,g''(m)\,e^2, \qquad g''(m) = 2.Because E[e]=0\mathbb{E}[e] = 0 and E[e2]=σ2\mathbb{E}[e^2] = \sigma^2, the mean of yy is g(m)+12 g′′(m) σ2=g(m)+σ2g(m) + \tfrac{1}{2}\,g''(m)\,\sigma^2 = g(m) + \sigma^2. The linear and quadratic terms are uncorrelated, since E[e3]=0\mathbb{E}[e^3] = 0, and a Gaussian has Var⁡(e2)=2σ4\operatorname{Var}(e^2) = 2\sigma^4, soVar⁡(y)=g′(m)2 σ2+14 g′′(m)2⋅2σ4=g′(m)2 σ2+2σ4.\operatorname{Var}(y) = g'(m)^2\,\sigma^2 + \tfrac{1}{4}\,g''(m)^2 \cdot 2\sigma^4 = g'(m)^2\,\sigma^2 + 2\sigma^4.The tangent line keeps only g(m)g(m) and g′(m)2σ2g'(m)^2\sigma^2, so it misses σ2\sigma^2 in the mean and 2σ42\sigma^4 in the variance wherever the belief is centered. At m=μm = \mu the slope is zero, so the curvature terms are the whole output: mean σ2\sigma^2 and variance 2σ42\sigma^4. At m=6m = 6 with σ=1\sigma = 1 the slope is g′(6)=8g'(6) = 8, and the same terms are small next to it: the exact moments are 1717 and 64+2=6664 + 2 = 66, against the linearized 1616 and 6464. For a general gg the expansion goes on, and the third- and higher-order terms add further errors.
Inside a filter this error carries into the next step. Line 2 of the algorithm below predicts the covariance as GtΣt−1Gt⊤+RtG_t \Sigma_{t-1} G_t^\top + R_t. If the motion model is flat at the current estimate, then Gt=0G_t = 0 and the predicted covariance is just the motion noise RtR_t. The uncertainty in Σt−1\Sigma_{t-1} is dropped, and the filter is more confident than it should be. The Kalman gain in line 3 grows with the predicted covariance, so a small covariance gives a small gain. The update then gives little weight to the measurement ztz_t, and the measurement is what would correct the estimate. The predicted mean is also wrong: the filter predicts y=0y = 0, but the true mean is σ2\sigma^2. Over the next steps the estimate moves away from the true state, and the covariance stays small. This is called filter divergence. An EKF whose covariance is smaller than its actual error is called inconsistent. The measurement model can fail the same way. If hh is flat at the predicted mean, then Ht=0H_t = 0, the gain KtK_t is zero, and the update ignores the measurement however informative it is.
In robots a Jacobian is rarely zero in every direction. More often it is flat in some directions of the state, or its slope is small compared with how much the model bends across the belief.Measurement models:
  • GPS. A GPS position fix does not depend on the heading, so the heading column of HtH_t is zero. The heading becomes observable only through motion, by comparing successive fixes or through the GPS velocity v(cos⁡θ,sin⁡θ)v(\cos\theta, \sin\theta). That velocity has derivative v(−sin⁡θ,cos⁡θ)v(-\sin\theta, \cos\theta) with respect to θ\theta, which vanishes when the robot stands still.
  • Terrain-aided navigation underwater. An autonomous underwater vehicle can fix its position by matching multibeam echosounder soundings to a stored depth map of the seabed. Over a flat ocean floor every nearby position predicts the same depths, so hh is flat in both horizontal directions and the soundings give no position information. The terrain navigation module of the Kongsberg HUGIN vehicles therefore needs “a certain degree of terrain variation” (Ånonsen et al.). A 2022 study of bathymetric SLAM on HUGIN 3000 survey data includes such a flat stretch of seabed (Torroba et al., 2022).
  • Range sensors (lidar, sonar, ultra-wideband). A single range does not change as the robot moves on a circle around the landmark, so that direction is flat.
  • Bearing sensors and cameras. Moving along the line of sight changes neither the bearing nor the pixel, so depth is flat. The bearing derivative also shrinks with distance, so far landmarks give a nearly flat HtH_t.
  • Saturated readings. A lidar return at maximum range or a saturated pixel stays the same when the state changes a little.
Motion models:
  • Contact and limits. A robot pushed against a wall, a joint at its limit, or a saturated actuator. In the blocked direction the next state does not depend on the current one, so that part of GtG_t is zero.
  • Wide heading uncertainty. After startup or after the robot is moved by hand, the heading can be almost anywhere. The terms cos⁡θ\cos\theta and sin⁡θ\sin\theta in the motion model bend strongly across the belief, and the tangent at the mean describes them poorly.
Linearization works when the function is close to a straight line over the region where the belief has most of its mass. A narrow belief, or a function that bends slowly, keeps the EKF accurate. A wide belief near a sharp bend breaks it.

Nonlinearity and uncertainty

Probabilistic Robotics (Thrun, Burgard and Fox, Section 3.3) names two factors that decide how good the EKF’s linearization is: the degree of local nonlinearity of the function, and the degree of uncertainty in the belief. The comparison above varied the first factor. It kept the width of the belief fixed and moved it from the arm, where gg is nearly straight, to the bottom of the U, where gg bends sharply. The comparison below varies the second factor. Both beliefs are centered at x=6x = 6 on the arm, where the tangent line worked well, and only their standard deviations differ: σ=0.5\sigma = 0.5 and σ=2.5\sigma = 2.5.
Output from cell 7 The narrow belief covers a short, nearly straight piece of gg, and its linearized Gaussian matches the sampled output. The wide belief reaches past the bottom of the U at x=2x = 2. That part of the belief folds back, and the sampled output is skewed, with extra mass near y=0y = 0 and a long right tail. The tangent line cannot see the fold. Its mean is too low by σ2=6.25\sigma^2 = 6.25, and its Gaussian puts probability on negative values of yy that gg never produces. The same point on the same function is easy to linearize for a confident belief and hard for an uncertain one. A robot that is less certain has a wider belief, and a wider belief covers more of the curvature of the motion and measurement models. This is why an EKF works best when the uncertainty of its estimate stays small, and why the overconfidence described above is so damaging: a filter that underestimates its uncertainty also stops noticing that its linearization has become poor.

The extended Kalman filter algorithm

Write the state as sts_t, the action as ata_t and the measurement as ztz_t. The motion model and the measurement model are now nonlinear functions with additive Gaussian noise: st=g(at,st−1)+εt,εt∼N(0,Rt),s_t = g(a_t, s_{t-1}) + \varepsilon_t, \qquad \varepsilon_t \sim \mathcal{N}(0, R_t), and zt=h(st)+δt,δt∼N(0,Qt).z_t = h(s_t) + \delta_t, \qquad \delta_t \sim \mathcal{N}(0, Q_t). From here on, gg is the motion model and no longer the U-shaped function above. The EKF linearizes gg around the previous mean μt−1\mu_{t-1} and hh around the predicted mean μˉt\bar\mu_t. The two Jacobians are Gt=∂g(at,st−1)∂st−1∣st−1=μt−1,Ht=∂h(st)∂st∣st=μˉt.G_t = \left.\frac{\partial g(a_t, s_{t-1})}{\partial s_{t-1}}\right|_{s_{t-1} = \mu_{t-1}}, \qquad H_t = \left.\frac{\partial h(s_t)}{\partial s_t}\right|_{s_t = \bar\mu_t}. One step of the filter takes the previous belief (μt−1,Σt−1)(\mu_{t-1}, \Sigma_{t-1}), the action ata_t and the measurement ztz_t, and returns the new belief (μt,Σt)(\mu_t, \Sigma_t): 1:μˉt=g(at,μt−1)predict the mean2:Σˉt=Gt Σt−1 Gt⊤+Rtpredict the covariance3:Kt=ΣˉtHt⊤(HtΣˉtHt⊤+Qt)−1Kalman gain4:μt=μˉt+Kt(zt−h(μˉt))update the mean5:Σt=(I−KtHt) Σˉtupdate the covariance\begin{array}{lll} 1: & \bar\mu_t = g(a_t, \mu_{t-1}) & \text{predict the mean} \\ 2: & \bar\Sigma_t = G_t\, \Sigma_{t-1}\, G_t^\top + R_t & \text{predict the covariance} \\ 3: & K_t = \bar\Sigma_t H_t^\top \left(H_t \bar\Sigma_t H_t^\top + Q_t\right)^{-1} & \text{Kalman gain} \\ 4: & \mu_t = \bar\mu_t + K_t \left(z_t - h(\bar\mu_t)\right) & \text{update the mean} \\ 5: & \Sigma_t = (I - K_t H_t)\, \bar\Sigma_t & \text{update the covariance} \end{array} In this notation RtR_t is the motion noise covariance and QtQ_t is the measurement noise covariance. Compared with the Kalman filter:
  1. The means pass through the nonlinear functions themselves, in lines 1 and 4.
  2. The covariances pass through the Jacobians GtG_t and HtH_t, which take the place of the fixed matrices of the linear model.

A differential-drive robot with an uncertain heading

The U-shaped function we used for the state transition was synthetic and was used to illustrate the EKF but it was not accidental. The same fold appears in the motion model of a differential-drive robot such as the TurtleBot. As in wheeled robots, two wheels of radius rr sit at distance dd on either side of the chassis origin. The action is the pair of wheel speeds at=(uL,uR)a_t = (u_L, u_R), which set the forward speed and the turning rate: v=r2(uL+uR),ω=r2d(uR−uL).v = \frac{r}{2}(u_L + u_R), \qquad \omega = \frac{r}{2d}(u_R - u_L). With the state s=(x,y,θ)s = (x, y, \theta) and a time step Δt\Delta t, the motion model and its Jacobian are g(at,st−1)=[x+v Δtcos⁡θy+v Δtsin⁡θθ+ω Δt],Gt=[10−v Δtsin⁡θ01v Δtcos⁡θ001].g(a_t, s_{t-1}) = \begin{bmatrix} x + v\,\Delta t\cos\theta \\ y + v\,\Delta t\sin\theta \\ \theta + \omega\,\Delta t \end{bmatrix}, \qquad G_t = \begin{bmatrix} 1 & 0 & -v\,\Delta t\sin\theta \\ 0 & 1 & v\,\Delta t\cos\theta \\ 0 & 0 & 1 \end{bmatrix}. Let the robot start at the origin and drive straight down the page, uL=uRu_L = u_R, with its heading known only roughly. Write the heading as θ=−90°+ψ\theta = -90° + \psi, where ψ\psi is the heading error. After the drive the robot is at x=v Δtsin⁡ψx = v\,\Delta t\sin\psi and y=−v Δtcos⁡ψ≈−v Δt+v Δt2ψ2y = -v\,\Delta t\cos\psi \approx -v\,\Delta t + \frac{v\,\Delta t}{2}\psi^2. The forward coordinate yy is the U-shaped function from the start of this section, with its lowest point at ψ=0\psi = 0: a heading error of +δ+\delta or −δ-\delta shortens the progress by the same amount, and the two halves of the heading belief fold together. The Jacobian entry ∂y/∂θ=v Δtcos⁡θ\partial y / \partial\theta = v\,\Delta t\cos\theta is zero at θ=−90°\theta = -90°, so the EKF passes none of the heading uncertainty into yy. Sideways, sin⁡ψ≈ψ\sin\psi \approx \psi is close to linear, and the EKF handles xx well. The code below uses the geometry of a TurtleBot3 Waffle and drives it straight down the page at 0.25 m/s for 4 s, starting from a belief that knows the position to 2 cm but the heading only to 30°30°. It predicts the new pose twice: by pushing samples of the starting pose through gg, and with one prediction step of the EKF class above. To isolate the effect of the heading, the motion noise RtR_t is left out.
Output from cell 10 The dashed ellipse is the EKF’s predicted belief about the position. Write μˉ\bar\mu and Σˉ\bar\Sigma for the x,yx, y parts of the predicted mean and covariance. The ellipse is the set of positions pp whose Mahalanobis distance (p−μˉ)⊤Σˉ−1(p−μˉ)\sqrt{(p - \bar\mu)^\top \bar\Sigma^{-1} (p - \bar\mu)} equals 2. Its axes point along the eigenvectors of Σˉ\bar\Sigma, each half-axis is two standard deviations long, and it encloses about 86% of the probability of the Gaussian. The samples form a curved band, the shape often called a banana. It opens upward like the U, because every heading error shortens the forward progress: no sample gets below y=−1y = -1 m, and the sampled mean stops short of it. The EKF puts its mean at exactly y=−1y = -1 m. Its ellipse is wide in xx, where sin⁡ψ\sin\psi is nearly linear, but only as thick in yy as the 2 cm starting uncertainty, because the Jacobian passes no heading uncertainty into yy. The histogram on the right shows the fold directly: the forward position has a hard limit at −1-1 m and a long tail toward shorter progress, while the EKF predicts a narrow spike at the limit. This is the U-shaped example from the start of the section, now in a real motion model. The EKF is confidently wrong about how far the robot went. The localization section continues this example: the robot measures the range and bearing to a landmark, and the EKF uses the measurement to correct its belief.
Found an error on this page? Report an issue.