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.
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.
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 or
- The expected product of the deviations from the means. Unit: unit of times unit of .
- Correlation or
- The covariance divided by the product of the standard deviations. No unit, between −1 and +1.
- Covariance matrix
- 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
- . Weaker than independent.
Derivation: bounds, directions and the value of correlated readings
Why the correlation is between −1 and 1
Standardize both quantities: and each have mean 0 and variance 1, and .
Use the formula for the variance of a sum from the last lesson, with the covariance term kept. .
A variance cannot be negative.
A variance of zero means the quantity never varies: is an exact straight-line function of .
Variance along a direction
Definition, with the centred variables.
Expand the square and use linearity of expectation.
Recognise a quadratic form with . For a unit vector it is the variance of the error along that direction.
How many independent readings are n correlated readings worth?
Let readings each have variance and every pair have correlation . Their average has:
The variance of a sum is the sum of all entries of the covariance matrix, and scales it by .
There are variances and ordered pairs, each with covariance .
Define the equivalent number of independent readings as the number that would give the same variance of the average. For it is ; for it is 1; and as it approaches 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.
- Covariance matrix by calculation. The variances along and across are and cm². With : and . So cm² and .
- By simulation. 20 000 simulated end points give [[13.10, 12.09], [12.09, 13.10]] and a correlation of 0.923.
- Variance along a direction. Along the path : 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.
- A ring. If the end point is spread uniformly around a pillar, , the simulated correlation is −0.003: zero, within sampling error, although always.
- Correlated readings. Five readings with pairwise correlation 0.8: the variance of their average is (simulation 0.827), the same as 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 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 displaces the position sideways by about after a distance . 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, , 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
- 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.
- The correlation coefficient ρ = σxy/(σxσy) is unit-free, between −1 and +1, and is ±1 exactly for a straight-line relationship.
- The covariance matrix collects the variances (diagonal) and covariances (off diagonal). The variance along a unit vector u is uᵀΣu.
- Independent implies uncorrelated; uncorrelated does not imply independent (a ring has ρ = 0).
- 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.
- J. Tsitsiklis and P. Jaillet, MIT RES.6-012 Introduction to Probability , lecture 12 (sum of independent random variables, covariance and correlation). Free videos and notes, licence CC BY-NC-SA 4.0.
- T. D. Barfoot, State Estimation for Robotics, 2nd edition, Cambridge University Press, 2024 , chapter “Primer on Probability Theory” (covariance and the multivariate Gaussian). Free to read as the author’s PDF; the book is copyrighted.
- J. Blitzstein and J. Hwang, Introduction to Probability, 2nd edition, CRC Press, 2019 , chapter 7 (covariance and correlation). Free to read online; the book is copyrighted.
- ROS 2 geometry_msgs/PoseWithCovariance message definition , which fixes the order of the six covariance rows (x, y, z and rotations about them). Free documentation.
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.
Related content
Nothing below is required to finish this lesson.
- BeforeM4-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.
- LabM4-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:
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.