Random Variables And Joint Distributions
On this page
A stochastic variable (or random variable) is a variable whose value is determined by chance. Roll a die and let be the number of pips: before the roll you do not know , but you know exactly which values it can take and how likely each one is. Random variables turn outcomes into numbers, which is what makes it possible to compute with them, tabulate them, and later build distributions like the binomial and normal distribution on top of them. This note builds on "Probability Foundations".
The Distribution Of One Variable
The probability distribution of lists the probability of every value can take. Building one is always a two step job:
- Determine which values can take.
- Determine how the probability is divided over those values.
For "roll a fair die", with the number of pips:
The probabilities in a distribution always add up to , because must take some value. That is the first thing to check in any distribution table you build.
A Variable Built From Another
Random variables can be computed from other random variables. Pick an input size uniformly from for an algorithm with running time proportional to , and let (the running time with the constant factor left out). inherits its probabilities from :
The values get stretched far apart, but each one keeps the probability of the input that produced it.
Joint Distributions
Often you observe two variables at once and care about how they relate. In a survey of 51 students, is "does the student live in student housing?" and is "does the student smoke?". Every student gives a pair of answers , such as (not in housing, smoker).
The Joint Table
Each variable here takes only two values, so there are four combinations. The probability of one combination, such as , is a joint probability (the comma means and). The table of all joint probabilities is the joint probability distribution of and :
| smoker | non-smoker | |
|---|---|---|
| student housing | ||
| not in housing |
Interpretation: 5 of the 51 students live in student housing and smoke, so a randomly chosen student has about a chance of being a smoker in student housing.
Marginal Distributions
Add each row and each column and write the sums in the margins of the table. The row sums give the distribution of on its own, the column sums the distribution of on its own. That is where the name marginal distribution comes from.
| smoker | non-smoker | ||
|---|---|---|---|
| student housing | |||
| not in housing | |||
So about of the students live in student housing. In general, if takes values and takes :
This works because the rows split the event "" into mutually exclusive pieces, one per value of , and mutually exclusive pieces add.
Conditional Probabilities From A Table
What is the probability that a student smokes, given that they live in student housing? The condition restricts you to the housing row. There are two equivalent ways to compute it.
With counts (Laplace inside the row): .
With probabilities only (the definition of conditional probability):
The rule of thumb: a condition on means divide by a row total, a condition on means divide by a column total.
Independent Random Variables
For events, independence means . Applied to every cell of the table, this gives the definition for random variables: and are independent if and only if
Every cell must equal its row margin times its column margin. One cell that fails is enough to conclude that and are dependent. To prove independence, you have to check all cells.
Checking A Table
The joint distribution below has margins , , and , , , .
The whole first row matches (, ), and so does the first cell of the second row. But while . So and are not independent. The first few matching cells prove nothing.
Building An Independent Table
To construct an independent joint distribution yourself, pick any two marginal distributions and fill every cell with the product of its margins. Rolling two dice is the standard example: every cell is .
Computing Tables In Code
With raw survey data, pandas builds the joint table and margins in one call. normalize="all" divides every count by the total, which turns counts into joint probabilities.
import pandas as pd
df = pd.DataFrame({
"housing": ["yes"] * 21 + ["no"] * 30,
"smoker": ["yes"] * 5 + ["no"] * 16 + ["yes"] * 8 + ["no"] * 22,
})
joint = pd.crosstab(df["housing"], df["smoker"], normalize="all", margins=True)
print(joint.round(3))
# conditional distribution of smoking within each housing row
print(pd.crosstab(df["housing"], df["smoker"], normalize="index"))Comparing Groups
Joint tables answer questions like "are female students more careful cyclists than male students?". The key is to compare conditional probabilities (relative frequencies within each group), never raw counts.
Bicycle Accidents
= number of bicycle accidents, = gender:
| male | female | ||
|---|---|---|---|
| 0 accidents | |||
| 1 accident | |||
| 2 accidents | |||
| 3 accidents | |||
The raw counts for one accident (5 men, 6 women) suggest women have more accidents. But there are only 13 men and 38 women, so condition on the column:
| male | female | |
|---|---|---|
Relative to their group size, the women in this sample had clearly fewer accidents.
Small Groups Are Fragile
Be careful with conclusions from small groups. One extra or one fewer man in a cell changes the male percentage by , almost 8 percentage points. For women it changes by only , about 3 points. Percentages computed from small groups jump around a lot, which is the intuition behind statistical uncertainty and confidence intervals.
Motivation
A second question from the same survey: do students study because it is necessary for their future, or because they enjoy it?
| male | female | ||
|---|---|---|---|
| necessary | |||
| enjoy it | |||
versus . In this group the answers depend strongly on gender, so the two variables are clearly not independent.
Worked Exercises
These come from the joint distribution exercises in the book.
Motivation And Housing
| housing | not in housing | ||
|---|---|---|---|
| must | |||
| enjoys studying | |||
and . The conditional probabilities differ, so motivation and housing are related in this data.
Vaccine Side Effects
77 staff members: 15 men and 6 women report side effects, 40 men and 16 women do not. The margins are 55 men, 22 women, 21 with and 56 without side effects.
, , . All three are equal, so gender and side effects are independent here. Checking every cell confirms it, for example .
Given that someone had side effects, the probability that it is a woman is , simply the share of women in the population, as independence predicts.
Quick Recap
- A random variable assigns a number to each outcome; its value is determined by chance.
- Its distribution lists every value with its probability: first the values, then the probabilities. They sum to .
- A joint distribution tabulates for every pair; the comma means and.
- Marginal distributions are the row and column sums.
- Conditional from a table: divide the cell by its row total (condition on ) or column total (condition on ).
- Independent means every cell equals row margin column margin; one failing cell means dependent.
- Compare groups with conditional percentages, not raw counts, and distrust percentages from small groups.