Search

Search papers and lessons

Kalman Filter

Level 3 · 11 min read

A Kalman filter tracks a hidden state under linear motion and Gaussian noise by alternating a prediction that spreads uncertainty with an update that shrinks it using each new measurement.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Calculus
  • Control systems
  • Discrete math
  • Perception
  • Sign in to see your progress

TL;DR

A Kalman filter estimates a hidden state that moves by a linear rule and is seen only through noisy measurements. It keeps two things: a best guess of the state, which is a mean vector, and how uncertain that guess is, which is a covariance matrix. Each step it predicts forward, which makes the uncertainty grow, then corrects the guess with the new measurement, which makes the uncertainty shrink. When the noise is Gaussian, these two updates give the exact mean and covariance of the state's posterior.

Where you'll see it: in GPS and inertial navigation on phones, aircraft and spacecraft, in robot localization and object tracking, and in control loops that need an estimate of the state they cannot measure directly.

Intuition

Tracking a boat in fog

Suppose you are tracking a boat in fog. You know roughly where it was a moment ago and how fast it was moving. That gives you a prediction of where it is now, but the prediction is fuzzy because currents and wind push the boat in ways you did not model. Then a radar gives you a noisy fix on its position. You do not fully trust the prediction and you do not fully trust the radar. You blend them, leaning on whichever one is more reliable at that moment.

That blend is the whole filter. If the prediction is very uncertain, you lean on the radar. If the radar is very noisy, you lean on the prediction. The weight that controls this blend is called the Kalman gain, and the filter computes it for you from the two uncertainties.

Why the filter exists

A robot's sensors each give partial, noisy information. Wheel encoders measure how far the wheels turned, but slip ruins that. GPS gives position, but it is slow and jumpy. An inertial sensor gives acceleration, but errors pile up when you integrate them. If you trust only the latest reading, your estimate jitters. If you average the whole history, you respond too slowly and ignore the motion you already know about.

The Kalman filter sits between these extremes. It combines a model of how the state moves with each new measurement, and it stores only the last estimate and its uncertainty. It never needs the full history. Each cycle has the same two steps:

flowchart LR
  A[previous estimate and uncertainty] --> B[predict: move the state forward, uncertainty grows]
  B --> C[new measurement arrives]
  C --> D[update: blend prediction and measurement, uncertainty shrinks]
  D --> A

The picture also shows how uncertainty behaves over time. Without measurements it typically grows, because the motion model is imperfect. Each measurement cuts it down. The plot below shows this for a constant value measured repeatedly with noise variance 4, starting from a prior variance of 10.

01234560246810k, number of measurementsvariance
  • posterior variance after k measurements
Each measurement adds precision 1/4, so the variance falls like 1 over the total precision.

The math

Definition: Linear Gaussian state-space model

The hidden state xk\mathbf{x}_k evolves and is observed by xk=Axk−1+Buk+wk,wk∼N(0,Q),\mathbf{x}_k = \mathbf{A}\mathbf{x}_{k-1} + \mathbf{B}\mathbf{u}_k + \mathbf{w}_k, \qquad \mathbf{w}_k \sim \mathcal{N}(\mathbf{0}, \mathbf{Q}), zk=Hxk+vk,vk∼N(0,R).\mathbf{z}_k = \mathbf{H}\mathbf{x}_k + \mathbf{v}_k, \qquad \mathbf{v}_k \sim \mathcal{N}(\mathbf{0}, \mathbf{R}). Here xk\mathbf{x}_k is the state at step kk, A\mathbf{A} is the state transition matrix, uk\mathbf{u}_k is a known control input with matrix B\mathbf{B}, and wk\mathbf{w}_k is process noise with covariance Q\mathbf{Q}. The measurement is zk\mathbf{z}_k, read through the measurement matrix H\mathbf{H}, with measurement noise vk\mathbf{v}_k of covariance R\mathbf{R}.

The filter tracks a belief about xk\mathbf{x}_k given all measurements up to step kk. The belief is Gaussian, so it is fully described by a mean x^\hat{\mathbf{x}} and a covariance P\mathbf{P}. A superscript minus marks the prediction made before the measurement arrives. The standard forms of the mean and covariance appear in the linear dynamical system lesson, and the covariance rule below depends on the Covariance Matrix lesson.

The predict step

x^k−=Ax^k−1+Buk,Pk−=APk−1A⊤+Q.\hat{\mathbf{x}}_k^- = \mathbf{A}\hat{\mathbf{x}}_{k-1} + \mathbf{B}\mathbf{u}_k, \qquad \mathbf{P}_k^- = \mathbf{A}\mathbf{P}_{k-1}\mathbf{A}^\top + \mathbf{Q}.

The mean moves by the motion model. The covariance is pushed through the same linear map, which gives APA⊤\mathbf{A}\mathbf{P}\mathbf{A}^\top, and then the independent process noise adds Q\mathbf{Q}. The added Q\mathbf{Q} always increases uncertainty, which is the honest effect of an imperfect model.

The update step

Let the innovation be the part of the measurement the prediction did not expect:

yk=zk−Hx^k−,Sk=HPk−H⊤+R.\mathbf{y}_k = \mathbf{z}_k - \mathbf{H}\hat{\mathbf{x}}_k^-, \qquad \mathbf{S}_k = \mathbf{H}\mathbf{P}_k^-\mathbf{H}^\top + \mathbf{R}.

The Kalman gain, the weight on the innovation, and the corrected estimate are

Kk=Pk−H⊤Sk−1,x^k=x^k−+Kkyk,Pk=(I−KkH)Pk−.\mathbf{K}_k = \mathbf{P}_k^-\mathbf{H}^\top\mathbf{S}_k^{-1}, \qquad \hat{\mathbf{x}}_k = \hat{\mathbf{x}}_k^- + \mathbf{K}_k\mathbf{y}_k, \qquad \mathbf{P}_k = (\mathbf{I} - \mathbf{K}_k\mathbf{H})\mathbf{P}_k^-.

The update is Bayes' rule applied to two Gaussians: the prior from the prediction and the likelihood of the measurement. A product of two Gaussians is again Gaussian, so the posterior has a closed form.

Where the gain comes from, in one dimension

Take a scalar state with prior mean mm and variance pp, and a measurement zz with noise variance rr. The posterior density is proportional to a Gaussian in xx with variance pp times a Gaussian in xx with variance rr centred on zz. Their precisions add:

  1. Posterior precision: 1P=1p+1r\dfrac{1}{P} = \dfrac{1}{p} + \dfrac{1}{r}.
  2. Solve for PP: P=prp+r=(1−pp+r)pP = \dfrac{pr}{p + r} = \left(1 - \dfrac{p}{p + r}\right)p.
  3. Define K=pp+rK = \dfrac{p}{p + r}. Then P=(1−K)pP = (1 - K)p, which is the matrix formula with H=1\mathbf{H} = 1.
  4. The posterior mean is the precision-weighted average of mm and zz, which rearranges to m+K(z−m)m + K(z - m).

So the gain is the prior variance divided by the sum of the two variances. If the measurement is very precise, rr is small and KK is near 1, so the filter follows the sensor. If the prior is very precise, pp is small and KK is near 0, so the filter ignores the sensor.

Worked examples

Example
A constant value, one number at a time

A sensor reads a constant voltage xx. The model is A=1A = 1, B=0B = 0, H=1H = 1, Q=0Q = 0, because the voltage does not move. The measurement noise variance is R=4R = 4. The prior is mean 00 and variance 1010, a weak belief.

Measurement 1 arrives with z=5z = 5.

  1. Predict: nothing changes, so x^−=0\hat{x}^- = 0 and P−=10P^- = 10.
  2. Gain: K=1010+4=0.714K = \dfrac{10}{10 + 4} = 0.714.
  3. Mean: x^=0+0.714⋅(5−0)=3.571\hat{x} = 0 + 0.714 \cdot (5 - 0) = 3.571.
  4. Variance: P=(1−0.714)⋅10=2.857P = (1 - 0.714) \cdot 10 = 2.857.
  5. Check: 1P=110+14=0.35\dfrac{1}{P} = \dfrac{1}{10} + \dfrac{1}{4} = 0.35, so P=2.857P = 2.857.

Measurement 2 arrives with z=4.6z = 4.6.

  1. Predict: Q=0Q = 0, so P−=2.857P^- = 2.857 and x^−=3.571\hat{x}^- = 3.571.
  2. Gain: K=2.8572.857+4=0.417K = \dfrac{2.857}{2.857 + 4} = 0.417.
  3. Mean: x^=3.571+0.417⋅(4.6−3.571)=4.000\hat{x} = 3.571 + 0.417 \cdot (4.6 - 3.571) = 4.000.
  4. Variance: P=(1−0.417)⋅2.857=1.667P = (1 - 0.417) \cdot 2.857 = 1.667.
  5. Check: 1P=0.35+0.25=0.6\dfrac{1}{P} = 0.35 + 0.25 = 0.6, so P=1.667P = 1.667.

Interpretation: the estimate is 4.0, not the simple average of the readings, 4.8. The prior at 0 still has a small weight, because it is precision 0.10.1 against 0.250.25 for each reading. A precision-weighted average confirms it: (0⋅0.1+5⋅0.25+4.6⋅0.25)/0.6=4.0(0 \cdot 0.1 + 5 \cdot 0.25 + 4.6 \cdot 0.25) / 0.6 = 4.0. The variance fell from 10 to 1.67, a factor of six, after two readings.

Example
Position and velocity from a position sensor

Now the state is a cart with position and velocity, x=(p,v)\mathbf{x} = (p, v). Over one second with constant velocity, A=(1101)\mathbf{A} = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}. The sensor reads position only, so H=(1    0)\mathbf{H} = (1 \;\; 0), with R=1R = 1 and Q=0\mathbf{Q} = 0 for clarity. Start from x^=(0,1)\hat{\mathbf{x}} = (0, 1) with P=I\mathbf{P} = \mathbf{I}. The sensor reads z=3z = 3.

  1. Predicted mean: x^−=A(0,1)⊤=(1,1)⊤\hat{\mathbf{x}}^- = \mathbf{A}(0, 1)^\top = (1, 1)^\top.
  2. Predicted covariance: P−=AA⊤=(2111)\mathbf{P}^- = \mathbf{A}\mathbf{A}^\top = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix}.
  3. Innovation variance: S=HP−H⊤+R=2+1=3S = \mathbf{H}\mathbf{P}^-\mathbf{H}^\top + R = 2 + 1 = 3.
  4. Gain: K=P−H⊤/S=(2,1)⊤/3=(0.667,0.333)⊤\mathbf{K} = \mathbf{P}^-\mathbf{H}^\top / S = (2, 1)^\top / 3 = (0.667, 0.333)^\top.
  5. Innovation: y=3−1=2y = 3 - 1 = 2.
  6. Mean: x^=(1,1)⊤+(0.667,0.333)⊤⋅2=(2.333,1.667)⊤\hat{\mathbf{x}} = (1, 1)^\top + (0.667, 0.333)^\top \cdot 2 = (2.333, 1.667)^\top.
  7. Covariance: P=(I−KH)P−=(0.6670.3330.3330.667)\mathbf{P} = (\mathbf{I} - \mathbf{K}\mathbf{H})\mathbf{P}^- = \begin{pmatrix} 0.667 & 0.333 \\ 0.333 & 0.667 \end{pmatrix}.

Interpretation: the sensor measures position only, yet the velocity estimate also changed, from 1 to 1.67. That happens because the predicted covariance has an off-diagonal term of 1, so position and velocity are correlated. An error in position is partly an error in velocity, and the filter uses the correlation to spread the correction to both. Here the gain on velocity is 0.3330.333, half the gain on position. The new covariance is symmetric and smaller in every direction than the prediction.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Nonlinear motion or sensor, such as a turning robot or a range-bearing sensorThe linear model is wrong, and linearizing it introduces error. The extended Kalman filter can divergeUnscented Kalman filter, which propagates sample points through the nonlinearity, or a particle filter
Non-Gaussian noise, such as sensor outliers or a belief with several peaksA single Gaussian cannot represent the belief, and one bad reading can pull the estimate far offRobust updates that down-weight outliers, or a particle filter
Wrong Q\mathbf{Q} or R\mathbf{R}If Q\mathbf{Q} is too small or R\mathbf{R} too large, the filter becomes sluggish. If Q\mathbf{Q} is too large or R\mathbf{R} too small, it becomes overconfident and jumpyEstimate the noise levels from logged data, and check that the innovations have the variance S\mathbf{S} predicts
Large state vectorStoring and inverting P\mathbf{P} costs memory that grows with the square of the state size and time that grows with its cubeReduce the state, or use a sparse or factored form
Round-off over long runsP\mathbf{P} loses symmetry or positive definiteness, and the filter failsJoseph form, P=(I−KH)P−(I−KH)⊤+KRK⊤\mathbf{P} = (\mathbf{I} - \mathbf{K}\mathbf{H})\mathbf{P}^-(\mathbf{I} - \mathbf{K}\mathbf{H})^\top + \mathbf{K}\mathbf{R}\mathbf{K}^\top, which keeps P\mathbf{P} symmetric
A state that no sensor seesIts variance never shrinks, so the estimate stays uncertain. Heading with no compass or magnetometer is an exampleAdd a sensor that observes the state, or accept and report the uncertainty
Offline analysis, when you can wait for all the dataThe filter uses only past data, so early estimates are poorA smoother such as the Rauch–Tung–Striebel smoother, which uses future measurements too

History

Rudolf E. Kálmán published the linear filter in 1960, in a paper on linear filtering and prediction. With Richard Bucy he wrote the continuous-time version in 1961. The problem was navigation and tracking, where the earlier approach, the Wiener filter, was awkward to compute for multidimensional state.

Stanley Schmidt at NASA Ames adapted the idea to nonlinear trajectory estimation in the early 1960s. That adaptation became the basis of the extended Kalman filter, which was used in the navigation software for the Apollo program. The filter caught on because it is a fixed recursion over a small state: digital computers of the time could run it on each measurement, and it needed to store only the current estimate and its covariance. It is now used in phones, aircraft and robots, mostly without anyone naming it.

Check yourself

QuestionWhat two quantities does a Kalman filter store between steps, and what does each one describe?Show answer

It stores the mean x^\hat{\mathbf{x}}, the best estimate of the hidden state, and the covariance P\mathbf{P}, which describes how uncertain that estimate is and how the uncertainties of the state components are correlated. Together they describe the whole Gaussian belief, so nothing about older measurements needs to be kept.

QuestionA scalar prior has mean 2 and variance 4. A measurement arrives with z=6z = 6 and noise variance r=4r = 4. Find the gain, the posterior mean and the posterior variance.Show answer

The gain is K=44+4=0.5K = \dfrac{4}{4 + 4} = 0.5. The posterior mean is 2+0.5⋅(6−2)=42 + 0.5 \cdot (6 - 2) = 4. The posterior variance is (1−0.5)⋅4=2(1 - 0.5) \cdot 4 = 2. Check: 1P=14+14=12\dfrac{1}{P} = \dfrac{1}{4} + \dfrac{1}{4} = \dfrac{1}{2}, so P=2P = 2.

QuestionWhy does the predict step increase the covariance even when no measurement arrives?Show answer

The motion model is imperfect. The process noise Q\mathbf{Q} represents the part of the motion that the model does not capture, and it adds variance at every step. Pushing the old covariance through A\mathbf{A} can also spread it out. Without a measurement to cut it down, the uncertainty keeps growing, so the update step is the only place it shrinks.

QuestionA sensor is replaced by a much more precise one, so rr drops toward zero. What happens to the gain, and how does the estimate behave?Show answer

The gain K=p/(p+r)K = p / (p + r) approaches 1, so the posterior mean moves almost all the way to the measurement and the posterior variance approaches rr. The filter now trusts the sensor. The estimate follows each reading closely, so it is responsive, but it also passes through the sensor's noise. The filter has no way to smooth noise it is told to believe.

Sources