AI Knowledge Tree

Joint, marginal, and conditional distributions

Work with joint distributions, marginalize, condition, and define independence.

⏱ 32 minPrerequisites:Random variables and distributions

Most interesting questions involve more than one quantity at once: a pixel and its neighbor, a feature and a label, today's price and tomorrow's. The joint distribution describes how two random variables vary together, and from it come the two operations at the heart of probabilistic modeling: marginalizing and conditioning[1].

Joint distribution

For two discrete random variables X,YX, Y, the joint PMF is

pX,Y(x,y)=P(X=x, Y=y),p_{X,Y}(x, y) = P(X = x,\ Y = y),

the probability that both take their stated values. It is nonnegative and sums to 1 over all pairs. The continuous analogue is a joint PDF fX,Y(x,y)f_{X,Y}(x, y) with ∬fX,Y=1\iint f_{X,Y} = 1, where probabilities come from integrating over a region.

Marginalization

To recover the distribution of one variable alone, sum (or integrate) the joint over the other. The marginal PMF of XX is

pX(x)=∑ypX,Y(x,y).p_X(x) = \sum_y p_{X,Y}(x, y).

Why. The event {X=x}\{X = x\} is the disjoint union over all yy of the events {X=x,Y=y}\{X = x, Y = y\}. Countable additivity turns that union into the sum above. Marginalizing is summing out the variables you do not care about.

Conditional distribution

The conditional PMF of YY given X=xX = x rescales the slice of the joint at X=xX = x so it sums to 1:

pY∣X(y∣x)=pX,Y(x,y)pX(x),pX(x)>0.p_{Y \mid X}(y \mid x) = \frac{p_{X,Y}(x, y)}{p_X(x)}, \qquad p_X(x) > 0.

This is the definition P(A∣B)=P(A∩B)/P(B)P(A \mid B) = P(A \cap B)/P(B) written for random variables. Conditioning answers "given that I have observed X=xX = x, how is YY distributed?" The denominator pX(x)p_X(x) is exactly the marginal, which is why it is the normalizing constant of the slice.

Rearranged, the definition gives the chain rule for distributions:

pX,Y(x,y)=pX(x) pY∣X(y∣x).p_{X,Y}(x, y) = p_X(x)\, p_{Y \mid X}(y \mid x).

A joint factors into a marginal times a conditional, the basic move behind every generative model.

Independence

XX and YY are independent if the joint factors into the product of marginals for all values[2]:

pX,Y(x,y)=pX(x) pY(y).p_{X,Y}(x, y) = p_X(x)\, p_Y(y).

Equivalently, pY∣X(y∣x)=pY(y)p_{Y \mid X}(y \mid x) = p_Y(y): knowing XX tells you nothing about YY. Independence is a strong assumption, and the factorization is what makes likelihoods of independent samples multiply.

Covariance

The covariance measures the linear co-movement of two variables:

Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY.\operatorname{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)] = \mathbb{E}[XY] - \mu_X \mu_Y.

The second form follows by expanding and using linearity, exactly as in the single-variable variance formula. Positive covariance means they tend to move together; negative means opposite; and independence implies zero covariance (since then E[XY]=E[X]E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y]). The converse fails: zero covariance does not imply independence, because covariance only sees linear structure.

That last point names a confusion worth pinning to a concrete case, since "zero covariance" and "independent" are routinely treated as the same thing. Take XX uniform on {−1,0,1}\{-1, 0, 1\} and set Y=X2Y = X^2. Here YY is a deterministic function of XX (the strongest possible dependence: observe XX and you know YY exactly), yet E[X]=0\mathbb{E}[X] = 0, E[XY]=E[X3]=0\mathbb{E}[XY] = \mathbb{E}[X^3] = 0, so Cov⁡(X,Y)=0−0⋅23=0\operatorname{Cov}(X, Y) = 0 - 0 \cdot \tfrac{2}{3} = 0. The covariance is blind to this dependence because the dependence is curved, not linear: XX and −X-X contribute opposite-signed cross terms that cancel. Zero covariance rules out linear co-movement, nothing more.

The variance of a sum picks up a covariance term:

Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y).\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y).

For independent variables the cross term vanishes and variances simply add.

A joint distribution couples two variables; its marginals are the projections onto each axis. Here X and Y are positively correlated.
A joint distribution couples two variables; its marginals are the projections onto each axis. Here X and Y are positively correlated.

Check it on arrays

python · numpy
import numpy as np

rng = np.random.default_rng(4)
N = 500_000

# Correlated pair via a shared component
Z = rng.normal(size=N)
X = Z + rng.normal(size=N) * 0.5
Y = Z - rng.normal(size=N) * 0.5

cov = np.mean((X - X.mean()) * (Y - Y.mean()))
print("Cov(X, Y):", round(cov, 4), "  (positive: shared Z)")

# Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y)
lhs = (X + Y).var()
rhs = X.var() + Y.var() + 2 * cov
print("Var(X+Y):", round(lhs, 4), "  decomposition:", round(rhs, 4))

# Independent pair: covariance ~ 0
U = rng.normal(size=N)
V = rng.normal(size=N)
print("Cov(indep):", round(np.mean((U - U.mean()) * (V - V.mean())), 4))

# Dependent yet zero-covariance: X uniform on {-1,0,1}, Y = X^2 (Y is a function of X)
Xd = rng.integers(-1, 2, size=N)        # -1, 0, or 1
Yd = Xd**2
print("Cov(X, X^2):", round(np.mean((Xd - Xd.mean()) * (Yd - Yd.mean())), 4), " (~0, yet dependent)")

# Marginal by summing a joint table (discrete)
joint = np.array([[0.1, 0.2], [0.3, 0.4]])   # P(X=i, Y=j)
print("P(X) marginal:", joint.sum(axis=1))   # sum over Y
print("P(Y|X=0):", joint[0] / joint[0].sum())

The covariance is positive for the shared-component pair, the variance-of-sum decomposition holds, the independent pair has near-zero covariance, the function-dependent pair (X,X2)(X, X^2) also has near-zero covariance despite being fully dependent, and summing a joint table recovers the marginal.

Exercises

Exercise 1. The joint PMF of (X,Y)(X, Y) is given by the table below, with XX the row index and YY the column index.

p(x,y)p(x, y)Y=0Y = 0Y=1Y = 1
X=0X = 00.10.10.20.2
X=1X = 10.30.30.40.4

Compute the marginal pXp_X, the conditional pY∣X(y∣1)p_{Y \mid X}(y \mid 1), and decide whether XX and YY are independent.

Solution

Step 1. Marginalize over YY by summing each row: pX(0)=0.1+0.2=0.3p_X(0) = 0.1 + 0.2 = 0.3 and pX(1)=0.3+0.4=0.7p_X(1) = 0.3 + 0.4 = 0.7.

Step 2. Condition on X=1X = 1 by rescaling that row so it sums to 1:

pY∣X(0∣1)=0.30.7=37,pY∣X(1∣1)=0.40.7=47.p_{Y \mid X}(0 \mid 1) = \frac{0.3}{0.7} = \frac{3}{7}, \qquad p_{Y \mid X}(1 \mid 1) = \frac{0.4}{0.7} = \frac{4}{7}.

Step 3. Test independence against the product of marginals. The YY marginal is pY(0)=0.1+0.3=0.4p_Y(0) = 0.1 + 0.3 = 0.4, pY(1)=0.6p_Y(1) = 0.6. Then pX(0) pY(0)=0.3⋅0.4=0.12≠0.1=p(0,0)p_X(0)\,p_Y(0) = 0.3 \cdot 0.4 = 0.12 \neq 0.1 = p(0, 0), so the joint does not factor: XX and YY are dependent.

Exercise 2. Let XX and YY be independent with Var⁡(X)=4\operatorname{Var}(X) = 4 and Var⁡(Y)=9\operatorname{Var}(Y) = 9. Compute Var⁡(2X−Y)\operatorname{Var}(2X - Y).

Solution

Step 1. Scaling a variable by a constant scales its variance by the square: Var⁡(2X)=22Var⁡(X)=4⋅4=16\operatorname{Var}(2X) = 2^2 \operatorname{Var}(X) = 4 \cdot 4 = 16, and Var⁡(−Y)=(−1)2Var⁡(Y)=9\operatorname{Var}(-Y) = (-1)^2 \operatorname{Var}(Y) = 9.

Step 2. Apply the variance-of-sum identity with the covariance term:

Var⁡(2X−Y)=Var⁡(2X)+Var⁡(−Y)+2Cov⁡(2X,−Y).\operatorname{Var}(2X - Y) = \operatorname{Var}(2X) + \operatorname{Var}(-Y) + 2\operatorname{Cov}(2X, -Y).

Step 3. Independence forces Cov⁡(2X,−Y)=0\operatorname{Cov}(2X, -Y) = 0, so the cross term drops and Var⁡(2X−Y)=16+9=25\operatorname{Var}(2X - Y) = 16 + 9 = 25.

Exercise 3 (code). Build a 3×33 \times 3 joint table by normalizing a random nonnegative matrix. Then verify on samples drawn from it that the empirical conditional p(Y∣X=0)p(Y \mid X = 0) matches the table's conditional, and that E[XY]−E[X]E[Y]\mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] recovers the covariance computed directly from the table.

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(3)
vals = np.array([0, 1, 2], dtype=float)

raw = rng.random((3, 3))
joint = raw / raw.sum()                       # P(X=i, Y=j), sums to 1

# Table conditional P(Y | X=0)
cond_table = joint[0] / joint[0].sum()

# Sample (X, Y) pairs from the flattened joint
flat = joint.ravel()
idx = rng.choice(9, size=2_000_000, p=flat)
Xs, Ys = idx // 3, idx % 3
cond_emp = np.array([np.mean(Ys[Xs == 0] == j) for j in range(3)])
assert np.allclose(cond_table, cond_emp, atol=2e-3)

# Covariance from the table vs E[XY] - E[X]E[Y]
EX = (joint.sum(axis=1) * vals).sum()
EY = (joint.sum(axis=0) * vals).sum()
EXY = sum(joint[i, j] * vals[i] * vals[j] for i in range(3) for j in range(3))
cov_table = EXY - EX * EY
cov_emp = np.mean(vals[Xs] * vals[Ys]) - vals[Xs].mean() * vals[Ys].mean()
assert np.isclose(cov_table, cov_emp, atol=5e-3)
print("cond match:", np.round(cond_table, 4))
print("cov table:", round(cov_table, 4), " sampled:", round(cov_emp, 4))

The sampled conditional matches the table row, and the two covariance routes agree, confirming both the conditioning formula and the E[XY]−μXμY\mathbb{E}[XY] - \mu_X \mu_Y form.

The next lesson turns the conditioning formula around into Bayes' rule.


References

  1. [1]Wasserman, L. (2004). All of Statistics. Springer. Ch. 2-3, joint, marginal, and conditional distributions. doi:10.1007/978-0-387-21736-9↩
  2. [2]Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. §1.2, the sum and product rules and independence.↩