Definition
Covariance and correlation are measures of how two numerical quantities vary together, such as the run times of two program versions on the same inputs. Covariance is the average product of the two quantities’ deviations from their own means: positive when both tend to lie above their means together, negative when one tends to lie above while the other lies below; its unit is the product of the two units. Correlation, the Pearson correlation coefficient, divides the covariance by both standard deviations §11.14, giving a unit-free number from to .
The idea
Two versions of a function are timed on the same five inputs. Some inputs are much harder than others, so both versions’ times rise and fall together from input to input. The times of each version vary a lot. The difference between the two versions on the same input hardly varies at all, because the difficulty of the input appears in both times and cancels in the subtraction. Covariance measures this shared movement, and it is the reason a paired design §11.7, in which every measurement has a partner taken on the same input or under the same circumstances, can be far more precise than a comparison of two separate groups.
The definition follows the intuition. For each pair, measure how far each value lies from its own mean. If both lie above, or both below, the product of the two deviations is positive; if one lies above and the other below, the product is negative. Averaging the products gives a positive number when same-direction pairs dominate, a negative number when opposite-direction pairs dominate, and a number near zero when neither does. Correlation rescales that number so that it no longer depends on units. Points that lie exactly on a rising straight line give a correlation of exactly 1.
How it works
Take pairs of values , each pair measured on the same item, such as the same input, board or day.
Compute the mean of the values and the mean of the values.
For each pair, compute the deviations and and multiply them.
Add the products and divide by . The result is the sample covariance . Dividing by rather than corrects the small underestimate caused by measuring deviations from the sample means §11.12 instead of the unknown true means, the same correction that the sample variance uses.
Divide by the sample standard deviations and of the two lists, computed with the same convention, to get the sample correlation . It is defined only when neither list is constant, so that and are positive.
Covariance controls the spread of differences and sums. Write for variance, the average squared deviation from the mean, and for covariance. The deviation of from its mean is the deviation of minus the deviation of ; squaring that and averaging gives Two unrelated quantities have covariance 0, so their difference has variance . A positive covariance makes the difference less variable than that, and a negative covariance makes it more variable. For a sum the sign flips: .
Worked example
Time an old and a new version of a function on the same five inputs. In milliseconds (ms), the old version takes 10, 12, 14, 16 and 18, and the new version takes 9, 11, 12, 15 and 18 on the same inputs, in the same order.
Means: ms and ms.
Deviations of : . Deviations of : .
Products: , with sum 44. Sample covariance: .
Sample variances: and .
Correlation: .
The per-input differences are 1, 1, 2, 1 and 0 ms, with mean 1 ms. Their deviations from that mean are 0, 0, 1, 0 and , so their sample variance is . The covariance formula gives the same value: . Had each version been timed on different, unrelated inputs, the expected covariance would be 0, and one difference would have variance , 45 times larger.
The effect on the uncertainty of the mean difference is large. The standard error §11.15, the typical error of an estimate, of the mean of the five paired differences is ms. For two independent groups of five with the same variances it would be ms. Pairing cuts the uncertainty by a factor of about 6.7, because the difficulty of the input, the shared source of variation, cancels in each difference.
Stated precisely
Let and be random variables, numerical quantities whose values depend on the outcome of one random experiment, each with a finite variance. Write for the expected value, the probability-weighted average, and and for the means. The covariance is If the standard deviations and are both positive, the correlation is . The following facts hold.
, by the Cauchy–Schwarz inequality. or exactly when with probability 1 for constants and , and the sign of is the sign of .
.
If and are independent, then . The converse is false.
For constants , . Covariance therefore changes with the units, while correlation changes at most its sign.
For independent pairs, the variance of the mean difference is . Pairing lowers it below the unpaired value exactly when . This rests on one assumption: the design, not the data, decides which measurements form a pair, and both members of a pair share a source of variation, such as the same input.
Limits and common mistakes
Zero covariance does not mean independence. Let take the values , 0 and 1 with probability each, and let , so takes the values 1, 0 and 1. Then , and , so . Yet fixes completely.
Correlation measures straight-line association only. The curved relation above has correlation 0. A scatter plot shows such shapes; a single number does not.
Correlation is not causation. Two quantities can move together because a third one, a confounder §11.10, drives both. Two quantities that both drift during a day are correlated even if neither affects the other.
One outlier §11.31, a value far from the others, can create a strong correlation or hide one. Pooling data from different groups can produce a correlation that no single group shows, or one of the opposite sign.
Pairing must be fixed by the design. Pairing values after the fact, for example by sorting both lists and matching ranks, manufactures a positive covariance and makes the computed uncertainty falsely small.
A negative covariance increases the variance of a paired difference. And a covariance estimated from few pairs is itself uncertain.
Questions answered
Why can paired measurements have lower variance of their difference?
Paired measurements can have a lower variance of their difference because the variance of a difference is , where is the variance and the covariance, and measuring both members of a pair on the same item, such as the same input, usually makes their covariance positive. Covariance is the average product of the two quantities’ deviations from their means. A source of variation that the partners share, such as the difficulty of the input both versions run on, raises or lowers both values together and cancels in the subtraction.
With both variances 1 and a covariance of 0.8, the difference has variance ; with covariance 0, as for unrelated measurements, it would be . The gain needs pairs chosen by the design, not by sorting or matching after the data are seen. A negative covariance would increase the variance of the difference instead.
Related entries: Paired design (§11.7), Variance and standard deviation (§11.14), Standard error (§11.15), Confounding (§11.10).