Joint Distributions, Covariance and Correlation
Describe two random variables with a joint table, find marginals and conditionals, and measure how they move together with covariance and correlation.
Welcome back!
Step 1 of 13Learn
Two variables at once
So far each random variable has lived alone. Real questions usually involve several at once: a customer's age and spending, a stock's return and the market's, the temperature and the number of ice creams sold. To study them together, you need their joint distribution.
For two discrete variables, the joint PMF gives P(X = x and Y = y) for every pair. Here's an example. A store tracks whether a visitor has a loyalty card (X = 1) or not (X = 0), and how many items they buy (Y = 0, 1 or 2):
| Y = 0 | Y = 1 | Y = 2 | Total | |
|---|---|---|---|---|
| X = 0 | 0.20 | 0.15 | 0.05 | 0.40 |
| X = 1 | 0.10 | 0.25 | 0.25 | 0.60 |
| Total | 0.30 | 0.40 | 0.30 | 1 |
All six cells add to 1. The row and column totals are the marginal distributions: P(X = 1) = 0.60, and Y has marginal PMF 0.30, 0.40, 0.30. They're called marginal because they're written in the margins.
Conditional distributions come from restricting to a row or column. Given a loyalty card, P(Y = 2 | X = 1) = 0.25/0.60 ≈ 0.42. Without one, P(Y = 2 | X = 0) = 0.05/0.40 = 0.125. Card holders are more than three times as likely to buy two items.
X and Y are independent if every cell equals the product of its margins. Here P(X = 0, Y = 0) = 0.20, but P(X = 0)·P(Y = 0) = 0.40 × 0.30 = 0.12. One failing cell is enough: X and Y are dependent.
Step 2 of 13Practice · Work it out
Using the loyalty-card table, what is P(X = 1 | Y = 0), the chance a visitor who buys nothing has a card? Give a decimal to two places.
Step 3 of 13Chart
Temperature and ice creams sold
Each point is one day. The cloud slopes upward: days above average temperature tend to have above-average sales, so the covariance is positive.
| Point | Temperature (°C) | Ice creams sold |
|---|---|---|
| Day 1 | 14 | 120 |
| Day 2 | 16 | 150 |
| Day 3 | 18 | 140 |
| Day 4 | 19 | 190 |
| Day 5 | 21 | 210 |
| Day 6 | 22 | 200 |
| Day 7 | 24 | 260 |
| Day 8 | 26 | 250 |
| Day 9 | 28 | 310 |
| Day 10 | 31 | 330 |
Step 4 of 13Learn
Covariance
To summarize how two variables move together, use the covariance:
Cov(X, Y) = E[(X − μ_X)(Y − μ_Y)] = E[XY] − E[X]·E[Y]
When X is above its mean, is Y usually above its mean too? Then the products are mostly positive and the covariance is positive. If high X tends to go with low Y, it's negative. If there's no linear tendency either way, it's near zero.
Loyalty-card example.
- E[X] = 0.60 (from the margin).
- E[Y] = 0(0.30) + 1(0.40) + 2(0.30) = 1.00.
- E[XY]: only cells with X = 1 and Y ≥ 1 contribute: 1·1·0.25 + 1·2·0.25 = 0.75.
- Cov(X, Y) = 0.75 − 0.60 × 1.00 = 0.15.
Positive, as expected: card holders buy more.
Covariance completes the rule for adding variances. For any X and Y:
Var(X + Y) = Var(X) + Var(Y) + 2·Cov(X, Y)
Independent variables have covariance 0, which recovers the earlier rule. Positively related variables make sums more variable; negatively related ones make sums less variable. That's the logic of hedging: pairing an investment with one that tends to move the opposite way reduces the total risk.
The downside of covariance is its units. Measure spending in cents instead of dollars and the covariance becomes 100 times larger, though the relationship hasn't changed.
Step 5 of 13Practice · Work it out
X and Y have Var(X) = 4, Var(Y) = 9 and Cov(X, Y) = −3. What is Var(X + Y)?
Step 6 of 13Learn
Correlation
Correlation rescales covariance so it has no units and always lies between −1 and 1:
ρ = Corr(X, Y) = Cov(X, Y) / (σ_X · σ_Y)
- ρ = 1: perfect increasing linear relationship (Y = a + bX with b > 0).
- ρ = −1: perfect decreasing linear relationship.
- ρ = 0: no linear relationship.
For the loyalty card: Var(X) = 0.60 × 0.40 = 0.24, and Var(Y) = E[Y2] − 12 = (0.40 + 4 × 0.30) − 1 = 0.60. So ρ = 0.15 / √(0.24 × 0.60) ≈ 0.15 / 0.379 ≈ 0.40, a moderate positive association.
Three warnings come with every correlation:
- Correlation is not causation. Cities with more firefighters have more fire damage, because big fires bring more firefighters. Hidden third variables (confounders) often drive both.
- Zero correlation does not mean independence. Let X be −1, 0 or 1 with equal probability, and Y = X2. Y is completely determined by X, yet Cov(X, Y) = E[X3] − E[X]E[X2] = 0 − 0 = 0. Correlation detects only straight-line patterns.
- Correlation is sensitive to outliers and to the range of data. Restricting to a narrow range, such as only top students, can make a real relationship look weak.
Independence does guarantee zero correlation. The reverse holds only in special cases, most notably when X and Y are jointly normal.
Step 7 of 13Practice · Diagram
Reading a correlation
Which pair has the weakest linear relationship?
Tap the shape that answers the question, then press Check.
- Car age and resale price
Older cars sell for less: a strong negative linear link.
- Shoe size and exam score
Essentially no linear relationship among adults.
- Height and weight
Taller people tend to weigh more, with plenty of scatter.
Step 8 of 13Practice · True or false
If Cov(X, Y) = 0, then X and Y must be independent.
Step 9 of 13Note
Step 10 of 13Practice · Chart
50/50 portfolio risk as correlation changes
Two assets, each with SD 20%. The lower the correlation, the more their ups and downs cancel.
Tap the correlation that removes all the risk.
Tap the bar that answers the question, then press Check.
Step 11 of 13Try the calculator
Portfolio risk calculator
Two assets with return SDs σ₁ and σ₂, correlation ρ, and weight w in the first asset.
- Portfolio SD
- 16.12 %
- Covariance
- 120 %²
Step 12 of 13Practice · Select all that apply
Which statements about covariance and correlation are true?
Select all that apply.
Step 13 of 13Practice · Choose one
X and Y have SDs 2 and 5 and covariance 6. What is their correlation?
Lesson complete!