Chapter 6: Joint distributions and covariance
Previous: PS 05 Continuous Random Variables | PS 00 Course Map | Next: PS 07 The Multivariate Gaussian
Real problems involve several random quantities at once: position and velocity, temperature and success rate, every pixel of an image. A joint distribution describes them together. From it you can recover each one alone (marginals), one given another (conditionals), and a single number measuring how they move together (covariance and correlation). Covariance matrices then let you track uncertainty through any linear calculation.
- Read joint, marginal and conditional distributions from a table.
- Test independence of random variables.
- Compute covariance and correlation, and interpret them correctly.
- Recognize what correlation cannot tell you.
- Compute the variance of sums of dependent variables.
- Build covariance matrices and propagate them through linear maps with .
You need conditional probability (PS 02 Conditional Probability and Bayes Rule) and expectation and variance (PS 04 Expectation and Variance). Section 6.7 uses matrix multiplication (LA 03 Matrices as Linear Maps).
6.1 Joint distributions
Discrete case: a table
For discrete random variables and ,
All entries are non-negative and sum to 1.
Running example. Outdoor robot tests. is the weather, is whether the test succeeds.
| fail | success | |
|---|---|---|
| dry | 0.10 | 0.50 |
| rain | 0.25 | 0.15 |
Each cell is the probability of that combination. Check: ✓.
Continuous case: a surface
For continuous , the joint density is a surface over the plane. Probabilities are volumes under it:
Everything below works the same way, with sums replaced by integrals.
6.2 Marginals: forget one variable
(For densities: integrate out the other variable.)
Add across a row to get the row total; add down a column to get the column total. These totals are written in the margins of the table, hence the name.
| fail | success | ||
|---|---|---|---|
| dry | 0.10 | 0.50 | 0.60 |
| rain | 0.25 | 0.15 | 0.40 |
| 0.35 | 0.65 | 1 |
So and .
Why it works: “dry” is the union of the disjoint events “dry and fail” and “dry and success”, so their probabilities add (axiom 3).
Knowing and does not tell you . Many different tables have the same margins. The relationship between variables lives only in the joint.
6.3 Conditionals: fix one variable
Take one row of the table and divide by its total, so it sums to 1.
| fail | success | |
|---|---|---|
| given dry | ||
| given rain |
Rain more than halves the success rate.
How the three fit together
This is the multiplication rule from chapter 2 for random variables. Bayes’ rule follows immediately:
Example. . Most failures happen in rain.
6.4 Independence of random variables
and are independent if the joint factorizes for every pair of values:
Equivalently, : knowing never changes the distribution of .
Check the example. , but . Dependent.
One mismatched cell is enough to show dependence. To show independence you must check every cell.
An independent table, for contrast: if the margins were the same but every cell were the product of its margins:
| fail | success | |
|---|---|---|
| dry | ||
| rain |
Here success is 65% in both weathers.
6.5 Covariance
The definition
We want one number saying whether two variables tend to be high together, or one high when the other is low.
Why this formula measures “moving together”
Look at the product :
| vs its mean | vs its mean | product |
|---|---|---|
| above | above | positive |
| below | below | positive |
| above | below | negative |
| below | above | negative |
If high-high and low-low pairs dominate, the average product is positive. If high-low pairs dominate, it is negative. If there is no tendency either way, they cancel to about zero.
| Covariance | Meaning |
|---|---|
| positive | tend to move in the same direction |
| negative | tend to move in opposite directions |
| zero | no linear tendency |
Note that .
Worked example 6.1
Code dry , rain , fail , success .
- .
- .
- only for (rain, success), so .
Negative: rain goes with failure. (For the independent table it would be .)
Properties
| Property | Formula |
|---|---|
| symmetric | |
| constants vanish | |
| scaling | |
| shifting does nothing | |
| bilinear | |
| independence | independent |
6.6 Correlation
Removing the units
Covariance depends on units: measure in centimetres instead of metres and the covariance grows 100 times, though the relationship is unchanged. Divide out the standard deviations to get a pure number:
| Picture of the scatter plot | |
|---|---|
| points exactly on an upward line | |
| around | a clear upward cloud |
| no linear trend | |
| around | a clear downward cloud |
| points exactly on a downward line |
Why bounded by 1? After subtracting the means, correlation is exactly the cosine of the angle between the two variables viewed as vectors (LA 02 Dot Product Length and Angle), and Cauchy-Schwarz bounds any cosine by 1.
Worked example 6.2. For the weather table: and . So
A moderate negative relationship.
6.7 Three warnings about correlation
- Uncorrelated does not mean independent
Let be equally likely to be , and . Then is completely determined by . But
Correlation only detects straight-line relationships. A U-shaped relationship can have zero correlation.
Independent ⇒ uncorrelated. Uncorrelated ⇏ independent.
- Correlation does not imply causation
Ice cream sales and drownings are positively correlated. Ice cream does not cause drowning: hot weather drives both. A confounder (a common cause), reverse causation, or selection effects can all create correlation without the causal link you might assume.
In robot learning: if successful demonstrations all happened to be recorded in the morning, a policy may learn that morning light “causes” success.
- One number hides the shape
Datasets with completely different shapes (a line, a curve, a cloud with one outlier) can share the same correlation. Always plot the data.
6.8 Variance of a sum, in general
More generally,
Proof: expand ; the cross term is .
Meaning:
- Positive covariance: fluctuations reinforce each other; the sum is more variable. Correlated risks add up badly.
- Negative covariance: fluctuations partly cancel; the sum is steadier. This is the principle behind diversification and hedging.
- Zero covariance: back to the simple rule of chapter 4.
Worked example 6.3. , , .
- .
- .
- .
6.9 Random vectors and covariance matrices
Stacking variables into a vector
Collect random variables into a random vector . Its mean vector is , and its spread is described by the covariance matrix:
- Diagonal entries are the variances.
- Off-diagonal entries are the covariances.
- is symmetric () and positive semidefinite (LA 09 Symmetric and Positive Definite Matrices).
Example. For worked example 6.3:
Propagating uncertainty through a linear map
If , then
Why: , so ; take expectations.
This one formula handles all linear error propagation. Check it on the sum: , so
Worked example 6.4: sum and difference. independent with variances 1 and 4. Let , :
Even though and are independent, the sum and difference are negatively correlated. Transformations create correlations.
Worked example 6.5: robot position. A planar robot’s estimated position has covariance m². What is the uncertainty in the distance travelled along the direction ? That distance is , so its variance is
a standard deviation of about 0.144 m.
6.10 The law of total variance
Total variation splits into variation within each group and variation between the groups’ averages. The second term is the part “explained by” .
Worked example 6.6. Task times: light boxes (60%) have mean 20 s and variance 4; heavy boxes (40%) have mean 45 s and variance 25.
- Within: .
- Between: the group means 20 and 45 with weights 0.6, 0.4 have mean 30 and variance .
- Total: . Most of the spread comes from which kind of box it is, not from variation within a kind.
This decomposition is the basis of in regression and of the analysis of variance (ANOVA).
Summary
| Idea | Formula |
|---|---|
| marginal | |
| conditional | |
| independence | for all |
| covariance | |
| correlation | |
| sum | |
| covariance matrix | |
| linear map |
Three facts to remember:
- The relationship between variables lives in the joint, not in the marginals.
- Correlation measures straight-line association only, and says nothing about causation.
- propagates uncertainty through any linear calculation.
Exercises
Part A: warm-up
A1. From the weather table, find and .
A2. , , . Find and .
A3. Is this table independent? , , , .
A4. Write the covariance matrix of if , , .
A1. and .
A2. . .
A3. Margins: , , , . Products: . All match: independent.
A4. .
Part B: core
B1. Two dice. Let be the first die and the sum. Find and . (Hint: with independent of .)
B2. A portfolio puts weight in asset A (variance 0.04) and in asset B (variance 0.09), with correlation . Write the variance as a function of and find the that minimizes it.
B3. A camera estimates an object’s position with covariance mm² in the camera frame. The camera frame is rotated relative to the robot frame, . Find the covariance in the robot frame. Interpret.
B4. Give a small joint table where and are uncorrelated but not independent.
B5. Data shows that students who take more practice tests score higher. Give two explanations other than “practice tests cause higher scores”.
B1. . . .
B2. . . Derivative: , so and . Variance about : lower than either asset alone (0.04 and 0.09), because the negative correlation lets their fluctuations partly cancel.
B3. . The large uncertainty that was along the camera’s -axis is now along the robot’s -axis: the uncertainty ellipse rotates with the frame.
B4. The example: each with probability ; .
B5. Motivated students both take more practice tests and study more in other ways (confounder). Or students who already score well enjoy taking tests (reverse causation).
Part C: explain and prove
C1. Prove from the definition.
C2. Prove that if and are independent then . (Use for independent variables.)
C3. Prove that a covariance matrix is positive semidefinite. (Hint: for any fixed vector , is the variance of something.)
C4. Explain why averaging two positively correlated measurements reduces noise less than averaging two independent ones.
C1. Expand and take expectations: .
C2. .
C3. .
C4. . With this exceeds . Correlated errors do not cancel, because they tend to point the same way.
Part D: practise with code
D1. Generate 10,000 pairs from the weather table (sample a cell index with rng.choice(4, p=[...])) and estimate the covariance and correlation. Compare with and .
D2. Generate and . Compute np.corrcoef(X, Y) and make a scatter plot. What do you see?
D3. Draw 10,000 vectors with covariance (rng.multivariate_normal), apply , and compare np.cov of the result with .
Where this is used
- The Kalman filter propagates a covariance with at every prediction step (04 Perception and State).
- Correlated samples (consecutive frames of a video, repeated trials on one object) carry less information than their count suggests (16 Evaluation and Uncertainty).
- Spurious correlations in demonstration data are one source of policies that fail when conditions change (06 Imitation and Distribution Shift).
Next: PS 07 The Multivariate Gaussian: the most important joint distribution.