Skip to content

Random Variables And Joint Distributions

A stochastic variable (or random variable) is a variable whose value is determined by chance. Roll a die and let XX be the number of pips: before the roll you do not know XX, 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 XX lists the probability of every value XX can take. Building one is always a two step job:

  1. Determine which values XX can take.
  2. Determine how the probability is divided over those values.

For "roll a fair die", with XX the number of pips:

kk112233445566
P(X=k)P(X = k)16\frac{1}{6}16\frac{1}{6}16\frac{1}{6}16\frac{1}{6}16\frac{1}{6}16\frac{1}{6}

The probabilities in a distribution always add up to 11, because XX 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 nn uniformly from {100,200,300,400,500}\{100, 200, 300, 400, 500\} for an algorithm with running time proportional to n2n^2, and let Y=n2Y = n^2 (the running time with the constant factor left out). YY inherits its probabilities from nn:

YY10 00010\,00040 00040\,00090 00090\,000160 000160\,000250 000250\,000
P(Y=y)P(Y = y)15\frac{1}{5}15\frac{1}{5}15\frac{1}{5}15\frac{1}{5}15\frac{1}{5}

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, XX is "does the student live in student housing?" and YY is "does the student smoke?". Every student gives a pair of answers (xi,yi)(x_i, y_i), 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 P(X=housing,Y=smoker)P(X = \text{housing}, Y = \text{smoker}), is a joint probability (the comma means and). The table of all joint probabilities is the joint probability distribution of XX and YY:

X∖YX \setminus Ysmokernon-smoker
student housing551\frac{5}{51}1651\frac{16}{51}
not in housing851\frac{8}{51}2251\frac{22}{51}

Interpretation: 5 of the 51 students live in student housing and smoke, so a randomly chosen student has about a 10%10\% 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 XX on its own, the column sums the distribution of YY on its own. That is where the name marginal distribution comes from.

Joint distribution with margins

X∖YX \setminus Ysmokernon-smokerP(X=x)P(X = x)
student housing551\frac{5}{51}1651\frac{16}{51}2151\frac{21}{51}
not in housing851\frac{8}{51}2251\frac{22}{51}3051\frac{30}{51}
P(Y=y)P(Y = y)1351\frac{13}{51}3851\frac{38}{51}5151\frac{51}{51}

So about 2151≈41%\frac{21}{51} \approx 41\% of the students live in student housing. In general, if YY takes values y1,…,ymy_1, \dots, y_m and XX takes x1,…,xnx_1, \dots, x_n:

P(X=xi)=∑j=1mP(X=xi,Y=yj)P(Y=yj)=∑i=1nP(X=xi,Y=yj)P(X = x_i) = \sum_{j=1}^{m} P(X = x_i, Y = y_j) \qquad\qquad P(Y = y_j) = \sum_{i=1}^{n} P(X = x_i, Y = y_j)

This works because the rows split the event "X=xiX = x_i" into mutually exclusive pieces, one per value of YY, 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): housing and smokerhousing=521≈24%\frac{\text{housing and smoker}}{\text{housing}} = \frac{5}{21} \approx 24\%.

With probabilities only (the definition of conditional probability):

P(smoker∣housing)=P(housing∩smoker)P(housing)=5/5121/51=521P(\text{smoker} \mid \text{housing}) = \frac{P(\text{housing} \cap \text{smoker})}{P(\text{housing})} = \frac{5/51}{21/51} = \frac{5}{21}

The rule of thumb: a condition on XX means divide by a row total, a condition on YY means divide by a column total.


Independent Random Variables

For events, independence means P(A∩B)=P(A)⋅P(B)P(A \cap B) = P(A) \cdot P(B). Applied to every cell of the table, this gives the definition for random variables: XX and YY are independent if and only if

P(X=xi,Y=yj)=P(X=xi)⋅P(Y=yj)for every i and jP(X = x_i, Y = y_j) = P(X = x_i) \cdot P(Y = y_j) \quad \text{for every } i \text{ and } j

Every cell must equal its row margin times its column margin. One cell that fails is enough to conclude that XX and YY are dependent. To prove independence, you have to check all cells.

Checking A Table

The joint distribution below has margins P(X=2)=0.20P(X = 2) = 0.20, P(X=5)=0.50P(X = 5) = 0.50, P(X=7)=0.30P(X = 7) = 0.30 and P(Y=1)=0.30P(Y = 1) = 0.30, P(Y=2)=0.30P(Y = 2) = 0.30, P(Y=3)=0.20P(Y = 3) = 0.20, P(Y=4)=0.20P(Y = 4) = 0.20.

X∖YX \setminus Y11223344
220.060.060.060.060.040.040.040.04
550.150.150.120.120.100.100.130.13
770.090.090.120.120.060.060.030.03

The whole first row matches (0.20⋅0.30=0.060.20 \cdot 0.30 = 0.06, 0.20⋅0.20=0.040.20 \cdot 0.20 = 0.04), and so does the first cell of the second row. But P(X=5,Y=2)=0.12P(X = 5, Y = 2) = 0.12 while 0.50⋅0.30=0.150.50 \cdot 0.30 = 0.15. So XX and YY 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 16⋅16=136\frac{1}{6} \cdot \frac{1}{6} = \frac{1}{36}.

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.

python
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

XX = number of bicycle accidents, YY = gender:

X∖YX \setminus YmalefemaleP(X=x)P(X = x)
0 accidents651\frac{6}{51}2951\frac{29}{51}3551\frac{35}{51}
1 accident551\frac{5}{51}651\frac{6}{51}1151\frac{11}{51}
2 accidents151\frac{1}{51}351\frac{3}{51}451\frac{4}{51}
3 accidents151\frac{1}{51}00151\frac{1}{51}
P(Y=y)P(Y = y)1351\frac{13}{51}3851\frac{38}{51}5151\frac{51}{51}

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:

malefemale
P(0 accidents∣gender)P(0 \text{ accidents} \mid \text{gender})613≈46%\frac{6}{13} \approx 46\%2938≈76%\frac{29}{38} \approx 76\%
P(1 accident∣gender)P(1 \text{ accident} \mid \text{gender})513≈38.5%\frac{5}{13} \approx 38.5\%638≈15.8%\frac{6}{38} \approx 15.8\%

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 113\frac{1}{13}, almost 8 percentage points. For women it changes by only 138\frac{1}{38}, 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?

X∖YX \setminus YmalefemaleP(X=x)P(X = x)
necessary851\frac{8}{51}451\frac{4}{51}1251\frac{12}{51}
enjoy it551\frac{5}{51}3451\frac{34}{51}3951\frac{39}{51}
P(Y=y)P(Y = y)1351\frac{13}{51}3851\frac{38}{51}5151\frac{51}{51}

P(enjoy∣male)=513≈38%P(\text{enjoy} \mid \text{male}) = \frac{5}{13} \approx 38\% versus P(enjoy∣female)=3438≈89%P(\text{enjoy} \mid \text{female}) = \frac{34}{38} \approx 89\%. 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

X∖YX \setminus Yhousingnot in housingP(X=x)P(X = x)
must13%13\%36%36\%49%49\%
enjoys studying21%21\%30%30\%51%51\%
P(Y=y)P(Y = y)34%34\%66%66\%100%100\%

P(enjoys∣housing)=2134≈61.8%P(\text{enjoys} \mid \text{housing}) = \frac{21}{34} \approx 61.8\% and P(enjoys∣not in housing)=3066≈45.5%P(\text{enjoys} \mid \text{not in housing}) = \frac{30}{66} \approx 45.5\%. 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.

P(side effects)=2177=311P(\text{side effects}) = \frac{21}{77} = \frac{3}{11}, P(side effects∣man)=1555=311P(\text{side effects} \mid \text{man}) = \frac{15}{55} = \frac{3}{11}, P(side effects∣woman)=622=311P(\text{side effects} \mid \text{woman}) = \frac{6}{22} = \frac{3}{11}. All three are equal, so gender and side effects are independent here. Checking every cell confirms it, for example 1577=5577⋅2177\frac{15}{77} = \frac{55}{77} \cdot \frac{21}{77}.

Given that someone had side effects, the probability that it is a woman is 621=27≈28.6%\frac{6}{21} = \frac{2}{7} \approx 28.6\%, 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 11.
  • A joint distribution tabulates P(X=xi,Y=yj)P(X = x_i, Y = y_j) 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 XX) or column total (condition on YY).
  • Independent means every cell equals row margin ×\times column margin; one failing cell means dependent.
  • Compare groups with conditional percentages, not raw counts, and distrust percentages from small groups.