Skip to the lesson
FixTheRobot Explore Robotics / Probability for Robotics / M4 Expectation, Variance and Covariance / M4-T2

Probability for Robotics · Theory · M4

M4-T2 — Covariance and Correlation

Variance describes one error. Covariance describes how two errors move together, and it is what turns a pose estimate from three numbers into a shape.

  • Lesson 2 of 5
  • Beginner
  • Module 4: Expectation, Variance and Covariance
  • Theory
  • About 16 minutes to read

A pose is not three separate uncertainties. After a diagonal drive an error in x usually comes with an error in y, and a small heading error shows up as a sideways displacement growing with distance. The variance of each coordinate cannot express that. Covariance can. This lesson explains what the sign and size of a covariance mean physically, assembles the covariance matrix, shows where uncorrelated does not mean independent, and derives how the evidence of correlated readings is discounted.

Learning objectives

After this lesson, you should be able to:

  • Compute the covariance and the correlation coefficient of two quantities, and say what their sign and size mean physically.
  • Write and read a covariance matrix, and compute the variance of a quantity along any direction from it.
  • Show that correlation is a unit-free number between −1 and 1, and give an example of uncorrelated but dependent variables.
  • Derive the number of independent readings that n equally correlated readings are worth.

The intuition

Rover drives 5 m along a diagonal path, 45° to the corridor walls. The wheels slip a little more or less in each run, so it stops short or long along the diagonal by about 5 cm, and the heading drifts so that it ends up to one side by about 1 cm. In the world frame the final error has an x part and a y part, and they are not independent. When Rover stops long, both x and y are too large; when it stops short, both are too small. The cloud of end points is stretched along the diagonal.

Four clouds of two-dimensional errors with different covarianceFour scatter plots of an error in x against an error in y. The first is a round cloud: correlation 0, the errors are unrelated. The second is a cloud stretched along the rising diagonal: correlation 0.8, when x is large y tends to be large. The third is stretched along the falling diagonal: correlation minus 0.8. The fourth is a thin ring of points: the correlation is also 0, but the points are clearly dependent, since x determines y up to its sign. ρ = 0independent ρ = +0.8rise together ρ = −0.8one up, the other down ρ = 0 againbut dependent (a ring)
Four clouds of x and y errors. Left to right: unrelated errors; errors that rise together (positive covariance); errors that move oppositely (negative covariance); and a ring of points whose correlation is zero even though x and y are plainly dependent.

That is what covariance measures. It is the average of the product of the two errors, each measured from its own mean:

  • If large x errors tend to come with large y errors, and small with small, then the product is positive more often than negative: positive covariance.
  • If a large x error tends to come with a small y error, the covariance is negative.
  • If they have no consistent relation, the positive and negative products cancel, and the covariance is zero.

Covariance has a unit (cm² here), so its size is hard to judge. Dividing by the two standard deviations gives the correlation coefficient ρ, a pure number between −1 and +1 that says how closely the cloud hugs a straight line: 0 for a round cloud, ±1 for points exactly on a line. In the example ρ = 0.92, nearly a line.

The covariance matrix

For two errors, the four numbers (variance of x, variance of y, and the covariance twice) are written as a 2 × 2 table, the covariance matrix. The diagonal holds the variances, the off-diagonal entries the covariance. For the diagonal drive it is [[13, 12], [12, 13]] cm². The large off-diagonal entry is the diagonal stretch. For three errors (x, y, heading) it is 3 × 3, and every filter in the second half of this course carries one.

Uncorrelated is not the same as independent

Independent variables always have zero covariance. The reverse fails, and the fourth panel shows why: if Rover circles a pillar, the position (cos θ, sin θ) is spread over a ring. The positive and negative products cancel by symmetry, so ρ = 0, yet knowing x tells you y up to a sign. Correlation detects linear relationships only.

Covariance describes the tilt and stretch of an error cloud. A round cloud means no relationship; a thin diagonal cloud means that knowing one error nearly tells you the other.

Definitions

Covariance Cov(X,Y)\mathrm{Cov}(X, Y) or σXY\sigma_{XY}
The expected product of the deviations from the means. Unit: unit of XX times unit of YY.
Correlation ρ\rho or ρXY\rho_{XY}
The covariance divided by the product of the standard deviations. No unit, between −1 and +1.
Covariance matrix Σ\Sigma
The matrix of all variances (on the diagonal) and covariances (off the diagonal) of a vector of random variables. It is symmetric, and positive semi-definite: no direction has a negative variance.
Uncorrelated
Cov(X,Y)=0\mathrm{Cov}(X,Y) = 0. Weaker than independent.
Covariance
Cov(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY\mathrm{Cov}(X, Y) = \mathbb{E}\bigl[(X - \mu_X)(Y - \mu_Y)\bigr] = \mathbb{E}[XY] - \mu_X\mu_Y
The second form is the analogue of E[X²] − μ² for variance.
Correlation coefficient
ρ=Cov(X,Y)σX σY,−1≤ρ≤1\rho = \frac{\mathrm{Cov}(X, Y)}{\sigma_X\,\sigma_Y}, \qquad -1 \le \rho \le 1
ρ = ±1 exactly when Y is a linear function of X.
Covariance matrix of two variables
Σ=(σx2σxyσxyσy2)=(σx2ρ σxσyρ σxσyσy2)\Sigma = \begin{pmatrix} \sigma_x^2 & \sigma_{xy} \\ \sigma_{xy} & \sigma_y^2 \end{pmatrix} = \begin{pmatrix} \sigma_x^2 & \rho\,\sigma_x\sigma_y \\ \rho\,\sigma_x\sigma_y & \sigma_y^2 \end{pmatrix}
For a vector of any length, entry (i, j) is Cov(Xᵢ, Xⱼ).
Variance of a combination, or along a direction
Var(aX+bY)=a2σx2+2ab σxy+b2σy2=(ab)Σ(ab)\mathrm{Var}(a X + b Y) = a^2\sigma_x^2 + 2ab\,\sigma_{xy} + b^2\sigma_y^2 = \begin{pmatrix} a & b \end{pmatrix} \Sigma \begin{pmatrix} a \\ b \end{pmatrix}
For a unit vector u = (a, b), uᵀΣu is the variance of the error measured along u: the width of the cloud in that direction.

Derivation: bounds, directions and the value of correlated readings

Why the correlation is between −1 and 1

Standardize both quantities: U=(X−μX)/σXU = (X - \mu_X)/\sigma_X and V=(Y−μY)/σYV = (Y - \mu_Y)/\sigma_Y each have mean 0 and variance 1, and Cov(U,V)=ρ\mathrm{Cov}(U, V) = \rho.

  1. Var(U−sV)=Var(U)+s2 Var(V)−2s Cov(U,V)=2−2sρ\mathrm{Var}(U - sV) = \mathrm{Var}(U) + s^2\,\mathrm{Var}(V) - 2s\,\mathrm{Cov}(U, V) = 2 - 2s\rho

    Use the formula for the variance of a sum from the last lesson, with the covariance term kept. s=±1s = \pm 1.

  2. 2−2sρ≥0for s=+1 and s=−1    ⇒    −1≤ρ≤12 - 2s\rho \ge 0 \quad\text{for } s = +1 \text{ and } s = -1 \;\;\Rightarrow\;\; -1 \le \rho \le 1

    A variance cannot be negative.

  3. ρ=±1  ⇔  Var(U∓V)=0  ⇔  U=±V with probability 1\rho = \pm 1 \;\Leftrightarrow\; \mathrm{Var}(U \mp V) = 0 \;\Leftrightarrow\; U = \pm V \text{ with probability 1}

    A variance of zero means the quantity never varies: YY is an exact straight-line function of XX.

Variance along a direction

  1. Var(aX+bY)=E[(a(X−μX)+b(Y−μY))2]\mathrm{Var}(aX + bY) = \mathbb{E}\bigl[(a(X - \mu_X) + b(Y - \mu_Y))^2\bigr]

    Definition, with the centred variables.

  2. =a2 E[(X−μX)2]+2ab E[(X−μX)(Y−μY)]+b2 E[(Y−μY)2]= a^2\,\mathbb{E}[(X-\mu_X)^2] + 2ab\,\mathbb{E}[(X-\mu_X)(Y-\mu_Y)] + b^2\,\mathbb{E}[(Y-\mu_Y)^2]

    Expand the square and use linearity of expectation.

  3. =a2σx2+2ab σxy+b2σy2=u⊤Σ u= a^2\sigma_x^2 + 2ab\,\sigma_{xy} + b^2\sigma_y^2 = \mathbf{u}^{\top}\Sigma\,\mathbf{u}

    Recognise a quadratic form with u=(a,b)⊤\mathbf{u} = (a, b)^{\top}. For a unit vector it is the variance of the error along that direction.

How many independent readings are n correlated readings worth?

Let nn readings each have variance σ2\sigma^2 and every pair have correlation ρ\rho. Their average has:

  1. Var(Xˉ)=1n2[∑iVar(Xi)+∑i≠jCov(Xi,Xj)]\mathrm{Var}(\bar{X}) = \dfrac{1}{n^2}\Bigl[\sum_{i}\mathrm{Var}(X_i) + \sum_{i \ne j}\mathrm{Cov}(X_i, X_j)\Bigr]

    The variance of a sum is the sum of all entries of the covariance matrix, and Xˉ=1n∑Xi\bar{X} = \tfrac{1}{n}\sum X_i scales it by 1/n21/n^2.

  2. =1n2[nσ2+n(n−1) ρ σ2]=σ2n(1+(n−1)ρ)= \dfrac{1}{n^2}\Bigl[n\sigma^2 + n(n-1)\,\rho\,\sigma^2\Bigr] = \dfrac{\sigma^2}{n}\bigl(1 + (n-1)\rho\bigr)

    There are nn variances and n(n−1)n(n-1) ordered pairs, each with covariance ρσ2\rho\sigma^2.

  3. σ2neff=Var(Xˉ)    ⇒    neff=n1+(n−1)ρ\dfrac{\sigma^2}{n_{\text{eff}}} = \mathrm{Var}(\bar{X}) \;\;\Rightarrow\;\; n_{\text{eff}} = \dfrac{n}{1 + (n-1)\rho}

    Define the equivalent number of independent readings as the number that would give the same variance of the average. For ρ=0\rho = 0 it is nn; for ρ=1\rho = 1 it is 1; and as n→∞n \to \infty it approaches 1/ρ1/\rho however many readings you take.

Correlation limits what averaging can achieve. Equally correlated readings never improve beyond σ²ρ, however many are taken: the correlated part of the error is the same in every reading and does not average away.

A worked example

Rover drives along a 45° diagonal. The error along the path has standard deviation 5 cm, the error across it 1 cm, and the two are independent. Rotating into the world frame gives the x and y errors.

  1. Covariance matrix by calculation. The variances along and across are σa2=25\sigma_a^2 = 25 and σc2=1\sigma_c^2 = 1 cm². With cos⁡45°=sin⁡45°=1/2\cos 45° = \sin 45° = 1/\sqrt{2}: σx2=σy2=(25+1)/2=13\sigma_x^2 = \sigma_y^2 = (25 + 1)/2 = 13 and σxy=(25−1)/2=12\sigma_{xy} = (25 - 1)/2 = 12. So Σ=(13121213)\Sigma = \begin{pmatrix} 13 & 12 \\ 12 & 13 \end{pmatrix} cm² and ρ=12/13=0.923\rho = 12/13 = 0.923.
  2. By simulation. 20 000 simulated end points give [[13.10, 12.09], [12.09, 13.10]] and a correlation of 0.923.
  3. Variance along a direction. Along the path u=(0.707,0.707)\mathbf{u} = (0.707, 0.707): u⊤Σu=0.5(13)+2(0.5)(12)+0.5(13)=25\mathbf{u}^{\top}\Sigma\mathbf{u} = 0.5(13) + 2(0.5)(12) + 0.5(13) = 25 cm², a standard deviation of 5 cm. Across the path: 1 cm². Looking only at the x variance (13 cm², standard deviation 3.6 cm) would hide both.
  4. A ring. If the end point is spread uniformly around a pillar, (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta), the simulated correlation is −0.003: zero, within sampling error, although x2+y2=1x^2 + y^2 = 1 always.
  5. Correlated readings. Five readings with pairwise correlation 0.8: the variance of their average is 15(1+4×0.8)=0.84\tfrac{1}{5}(1 + 4 \times 0.8) = 0.84 (simulation 0.827), the same as 5/(1+3.2)=1.195/(1 + 3.2) = 1.19 independent readings. The five readings are worth little more than one.
import math
from robotprob import Rng, cov_matrix, correlation, variance

rng = Rng(3)
c, s = math.cos(math.pi / 4), math.sin(math.pi / 4)
pts = []
for _ in range(20000):
    a = rng.gauss(0.0, 5.0)                      # error along the direction of travel (cm)
    b = rng.gauss(0.0, 1.0)                      # error across it (cm)
    pts.append([c * a - s * b, s * a + c * b])   # rotated into the world frame
S = cov_matrix(pts)
xs = [p[0] for p in pts]
ys = [p[1] for p in pts]
print('covariance matrix  =', [[round(v, 2) for v in row] for row in S], ' theory [[13, 12], [12, 13]]')
print('correlation        =', round(correlation(xs, ys), 3), ' theory', round(12 / 13, 3))

def var_dir(S, u):                               # u^T S u: variance along the unit vector u
    return u[0] * u[0] * S[0][0] + 2 * u[0] * u[1] * S[0][1] + u[1] * u[1] * S[1][1]
print('variance along, across =', round(var_dir(S, [c, s]), 2), round(var_dir(S, [-s, c]), 2), ' theory 25, 1')

ring = []                                        # uncorrelated, but dependent
for _ in range(20000):
    t = rng.uniform(0.0, 2 * math.pi)
    ring.append([math.cos(t), math.sin(t)])
print('ring: correlation', round(correlation([p[0] for p in ring], [p[1] for p in ring]), 3), ' but x^2 + y^2 is always', round(ring[0][0] ** 2 + ring[0][1] ** 2, 3))

n, rho = 5, 0.8                                  # equally correlated readings
means = []
for _ in range(20000):
    common = rng.gauss(0.0, 1.0)                 # the part of the error shared by all five readings
    xs5 = [math.sqrt(rho) * common + math.sqrt(1 - rho) * rng.gauss(0.0, 1.0) for _ in range(n)]
    means.append(sum(xs5) / n)
print('variance of the mean of 5 readings with rho = 0.8:', round(variance(means), 3), ' formula', round((1 + (n - 1) * rho) / n, 3), ' equivalent independent readings', round(n / (1 + (n - 1) * rho), 2))
covariance matrix  = [[13.1, 12.09], [12.09, 13.1]]  theory [[13, 12], [12, 13]]
correlation        = 0.923  theory 0.923
variance along, across = 25.2 1.01  theory 25, 1
ring: correlation -0.003  but x^2 + y^2 is always 1.0
variance of the mean of 5 readings with rho = 0.8: 0.827  formula 0.84  equivalent independent readings 1.19
cov_matrix takes one sample per row. The shared “common” term is how correlated noise is generated: a part every reading has in common plus a private part.

In robotics

Pose uncertainty is a matrix

A robot's position estimate is reported with a covariance matrix, not a radius. A long, thin cloud means the robot knows its position well in one direction and poorly in the other: in a long corridor, lidar scan matching pins the position across the corridor and says little along it. A planner that uses only the larger of the two standard deviations is too cautious across the corridor and one that uses the smaller is reckless along it. ROS 2 pose messages carry this matrix (the 6 × 6 covariance field of geometry_msgs/PoseWithCovariance), and the Navigation course shows how to turn it into a safety margin along the direction to the nearest wall, exactly the formula u⊤Σu\mathbf{u}^{\top}\Sigma\mathbf{u} of this lesson, in Navigation Under Pose Uncertainty. The SLAM view is in Understand Pose Uncertainty.

Heading error produces correlated position errors

A small heading error δθ\delta\theta displaces the position sideways by about d δθd\,\delta\theta after a distance dd. Because the same heading error enters the position error of every later step, the position errors at different times are correlated, and the pose covariance develops off-diagonal terms between heading and position. A filter that ignored them (a diagonal covariance) would be overconfident about position after any turn. M5-T2: Odometry and Velocity Motion Models derives this for the odometry model.

Why a Kalman filter needs off-diagonal terms

A measurement of x alone improves the estimate of y when their errors are correlated, and by how much is exactly the correlation (module 4, M4-T5: Conditioning and Fusing Gaussians, and module 7). This is how a filter that only measures position can estimate velocity, and a camera that sees one landmark can correct a heading.

Correlated noise in sensors

Successive readings from an IMU, GNSS receiver or camera frame share slowly varying errors (bias, multipath, lighting), so their errors are correlated in time. The equivalent number of independent readings, n/(1+(n−1)ρ)n/(1 + (n-1)\rho), is the right number to use when averaging them, and the reason why 100 Hz data does not make a 100 times better estimate. The UAV course discusses this for inertial sensors in IMU noise and bias modelling.

Reading a covariance from data

To measure the correlation of two errors, collect repeated trials and compute the sample covariance matrix. Check it is not nearly singular (a correlation of 0.99999 is a sign that two of your states are the same quantity under two names), and remember that the sample covariance is itself uncertain when there are few samples.

Common misconceptions

“Correlation of zero means the variables are independent.”

Independent implies uncorrelated, not the other way round. A position on a ring (cos θ, sin θ) has correlation 0, and x determines y up to its sign. Correlation sees only linear relationships.

“A strong correlation means that one quantity causes the other.”

It only says they move together. Two sensors mounted on the same vibrating mast have correlated errors because of the mast, not because one sensor affects the other. In estimation, the practical use is prediction, not blame: correlation lets you infer one error from the other.

“The covariance matrix is just a list of standard deviations.”

The diagonal holds variances (squares of standard deviations), and the off-diagonal terms carry information the diagonal cannot: the orientation of the error cloud. Discarding them turns a thin diagonal cloud into a round one that is far too large in one direction and too small in the other.

“More readings per second always gives a better average.”

Only for independent errors. For equally correlated readings the average cannot be better than σ²ρ however many are taken; 1000 readings at ρ = 0.8 are worth 1.25 independent ones.

Key takeaways

  1. Covariance is the average product of two deviations from their means: positive when the errors rise together, negative when they oppose, zero when they have no linear relation.
  2. The correlation coefficient ρ = σxy/(σxσy) is unit-free, between −1 and +1, and is ±1 exactly for a straight-line relationship.
  3. The covariance matrix collects the variances (diagonal) and covariances (off diagonal). The variance along a unit vector u is uᵀΣu.
  4. Independent implies uncorrelated; uncorrelated does not imply independent (a ring has ρ = 0).
  5. n readings with pairwise correlation ρ are worth n/(1 + (n − 1)ρ) independent readings, and no more than about 1/ρ however large n is.

References and further reading

Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.

Knowledge check

Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A robot’s position covariance in the world frame is [[9, 0], [0, 1]] cm² after driving along the x axis, and [[1, 0], [0, 9]] cm² after driving along y. After driving along a 45° diagonal, what would you expect the off-diagonal entry to be, and what are the variances along and across the path?

Explanation

Driving along 45° with a 9 cm² error along the path and 1 cm² across it gives variances of (9 + 1)/2 = 5 cm² in both x and y and a covariance of (9 − 1)/2 = 4 cm². So the matrix is [[5, 4], [4, 5]] with correlation 0.8. The variance along the path (u = (0.707, 0.707)) is 0.5 × 5 + 2 × 0.5 × 4 + 0.5 × 5 = 9 cm², and across it is 1 cm², as built in. The rotation does not change the shape of the error cloud, only how it appears in x and y; the off-diagonal term is the tilt.

Question 2

Two sensors have errors with standard deviations of 3 cm and 4 cm. Their errors are positively correlated with ρ = 0.6. What is the standard deviation of the error of their sum, and of their difference? Compare with ρ = 0.

Explanation

Var(X + Y) = 9 + 16 + 2 × 0.6 × 3 × 4 = 39.4 cm², so a standard deviation of 6.28 cm. Var(X − Y) = 9 + 16 − 14.4 = 10.6 cm², so 3.26 cm. For ρ = 0 both would be 5 cm. Positive correlation makes the sum worse and the difference better: if the two sensors share an error, subtracting one from the other cancels most of it. That is why differential measurements (two antennas, two sensors) can be so much more accurate than either alone.

Question 3

A teammate computes the correlation between the x and y coordinates of a robot that circles a point at radius 1 m, finds it is about 0, and concludes that the two coordinates are unrelated. Why is this wrong, and what would be a better check?

Explanation

On the circle x = cos θ and y = sin θ, the correlation is zero by symmetry, and yet y = ±√(1 − x²), so the two are strongly dependent. Correlation only measures linear association, and the symmetric positive and negative contributions cancel. A better check is to plot the data, or to compute a measure that detects nonlinear dependence (for instance the correlation of x² with y², which here is perfectly −1), or to test whether knowing one improves prediction of the other with a nonlinear model.

What’s next

With the covariance matrix we can describe a cloud of errors in any number of dimensions. The next lesson puts it inside the Gaussian density, and turns the matrix into the ellipse that robotics engineers draw.

Practice this lesson

Robot Uncertainty Visualizer

The position estimate comes with a covariance that nobody on the team can read. Compute it from samples, draw its ellipse, and push it through a rotation and a range-bearing conversion.

Next lesson

M4-T3 — The Multivariate Gaussian and Covariance Ellipses

Write the multivariate Gaussian density, turn a covariance matrix into an ellipse with its eigenvectors, and use the Mahalanobis distance.

Nothing below is required to finish this lesson.

  • Before
    M4-T1: Expectation and Variance

    Compute the expectation and variance of discrete and continuous variables, use linearity of expectation, and relate the sample mean to the true mean.

  • Lab
    M4-L1: Robot Uncertainty Visualizer

    The position estimate comes with a covariance that nobody on the team can read. Compute it from samples, draw its ellipse, and push it through a rotation and a range-bearing conversion.

Other courses on this site treat neighbouring topics from their own angle: