Section 15.44

Correlation and covariance

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 −1-1 to 11.

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 nn pairs of values (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n), each pair measured on the same item, such as the same input, board or day.

  1. Compute the mean x‾\bar x of the xx values and the mean y‾\bar y of the yy values.

  2. For each pair, compute the deviations xi−x‾x_i - \bar x and yi−y‾y_i - \bar y and multiply them.

  3. Add the nn products and divide by n−1n - 1. The result is the sample covariance sxys_{xy}. Dividing by n−1n - 1 rather than nn 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.

  4. Divide by the sample standard deviations sxs_x and sys_y of the two lists, computed with the same n−1n - 1 convention, to get the sample correlation r=sxy/(sxsy)r = s_{xy}/(s_x s_y). It is defined only when neither list is constant, so that sxs_x and sys_y are positive.

Covariance controls the spread of differences and sums. Write Var\mathrm{Var} for variance, the average squared deviation from the mean, and Cov\mathrm{Cov} for covariance. The deviation of X−YX - Y from its mean is the deviation of XX minus the deviation of YY; squaring that and averaging gives Var(X−Y)=Var(X)+Var(Y)−2Cov(X,Y).\mathrm{Var}(X - Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) - 2\,\mathrm{Cov}(X, Y). Two unrelated quantities have covariance 0, so their difference has variance Var(X)+Var(Y)\mathrm{Var}(X) + \mathrm{Var}(Y). A positive covariance makes the difference less variable than that, and a negative covariance makes it more variable. For a sum the sign flips: Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) + 2\,\mathrm{Cov}(X, Y).

Worked example

Time an old and a new version of a function on the same five inputs. In milliseconds (ms), the old version xx takes 10, 12, 14, 16 and 18, and the new version yy takes 9, 11, 12, 15 and 18 on the same inputs, in the same order.

The per-input differences x−yx - y are 1, 1, 2, 1 and 0 ms, with mean 1 ms. Their deviations from that mean are 0, 0, 1, 0 and −1-1, so their sample variance is (0+0+1+0+1)/4=0.5ms2(0 + 0 + 1 + 0 + 1)/4 = 0.5\ \mathrm{ms}^2. The covariance formula gives the same value: 10+12.5−2×11=0.5ms210 + 12.5 - 2 \times 11 = 0.5\ \mathrm{ms}^2. Had each version been timed on different, unrelated inputs, the expected covariance would be 0, and one difference would have variance 10+12.5=22.5ms210 + 12.5 = 22.5\ \mathrm{ms}^2, 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 0.5/5≈0.32\sqrt{0.5/5} \approx 0.32 ms. For two independent groups of five with the same variances it would be 10/5+12.5/5=4.5≈2.12\sqrt{10/5 + 12.5/5} = \sqrt{4.5} \approx 2.12 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 XX and YY be random variables, numerical quantities whose values depend on the outcome of one random experiment, each with a finite variance. Write EE for the expected value, the probability-weighted average, and μX=E[X]\mu_X = E[X] and μY=E[Y]\mu_Y = E[Y] for the means. The covariance is Cov(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY.\mathrm{Cov}(X, Y) = E[(X - \mu_X)(Y - \mu_Y)] = E[XY] - \mu_X \mu_Y. If the standard deviations σX=Var(X)\sigma_X = \sqrt{\mathrm{Var}(X)} and σY=Var(Y)\sigma_Y = \sqrt{\mathrm{Var}(Y)} are both positive, the correlation is ρ=Cov(X,Y)/(σXσY)\rho = \mathrm{Cov}(X, Y)/(\sigma_X \sigma_Y). The following facts hold.

  1. −1≤ρ≤1-1 \le \rho \le 1, by the Cauchy–Schwarz inequality. ρ=1\rho = 1 or ρ=−1\rho = -1 exactly when Y=a+bXY = a + bX with probability 1 for constants aa and b≠0b \ne 0, and the sign of ρ\rho is the sign of bb.

  2. Var(X−Y)=Var(X)+Var(Y)−2Cov(X,Y)\mathrm{Var}(X - Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) - 2\,\mathrm{Cov}(X, Y).

  3. If XX and YY are independent, then Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0. The converse is false.

  4. For constants a,b,c,da, b, c, d, Cov(aX+b,cY+d)=acCov(X,Y)\mathrm{Cov}(aX + b, cY + d) = ac\,\mathrm{Cov}(X, Y). Covariance therefore changes with the units, while correlation changes at most its sign.

For nn independent pairs, the variance of the mean difference is Var(X−Y)/n\mathrm{Var}(X - Y)/n. Pairing lowers it below the unpaired value (Var(X)+Var(Y))/n(\mathrm{Var}(X) + \mathrm{Var}(Y))/n exactly when Cov(X,Y)>0\mathrm{Cov}(X, Y) > 0. 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 XX take the values −1-1, 0 and 1 with probability 1/31/3 each, and let Y=X2Y = X^2, so YY takes the values 1, 0 and 1. Then E[X]=0E[X] = 0, E[Y]=2/3E[Y] = 2/3 and E[XY]=E[X3]=(−1+0+1)/3=0E[XY] = E[X^3] = (-1 + 0 + 1)/3 = 0, so Cov(X,Y)=0−0×2/3=0\mathrm{Cov}(X, Y) = 0 - 0 \times 2/3 = 0. Yet XX fixes YY 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 Var(X)+Var(Y)−2Cov(X,Y)\mathrm{Var}(X) + \mathrm{Var}(Y) - 2\,\mathrm{Cov}(X, Y), where Var\mathrm{Var} is the variance and Cov\mathrm{Cov} 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 1+1−2×0.8=0.41 + 1 - 2 \times 0.8 = 0.4; with covariance 0, as for unrelated measurements, it would be 1+1=21 + 1 = 2. 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).