Hexa Scholar

Covariance and correlation

A cloud of points shows two quantities measured together. Can you read off from the picture how strongly y moves with x, as one number between −1 and 1?

That number is the correlation r, and the setup below gives its formula. Stage 1 asks you to guess r for six clouds before the page computes it.

Two small sailboats side by side, both riding high on the crest of the same broad wave, with the water lower on either side.
Two boats on the same swell. A wave that lifts one boat tends to lift the other. Their heights tend to rise and fall together, above their averages at the same moments and below them at the same moments. That is the pattern a positive covariance measures.
Before you start · 3 quick checks on an expectation, a standard deviation and independent events, about 3 min
Setup

Covariance. Let \(X\) and \(Y\) be random variables on the same experiment, each taking finitely many values (discrete random variables, as in the lesson on PMF, PDF and CDF), with means \(\mu_X = E[X]\) and \(\mu_Y = E[Y]\). Write \(U = X - \mu_X\) and \(V = Y - \mu_Y\) for their deviations from the means. The covariance of \(X\) and \(Y\) is the mean product of the deviations,

\[\begin{aligned} &\operatorname{Cov}(X, Y) \\ &= E[(X - \mu_X)(Y - \mu_Y)] \\ &= E[UV]. \end{aligned}\]

Each outcome contributes the product of the two deviations, weighted by its probability: a positive product when \(X\) and \(Y\) are on the same side of their means, a negative one when they are on opposite sides. With \(Y = X\) it is the variance: \(\operatorname{Cov}(X, X) = E[U^2] = \operatorname{Var}(X)\). Expanding the product and using linearity, as the lesson on expectation and variance did for the variance, gives the shortcut \(\operatorname{Cov}(X, Y) = E[XY] - \mu_X\mu_Y\).

Correlation. When \(\operatorname{SD}(X) \gt 0\) and \(\operatorname{SD}(Y) \gt 0\), the correlation is

\[\rho(X, Y) = \frac{\operatorname{Cov}(X, Y)}{\operatorname{SD}(X)\operatorname{SD}(Y)}.\]

The covariance carries the units of \(X\) times those of \(Y\); the correlation has no units, and it always lies between \(-1\) and 1 (stage 3 shows why).

A cloud of points. For \(n\) points \((x_1, y_1), \dots, (x_n, y_n)\) with means \(\bar{x} = \tfrac{1}{n}\sum x_i\) and \(\bar{y} = \tfrac{1}{n}\sum y_i\), the correlation is

\[r = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2 \sum (y_i - \bar{y})^2}},\]

defined when the \(x_i\) are not all equal and the \(y_i\) are not all equal. It is \(\rho(X, Y)\) for the pair \((X, Y)\) picked from the \(n\) points with probability \(\tfrac{1}{n}\) each: the factors \(\tfrac{1}{n}\) cancel.

Fine print: n or n − 1, and independence

Statistics software reports the sample covariance of data with \(n - 1\) in place of \(n\). The correlation \(r\) is the same either way, because the same factor appears above and below the fraction line.

Random variables \(X\) and \(Y\) with finitely many values are independent when \[\begin{aligned} &P(X = x, Y = y) \\ &= P(X = x)\,P(Y = y) \end{aligned}\] for all values \(x\) and \(y\): learning the value of one does not change the probabilities for the other. Stage 3 shows what independence does to the covariance.

1 See

Guess the correlation

Each cloud below has 80 points. Set your guess for its correlation \(r\) and check it; the page then computes \(r\) and shows where it comes from.

How the game works

Move the slider or type a number from \(-1\) to 1 (steps of 0.05), then press Check my guess. Your guess is locked, and the page shows the cloud's \(r\), how far your guess was from it, and the means and the sign of each point's product of deviations. Next cloud moves on; after the sixth cloud a table compares all your guesses with the true values. Start over clears everything.

The page made each cloud with a seeded random generator, so every visitor sees the same six clouds. The coordinates are rounded to two decimals.

2 Try

The variance of a sum

Two predictions first. The explorer below them opens once both are answered.

3 Prove

Why the covariance counts twice

Theorem

Assumptions. \(X\) and \(Y\) are random variables on the same experiment, each taking finitely many values; they need not be independent, and either may be a constant. As in the setup, \(\mu_X = E[X]\), \(\mu_Y = E[Y]\), \(U = X - \mu_X\) and \(V = Y - \mu_Y\), so that \(\operatorname{Var}(X) = E[U^2]\), \(\operatorname{Var}(Y) = E[V^2]\) and \(\operatorname{Cov}(X, Y) = E[UV]\).

Conclusion.

\[\begin{aligned} &\operatorname{Var}(X + Y) \\ &= \operatorname{Var}(X) + \operatorname{Var}(Y) \\ &\quad {} + 2\operatorname{Cov}(X, Y). \end{aligned}\]

Facts the proof uses.

  • linearity of expectation: for numbers \(a\), \(b\), \(c\), \(d\) and random variables \(A\), \(B\), \(C\) on the same experiment, \[\begin{aligned} &E[aA + bB + cC + d] \\ &= aE[A] + bE[B] \\ &\quad {} + cE[C] + d, \end{aligned}\] with no independence needed;
  • the definition of variance, \(\operatorname{Var}(Z) = E[(Z - E[Z])^2]\);
  • ordinary algebra of numbers, applied to the values of the random variables in each outcome.

Each option is one step about the \(X\) and \(Y\) of the theorem. Pick the one that is true for every pair the theorem covers, constants and dependent pairs included.

What else follows
  • Scaled sums. For numbers \(a\) and \(b\), the deviation of \(aX + bY\) is \(aU + bV\), and the same steps give \[\begin{aligned} &\operatorname{Var}(aX + bY) \\ &= a^2\operatorname{Var}(X) + b^2\operatorname{Var}(Y) \\ &\quad {} + 2ab\operatorname{Cov}(X, Y). \end{aligned}\] With \(a = 1\) and \(b = -1\) this is the formula for \(X - Y\) from stage 2.
  • Change of units. For numbers \(a\), \(b\), \(c\), \(d\), the deviations of \(aX + c\) and \(bY + d\) are \(aU\) and \(bV\), so \[\begin{aligned} &\operatorname{Cov}(aX + c, bY + d) \\ &= ab\operatorname{Cov}(X, Y). \end{aligned}\] When \(ab \gt 0\) the correlation (where it is defined) does not change, and when \(ab \lt 0\) it changes sign, because \(\operatorname{SD}(aX + c) = \lvert a \rvert\operatorname{SD}(X)\).
  • Independence. If \(X\) and \(Y\) are independent, then \[\begin{aligned} &E[XY] \\ &= \sum_x \sum_y xy\,P(X = x)P(Y = y) \\ &= E[X]E[Y], \end{aligned}\] so \(\operatorname{Cov}(X, Y) = 0\) and the variances add: \[\begin{aligned} &\operatorname{Var}(X + Y) \\ &= \operatorname{Var}(X) + \operatorname{Var}(Y). \end{aligned}\] The converse fails: if \(X\) is \(-1\), 0 or 1 with probability \(\tfrac{1}{3}\) each and \(Y = X^2\), then \(E[X] = 0\) and \(E[XY] = 0\), so \(\operatorname{Cov}(X, Y) = 0\), yet \(P(Y = 0 \mid X = 0) = 1\) while \(P(Y = 0) = \tfrac{1}{3}\).
  • Why \(-1 \le \rho \le 1\). Let \(\operatorname{SD}(X) = \sigma_X \gt 0\) and \(\operatorname{SD}(Y) = \sigma_Y \gt 0\). The scaled-sum formula with \(a = 1/\sigma_X\) and \(b = \pm 1/\sigma_Y\) gives \[\operatorname{Var}\Big(\frac{X}{\sigma_X} \pm \frac{Y}{\sigma_Y}\Big) = 2 \pm 2\rho,\] and a variance is never negative, so \(-1 \le \rho \le 1\). Equality \(\rho = -1\) or \(\rho = 1\) makes one of these variances 0, which happens exactly when \(Y = c + dX\) with probability 1 for numbers \(c\) and \(d \ne 0\). The same argument gives \(\lvert\operatorname{Cov}(X, Y)\rvert \le \operatorname{SD}(X)\operatorname{SD}(Y)\) whenever both standard deviations are positive; when one of them is 0, that variable is constant with probability 1 and both sides are 0. It also gives \(-1 \le r \le 1\) for every cloud.
4 Apply

Problems

Each problem has hints and a full solution.

Key point Compute a covariance as \(E[XY] - E[X]E[Y]\) or as the mean product of deviations, and a correlation by dividing by both standard deviations. For sums and portfolios, \[\begin{aligned} &\operatorname{Var}(aX + bY) \\ &= a^2\operatorname{Var}(X) + b^2\operatorname{Var}(Y) \\ &\quad {} + 2ab\operatorname{Cov}(X, Y), \end{aligned}\] and a correlation below 1 makes the spread of a combination smaller: for weights \(a, b \gt 0\) and positive standard deviations, \(\operatorname{SD}(aX + bY)\) is below \(a\operatorname{SD}(X) + b\operatorname{SD}(Y)\) exactly when \(\rho \lt 1\).
Level Intro Prerequisites expectation and variance (the lesson on expectation and variance), independent events (refreshed in Before you start) Time ~35 min

Further reading