Skip to main content
Open In Colab The one-dimensional Kalman filter keeps its belief Gaussian at every step. This holds under two assumptions. The motion model is an affine function of the state plus independent Gaussian noise, and an affine function of a Gaussian is Gaussian. The measurement is a linear function of the state plus independent Gaussian noise, and with such a measurement the posterior is Gaussian too. For a robot that drives in the plane, neither assumption holds. This section first shows where the nonlinearity enters, in the motion model and in the measurement model. It then shows what a nonlinear function does to a Gaussian belief, and builds the extended Kalman filter (EKF), which linearizes both models around the current estimate. The state also stops being a single number. A planar pose s=(x,y,θ)s = (x, y, \theta) has three components, and its uncertainty is a 3×33 \times 3 covariance matrix Σ\Sigma in place of the scalar variance σ2\sigma^2. The diagonal of Σ\Sigma holds the variance of each component. The off-diagonal entries are the covariances between pairs of components, which measure how an error in one component is correlated with an error in another. The multivariate Gaussian section plots such a covariance in two dimensions.

Nonlinear motion

Take the simplest motion a robot can make. It drives straight ahead at speed vv for a time Δt\Delta t, covering the distance v Δtv\,\Delta t, and does not turn. With the state st=(xt,yt,θt)s_t = (x_t, y_t, \theta_t), the pose after the drive is xt=xt−1+v Δtcos⁡θt−1,yt=yt−1+v Δtsin⁡θt−1,θt=θt−1.\begin{aligned} x_t &= x_{t-1} + v\,\Delta t\cos\theta_{t-1}, \\ y_t &= y_{t-1} + v\,\Delta t\sin\theta_{t-1}, \\ \theta_t &= \theta_{t-1} . \end{aligned} The path is a straight line, yet the new position depends on the heading through cos⁡θt−1\cos\theta_{t-1} and sin⁡θt−1\sin\theta_{t-1}. The command specifies how far to move in the robot’s own frame, along its heading. The state records the position in the world frame. Turning one into the other is a rotation by θt−1\theta_{t-1}, and a rotation is a nonlinear function of its angle. If the heading is uncertain, the same command can put the robot in many different places. The Kalman filter used the motion model st=st−1+v Δt+εts_t = s_{t-1} + v\,\Delta t + \varepsilon_t. It is the one-dimensional case of the linear model st=At st−1+Bt at+εt,s_t = A_t\, s_{t-1} + B_t\, a_t + \varepsilon_t , with At=1A_t = 1 and Bt at=v ΔtB_t\, a_t = v\,\Delta t. The Kalman filter needs a motion model of this form, with matrices AtA_t and BtB_t that do not depend on the state. No matrix turns θ\theta into cos⁡θ\cos\theta, so even driving straight does not fit this form. The EKF accepts any differentiable motion model, st=g(at,st−1)+εt.s_t = g(a_t, s_{t-1}) + \varepsilon_t .

Nonlinear measurements

The measurement model can be nonlinear too. The table lists three sensors found on many mobile robots. Each one measures the state s=(x,y,θ)s = (x, y, \theta), and the last two measure it relative to a landmark at a known position (mx,my)(m_x, m_y). A measurement model is linear when it is a linear function of the state you chose, and this does not depend on how complex the sensor hardware is. A GPS receiver is a complex instrument, but it reports the position directly, so hh picks xx and yy out of the state. A range sensor is simple, and its reading is still a nonlinear function of the position. The range 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. In one dimension the square root cancels the square. A landmark ahead of the robot gives r=m−xr = m - x, which is linear in xx, and that is why the one-dimensional filter needed no linearization. In the plane the derivative of rr with respect to the robot’s position is the unit vector from the landmark to the robot. Its direction changes when the robot moves sideways to the line of sight, and that change is the nonlinearity. The EKF localization section uses this model and its Jacobian. The beam model gets the expected range by ray casting through the map instead. A robot’s models are therefore nonlinear in two places: the motion model, through its heading, and the measurement model, through the geometry of range and bearing. The Kalman filter assumes both models are linear, so the EKF is needed when either model is nonlinear.

A Gaussian through a U-shaped function

The Kalman filter’s equations hold only for Gaussian beliefs, and the output of a nonlinear function of a Gaussian random variable is in general not Gaussian. The following example uses a simple function. Take a belief x∼N(μ,σ2)x \sim \mathcal{N}(\mu, \sigma^2) and pass it through the U-shaped function y=g(x)=(x−c)2,y = g(x) = (x - c)^2, whose lowest point is at x=cx = c. Place the mean of the belief at that point, μ=c\mu = c. Points at the same distance on either side of cc give the same output: x=c+δx = c + \delta and x=c−δx = c - \delta both give y=δ2y = \delta^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 keep the dashed curve, 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. The tangent line to the curve y=g(x)y = g(x) at the point x=μx = \mu is the line through (μ,g(μ))(\mu, g(\mu)) whose slope is the derivative g′(μ)g'(\mu): y=g(μ)+g′(μ) (x−μ).y = g(\mu) + g'(\mu)\,(x - \mu) . Near μ\mu the curve and its tangent line are close, so the tangent line approximates the function: g(x)≈g(μ)+g′(μ) (x−μ).g(x) \approx g(\mu) + g'(\mu)\,(x - \mu). This approximation is the first-order Taylor expansion of gg around μ\mu. Its right-hand side is linear in xx. If the input is x∼N(μ,σ2)x \sim \mathcal{N}(\mu, \sigma^2), the output of the tangent line is Gaussian again: y∼N(g(μ),  g′(μ)2 σ2).y \sim \mathcal{N}\left(g(\mu),\; g'(\mu)^2\,\sigma^2\right). In y=g(μ)+g′(μ) (x−μ)y = g(\mu) + g'(\mu)\,(x - \mu) only xx is random, so the mean and variance follow directly. Since E[x]=μ\mathbb E[x] = \mu, E[y]=g(μ)+g′(μ) (E[x]−μ)=g(μ),Var⁡[y]=g′(μ)2Var⁡[x]=g′(μ)2 σ2.\mathbb E[y] = g(\mu) + g'(\mu)\,(\mathbb E[x] - \mu) = g(\mu), \qquad \operatorname{Var}[y] = g'(\mu)^2 \operatorname{Var}[x] = g'(\mu)^2\,\sigma^2 . The mean comes out as g(μ)g(\mu) because the expansion point is the mean of the belief. This is the mean of the tangent line’s output. The mean of g(x)g(x) itself is in general different, because E[g(x)]≠g(E[x])\mathbb E[g(x)] \neq g(\mathbb E[x]) for a nonlinear gg. Try the tangent line on the U-shaped function. Center the belief on the right arm of the U, at μ=6\mu = 6, with the same width σ=1\sigma = 1. The slope there is g′(6)=8g'(6) = 8, so the tangent line predicts y∼N(16,64)y \sim \mathcal{N}(16, 64). The code below compares that prediction with samples pushed through gg itself.
Output from cell 5 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 leaves out. Because the slope at μ=6\mu = 6 is large, the gap is a small fraction of the output variance. The linearized output has standard deviation ∣g′(μ)∣ σ|g'(\mu)|\,\sigma, so the spread of the belief grows when ∣g′(μ)∣>1|g'(\mu)| > 1 and shrinks when ∣g′(μ)∣<1|g'(\mu)| < 1. With a vector input x∈Rn\mathbf x \in \mathbb R^n and output y∈Rm\mathbf y \in \mathbb R^m, the slope becomes the m×nm \times n Jacobian matrix GG, and the variance g′(μ)2σ2g'(\mu)^2\sigma^2 becomes the m×mm \times m covariance G Σ G⊤G\,\Sigma\,G^\top, where Σ\Sigma is the n×nn \times n covariance of x\mathbf x. In the EKF the motion model maps the state to the next state, so m=nm = n and GG is square.
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 covariance follows the same way the variance did in one dimension. There the output differs from its mean by y−g(μ)=g′(μ) (x−μ)y - g(\mu) = g'(\mu)\,(x - \mu), and squaring and taking the expectation gives g′(μ)2σ2g'(\mu)^2\sigma^2. For y=G x\mathbf y = G\,\mathbf x with E[x]=μ\mathbb E[\mathbf x] = \boldsymbol\mu and covariance Σ\Sigma, the output differs from its mean by G(x−μ)G(\mathbf x - \boldsymbol\mu), and the outer product takes the place of the square: Cov⁡[y]=E[G(x−μ)(x−μ)⊤G⊤]=G Σ G⊤.\operatorname{Cov}[\mathbf y] = \mathbb E\left[G(\mathbf x - \boldsymbol\mu)(\mathbf x - \boldsymbol\mu)^\top G^\top\right] = G\,\Sigma\,G^\top .
The linear algebra review covers the matrix products, and the multivariate Gaussian section plots a bivariate Gaussian whose covariance couples the two variables. Its linear-Gaussian theorem gives the covariance of y=Ax+b\mathbf y = A\mathbf x + \mathbf b plus independent noise as A Λ−1A⊤+L−1A\,\Lambda^{-1}A^\top + L^{-1}. With A=GtA = G_t, Λ−1=Σt−1\Lambda^{-1} = \Sigma_{t-1} and the motion noise L−1=QtL^{-1} = Q_t, this is the EKF’s predicted covariance Gt Σt−1 Gt⊤+QtG_t\,\Sigma_{t-1}\,G_t^\top + Q_t.

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,Qt),s_t = g(a_t, s_{t-1}) + \varepsilon_t, \qquad \varepsilon_t \sim \mathcal{N}(0, Q_t), and zt=h(st)+δt,δt∼N(0,Rt).z_t = h(s_t) + \delta_t, \qquad \delta_t \sim \mathcal{N}(0, R_t). 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⊤+Qtpredict the covariance3:Kt=ΣˉtHt⊤(HtΣˉtHt⊤+Rt)−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 + Q_t & \text{predict the covariance} \\ 3: & K_t = \bar\Sigma_t H_t^\top \left(H_t \bar\Sigma_t H_t^\top + R_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} Here QtQ_t is the motion noise covariance and RtR_t is the measurement noise covariance, the convention of most Kalman filter code. Probabilistic Robotics swaps the two letters: its RtR_t is the motion noise and its QtQ_t the measurement noise. With nn the size of the state, kk the size of the action and mm the size of the measurement, the quantities in the algorithm have these sizes: The matrix inverted in line 3 is m×mm \times m, the size of the measurement, and the gain turns an mm-dimensional innovation into an nn-dimensional correction of the mean. For the robot of the EKF localization section, the state is the pose (x,y,θ)(x, y, \theta) with n=3n = 3, the action is the two wheel speeds with k=2k = 2, and a range and bearing to one landmark has m=2m = 2. 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.
The EKF does not replace the nonlinear models with linear ones. It propagates the mean through the nonlinear model itself: the predicted mean μˉt=g(at,μt−1)\bar\mu_t = g(a_t, \mu_{t-1}) is exactly where the motion model puts it, and the update compares ztz_t with the measurement h(μˉt)h(\bar\mu_t) that the measurement model predicts. Only the covariance, and through it the gain KtK_t, uses the linear approximations GtG_t and HtH_t.

When linearization fails

Move the same belief to the lowest point of the U, μ=c=2\mu = c = 2, where the slope is zero, and compare again.
Output from cell 8 At the lowest point the tangent line is flat. The slope is g′(c)=0g'(c) = 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 uses only the slope at one point. Here the slope is zero and carries no information about how quickly the function rises on either side. The first-order expansion leaves out the curvature of gg, and here the whole output comes from the curvature. The tangent line is accurate where the slope dominates the curvature across the belief. At μ=6\mu = 6 the slope term g′(6)2σ2=64g'(6)^2\sigma^2 = 64 is large next to the 2σ4=22\sigma^4 = 2 that the tangent line misses, so the linearized Gaussian is close to the truth. At μ=c=2\mu = c = 2 the slope term is zero, the tangent line misses the whole output, and the EKF reports a belief with zero variance about a quantity that is in fact uncertain.
Write x=μ+ex = \mu + 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(μ+e)=g(μ)+g′(μ) e+12 g′′(μ) e2,g′′(μ)=2.g(\mu + e) = g(\mu) + g'(\mu)\,e + \tfrac{1}{2}\,g''(\mu)\,e^2, \qquad g''(\mu) = 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(μ)+12 g′′(μ) σ2=g(μ)+σ2g(\mu) + \tfrac{1}{2}\,g''(\mu)\,\sigma^2 = g(\mu) + \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′(μ)2 σ2+14 g′′(μ)2⋅2σ4=g′(μ)2 σ2+2σ4.\operatorname{Var}(y) = g'(\mu)^2\,\sigma^2 + \tfrac{1}{4}\,g''(\mu)^2 \cdot 2\sigma^4 = g'(\mu)^2\,\sigma^2 + 2\sigma^4.The tangent line keeps only g(μ)g(\mu) and g′(μ)2σ2g'(\mu)^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 μ=c\mu = c the slope is zero, so the curvature terms are the whole output: mean σ2\sigma^2 and variance 2σ42\sigma^4. At μ=6\mu = 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.
Inside a filter this error is passed on to the next step. The EKF predicts the covariance as GtΣt−1Gt⊤+QtG_t \Sigma_{t-1} G_t^\top + Q_t, the vector form of g′(μ)2σ2g'(\mu)^2\sigma^2 plus the motion noise. If the motion model is flat at the current estimate, then Gt=0G_t = 0 and the predicted covariance is only the motion noise QtQ_t. The uncertainty in Σt−1\Sigma_{t-1} is dropped, and the filter is more confident than it should be. The Kalman gain grows with the predicted covariance, as in the one-dimensional gain Kt=σˉt2/(σˉt2+Rt)K_t = \bar\sigma_t^2 / (\bar\sigma_t^2 + R_t) of the Kalman filter, 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.

Flat motion and measurement models

A robot’s entire Jacobian is rarely zero. More often it is zero or nearly zero in particular state directions, or its slope is small compared with the model’s curvature across the belief. The table lists common cases. In each, the measurement is not directly sensitive to the flat direction. The filter can still reduce the uncertainty in that direction when the prior correlates it with directions in which the model’s slope is not zero. A GPS position fix, for example, updates the heading through the position-heading cross-covariance. Without such correlations, the covariance in the flat direction stays large or grows. The echosounder case occurs in practice in terrain navigation. The terrain navigation module of the Kongsberg HUGIN vehicles requires “a certain degree of terrain variation” (Ånonsen et al.), and the HUGIN 3000 survey data used in a 2022 study of bathymetric SLAM contains such a flat stretch (Torroba et al., 2022). The TurtleBot example at the end of this section shows the last row for a heading known only to 30°. Linearization works when the function is close to a straight line over the region where the belief has most of its mass. The EKF approximation is accurate when the belief uncertainty is narrow, or when the nonlinear function has no significant curvature or sharp change near the mean of the belief. The case of belief uncertainty is treated in Belief uncertainty below.

Belief uncertainty

Probabilistic Robotics (Thrun, Burgard and Fox, Section 3.3) names two factors that determine 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 μ=6\mu = 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 10 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. Samples on either side of x=2x = 2 then give the same values of yy, and the sampled output is skewed, with extra mass near y=0y = 0 and a long right tail. The linearized Gaussian cannot have this skew, because the tangent line is straight. 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. The EKF is therefore most accurate when the uncertainty of its estimate is small. When the filter underestimates its covariance, it also underestimates how much of the curvature its belief covers, so its covariance does not show the linearization error. The next section puts a TurtleBot at the bottom of the U: a straight drive with an uncertain heading, where the covariance after one EKF prediction step is far smaller than the spread of the robot’s real motion.
Found an error on this page? Report an issue.

Footnotes

  1. atan2⁡(y,x)\operatorname{atan2}(y, x) is the two-argument arctangent: the angle of the point (x,y)(x, y), measured counterclockwise from the positive xx axis, in (−π,π](-\pi, \pi]. It differs from arctan⁡(y/x)\arctan(y/x), which depends only on the ratio y/xy/x and returns angles in (−π/2,π/2)(-\pi/2, \pi/2). The points (1,1)(1, 1) and (−1,−1)(-1, -1) both give arctan⁡(1)=45°\arctan(1) = 45°, while atan2⁡\operatorname{atan2} gives 45°45° and −135°-135°. The two agree when x>0x > 0, and atan2⁡\operatorname{atan2} is also defined at x=0x = 0. ↩