Two random variables on one experiment
A single experiment can give several numbers at once. Toss three fair coins. Let count the heads in the first two tosses, and the heads in the last two.
The middle coin counts in both, so and are linked.
Each of the 8 equally likely outcomes (lesson 5.1.1) gives one pair . Tails, heads, heads gives and ; heads, heads, heads gives and . Counting the outcomes for each pair gives the joint probability mass function , in eighths:
| 1/8 | 1/8 | 0 | |
| 1/8 | 2/8 | 1/8 | |
| 0 | 1/8 | 1/8 |
Row and column hold ; the comma reads "and". The pair cannot happen: no heads in the first two tosses leaves at most one head in the last two.
Rule: Joint pmf
One entry for every pair of values. The probability of any statement about both variables is the sum of the cells where it holds.
Three events read off the table:
- : the diagonal, .
- : the cells below the diagonal, .
- : the cells , and , .
Try it
The cell lists the 8 outcomes with itertools.product, adds to the cell of each, and prints the table in eighths. Fraction keeps the arithmetic exact.
Then it simulates 100,000 tosses of three coins with a fixed seed and prints the share of each cell, times 8. In this one seeded run every entry is within 0.02 of the exact count.
Marginal distributions
The distribution of alone sits in the table too. happens in three cells, so .
Adding each row gives , and adding each column gives . Written in the margins of the table, they are called the marginal distributions.
| 1/8 | 1/8 | 0 | 1/4 | |
| 1/8 | 2/8 | 1/8 | 1/2 | |
| 0 | 1/8 | 1/8 | 1/4 | |
| 1/4 | 1/2 | 1/4 | 1 |
Both are binomial with and (lesson 5.2.2), as two tosses each should be.
Rule: Marginal pmf
Add over every value of the other variable: across a row for , down a column for . Each marginal adds up to 1.
The marginals do not fix the joint. Count on two new coins instead, so the four tosses share nothing. The marginals stay , but every cell becomes a product: instead of 0.
Then , not . Two variables need their joint distribution, not just one distribution each.
Conditional distributions
Suppose you learn that : the first two tosses were tails. Only the row is still possible, so the sample space shrinks to it, as in lesson 5.1.3. Its cells, , and 0, add up to ; divided by that total, they become probabilities again: , and 0.
Rule: Conditional pmf
The row of divided by its own total. It is the distribution of once you know that , and it adds up to 1 over . Given a value with , it is not defined.
Each conditional distribution has a mean, the conditional mean (lesson 5.3.1):
| given | ||||
|---|---|---|---|---|
| 1/2 | 1/2 | 0 | 0.5 | |
| 1/4 | 1/2 | 1/4 | 1 | |
| 0 | 1/2 | 1/2 | 1.5 |
Knowing moves : each extra head among the first two tosses raises the expected by 0.5. Weighting the three conditional means by gives back , the law of total probability of lesson 5.1.3 applied to means. Case study 5.5.5 uses conditional means of asset returns.
Conditioning the other way divides by a column total instead. , but .
Try it
The first lines divide each row of the table by its total and print the conditional distribution and mean for each .
The loop repeats it on the seeded simulation: among the tosses with , the share of each value of and their average. They land within 0.005 of the exact table.
Joint densities
Two continuous random variables have a joint density : a surface over the plane, never negative, with volume 1 under it. The probability that the pair lands in a region is the volume over , a double integral (lesson 4.7.1). A joint density is a probability per unit area: per unit of times unit of .
Case study 4.7.5 met one joint density, for two normal returns. Here is a simpler one: on the unit square , , and 0 outside it. Its volume is .
- , a square: .
- , the triangle below the line : .
The density is a height, not a probability: . As in lesson 5.4.1, the probability of any single point, or of a line, is 0.
Marginal density. A sum over becomes an integral over : on . That is the area of the slice of the surface at (lesson 4.7.1). By symmetry .
Conditional density. Given , divide the slice at by its area, so that it has area 1:
At it runs from 0.3333 at to 1.6667 at , and its mean is . At it is flatter, from 0.6 to 1.4, with mean 0.5667.
The density and two slices of it, each rescaled to area 1: the conditional densities of . The slice at tilts more, so it puts more weight on large and has the larger mean.
Rule: Joint, marginal and conditional densities
The same three ideas as for a table, with sums replaced by integrals. The conditional density is defined where , even though is 0.
Since , the conditional density cannot come from lesson 5.1.3's directly. Think of it as conditioning on falling in a thin strip around , and letting the strip shrink. The simulation below does exactly that.
Try it
dblquad (lesson 4.7.1) checks the volume and the two probabilities; its function takes the inner variable first, f(y, x). quad gives the conditional mean at .
To simulate, the cell draws points uniformly in the square and keeps each with probability , half its height. Kept points pile up where is high, so they follow the density.
In this seeded run about 200,000 are kept. Among those with , the average is 0.6123; the exact average over that strip is 0.6111.
Independence
Lesson 5.1.3 called two events independent when . Two random variables are independent when this holds for every event about and every event about . For a table, that means every cell equals the product of its two margins.
Rule: Independent random variables
The joint distribution is the product of the marginals. Then every conditional distribution of equals its marginal: knowing tells you nothing about . With variables, the joint is the product of all marginals.
Four checks:
- The three coins: dependent. The cell holds 0, but . One failed cell is enough. Five of the nine cells pass, the middle one among them, , which proves nothing.
- on the square: dependent. At the density is 0, but . The figure shows it too: the conditional density of changes with .
- on the square: independent. It factors as , and the marginals are and .
- 2 on the triangle : dependent. The formula is a constant, which looks like a product, but the density is 0 off the triangle. The marginals are and , and at their product is 0.16 where is 0. Knowing forces .
The last check gives a quick test. If the region where the density is positive is not a rectangle, the variables are dependent, whatever the formula.
Independent normal draws
Case study 3.7.4 drew a table of standard normal numbers and said its columns were independent, "one tells you nothing about the others". Now that is precise. The three numbers in a row, , and , have the joint density , with the standard normal density.
So the conditional density of given is , whatever is. In that table (seed 37), the 1,597 rows with have with mean and standard deviation 0.996. All 10,000 rows give and 0.993.
Case study 4.7.5's correlated returns are the opposite case. Their density is not a product. So the chance that both lose more than 10% was 0.0288, not the product of the single chances, 0.0145.
Try it
The first lines test every cell of the coin table against the product of its margins: the four corners fail. Then the triangle's marginals at .
The last lines rebuild case study 3.7.4's table of normal draws and compare in the rows with , then , with all rows.
Lesson 5.5.2 measures how strongly two dependent variables move together. Case study 5.5.5 uses independence across months and days to scale risk over time.
Common mistakes
- The marginals are not the whole story. The coin table and the four-coin table share their marginals, yet is in one and in the other.
- Divide by the total of what you know. divides by the row total . and are different numbers.
- Independence needs every cell. Five of the coin table's nine cells equal their products, yet and are dependent.
- A density can exceed 1. is a height; only volumes are probabilities.
- Look at the region, not only the formula. A constant density on a triangle gives dependent variables.
- No conditioning on the impossible. Given a value with , the conditional distribution is not defined.
Joint, marginal and conditional distributions
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- The joint pmf is a table adding up to 1; a joint density is a surface with volume 1, and .
- Marginals: and . They do not determine the joint.
- Conditionals: and : a row or a slice rescaled to total 1. Their means are the conditional means .
- Independent: the joint is the product of the marginals at every point, so every conditional equals the marginal. One failed cell, or a region that is not a rectangle, means dependent.
- Independent standard normals have joint density .