Linear Algebra · Theory · M9
M9-T2 — Covariance Matrices
One matrix holds every variance and every pairwise relation of an uncertain vector. Its eigen-decomposition is the uncertainty ellipse.
A robot pose has several uncertain components, and their errors are rarely independent: if the heading estimate is off to the left, the position estimate tends to be off in a related way. The covariance matrix records this as a symmetric matrix, and everything you learned about symmetric and positive definite matrices in Module 6 applies directly.
Learning objectives
After this lesson, you should be able to:
- Compute the sample covariance matrix of data and read variances, covariances and the correlation coefficient.
- Explain why a covariance matrix is symmetric and positive semi-definite and how to test a candidate.
- Relate the eigenvectors and eigenvalues of Σ to the axes and lengths of the uncertainty ellipse.
- Interpret the covariance matrix of a robot pose, in particular the meaning of off-diagonal terms.
Introduction and motivation
A SLAM system outputs a pose estimate and a 3 × 3 covariance. A planner reads it to decide how close it may pass to an obstacle: the ellipse is long along the corridor and thin across it, so passing sideways is safe but a head-on stop is not. A filter reads it to decide how much to trust a new LiDAR measurement. Both are reading a covariance matrix; both need to know that it is a legitimate one.
The intuition
The diagonal entries are the variances of each component: how much each coordinate scatters on its own. The off-diagonal entries are covariances: when x is above its mean, is y more likely above or below? Positive means they move together, negative opposite, zero unrelated (to second order). Together they describe an ellipse: its tilt shows which combination of coordinates is most uncertain.
Interactive figure. A cloud of points from a two-dimensional Gaussian with nested ellipses at one, two and three standard deviations. Sliders set the standard deviations and the correlation; the readout lists the covariance matrix and its eigenvalues. This figure needs JavaScript to be interactive.
Geometric and physical interpretation
Geometric view. The contour (x − μ)ᵀ Σ⁻¹ (x − μ) = k² is an ellipse centred at the mean μ, with axes along the eigenvectors of Σ and semi-axis lengths k√λᵢ (Module 6, spectral theorem). Correlation tilts it. Physical view. A heading error in a differential-drive robot makes the position error grow along the direction perpendicular to the path, so after a while the covariance of (x, y) is a long ellipse tilted along that direction. The off-diagonal term encodes that coupling.
The mathematics
- x
- a random vector in ℝⁿ with mean μ = E[x].
- Σ
- the covariance matrix, Σ = E[(x − μ)(x − μ)ᵀ], n × n.
- Σᵢᵢ = σᵢ²
- the variance of component i; Σᵢⱼ = the covariance of i and j.
- ρᵢⱼ
- correlation coefficient Σᵢⱼ/(σᵢσⱼ), between −1 and 1.
Definition
Σ = E[ (x − μ)(x − μ)ᵀ ] sample: Σ̂ = (1/(N − 1)) Σₖ (xₖ − x̄)(xₖ − x̄)ᵀ = X̃ᵀX̃/(N − 1)
What it means. The outer product of the deviation with itself, averaged. The sample version is the same matrix as the PCA covariance of Module 6, built from the centred data matrix X̃.
Properties
Σ = Σᵀ, vᵀ Σ v = Var(vᵀ x) ≥ 0 for every v ⇒ Σ ⪰ 0
What it means. The variance of any linear combination vᵀx is vᵀΣv, which cannot be negative. So Σ is symmetric positive semi-definite, positive definite unless some combination is exactly known.
Correlation and the ellipse
Σ = Q Λ Qᵀ, ellipse axes = columns of Q, semi-axes k√λᵢ, ρ = Σ₁₂/√(Σ₁₁Σ₂₂)
What it means. The eigenvectors give the principal directions of uncertainty and the eigenvalues the variances along them. The correlation coefficient measures the tilt, with |ρ| = 1 meaning a degenerate line.
Σ = E[(x−μ)(x−μ)ᵀ]: symmetric, positive semi-definite, diagonal = variances, off-diagonal = covariances. Eigenvectors = ellipse axes, eigenvalues = variances along them.
Step-by-step derivation: why Σ is PSD, and the variance of a linear combination
- Symmetry.
Σᵢⱼ = E[(xᵢ − μᵢ)(xⱼ − μⱼ)] = E[(xⱼ − μⱼ)(xᵢ − μᵢ)] = Σⱼᵢ: multiplication of numbers commutes. - Variance of a combination. Let
y = vᵀx(a scalar). Theny − E[y] = vᵀ(x − μ)andVar(y) = E[(vᵀ(x − μ))²] = E[vᵀ(x − μ)(x − μ)ᵀv] = vᵀΣv. - Positive semi-definite. A variance is non-negative, so
vᵀΣv ≥ 0for every v, which is the definition of positive semi-definiteness (Module 6). Equality for some v ≠ 0 means vᵀx is deterministic: the data lie in a lower-dimensional subspace. - The 2 × 2 case. Positive definiteness requires
σₓ² > 0anddet Σ = σₓ²σᵧ² − σₓᵧ² > 0, i.e.|ρ| < 1. A “correlation” of 1.1 is impossible, as the positive-definite lesson showed. - Ellipse. By the spectral theorem
Σ = QΛQᵀ. In the rotated coordinatesy = Qᵀ(x − μ)the components are uncorrelated with variances λᵢ. The set whereΣ yᵢ²/λᵢ = k²is an ellipse with semi-axesk√λᵢ, which in the original coordinates is the contour of the Mahalanobis distance (next lessons). - Sample covariance from data. With the centred data matrix
X̃(rows xₖ − x̄),Σ̂ = X̃ᵀX̃/(N − 1), the covariance in the PCA lesson. It is automatically PSD, and has rank at most N − 1 (centring removes one dimension). - Uncorrelated versus independent. Independence implies zero covariance, but zero covariance does not imply independence (for example
y = x²with symmetric x has zero covariance with x). For jointly Gaussian variables, however, uncorrelated does mean independent.
Block structure. For a pose-and-landmark state the covariance has blocks: pose-pose, pose-landmark, landmark-landmark. The off-diagonal block says how much knowing the robot pose tells you about a landmark, which is exactly what makes a loop closure correct the whole map in EKF-SLAM.
Units. Entries carry products of units (m², m·rad, rad²). Mixed-unit covariances are fine, but the eigenvalues of a mixed matrix are not interpretable as variances along a single physical direction; compare only like units, or scale first.
A worked example: from four data points to an ellipse
Four paired measurements (x, y) = (1, 2), (2, 3), (3, 5), (4, 6).
- Means.
x̄ = 2.5,ȳ = 4. Deviations:(−1.5, −2), (−0.5, −1), (0.5, 1), (1.5, 2). - Sums.
Σdx² = 2.25 + 0.25 + 0.25 + 2.25 = 5,Σdxdy = 3 + 0.5 + 0.5 + 3 = 7,Σdy² = 4 + 1 + 1 + 4 = 10. - Covariance (N − 1 = 3).
Σ̂ = [[1.667, 2.333], [2.333, 3.333]]. - Correlation.
ρ = 2.333/√(1.667 · 3.333) = 0.990: nearly a line. - Eigenvalues. Trace 5, determinant
1.667·3.333 − 2.333² = 0.1111, soλ = (5 ± √(25 − 0.444))/2 = 4.9776, 0.0223. The ellipse is extremely thin: the data are almost one-dimensional, as the PCA lesson found for a similar cloud. - A pose covariance. A robot pose
(x, y, θ)withΣ = [[0.04, 0, 0], [0, 0.09, 0.012], [0, 0.012, 0.01]]hasσₓ = 0.2 m,σ_y = 0.3 m,σ_θ = 0.1 rad (5.7°); the correlation between y and θ is0.012/(0.3·0.1) = 0.4: a heading error to the left tends to go with a lateral error to the left. - Validity check. That Σ has eigenvalues
0.0083,0.04,0.0918, all positive: valid. If the 0.012 were 0.04, the lower block[[0.09, 0.04], [0.04, 0.01]]would have determinant0.0009 − 0.0016 < 0: not a covariance.
Robotics application
SLAM and localisation. The filter state covariance and the inverse (information) matrix of a pose graph are exactly this object; see probability and uncertainty, EKF-SLAM and pose graphs.
UAV estimation. The covariance of position, velocity, attitude and sensor biases drives the Kalman gain; its diagonal gives the 1σ bounds plotted in flight logs; see Gaussian distributions and covariance and the extended Kalman filter.
LiDAR and point clouds. The covariance of a point neighbourhood is the PCA of Module 6, used for normals and for per-point uncertainty; see point cloud preprocessing.
Navigation and planning. The ellipse of the position estimate inflates the robot footprint for safe path planning; see frames, poses and footprints. IMU and GPS noise models are specified as diagonal covariances and then correlated by the filter.
Engineering implications and limitations
Practical rules
- Check validity. Symmetric, all eigenvalues ≥ 0 (use eigh), diagonal positive, |ρ| ≤ 1. Test with Cholesky and repair with eigenvalue clipping or a small ridge if rounding broke it (positive-definite lesson).
- Symmetrise after every update. (Σ + Σᵀ)/2 removes rounding asymmetry that accumulates in filters.
- Keep units in mind. Report σ per component with units; compare eigenvalues only for like units.
- Do not drop off-diagonals casually. Zeroing correlations makes the filter over-confident in some directions and wrong in others. A diagonal approximation must be inflated to stay conservative.
- Mind the sample size. N points give a rank-(N − 1) covariance in a high dimension; with few samples the estimate is singular or noisy (regularise with shrinkage).
- Plot the ellipse, not only the numbers. It exposes directions of weak knowledge immediately (corridors, featureless fields).
Limits
A covariance is a second-order summary. It describes a Gaussian completely but captures only the spread (not the shape) of skewed or multimodal distributions, and covariance of angles needs care with wrapping.
Common misconceptions
“Zero covariance means independent.”
Only for jointly Gaussian variables. In general independence is stronger.
“Covariance can be any symmetric matrix.”
It must be positive semi-definite; for 2 × 2, |ρ| ≤ 1.
“The diagonal entries are the standard deviations.”
They are variances (squared units); take square roots for σ.
“Small off-diagonal numbers can be ignored.”
Their size must be judged against the product σᵢσⱼ: a covariance of 0.012 is a correlation of 0.4 here, which is substantial.
Engineering takeaways
- Σ = E[(x − μ)(x − μ)ᵀ]; diagonal entries are variances, off-diagonal entries are covariances, ρ = Σᵢⱼ/(σᵢσⱼ).
- Σ is symmetric positive semi-definite because vᵀΣv = Var(vᵀx) ≥ 0; for 2 × 2 this means |ρ| ≤ 1.
- The sample covariance is X̃ᵀX̃/(N − 1), the PCA covariance of Module 6.
- Eigenvectors of Σ are the ellipse axes and eigenvalues the variances along them; semi-axes are k√λᵢ.
- Validate, symmetrise and plot covariances; do not discard correlations without inflating the rest.
References and further reading
Primary papers and authoritative documentation.
- Barfoot, State Estimation for Robotics (author-hosted first edition, free to read): chapter 2, multivariate probability and covariance
- Mathematics for Machine Learning: chapter 6.4, summary statistics: covariance matrices
- Boyd and Vandenberghe, Introduction to Applied Linear Algebra (VMLS): statistics of vectors: covariance and correlation; the site did not load when we last checked
- UCSD ECE276A: Sensing and Estimation in Robotics: covariance matrices in state estimation
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
Compute the sample covariance matrix for the points (0, 0), (2, 1), (4, 3).
Explanation
Means: x̄ = 2, ȳ = 4/3. Deviations (−2, −4/3), (0, −1/3), (2, 5/3). Σdx² = 8, Σdxdy = 8/3 + 0 + 10/3 = 6, Σdy² = 16/9 + 1/9 + 25/9 = 42/9 = 4.667. Dividing by N − 1 = 2: Σ̂ = [[4, 3], [3, 2.333]].
Question 2
Is Σ = [[1, 0.9, 0.9], [0.9, 1, −0.9], [0.9, −0.9, 1]] a valid covariance matrix? (Look at the smallest eigenvalue.)
Explanation
The determinant is 1 + 2·0.9·0.9·(−0.9) − 3·0.81 = 1 − 1.458 − 2.43 = −2.888 < 0, so at least one eigenvalue is negative. The three correlations cannot all hold jointly: not valid.
Question 3
A pose covariance has σₓ = 0.2 m, σy = 0.3 m and Σxy = 0.03 m². What is the correlation, and which way is the ellipse tilted?
Explanation
ρ = 0.03/(0.2·0.3) = 0.5. Positive correlation: the ellipse is tilted with positive slope, extending toward the first and third quadrants: when x is above its mean, y tends to be above its mean too.
What’s next
A covariance matrix plus a mean fully describes a Gaussian, the distribution that robotics uses almost everywhere. The next lesson writes its density, shows how marginals and conditionals look, and what its level sets mean.
Related content
Nothing below is required to finish this lesson.
- BeforeM9-T1: Introduction to Uncertainty
Describe measurement uncertainty with means and variances and say why a robot must track it.
- BeforeM6-T4: Positive Definite Matrices
Test whether a symmetric matrix is positive definite and explain why covariance matrices must be.
- LabM9-L1: Robot Localization with Uncertainty
Localise a robot with range measurements: update a position estimate and its covariance, and find why an overconfident filter fails.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.