Kalman Filter, Live
Lesson 2 · Predict

The predict step: move the estimate, grow the variance

Slide 2 ended with two flawed sensors: the encoder is smooth but drifts, and the GPS is centred but jumpy. The Kalman filter uses both, in a loop of two steps. The first is the predict step, and it uses the encoder. Picture standing at the kitchen door, fairly sure where you are, and taking one big step with your eyes closed. You are now about one step further on, and a little less sure than before. The predict step does exactly that to the filter's bell curve: it slides it forward by the encoder's move and makes it a bit wider.

Lesson 2 · slide 3Predict
x̂⁻ = x̂ + u    P⁻ = P + Q    (u = the move the encoder reports)
Key ideas
  1. The prediction x̂⁻ (say "x-hat minus") is our estimate after the move, before the GPS reading. P⁻ is its variance.
  2. The mean moves by u, the move the encoder reports (textbooks call u the control input).
  3. The variance grows by the process noise Q. Predicting never makes you more certain.
  4. Variances add, not standard deviations: σ⁻ = √(P + Q).

One step in the dark

You know you are at the kitchen door, give or take a little. You close your eyes and take one big step forward. Where are you now? About one step further on. How sure are you? A bit less than before, because the step could have been long or short. That is the whole predict step.

The new words

The filter keeps two numbers about the robot: the estimate x̂ and its variance P. Together they describe a bell curve, N(x̂, P): "the robot is probably near x̂, give or take √P". For example, x̂ = 30 m with P = 4 says "near 30 m, give or take 2 m", and by the 68-95 rule we are about 95% sure it is between 26 and 34 m.

The prediction is written with a small minus: x̂⁻ and P⁻. Read it as "before the measurement". It is where we think the robot is after it moves, before we look at the GPS.

From now on we drop the [n]: x̂ is the estimate from the last step, and x̂⁻ is the new prediction. In the same way, u is the u[n] from slide 1, the move the encoder reports for this step. Textbooks call u the control input, because many robots move by commands ("drive 5 m"). Here it is simply the move the encoder measured. When the encoder measures speed instead, u = speed · Δt, where Δt ("delta t") is the time between steps: a measured speed of 2 m/s with Δt = 0.5 s gives u = 1 m. (In code, Δt is written dt.) The process noise Q is the variance of that move's error. It covers encoder noise, wheel slip and bumps in the floor.

The two equations

x̂⁻ = x̂ + u    slide the bell forward
P⁻ = P + Q    make the bell wider

The first line moves the mean. The second adds the new doubt to the old doubt. P⁻ is never smaller than P, because Q is never negative. This is the only place the filter's uncertainty grows. With x̂ = 30 m, P = 4, u = 5 m and Q = 1:

x̂⁻ = 30 + 5 = 35 m    P⁻ = 4 + 1 = 5  →  σ⁻ = √5 ≈ 2.24 m
The dots are places the robot might be now. After the move, the bell sits 5 m further right and is a little lower and wider: σ goes from 2 m to about 2.24 m.

Why variances add

The old error and the new step's error are independent. When independent errors add, their variances add. Their σ values do not. With an old σ of 3 m and a step error of σ = 4 m:

P⁻ = 3² + 4² = 9 + 16 = 25  →  σ⁻ = 5 m, not 3 + 4 = 7 m

The two errors sometimes point in opposite directions and partly cancel, so the total is less than the plain sum.

Three error bells, all centred on 0: the old error (σ = 3 m), the step error (σ = 4 m) and their total. The total is wider than each part, but only σ = 5 m, not 7 m.

Predict, predict, predict

If the GPS goes silent, the filter keeps predicting. Each step adds another Q, exactly like dead reckoning on slide 1. Starting from 30 m with P = 4, with u = 5 m and Q = 1 each step:

after 1 step: 35 m, P = 5    after 4 steps: 50 m, P = 8    after 8 steps: 70 m, P = 12
Eight predict steps in a tunnel with no GPS. The estimate moves 5 m per step, and its ±2σ band keeps widening. Only a measurement can make it narrow again.

A bigger example: a robot on a floor

A floor robot moves east and north. For now, treat the two directions as two separate 1D filters, each with its own x̂, P, u and Q. The robot is at 30 m east (P = 4) and 12 m north (P = 2). Each step the encoder says it moved 5 m east (Q = 1) and 2 m south, so −2 m north (Q = 0.5).

step 1: east 35 m, P = 5 (σ ≈ 2.24 m)    north 10 m, P = 2.5 (σ ≈ 1.58 m)
step 2: east 40 m, P = 6 (σ ≈ 2.45 m)    north 8 m, P = 3 (σ ≈ 1.73 m)
step 3: east 45 m, P = 7 (σ ≈ 2.65 m)    north 6 m, P = 3.5 (σ ≈ 1.87 m)

Each direction grows its own doubt, by its own Q. A drone does the same for east, north and up, three numbers at once. In real robots the directions are often linked (a wheel slip spoils east and north together). Lesson 3 shows how to handle that.

Where this shows up

A car's navigation keeps your arrow moving in a tunnel by predicting from wheel speed. A drone predicts its next position from its motor commands and its gyro (a sensor that measures how fast it turns) many times a second, between slower GPS readings (called fixes). In both, the predict step is what fills the gaps.

Next: a GPS reading arrives. How far should the filter move toward it, and how much does its doubt shrink? That is the correct step.

Examples

Example 1 · One predict step

The last estimate is x̂ = 30 m with P = 4 (σ = 2 m). The encoder says the robot moved u = 5 m this step, with Q = 1.

Show solutionHide solution
x̂⁻ = 30 + 5 = 35 m
P⁻ = 4 + 1 = 5  →  σ⁻ = √5 ≈ 2.24 m

The bell moved 5 m to the right and got a bit wider. We use this prediction again on the next slide.

Example 2 · Three steps in a tunnel

Start from the prediction above, 35 m with P = 5. No GPS for two more steps, same u = 5 and Q = 1.

Show solutionHide solution
step 2: x̂⁻ = 40, P⁻ = 6, σ⁻ ≈ 2.45 m
step 3: x̂⁻ = 45, P⁻ = 7, σ⁻ ≈ 2.65 m

P climbs by the same 1 every step. σ climbs a little less each time, because of the square root.

Example 3 · A faster loop

A car drives at 20 m/s, and its filter runs every Δt = 0.1 s. Each step adds Q = 0.01 m².

Show solutionHide solution
u = 20 · 0.1 = 2 m per step
one second = 10 steps: P grows by 10 · 0.01 = 0.1 m²

A note for later: Q is "per step". If you run the filter twice as often (halve Δt), roughly halve Q too, so one second still adds about 0.1 m².

Example 4

Start with P = 1 and Q = 0.5. How many predict steps, with no GPS, until σ has doubled?

Show solutionHide solution

σ starts at 1, so doubled means σ = 2 and P = 4.

P = 1 + 0.5 · n = 4 gives n = 6 steps.

Example 5

P = 36 (σ = 6 m) and Q = 64 (a step error of σ = 8 m). What is σ⁻ after one predict step? Why is it not 14 m?

Show solutionHide solution

P⁻ = 36 + 64 = 100, so σ⁻ = 10 m.

Variances add, σ values do not. 6 + 8 = 14 would only be right if the two errors always pushed the same way. Independent errors partly cancel.

Exercise The GPS drops out in a tunnel. Set steps = 5 and predict five steps in a row. Where is the robot, and how wide is the bell now? Edit the code and press Run.
slide_3.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Enjoying lesson 2? Get the full course: all 4 lessons through the matrix filter and the EKF, your progress saved lesson by lesson, every solution and 5 extra problems per lesson. $20 once, yours for life.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From noisy sensors to the EKF, by running it

Lessons 1 and 2 are free. Finish all four and you will be able to:

You get: all 4 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · Steppe, your AI tutor inside every lesson, day or night · every future update.

$20 once · lifetime access · 14-day money-back guarantee
Continue to checkout →

Already a member? Log in to the course