TExcelTAD
Home › Probability Theory › Joint Distributions

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 XX and YY, the joint distribution is a table p(x,y)=Pr⁡[X=x,Y=y]p(x, y) = \Pr[X = x, Y = y] of non-negative numbers that sum to 1. Any probability about the pair is a sum over cells:

Pr⁡[(X,Y)∈A]=∑(x,y)∈Ap(x,y)\Pr[(X, Y) \in A] = \sum_{(x, y) \in A} p(x, y)

Example: roll two dice; let XX be the smaller and YY the larger value.

python · try it yourself
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 1/361/36 (both dice equal); off-diagonal cells above it have 2/362/36 (two orders); cells below the diagonal are impossible.

Continuous joint densities

XX and YY have a joint density f(x,y)≥0f(x, y) \ge 0 with total integral 1 if

Pr⁡[(X,Y)∈A]=∬Af(x,y) dx dy\Pr[(X, Y) \in A] = \iint_A f(x, y)\,dx\,dy

The uniform density on a region is constant: probabilities are areas, as in the meeting problem. Example: f(x,y)=x+yf(x, y) = x + y on the unit square. Then Pr⁡[X+Y≤1]=∫01∫01−x(x+y) dy dx=13\Pr[X + Y \le 1] = \int_0^1 \int_0^{1 - x} (x + y)\,dy\,dx = \frac{1}{3}.

python · try it yourself
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 ρ\rho can be built from independent standard normals Z1,Z2Z_1, Z_2:

X=Z1,Y=ρZ1+1−ρ2 Z2X = Z_1, \qquad Y = \rho Z_1 + \sqrt{1 - \rho^2}\, Z_2

In general, X=μ+LZ\mathbf{X} = \boldsymbol{\mu} + L\mathbf{Z} with LLT=ΣLL^T = \Sigma (a Cholesky factor of the covariance matrix) has mean μ\boldsymbol{\mu} and covariance Σ\Sigma. This is how libraries sample correlated normals.

python · try it yourself
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

p(0,0)=0.2p(0, 0) = 0.2, p(0,1)=0.3p(0, 1) = 0.3, p(1,0)=0.1p(1, 0) = 0.1, p(1,1)=0.4p(1, 1) = 0.4. Find Pr⁡[X=Y]\Pr[X = Y] and Pr⁡[X=1]\Pr[X = 1].

Answer

0.60.6 and 0.50.5.

Solution

Pr⁡[X=Y]=0.2+0.4\Pr[X = Y] = 0.2 + 0.4; Pr⁡[X=1]=0.1+0.4\Pr[X = 1] = 0.1 + 0.4.

Exercise 2: A joint density constant

f(x,y)=c xyf(x, y) = c\,xy on the unit square. Find cc and Pr⁡[X<1/2]\Pr[X < 1/2].

Answer

c=4c = 4; 1/41/4.

Solution

∫01∫01xy=1/4\int_0^1\int_0^1 xy = 1/4, so c=4c = 4. Pr⁡[X<1/2]=∫01/22x dx⋅∫012y dy=1/4\Pr[X < 1/2] = \int_0^{1/2} 2x\,dx \cdot \int_0^1 2y\,dy = 1/4.

Exercise 3: Correlated normals (code)

Generate correlated standard normals with ρ=−0.8\rho = -0.8 using the formula above, and estimate Pr⁡[X>0,Y>0]\Pr[X > 0, Y > 0]. The exact value is 14+arcsin⁡ρ2π\frac{1}{4} + \frac{\arcsin \rho}{2\pi}.

Answer

About 0.1024.

Solution
python · try it yourself
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 f(x,y)f(x, y), P((X,Y)∈A)P((X, Y) \in A) is…

  1. ff evaluated at the centre of AA
  2. a double integral of ff over AA
  3. the area of AA
  4. max⁡Af\max_A f

How do libraries sample a multivariate normal with covariance Σ\Sigma?

  1. Sample each coordinate independently
  2. Multiply standard normals by a matrix LL with LLT=ΣLL^T = \Sigma
  3. Use rejection sampling
  4. Sort uniform numbers