Joint Distributions
Real problems involve several random quantities at once: height and weight, the latency and size of a request, the positions of two particles. A joint distribution describes them together, including how they depend on each other.
Discrete joint distributions
For discrete and , the joint distribution is a table of non-negative numbers that sum to 1. Any probability about the pair is a sum over cells:
Example: roll two dice; let be the smaller and the larger value.
from fractions import Fraction
from itertools import product
p = {}
for a, b in product(range(1, 7), repeat=2):
key = (min(a, b), max(a, b))
p[key] = p.get(key, 0) + Fraction(1, 36)
print(" y=" + "".join(f"{y:>6}" for y in range(1, 7)))
for x in range(1, 7):
print(f"x={x} " + "".join(f"{str(p.get((x, y), 0)):>6}" for y in range(1, 7)))
print("total:", sum(p.values()))
print("P(Y - X >= 3) =", sum(v for (x, y), v in p.items() if y - x >= 3)) y= 1 2 3 4 5 6
x=1 1/36 1/18 1/18 1/18 1/18 1/18
x=2 0 1/36 1/18 1/18 1/18 1/18
x=3 0 0 1/36 1/18 1/18 1/18
x=4 0 0 0 1/36 1/18 1/18
x=5 0 0 0 0 1/36 1/18
x=6 0 0 0 0 0 1/36
total: 1
P(Y - X >= 3) = 1/3
Diagonal cells have probability (both dice equal); off-diagonal cells above it have (two orders); cells below the diagonal are impossible.
Continuous joint densities
and have a joint density with total integral 1 if
The uniform density on a region is constant: probabilities are areas, as in the meeting problem. Example: on the unit square. Then .
from sympy import symbols, integrate, Rational
x, y = symbols("x y")
f = x + y
print("total:", integrate(f, (y, 0, 1), (x, 0, 1)))
print("P(X + Y <= 1):", integrate(f, (y, 0, 1 - x), (x, 0, 1)))
print("P(X < Y):", integrate(f, (y, x, 1), (x, 0, 1)))
print("E[XY]:", integrate(x * y * f, (y, 0, 1), (x, 0, 1)))total: 1
P(X + Y <= 1): 1/3
P(X < Y): 1/2
E[XY]: 1/3
The multivariate normal
The most important joint density is the bivariate normal. Two standard normals with correlation can be built from independent standard normals :
In general, with (a Cholesky factor of the covariance matrix) has mean and covariance . This is how libraries sample correlated normals.
import numpy as np
rng = np.random.default_rng(4)
Sigma = np.array([[4.0, 1.2], [1.2, 1.0]])
L = np.linalg.cholesky(Sigma)
Z = rng.standard_normal((2, 200_000))
X = np.array([[1.0], [-2.0]]) + L @ Z
print("sample means:", np.round(X.mean(axis=1), 3))
print("sample covariance:\n", np.round(np.cov(X), 3))
print("correlation:", round(float(np.corrcoef(X)[0, 1]), 3), " theory:", 1.2 / (2 * 1))sample means: [ 1.008 -2. ]
sample covariance:
[[3.997 1.195]
[1.195 0.997]]
correlation: 0.599 theory: 0.6
In software
Joint distributions are what generative models learn: an image model learns a joint distribution over millions of pixels. Probabilistic graphical models (Bayesian networks) factor a big joint distribution into small conditional pieces. Synthetic data generators must preserve joint structure, not just each column's distribution, or correlations between fields are lost.
Exercises
Exercise 1: A joint table
, , , . Find and .
Answer
and .
Solution
; .
Exercise 2: A joint density constant
on the unit square. Find and .
Answer
; .
Solution
, so . .
Exercise 3: Correlated normals (code)
Generate correlated standard normals with using the formula above, and estimate . The exact value is .
Answer
About 0.1024.
Solution
import numpy as np
rng = np.random.default_rng(6)
rho = -0.8
z1, z2 = rng.standard_normal((2, 500_000))
x, y = z1, rho * z1 + np.sqrt(1 - rho ** 2) * z2
print(round(((x > 0) & (y > 0)).mean(), 4), round(0.25 + np.arcsin(rho) / (2 * np.pi), 4))0.1024 0.1024
Test yourself
For a joint density , is…
- evaluated at the centre of
- a double integral of over
- the area of
How do libraries sample a multivariate normal with covariance ?
- Sample each coordinate independently
- Multiply standard normals by a matrix with
- Use rejection sampling
- Sort uniform numbers