09 / 22 · Several Random Variables
Joint distributions and independence
Several random variables at once: joint, marginal and conditional distributions in a table and as densities, independence of random variables, covariance and correlation and why zero correlation is not independence, the variance of an average of correlated estimates (why ensembles stop improving), and sums of independent variables as convolutions.
On this page 12
Data rarely come one number at a time. A patient has a blood pressure and an age, a house a price and an area, a training example a feature vector and a label, a sentence one word and the next. To ask how one of these quantities depends on another, the probabilities of all their combinations are needed: the joint distribution. This note defines it, recovers from it the distribution of each variable alone and of one variable given another, defines independence for random variables, and introduces the covariance and the correlation, the numbers that summarize how two variables move together, and do so only partly.
Notation. is a joint PMF and a joint density; , are the marginal distributions; and the conditional ones. is the covariance and the correlation.
Joint and marginal distributions#
For discrete variables the joint PMF is a table: one cell for every pair of values, the cells adding up to 1. Roll two dice and let be the first and the larger of the two. The table, its row and column sums, and one of its rows renormalized, are the three objects of this note:
Joint, marginal and conditional distributions of two dice
The distribution of alone is recovered by summing over the values of , and that of by summing over :
They are called marginal because they are written in the margins of the table. The marginals do not determine the joint distribution: variables and drawn independently with the same marginals as and have the same row and column sums and a completely different table, with no zeros in it.
For continuous variables the table becomes, when it exists, a joint density that integrates to 1 over the plane; probabilities are volumes, , and marginals integrate the other variable out, . A point uniform in the unit disk has inside the disk. Its first coordinate is not uniform: integrating over the vertical chord at gives , larger near the centre, where the chords are long. Continuous marginals do not guarantee a joint density. For a standard normal , both and are continuous, but the pair lies on a parabola, a curve of zero area, so no function on the plane integrates to its probabilities. The formulas of this note with assume that a joint density exists.
Conditional distributions#
Fixing the value of one variable and renormalizing what is left gives the distribution of the other:
Both ratios are defined where the denominator is positive, or . The continuous version is determined only up to the values of in a set of probability zero, which changes no probability computed from it. In the table, leaves row 3, and equals 3 with probability and each of 4, 5, 6 with probability . The continuous version answers the question left open by the conditional probability note: conditioning on , an event of probability zero, is defined through densities. For the point in the disk, given the coordinate is uniform on the chord . Rearranged, the definition is the chain rule for distributions, : a generative model often specifies a joint distribution exactly this way, a distribution of and a conditional of given .
Independence of random variables#
and are independent if every event about is independent of every event about , which is equivalent to the joint distribution being the product of the marginals:
or the same with densities. Equivalently, every conditional distribution of given equals the marginal of : learning says nothing about . The first die and the larger value are dependent; already , while . The coordinates of a point in the disk are dependent too: if is close to 1, must be close to 0.
For independent variables, expectations of products factorize: , since the double sum or integral splits into a product. This is the step used in the previous note to multiply moment generating functions.
Covariance and correlation#
The covariance measures whether two variables tend to be above their means together:
It exists when both variances are finite. It is positive when large values of come with large values of , negative when they come with small ones. It is linear in each argument, and . Expanding the square of a sum gives the rule promised in the previous note:
so variances add exactly when the covariance is zero, in particular for independent variables, whose covariance is .
The covariance has the units of times those of . Dividing by the standard deviations gives the unit-free correlation
defined when and , so not for a constant. It is only if exactly. For the dice, , and .
Independence implies zero correlation; the converse is false. If , then , although is a function of . The correlation sees only the linear part of a dependence:
Correlation measures only linear dependence
Two more warnings. A correlation computed from pooled groups can differ in sign from the correlation inside each group, as in Simpson’s paradox of the opening note. And correlation is not causation: ice cream sales and drownings are correlated because both rise in summer.
Averaging correlated estimates#
Covariance explains why averaging does not always help. Take estimates of the same quantity, each with variance and every pair with correlation : models in an ensemble, measurements by the same instrument, annotations by overlapping annotators. The variance of their average is
The second term vanishes as grows; the first does not. With , averaging 10 estimates leaves and averaging 100 leaves : the floor is . Ensembles of nearly identical models gain almost nothing, which is why random forests decorrelate their trees by training each on a bootstrap sample and choosing every split among a random subset of the features. The same formula is why many examples from the same user, or near-duplicates in a test set, shrink the error bar far less than their count suggests.
Sums of independent variables#
The distribution of for independent and collects every way to reach each total:
This operation is the convolution of the two distributions. Two dice give the triangular PMF of the sum; two independent give the triangular density on , with . Convolving again and again makes the shape smoother and more bell-like, which is what the central limit theorem makes precise.
What changed#
| One variable at a time | Joint distribution | |
|---|---|---|
| Describes | each variable separately | all combinations of values |
| Recovers | marginals by summing out, conditionals by renormalizing | |
| Dependence | invisible | independence: joint = product of marginals; covariance and correlation summarize the linear part |
| Variance of a sum | added, assuming independence | |
| Average of correlated estimates |
Limitations#
- Joint distributions grow fast. Ten binary variables have cells, a hundred have more than ; real models assume structure, such as conditional independence in naive Bayes or graphical models, or a parametric family such as the multivariate Gaussian.
- Correlation is a summary of linear dependence. It can be zero when one variable completely determines the other, and it is sensitive to outliers; mutual information or rank correlations capture more.
- Neither correlation nor any joint distribution reveals causation. The same joint distribution of observational data is compatible with different causal structures.
What you should remember#
- The joint distribution gives the probability of every combination of values; marginals sum or integrate the other variables out; conditionals renormalize a slice.
- and are independent if the joint distribution is the product of the marginals; then .
- ; ; .
- Independent implies uncorrelated, not the other way round: and are uncorrelated.
- The average of estimates with pairwise correlation has variance .
- The distribution of a sum of independent variables is the convolution of their distributions.
Code#
The table of the figure in exact fractions, and the other numbers by simulation:
import numpy as np
from fractions import Fraction
# two dice: X the first, Y the larger; the joint PMF as a 6 x 6 table of fractions
J = np.full((6, 6), Fraction(0), dtype=object)
for a in range(1, 7):
for b in range(1, 7):
J[a - 1, max(a, b) - 1] += Fraction(1, 36)
pX, pY = J.sum(axis=1), J.sum(axis=0)
print([str(p) for p in pX], [int(p * 36) for p in pY]) # ['1/6', ...] [1, 3, 5, 7, 9, 11]
print([str(v / pX[2]) for v in J[2]]) # ['0', '0', '1/2', '1/6', '1/6', '1/6']
# covariance and correlation of X and Y
v = np.arange(1, 7)
EX, EY = (pX * v).sum(), (pY * v).sum()
EXY = sum(J[i, j] * v[i] * v[j] for i in range(6) for j in range(6))
cov = EXY - EX * EY
varX, varY = (pX * v**2).sum() - EX**2, (pY * v**2).sum() - EY**2
print(EY, cov, round(float(cov) / float(varX * varY) ** 0.5, 3)) # 161/36 35/24 0.608
# uncorrelated but dependent: Z and Z^2
rng = np.random.default_rng(0)
z = rng.normal(size=1_000_000)
print(round(float(np.corrcoef(z, z**2)[0, 1]), 3)) # 0.0
# the average of n estimates with variance 1 and pairwise correlation rho
for rho in (0.0, 0.5, 0.9):
print(rho, [round(rho + (1 - rho) / n, 3) for n in (1, 10, 100)])
# 0.0 [1.0, 0.1, 0.01] 0.5 [1.0, 0.55, 0.505] 0.9 [1.0, 0.91, 0.901]
shared, own = rng.normal(size=(100_000, 1)), rng.normal(size=(100_000, 10))
estimates = np.sqrt(0.5) * shared + np.sqrt(0.5) * own # pairwise correlation 0.5
print(round(float(estimates.mean(axis=1).var()), 3)) # 0.547, against 0.55
# the sum of two independent uniforms has a triangular density
print(round(float((rng.random((2, 1_000_000)).sum(axis=0) <= 0.5).mean()), 4)) # 0.125
Next#
With two variables the covariance is one number; with variables it is a matrix. One family of joint distributions is completely described by its means and that matrix, keeps its form under linear maps, marginalization and conditioning, and sits under linear regression, Gaussian processes and the latent spaces of generative models: the multivariate Gaussian.
References#
- J. K. Blitzstein, J. Hwang. Introduction to Probability, 2nd ed. CRC Press, 2019. Chapter 7 (joint, marginal and conditional distributions, covariance and correlation), Chapter 8 (convolutions).
- W. Feller. An Introduction to Probability Theory and Its Applications, Vol. 1, 3rd ed. Wiley, 1968. Chapter IX (joint distributions, covariance, correlation).
- L. Breiman. Random Forests. Machine Learning, 45(1), 2001. The correlation between trees and the error of the forest.