Kalman Filter, Live
Lesson 3 · The covariance matrix

The covariance matrix and its ellipse

Slide 1 found three numbers for a pair of variables: how much x spreads, how much y spreads, and how they move together. Now we store them in one place. Think of the distance table in a road atlas: every pair of cities gets one box, and the table reads the same from either side, because Paris to Rome is as far as Rome to Paris. The covariance matrix is that kind of table for uncertainty. The variances sit on its diagonal and the covariances fill the other boxes. Draw it and you get an ellipse, and that ellipse is what the Kalman filter carries around as its belief about the state (the numbers the filter tracks; slide 4 defines it).

Lesson 3 · slide 2Covariance matrix
P = σₓ²σₓᵧσₓᵧσᵧ²    ρ = σₓᵧ / (σₓ σᵧ)    −1 ≤ ρ ≤ 1
Key ideas
  1. The diagonal holds the variances: how much each variable spreads on its own.
  2. The off-diagonal holds the covariance: how the pair moves together.
  3. The matrix is symmetric, because cov(x, y) = cov(y, x).
  4. The correlation ρ = σₓᵧ / (σₓσᵧ) is the covariance on a fixed scale from −1 to 1.
  5. The 2σ ellipse is the picture of P: its width is set by the diagonal, its tilt by the off-diagonal.

Three numbers in one box

Track two numbers at once, for example a robot's x and y position. Three things describe the uncertainty: the variance of x, the variance of y, and their covariance. We put them in a matrix:

P = σₓ²σₓᵧσᵧₓσᵧ²

A note on writing: the pictures below print P on one line as [a b; c d]. The semicolon starts a new row, so [4 0; 0 1] has rows 4, 0 and 0, 1. In the same style, [p; v] is a column, p on top of v.

The diagonal (top left to bottom right) holds the variances. The off-diagonal holds the covariance. Since cov(x, y) and cov(y, x) are the same product of deviations, σₓᵧ = σᵧₓ and the matrix is symmetric: it equals its own transpose (rows and columns swapped).

The café from slide 1, temperature and ice cream:

var(temperature) = 20,   var(ice cream) = 162.5,   cov = 50
P = 205050162.5

Now stack the two numbers into one vector, x = (x, y). We write x ~ N(μ, P) to say: the vector x follows a Gaussian with mean vector μ and covariance matrix P. It is the 2D version of the bell curve N(μ, σ²) from lesson 1. The Kalman filter calls this matrix P, the same letter as the 1D variance.

Correlation: covariance on a fixed scale

The size of a covariance depends on units. Measure x in centimetres instead of metres and it grows 100 times. To compare links fairly, divide by the two standard deviations:

ρ = σₓᵧ / (σₓ σᵧ)

This is the correlation coefficient, written ρ (the Greek letter "rho"). It always lies between −1 and 1. At ρ = 1 the points sit on a rising line. At ρ = −1 they sit on a falling line. At ρ = 0 the points don't lean along any tilted line. Going back is easy: σₓᵧ = ρ σₓ σᵧ.

Café: σₓ = √20 ≈ 4.47,   σᵧ = √162.5 ≈ 12.75
ρ = 50 / (4.47 · 12.75) ≈ 0.88: a strong link that rises

That gives a quick validity test. If |σₓᵧ| is bigger than σₓσᵧ, the matrix claims |ρ| > 1, which is impossible. It is not a real covariance matrix. If you have met determinants (ad − bc for a 2 by 2 matrix), here is the same test in another form: σₓ²σᵧ² − σₓᵧ² is never negative for a real covariance matrix. You can skip this if determinants are new to you.

Café: |σₓᵧ| = 50 is less than σₓσᵧ ≈ 4.47 · 12.75 ≈ 57 ✓   (determinant: 20 · 162.5 − 50² = 750 ≥ 0)

The 2σ ellipse

In 1D we drew a band of ±2σ around the estimate. In 2D the band becomes an ellipse: the 2σ ellipse is the curve that fences in the most likely points, about 86% of them, the 2D version of "within 2σ of the mean". With no covariance, the ellipse reaches 2σₓ to the left and right of the mean and 2σᵧ above and below it, with its axes along x and y.

P with 4 and 1 on the diagonal and 0 off it: σₓ = 2, σᵧ = 1. The inner ring is 1σ, the dashed ring is 2σ: it reaches x = ±4 and y = ±2, with no tilt.

Covariance tilts it. Even when it tilts, its furthest left and right points are still 2σₓ from the mean, and its highest and lowest points are still 2σᵧ from it.

Now σₓ = 2, σᵧ = 1.4 (the lab's numbers) and a covariance of 1.68, so ρ = 1.68 / (2 · 1.4) = 0.6. The cloud leans up to the right: when x is high, y tends to be high.

About 86% of samples land inside the 2σ ellipse (no need to derive this number). Why fewer than the 95% inside the 1D band? A point must now be close in both directions at once. Being near in x is not enough if it is far off in y.

Read the ellipse like this. A wide ellipse means x is uncertain. A tall one means y is uncertain. A round one with no tilt means equal spread and no correlation. A tilt up to the right means positive covariance: if x is too big, y is probably too big as well. A tilt down to the right means negative covariance.

σₓ = 1, σᵧ = 2 and a covariance of −1 (ρ = −0.5). Taller than wide, and leaning down to the right.
Read P = 1−1−14:   σₓ = 1,   σᵧ = 2
ρ = −1 / (1 · 2) = −0.5  →  tilts down; 2σ reach: ±2 across, ±4 up and down

The lab under this slide draws the same picture, with three sliders under the picture for the entries of P: P11 = σₓ² (top left), P22 = σᵧ² (bottom right) and P12 = σₓᵧ (the off-diagonal).

Keep reading with the full course

Finish lesson 3 and you will be able to: track position and velocity in 2D with the matrix Kalman filter.

Still locked here:

  • The rest of this explanation
  • 3 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 and 2 stay free.

Examples

Example 1 · Build P from spreads and correlation

σₓ = 2, σᵧ = 1.4, ρ = 0.6 (the numbers in the lab).

Show solutionHide solution
Diagonal: σₓ² = 4,   σᵧ² = 1.96
Off-diagonal: σₓᵧ = 0.6 · 2 · 1.4 = 1.68
P = 41.681.681.96

Positive covariance: the ellipse is wider than tall and tilts up to the right.

Example 2 · Read a matrix

P = 9−6−616. What does it say?

Show solutionHide solution
σₓ = √9 = 3,   σᵧ = √16 = 4
ρ = −6 / (3 · 4) = −0.5

y spreads more than x, so the ellipse is taller than wide. The negative sign tilts it down to the right: when x is high, y tends to be low.

Exercise Make the ellipse tilt the other way. Set r = -0.6 and keep σx = 2 and σy = 1.4. Edit the code and press Run.
slide_2.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

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