Skip to content
BIASED

Joint distributions and independence

Joint pmfs and densities, marginal and conditional distributions, independence of random variables, covariance and correlation, averaging correlated estimates, and convolution for the sum of independent variables.

Two variables at once

The topic Expectation and variance summarized one random variable with a few numbers. Linearity already let us add expectations, E[X+Y]=E[X]+E[Y]\mathbb E[X + Y] = \mathbb E[X] + \mathbb E[Y], without asking how XX and YY are related. Variance is less forgiving. Whether two errors tend to strike together decides how much their sum varies. To see that, we need to describe several variables at once.

Roll two fair dice. Let XX be the first die and YY the larger of the two. The joint distribution of (X,Y)(X, Y) gives the probability of every pair of values. For discrete variables it is the joint pmf

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

a table with one cell per pair of values. The cells are nonnegative and sum to 1.

Each of the 36 equally likely rolls (a,b)(a, b) lands in the cell (a,max⁡(a,b))(a, \max(a, b)). A cell (x,y)(x, y) with y>xy > x gets one roll, the one where the second die shows yy, so it holds 1/361/36. A diagonal cell (x,x)(x, x) gets every roll where the second die shows at most xx, so it holds x/36x/36. A cell with y<xy < x gets nothing.

For continuous variables the table becomes a joint density pX,Y(x,y)≥0p_{X,Y}(x, y) \ge 0 on the plane, with total volume 1 under its surface. Probabilities are volumes over a region AA:

P((X,Y)∈A)=∬ApX,Y(x,y) dx dy.P\big((X, Y) \in A\big) = \iint_A p_{X,Y}(x, y)\, dx\, dy.

A double integral is computed one variable at a time: integrate over yy with xx fixed, then over xx. As in one dimension, a density is not a probability and can exceed 1. A point uniform in the square [0,0.5]2[0, 0.5]^2 has density 4 there, since the square's area is 1/41/4.

Marginal distributions

The joint table holds each variable's own distribution. To get P(X=x)P(X = x), add up row xx. The events {X=x,Y=y}\{X = x, Y = y\} for different yy are disjoint and together make up {X=x}\{X = x\}. This is the law of total probability from Conditional probability and Bayes' formula, applied to every value at once:

pX(x)=∑ypX,Y(x,y),pY(y)=∑xpX,Y(x,y).p_X(x) = \sum_y p_{X,Y}(x, y), \qquad p_Y(y) = \sum_x p_{X,Y}(x, y).

These are the marginal distributions, named after the sums written in the margins of the table. Here is the dice table in units of 1/361/36.

y=1y = 1 22 33 44 55 66 row sum
x=1x = 1 1 1 1 1 1 1 6
x=2x = 2 0 2 1 1 1 1 6
x=3x = 3 0 0 3 1 1 1 6
x=4x = 4 0 0 0 4 1 1 6
x=5x = 5 0 0 0 0 5 1 6
x=6x = 6 0 0 0 0 0 6 6
column sum 1 3 5 7 9 11 36

Every row sums to 6/366/36: the first die is fair. The columns give pY(y)=(2y−1)/36p_Y(y) = (2y - 1)/36, so the maximum leans toward large values.

For densities, sums become integrals: pX(x)=∫pX,Y(x,y) dyp_X(x) = \int p_{X,Y}(x, y)\, dy. Take a point uniform in the unit disk, with density 1/π1/\pi inside. At a given xx the integral runs along a vertical chord of length 21−x22\sqrt{1 - x^2}, so

pX(x)=2π1−x2,−1≤x≤1.p_X(x) = \frac{2}{\pi}\sqrt{1 - x^2}, \qquad -1 \le x \le 1.

This marginal is not uniform. It is highest at the centre, where the chords are longest.

The way back is closed: marginals do not determine the joint. Many different tables share the same row and column sums. What the marginals lose is exactly how the variables go together.

Conditional distributions

Suppose we learn that X=3X = 3. Of the whole table, only row 3 stays possible, and its cells sum to pX(3)=1/6p_X(3) = 1/6, not to 1. Divide by that sum and the row becomes a distribution of its own, the conditional distribution of YY given X=xX = x:

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 P(B∣A)=P(A∩B)/P(A)P(B \mid A) = P(A \cap B) / P(A) from the topic Conditional probability and Bayes' formula, with A={X=x}A = \{X = x\} and B={Y=y}B = \{Y = y\}. Row 3 reads 0,0,3,1,1,10, 0, 3, 1, 1, 1 in units of 1/361/36. Dividing by 6/366/36 gives P(Y=3∣X=3)=1/2P(Y = 3 \mid X = 3) = 1/2 and 1/61/6 for each of Y=4,5,6Y = 4, 5, 6. Slice, then renormalize.

Read backwards, the definition is the multiplication 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).

Many models specify a joint distribution exactly this way: first draw XX, then draw YY given XX. A language model does it token by token. It never writes down the distribution of a whole sentence, only the distribution of each next token given the ones before it.

For continuous variables the same formulas hold with densities. They also close a gap left by Conditional probability and Bayes' formula. For a continuous XX the event {X=x}\{X = x\} has probability zero, so P(⋅∣X=x)P(\cdot \mid X = x) could not be defined as a ratio of probabilities. A ratio of densities is fine wherever pX(x)>0p_X(x) > 0. In the disk, fix X=xX = x: the joint density is the constant 1/π1/\pi along the chord, so given X=xX = x, YY is uniform on [−1−x2,1−x2][-\sqrt{1 - x^2}, \sqrt{1 - x^2}].

Independent variables

Sometimes learning XX tells us nothing about YY. Then every row, once renormalized, is the same distribution: pY∣X(y∣x)=pY(y)p_{Y \mid X}(y \mid x) = p_Y(y). Multiply both sides by pX(x)p_X(x) and you get the definition. Random variables XX and YY are independent if

pX,Y(x,y)=pX(x) pY(y)for all x,y,p_{X,Y}(x, y) = p_X(x)\, p_Y(y) \quad \text{for all } x, y,

with pmfs or with densities. This is independence of events from Conditional probability and Bayes' formula, demanded for every pair of values at once. Variables X1,…,XnX_1, \dots, X_n are independent when their joint distribution is the product of all nn marginals. That is the first "i" in "i.i.d.": the joint distribution of a dataset is the product of the distributions of its examples.

One cell is enough to prove dependence. In the dice table P(X=3,Y=1)=0P(X = 3, Y = 1) = 0, while pX(3) pY(1)=16⋅136≠0p_X(3)\, p_Y(1) = \tfrac16 \cdot \tfrac1{36} \ne 0. So the first die and the maximum are dependent. The coordinates of a point in the disk are dependent too: if XX is near 1, YY must be near 0. In the unit square the uniform density is 1=1⋅11 = 1 \cdot 1, a product of two uniform densities, so the coordinates are independent. A product needs a rectangular region where the density is positive, but a rectangle alone is not enough.

Independence makes expectations of products factor:

E[g(X) h(Y)]=E[g(X)] E[h(Y)].\mathbb E[g(X)\, h(Y)] = \mathbb E[g(X)]\, \mathbb E[h(Y)].

In the discrete case the sum of g(x) h(y) pX(x) pY(y)g(x)\, h(y)\, p_X(x)\, p_Y(y) over all cells splits into a product of two separate sums. In particular, E[XY]=E[X] E[Y]\mathbb E[XY] = \mathbb E[X]\, \mathbb E[Y] for independent variables. The next block needs exactly this.

Covariance and correlation

Independence is all or nothing. Often we want one number instead: do large values of XX come with large values of YY? Write μX=E[X]\mu_X = \mathbb E[X] and μY=E[Y]\mu_Y = \mathbb E[Y]. The covariance is

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

The product of deviations is positive when both variables sit on the same side of their means. So the covariance is positive when they tend to rise together and negative when one rises as the other falls. The second form comes from expanding and using linearity, just like Var⁡(X)=E[X2]−E[X]2\operatorname{Var}(X) = \mathbb E[X^2] - \mathbb E[X]^2. In fact Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X).

Covariance is symmetric and bilinear: shifts drop out, factors come out, sums split.

Cov⁡(aX+b,Y)=aCov⁡(X,Y),Cov⁡(X1+X2,Y)=Cov⁡(X1,Y)+Cov⁡(X2,Y).\operatorname{Cov}(aX + b, Y) = a \operatorname{Cov}(X, Y), \qquad \operatorname{Cov}(X_1 + X_2, Y) = \operatorname{Cov}(X_1, Y) + \operatorname{Cov}(X_2, Y).

Expanding Cov⁡(X+Y,X+Y)\operatorname{Cov}(X + Y, X + Y) this way answers the question we started with:

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

Variances add exactly when the covariance is zero, and independent variables have zero covariance because E[XY]=E[X] E[Y]\mathbb E[XY] = \mathbb E[X]\, \mathbb E[Y]. For two independent dice the variance of the sum is 2⋅35/12=35/62 \cdot 35/12 = 35/6.

Covariance carries units: measure XX in centimetres instead of metres and it grows a hundredfold. The correlation removes them:

ρ(X,Y)=Cov⁡(X,Y)σX σY∈[−1,1].\rho(X, Y) = \frac{\operatorname{Cov}(X, Y)}{\sigma_X\, \sigma_Y} \in [-1, 1].

It is undefined when either variable is constant. The bounds follow from Var⁡(X/σX±Y/σY)=2±2ρ≥0\operatorname{Var}(X/\sigma_X \pm Y/\sigma_Y) = 2 \pm 2\rho \ge 0. Equality, ρ=±1\rho = \pm 1, holds only when Y=aX+bY = aX + b exactly, with the sign of aa. For the dice, E[Y]=161/36\mathbb E[Y] = 161/36, Cov⁡(X,Y)=35/24\operatorname{Cov}(X, Y) = 35/24 and ρ≈0.61\rho \approx 0.61: a clear linear trend, far from exact.

Uncorrelated is not independent

Independent variables are uncorrelated: their covariance is zero. The converse fails. Take Z∼N(0,1)Z \sim \mathcal N(0, 1) and W=Z2W = Z^2. WW is a function of ZZ, as dependent as it gets. Yet

Cov⁡(Z,Z2)=E[Z3]−E[Z] E[Z2]=0,\operatorname{Cov}(Z, Z^2) = \mathbb E[Z^3] - \mathbb E[Z]\, \mathbb E[Z^2] = 0,

because the normal density is symmetric about zero, so E[Z]=0\mathbb E[Z] = 0 and E[Z3]=0\mathbb E[Z^3] = 0. A positive ZZ and a negative ZZ of the same size give the same Z2Z^2. The upward trend on the right and the downward trend on the left cancel.

Correlation measures only the linear part of a dependence. A point cloud on a parabola or on a ring has ρ≈0\rho \approx 0, even though one coordinate tightly constrains the other. Correlation is also fragile: a single far-away point can create a strong correlation or hide one.

Two more warnings. In Simpson's paradox the correlation in the pooled data has the opposite sign to the correlation inside every group. And correlation is not causation: ice-cream sales and drownings rise together because both rise in summer. No joint distribution of observational data, however exact, tells by itself which variable drives which.

Averaging correlated estimates

Back to the variance of a sum, now with nn terms. Bilinearity gives nn variances plus a covariance for every ordered pair i≠ji \ne j:

Var⁡(∑i=1nXi)=∑iVar⁡(Xi)+∑i≠jCov⁡(Xi,Xj).\operatorname{Var}\Big(\sum_{i=1}^n X_i\Big) = \sum_{i} \operatorname{Var}(X_i) + \sum_{i \ne j} \operatorname{Cov}(X_i, X_j).

Suppose nn models each give an estimate with variance σ2\sigma^2, and the errors of every pair have correlation ρ\rho. There are n(n−1)n(n - 1) ordered pairs, each with covariance ρσ2\rho\sigma^2. Divide the sum by nn, which divides the variance by n2n^2:

Var⁡(1n∑i=1nXi)=nσ2+n(n−1)ρσ2n2=ρσ2+(1−ρ)σ2n.\operatorname{Var}\Big(\frac1n \sum_{i=1}^n X_i\Big) = \frac{n\sigma^2 + n(n - 1)\rho\sigma^2}{n^2} = \rho\sigma^2 + \frac{(1 - \rho)\sigma^2}{n}.

With ρ=0\rho = 0 this is σ2/n\sigma^2/n: averaging nn independent estimates cuts the variance nn times. The law of large numbers will grow out of exactly this. With ρ>0\rho > 0 the second term still vanishes as nn grows, but the first one is a floor. At ρ=0.5\rho = 0.5, ten models give 0.55σ20.55\sigma^2 and a hundred give 0.505σ20.505\sigma^2.

This is why an ensemble of similar models gains little: their errors are correlated. A random forest fights the floor directly. It trains each tree on its own bootstrap sample and picks each split from a random subset of features, so the trees' errors are less correlated. The same floor hides in data. A thousand test examples from fifty users, or a test set full of near-duplicates, shrinks the error bar of a classifier's error rate far less than a thousand independent examples would.

Sums of independent variables

The variance of a sum is one number. Often we need the whole distribution of X+YX + Y. For independent XX and YY, the event {X+Y=z}\{X + Y = z\} splits into disjoint events {X=x,Y=z−x}\{X = x, Y = z - x\}, and independence turns each one into a product. The same holds for densities:

P(X+Y=z)=∑xpX(x) pY(z−x),pX+Y(z)=∫pX(x) pY(z−x) dx.P(X + Y = z) = \sum_x p_X(x)\, p_Y(z - x), \qquad p_{X + Y}(z) = \int p_X(x)\, p_Y(z - x)\, dx.

This operation on two distributions is called convolution. For two dice it gives the triangular pmf of the sum, (6−∣z−7∣)/36(6 - \lvert z - 7 \rvert)/36 for z=2,…,12z = 2, \dots, 12.

For two independent U(0,1)U(0, 1) variables the integrand equals 1 when both xx and z−xz - x lie in [0,1][0, 1], and 0 otherwise. So the density at zz is the length of the overlap:

pU1+U2(z)={z,0≤z≤1,2−z,1≤z≤2.p_{U_1 + U_2}(z) = \begin{cases} z, & 0 \le z \le 1, \\ 2 - z, & 1 \le z \le 2. \end{cases}

A triangle on [0,2][0, 2]. Convolve again and three uniforms give a curve made of parabolas. Each new term makes the shape smoother and more bell-like, a hint of the central limit theorem.

Everything in this topic works for any number of variables, but tables explode. Ten binary variables need 210=10242^{10} = 1024 cells, and a hundred need more than 103010^{30}. Models therefore need structure. The most useful family is described completely by the means and the covariances of every pair, collected into one matrix. Linear maps, marginals and conditionals keep it inside the same family. It is the multivariate Gaussian, the subject of the next topic, The multivariate Gaussian.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.