Covariance and the covariance matrix, with an engineering example
Covariance measures how two variables vary together, in the product of their units. It is positive when both tend to be above their means at the same time, negative when one tends to be high while the other is low, and near zero when there is no linear relation. A covariance matrix lists the covariance of every pair of variables, with each variable's variance on the diagonal. It is the raw material behind correlation, regression, principal components and multivariate charts such as the T² chart.
When to use it
- You need the variance of a sum or difference of correlated dimensions, as in a tolerance stack-up.
- You are setting up a multivariate analysis or chart that takes the covariance matrix as input.
- You want to see the joint variation of several measurements in their own units before standardizing it to correlations.
- For judging how strong a relation is, correlation is easier to read, because covariance depends on the units.
How it works
For n pairs (xᵢ, yᵢ), the sample covariance is:
cov(x, y) = Σ (xᵢ − x̄)(yᵢ − ȳ) / (n − 1)Like the sample variance, it divides by n − 1. The covariance of a variable with itself is its variance.
Dividing by both standard deviations removes the units and gives Pearson's correlation:
r = cov(x, y) / (sₓ · s_y)The most useful property in engineering is the variance of a sum or difference:
var(X ± Y) = var(X) + var(Y) ± 2 cov(X, Y)Tolerance stack-ups often assume the parts are independent, so the covariance term is zero. When two dimensions share a cause, such as the same machine setup or the same temperature, that assumption can be far off in either direction.
Example
A grinding machine finishes two diameters on the same shaft in one setup: D1 (nominal 25.000 mm) and D2 (nominal 20.000 mm). The step between them, D1 − D2, controls the fit of a bearing spacer. The engineer suspects that coolant temperature moves both diameters together. For 12 shafts she records the coolant temperature and both diameters as deviations from nominal, in µm:
| Shaft | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Coolant (°C) | 21.9 | 20.7 | 22.7 | 22.6 | 22.1 | 20.6 | 21.2 | 21.3 | 21.9 | 22.9 | 23.9 | 20.7 |
| D1 (µm) | 1.1 | −3.8 | 1.4 | 2.0 | 1.8 | −1.1 | −0.5 | 0.1 | 1.0 | 2.3 | 5.4 | −1.5 |
| D2 (µm) | 1.6 | −3.8 | 0.9 | 2.1 | 0.6 | −1.3 | −2.8 | −0.1 | −1.9 | 4.0 | 4.0 | −3.1 |
The covariance matrix (units: °C², °C·µm and µm²):
| Coolant | D1 | D2 | |
|---|---|---|---|
| Coolant | 1.053 | 2.206 | 2.391 |
| D1 | 2.206 | 5.311 | 5.361 |
| D2 | 2.391 | 5.361 | 7.031 |
The diagonal holds the variances, so the standard deviations are 1.03 °C, 2.30 µm and 2.65 µm. Dividing each covariance by the two standard deviations gives the correlation matrix:
| Coolant | D1 | D2 | |
|---|---|---|---|
| Coolant | 1 | 0.933 | 0.879 |
| D1 | 0.933 | 1 | 0.877 |
| D2 | 0.879 | 0.877 | 1 |
Now the step D1 − D2:
- Assuming independence: var = 5.311 + 7.031 = 12.34 µm², a standard deviation of 3.51 µm.
- With the covariance: var = 5.311 + 7.031 − 2 × 5.361 = 1.62 µm², a standard deviation of 1.27 µm.
Computing the standard deviation of the 12 step values directly gives the same 1.27 µm.
How to read the results
- All the covariances are positive: when the coolant runs warm, both diameters come out large. The covariance of 2.21 °C·µm between coolant and D1 means D1 rises about 2.21/1.053 = 2.1 µm per °C.
- The units make covariances hard to compare. 5.36 µm² between the diameters and 2.21 °C·µm between coolant and D1 cannot be ranked by size. The correlation matrix can: all three relations are strong (0.88 to 0.93).
- The stack-up is the practical payoff. Because D1 and D2 move together, the step varies far less than an independent stack-up predicts: 1.27 µm instead of 3.51 µm. A tolerance analysis that ignored the covariance would overstate the step variation almost threefold and might call for a costly process change that is not needed.
- The same logic can work against you. For a sum of two positively correlated dimensions, or a difference of negatively correlated ones, the real variation is larger than the independent estimate.
Common mistakes
- Comparing covariances across different units. A covariance of 5 is not "stronger" than one of 2 if the units differ. Use correlation for strength.
- Assuming independence in a stack-up. Dimensions made in the same setup, from the same material lot or at the same temperature are rarely independent. Measure them on the same parts and compute the covariance.
- Estimating covariance from separate samples. Covariance needs both values from the same unit. Diameters measured on different shafts cannot show how they move together.
- Trusting a covariance matrix from very few rows. With 12 parts, each covariance is uncertain. Treat the numbers as estimates, and check them with more data before relying on them.
Frequently asked questions
What is the difference between covariance and correlation?
Covariance is in the units of the two variables multiplied together, so its size depends on the units. Correlation divides covariance by both standard deviations, which makes it unit-free and always between −1 and +1.
Why does the covariance matrix have variances on the diagonal?
Because the covariance of a variable with itself, Σ(xᵢ − x̄)(xᵢ − x̄)/(n − 1), is exactly its variance.
Can covariance be negative?
Yes. A negative covariance means that when one variable is above its mean, the other tends to be below its mean, such as wall thickness and inner diameter on a tube with a fixed outer diameter.
Is there a test for covariance?
Testing a covariance against zero is the same as testing the correlation against zero, since one is a rescaled version of the other. Use the correlation test.