Chapter 6: Joint distributions and covariance

Previous: PS 05 Continuous Random Variables | PS 00 Course Map | Next: PS 07 The Multivariate Gaussian

The big idea

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.

In this chapter you will learn to
  1. Read joint, marginal and conditional distributions from a table.
  2. Test independence of random variables.
  3. Compute covariance and correlation, and interpret them correctly.
  4. Recognize what correlation cannot tell you.
  5. Compute the variance of sums of dependent variables.
  6. Build covariance matrices and propagate them through linear maps with .
Before you start

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

Definition: joint PMF

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
dry0.100.50
rain0.250.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

Definition: marginal distribution

(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.

failsuccess
dry0.100.500.60
rain0.250.150.40
0.350.651

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).

Marginals do not determine the joint

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

Definition: conditional distribution

Take one row of the table and divide by its total, so it sums to 1.

failsuccess
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

Definition: independent 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:

failsuccess
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.

Definition: covariance

Why this formula measures “moving together”

Look at the product :

vs its mean vs its meanproduct
aboveabovepositive
belowbelowpositive
abovebelownegative
belowabovenegative

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.

CovarianceMeaning
positivetend to move in the same direction
negativetend to move in opposite directions
zerono 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

PropertyFormula
symmetric
constants vanish
scaling
shifting does nothing
bilinear
independenceindependent

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:

Definition: correlation
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

Warning
  1. 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.

Warning
  1. 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.

Warning
  1. 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

Variance of a sum

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:

Definition: covariance matrix

Example. For worked example 6.3:

Propagating uncertainty through a linear map

Linear transformation of a random vector

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

IdeaFormula
marginal
conditional
independence for all
covariance
correlation
sum
covariance matrix
linear map

Three facts to remember:

  1. The relationship between variables lives in the joint, not in the marginals.
  2. Correlation measures straight-line association only, and says nothing about causation.
  3. 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 , , .

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”.

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.

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

Next: PS 07 The Multivariate Gaussian: the most important joint distribution.