Covariance
Covariance measures how two random variables vary together: Cov(X, Y) = E[(X − μ_X)(Y − μ_Y)] = E(XY) − E(X)E(Y). It is positive when large values of X tend to go with large values of Y, and negative when they go with small values. Dividing by both standard deviations gives the correlation ρ, which is always between −1 and 1.
Before this lesson: Independent Random Variables, Expected Value of a Continuous Random Variable and Joint Probability Distributions
What covariance measures
Pick a point (X, Y) from a joint distribution and compare it with the center (μ_X, μ_Y). If X is above its mean and Y is above its mean, the product (X − μ_X)(Y − μ_Y) is positive. If one is above and the other below, the product is negative.
Covariance is the average of that product. When most of the probability sits in the “both above” and “both below” regions, the covariance is positive, and the variables tend to rise together. When most sits in the mixed regions, it is negative.
The size of a covariance depends on units: measuring in centimeters instead of meters multiplies it by 100. The correlation fixes this by dividing by both standard deviations, which gives a number with no units between −1 and 1.
Covariance formula and correlation
\operatorname{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big] = E(XY) - E(X)\,E(Y)For discrete variables E(XY) = Σ Σ xy p(x, y); for continuous variables E(XY) = ∬ xy f(x, y) dA. The correlation coefficient is
\rho = \operatorname{Corr}(X, Y) = \dfrac{\operatorname{Cov}(X, Y)}{\sigma_X\,\sigma_Y}, \qquad -1 \le \rho \le 1Properties
- Cov(X, X) = Var(X), and Cov(X, Y) = Cov(Y, X).
- Cov(aX + b, cY + d) = ac Cov(X, Y). Adding constants changes nothing; scaling multiplies.
- Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y), and Var(X − Y) = Var(X) + Var(Y) − 2 Cov(X, Y).
- If X and Y are independent, then Cov(X, Y) = 0. The converse is false.
- ρ = ±1 exactly when Y = aX + b for some constants, with the sign of a.
Steps
- Find E(X) and E(Y) from the marginals.
- Find E(XY) by summing or integrating xy times the joint PMF or density.
- Subtract: Cov(X, Y) = E(XY) − E(X)E(Y).
- For the correlation, find Var(X) = E(X²) − [E(X)]² and Var(Y) the same way, then divide the covariance by σ_Xσ_Y.
Covariance examples
Example 1: Coffee and pastry (word problem)
At a café, X = 1 if a customer buys coffee and 0 if not, and Y = 1 if they buy a pastry and 0 if not. The joint PMF is p(0, 0) = 0.4, p(0, 1) = 0.1, p(1, 0) = 0.2 and p(1, 1) = 0.3. Find Cov(X, Y) and the correlation.
E(X) = P(X = 1) = 0.2 + 0.3 = 0.5
X is 0 or 1, so its mean is P(X = 1).
E(Y) = P(Y = 1) = 0.1 + 0.3 = 0.4
Same idea for Y.
E(XY) = 1 \cdot 1 \cdot p(1, 1) = 0.3
Every other cell has xy = 0.
\operatorname{Cov}(X, Y) = 0.3 - 0.5 \times 0.4 = 0.1Shortcut formula.
\operatorname{Var}(X) = 0.5 - 0.5^2 = 0.25,\quad \operatorname{Var}(Y) = 0.4 - 0.4^2 = 0.24X² = X for 0–1 variables, so E(X²) = E(X).
\rho = \dfrac{0.1}{\sqrt{0.25 \times 0.24}} = \dfrac{0.1}{\sqrt{0.06}} \approx 0.408Divide by the product of the standard deviations (see square roots).
Answer: Cov(X, Y) = 0.1 and ρ ≈ 0.408. Coffee buyers are more likely to buy a pastry: P(Y = 1 | X = 1) = 0.6, compared with 0.2 for non-coffee buyers.
Example 2: Zero covariance, but dependent
Use the laptop and tablet table from the independent random variables lesson: p(0, 0) = 0.20, p(0, 1) = 0.10, p(1, 0) = 0.25, p(1, 1) = 0.15, p(2, 0) = 0.20, p(2, 1) = 0.10. Find Cov(X, Y).
E(X) = 0(0.30) + 1(0.40) + 2(0.30) = 1
Mean of X from its marginal 0.30, 0.40, 0.30 (see expected value).
E(Y) = 0(0.65) + 1(0.35) = 0.35
Mean of Y from its marginal.
E(XY) = 1 \cdot 1 \cdot 0.15 + 2 \cdot 1 \cdot 0.10 = 0.35
Only the cells with x and y both nonzero count.
\operatorname{Cov}(X, Y) = 0.35 - 1 \times 0.35 = 0Shortcut formula.
Answer: Cov(X, Y) = 0, yet that lesson showed X and Y are dependent. Zero covariance means no straight-line trend, not independence.
Example 3: Covariance from a joint density
X and Y have joint density f(x, y) = x + y on the unit square. Find Cov(X, Y) and ρ.
f_X(x) = x + \tfrac{1}{2},\quad f_Y(y) = y + \tfrac{1}{2}Marginals (integrate out the other variable).
E(X) = \int_0^1 x\left(x + \tfrac{1}{2}\right)dx = \dfrac{1}{3} + \dfrac{1}{4} = \dfrac{7}{12}Mean of X; by symmetry E(Y) = 7/12 too.
E(XY) = \int_0^1\int_0^1 xy(x + y)\,dy\,dx = \int_0^1\int_0^1 (x^2y + xy^2)\,dy\,dx
Multiply out xy(x + y).
E(XY) = \dfrac{1}{3} \cdot \dfrac{1}{2} + \dfrac{1}{2} \cdot \dfrac{1}{3} = \dfrac{1}{3}Each term splits into two single integrals.
\operatorname{Cov}(X, Y) = \dfrac{1}{3} - \left(\dfrac{7}{12}\right)^2 = \dfrac{48}{144} - \dfrac{49}{144} = -\dfrac{1}{144}Shortcut formula with a common denominator.
E(X^2) = \int_0^1 x^2\left(x + \tfrac{1}{2}\right)dx = \dfrac{1}{4} + \dfrac{1}{6} = \dfrac{5}{12}Second moment.
\operatorname{Var}(X) = \dfrac{5}{12} - \dfrac{49}{144} = \dfrac{11}{144}Variance shortcut; Var(Y) is the same.
\rho = \dfrac{-1/144}{\sqrt{(11/144)(11/144)}} = \dfrac{-1/144}{11/144} = -\dfrac{1}{11} \approx -0.091Divide by σ_Xσ_Y = 11/144.
Answer: Cov(X, Y) = −1/144 and ρ = −1/11 ≈ −0.091, a very weak negative relationship.
Example 4: Covariance on a triangle
X and Y have joint density f(x, y) = 8xy on the triangle 0 ≤ y ≤ x ≤ 1, with marginals f_X(x) = 4x³ and f_Y(y) = 4y − 4y³. Find Cov(X, Y) and ρ.
E(X) = \int_0^1 x \cdot 4x^3\,dx = \dfrac{4}{5}Mean of X.
E(Y) = \int_0^1 y(4y - 4y^3)\,dy = \dfrac{4}{3} - \dfrac{4}{5} = \dfrac{8}{15}Mean of Y.
E(XY) = \int_0^1\int_0^x 8x^2y^2\,dy\,dx = \int_0^1 \dfrac{8x^5}{3}\,dx = \dfrac{8}{18} = \dfrac{4}{9}Inner integral gives 8x² · x³/3.
\operatorname{Cov}(X, Y) = \dfrac{4}{9} - \dfrac{4}{5} \cdot \dfrac{8}{15} = \dfrac{100}{225} - \dfrac{96}{225} = \dfrac{4}{225}Shortcut formula with denominator 225.
\operatorname{Var}(X) = \int_0^1 4x^5\,dx - \dfrac{16}{25} = \dfrac{2}{3} - \dfrac{16}{25} = \dfrac{2}{75}E(X²) − [E(X)]².
\operatorname{Var}(Y) = \int_0^1 (4y^3 - 4y^5)\,dy - \dfrac{64}{225} = \dfrac{1}{3} - \dfrac{64}{225} = \dfrac{11}{225}E(Y²) − [E(Y)]².
\rho = \dfrac{4/225}{\sqrt{(2/75)(11/225)}} \approx \dfrac{0.017778}{0.036107} \approx 0.492Divide by σ_Xσ_Y.
Answer: Cov(X, Y) = 4/225 ≈ 0.0178 and ρ ≈ 0.492 (exactly 4/√66). Since y can only be as large as x, large values of X allow large values of Y, so the correlation is positive.
Example 5: Risk of a two-stock portfolio (word problem)
Stock A has return X with standard deviation 10%, and stock B has return Y with standard deviation 15%. Their correlation is −0.3. You put half your money in each. Find the standard deviation of the portfolio return 0.5X + 0.5Y.
\operatorname{Cov}(X, Y) = \rho\,\sigma_X\sigma_Y = -0.3 \times 10 \times 15 = -45Solve the correlation formula for the covariance (units: percent squared).
\operatorname{Var}(0.5X + 0.5Y) = 0.25\operatorname{Var}(X) + 0.25\operatorname{Var}(Y) + 2(0.5)(0.5)\operatorname{Cov}(X, Y)Variance of a sum, with the constants squared.
\operatorname{Var}(0.5X + 0.5Y) = 0.25(100) + 0.25(225) + 0.5(-45)Substitute Var(X) = 10² and Var(Y) = 15².
\operatorname{Var}(0.5X + 0.5Y) = 25 + 56.25 - 22.5 = 58.75Add.
\sigma = \sqrt{58.75} \approx 7.66Take the square root.
Answer: about 7.66%, lower than either stock alone. The negative covariance is why mixing the two stocks reduces risk.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Using E(X)E(Y) for E(XY) | Computing E(XY) = 0.5 × 0.4 in Example 1, so the covariance is 0 | Find E(XY) from the joint distribution; it equals E(X)E(Y) only when the covariance is 0. |
| Treating zero covariance as independence | Saying the variables in Example 2 are independent | Zero covariance rules out only a straight-line trend. |
| Forgetting 2Cov in a variance | Var(X + Y) = Var(X) + Var(Y) for dependent variables | Add 2 Cov(X, Y) unless X and Y are uncorrelated. |
| Mixing up covariance and correlation | Calling Cov = 45 a strong relationship | Covariance depends on units; judge strength with ρ. |
| Not squaring constants | Var(0.5X) = 0.5 Var(X) | Var(aX) = a² Var(X), and Cov(aX, bY) = ab Cov(X, Y). |
Related ideas
Sample correlation in statistics
The correlation coefficient r in statistics is the sample version of ρ, computed from data pairs. It estimates ρ for the population the data came from.
Variance of sums
Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y) extends the rule from combining random variables to dependent variables. For n variables, add every variance and twice every pairwise covariance.
Covariance matrices
For several variables, the covariances are collected in a symmetric covariance matrix, with the variances on the diagonal. The Bivariate Normal Distribution, later in this unit, is built from one.
Practice problems
- p(0, 0) = 0.3, p(0, 1) = 0.1, p(1, 0) = 0.1 and p(1, 1) = 0.5. Find Cov(X, Y).
- For the table in problem 1, find the correlation ρ.
- Cov(X, Y) = 4. Find Cov(3X + 2, −2Y + 5).
- Var(X) = 9, Var(Y) = 16 and Cov(X, Y) = −6. Find Var(X + Y) and Var(X − Y).
- f(x, y) = 4xy on the unit square. Find Cov(X, Y).
- f(x, y) = 2 on the triangle 0 ≤ x ≤ y ≤ 1. Find Cov(X, Y).
- For the density in problem 6, find ρ.
- X is uniform on [−1, 1] and Y = X². Find Cov(X, Y). Are X and Y independent?
- For the density in Example 3, find Var(X + Y).
- In Example 5, you put 60% in stock A and 40% in stock B. Find the portfolio standard deviation.
Show answers
1.
E(X) = 0.1 + 0.5 = 0.6,\quad E(Y) = 0.1 + 0.5 = 0.6
P(X = 1) and P(Y = 1).
E(XY) = p(1, 1) = 0.5
Only the (1, 1) cell has xy = 1.
\operatorname{Cov}(X, Y) = 0.5 - 0.6 \times 0.6 = 0.14Shortcut formula.
Answer: 0.14
2.
\operatorname{Var}(X) = 0.6 - 0.36 = 0.24 = \operatorname{Var}(Y)E(X²) = E(X) for 0–1 variables.
\rho = \dfrac{0.14}{\sqrt{0.24 \times 0.24}} = \dfrac{0.14}{0.24} = \dfrac{7}{12} \approx 0.583Divide by σ_Xσ_Y = 0.24.
Answer: 7/12, about 0.583
3.
\operatorname{Cov}(3X + 2, -2Y + 5) = 3(-2)\operatorname{Cov}(X, Y) = -6 \times 4 = -24Constants drop out; multipliers multiply.
Answer: −24
4.
\operatorname{Var}(X + Y) = 9 + 16 + 2(-6) = 13Add 2Cov.
\operatorname{Var}(X - Y) = 9 + 16 - 2(-6) = 37Subtract 2Cov.
Answer: 13 and 37
5.
E(XY) = \int_0^1\int_0^1 4x^2y^2\,dy\,dx = 4 \cdot \dfrac{1}{3} \cdot \dfrac{1}{3} = \dfrac{4}{9}Splits into single integrals.
E(X) = E(Y) = \int_0^1 x \cdot 2x\,dx = \dfrac{2}{3}Marginals 2x and 2y.
\operatorname{Cov}(X, Y) = \dfrac{4}{9} - \dfrac{2}{3} \cdot \dfrac{2}{3} = 0As expected: X and Y are independent.
Answer: 0
6.
E(X) = \int_0^1 x \cdot 2(1 - x)\,dx = 1 - \dfrac{2}{3} = \dfrac{1}{3}Marginal f_X(x) = 2(1 − x).
E(Y) = \int_0^1 y \cdot 2y\,dy = \dfrac{2}{3}Marginal f_Y(y) = 2y.
E(XY) = \int_0^1\int_0^y 2xy\,dx\,dy = \int_0^1 y^3\,dy = \dfrac{1}{4}Inner integral in x from 0 to y gives y · y².
\operatorname{Cov}(X, Y) = \dfrac{1}{4} - \dfrac{1}{3} \cdot \dfrac{2}{3} = \dfrac{9}{36} - \dfrac{8}{36} = \dfrac{1}{36}Shortcut formula.
Answer: 1/36
7.
\operatorname{Var}(X) = \int_0^1 2x^2(1 - x)\,dx - \dfrac{1}{9} = \dfrac{1}{6} - \dfrac{1}{9} = \dfrac{1}{18}E(X²) − [E(X)]².
\operatorname{Var}(Y) = \int_0^1 2y^3\,dy - \dfrac{4}{9} = \dfrac{1}{2} - \dfrac{4}{9} = \dfrac{1}{18}E(Y²) − [E(Y)]².
\rho = \dfrac{1/36}{1/18} = \dfrac{1}{2}σ_Xσ_Y = 1/18.
Answer: 1/2
8.
\operatorname{Cov}(X, X^2) = E(X^3) - E(X)\,E(X^2) = 0 - 0 \cdot \dfrac{1}{3} = 0E(X³) = 0 and E(X) = 0 by symmetry about 0.
Answer: Cov = 0, but Y is a function of X, so they are dependent
9.
\operatorname{Var}(X + Y) = \dfrac{11}{144} + \dfrac{11}{144} + 2\left(-\dfrac{1}{144}\right) = \dfrac{20}{144} = \dfrac{5}{36}Variance of a sum with 2Cov.
Answer: 5/36
10.
\operatorname{Var} = 0.36(100) + 0.16(225) + 2(0.6)(0.4)(-45)Weights squared, plus 2ab Cov.
\operatorname{Var} = 36 + 36 - 21.6 = 50.4Add.
\sigma = \sqrt{50.4} \approx 7.10Square root.
Answer: about 7.10%
Covariance FAQ
What is the formula for covariance?
Cov(X, Y) = E(XY) − E(X)E(Y). It equals the definition E[(X − μ_X)(Y − μ_Y)], and it is usually faster to compute.
What does a negative covariance mean?
Above-average values of one variable tend to occur with below-average values of the other. In Example 5, that is why combining the two stocks lowers risk.
What is the difference between covariance and correlation?
Covariance depends on the units of X and Y and can be any real number. Correlation divides by both standard deviations, so it has no units and always lies between −1 and 1.
If the covariance is 0, are X and Y independent?
Not necessarily. Independent variables have zero covariance, but Example 2 and practice problem 8 show dependent variables with zero covariance too.
Summary
- Cov(X, Y) = E(XY) − E(X)E(Y), the average product of deviations from the means.
- Positive covariance: X and Y tend to rise together. Negative: one rises as the other falls.
- ρ = Cov(X, Y)/(σ_Xσ_Y) is between −1 and 1 and has no units.
- Var(X ± Y) = Var(X) + Var(Y) ± 2 Cov(X, Y).
- Independent implies zero covariance, but not the other way around.
Previous lesson: Independent Random Variables
Next lesson: Conditional Expectation
Back to the Probability Theory Index