Marginals and Independence
From a joint distribution you can always recover the distribution of each variable alone, its marginal. The reverse is not possible in general: the marginals do not determine the joint distribution, unless the variables are independent.
Marginal distributions
Sum (or integrate) out the other variable:
The name comes from writing the row and column sums in the margins of the joint table.
import numpy as np
# joint table: rows = weather (sun, cloud, rain), columns = commute (bike, bus, car)
P = np.array([[0.30, 0.08, 0.02],
[0.10, 0.15, 0.05],
[0.02, 0.13, 0.15]])
weather = ["sun", "cloud", "rain"]
commute = ["bike", "bus", "car"]
print("P(weather):", dict(zip(weather, P.sum(axis=1).round(2).tolist())))
print("P(commute):", dict(zip(commute, P.sum(axis=0).round(2).tolist())))
indep = np.outer(P.sum(axis=1), P.sum(axis=0))
print("independent? ", np.allclose(P, indep))
print("P(bike | rain) =", round(P[2, 0] / P[2].sum(), 3), " vs P(bike) =", round(P[:, 0].sum(), 3))P(weather): {'sun': 0.4, 'cloud': 0.3, 'rain': 0.3}
P(commute): {'bike': 0.42, 'bus': 0.36, 'car': 0.22}
independent? False
P(bike | rain) = 0.067 vs P(bike) = 0.42
Independence of random variables
and are independent exactly when the joint distribution is the product of the marginals:
For densities, a quick test: if factors as and the region where is a rectangle, the variables are independent. The density on the unit square is independent; on the triangle is constant, but the triangle is not a rectangle: knowing forces .
from sympy import symbols, integrate, simplify
x, y = symbols("x y", nonnegative=True)
for name, f, region in [("4xy on square", 4 * x * y, ((y, 0, 1), (x, 0, 1))),
("2 on triangle x <= y", 2 + 0 * x, None)]:
if region:
fx = integrate(f, (y, 0, 1))
fy = integrate(f, (x, 0, 1))
print(name, "| f_X =", fx, " f_Y =", fy, " product equals f:", simplify(fx * fy - f) == 0)
else:
fx = integrate(f, (y, x, 1)) # y runs from x to 1
fy = integrate(f, (x, 0, y)) # x runs from 0 to y
print(name, "| f_X =", fx, " f_Y =", fy, " product equals f:", simplify(fx * fy - f) == 0)4xy on square | f_X = 2*x f_Y = 2*y product equals f: True
2 on triangle x <= y | f_X = 2 - 2*x f_Y = 2*y product equals f: False
Same marginals, different joints
Two fair coins can be independent, always equal, or always opposite: three different joint distributions with identical marginals. This is why a dataset's column summaries say nothing about how the columns relate, and why a model that matches each feature's distribution can still miss the structure that matters. (Mathematically, the joint distributions with given marginals are described by copulas.)
import numpy as np
joints = {
"independent": np.array([[0.25, 0.25], [0.25, 0.25]]),
"always equal": np.array([[0.5, 0.0], [0.0, 0.5]]),
"always opposite": np.array([[0.0, 0.5], [0.5, 0.0]]),
}
for name, J in joints.items():
print(f"{name:16s} marginals {J.sum(axis=1).tolist()} {J.sum(axis=0).tolist()} P(X = Y) = {J[0, 0] + J[1, 1]}")independent marginals [0.5, 0.5] [0.5, 0.5] P(X = Y) = 0.5
always equal marginals [0.5, 0.5] [0.5, 0.5] P(X = Y) = 1.0
always opposite marginals [0.5, 0.5] [0.5, 0.5] P(X = Y) = 0.0
Functions of independent variables
If and are independent, so are and for any functions . And for independent variables, , which gives zero covariance and the variance sum rule. These facts are used constantly, often silently.
In software
Independence assumptions are built into many systems: naive Bayes assumes features are independent given the class (Naive Bayes Classifiers); query optimisers assume predicates on different columns are independent when estimating result sizes, and their estimates go badly wrong on correlated columns (city and zip code). Synthetic data and privacy tools must decide which joint structure to keep.
Exercises
Exercise 1: Marginals from a table
From the weather and commute table above, what is and ?
Answer
and .
Solution
Column sum: . Row sun sums to 0.4, so .
Exercise 2: A density test
Are and independent if for ?
Answer
Yes: is exponential with rate 2 and exponential with rate 3.
Solution
on the quadrant, which is a product region, so the factorisation shows independence.
Exercise 3: Checking a table (code)
Build the joint table of = first die and = (sum of two dice is even). Are they independent?
Answer
Yes: whatever the first die, the sum is even with probability .
Solution
from fractions import Fraction
from itertools import product
joint = {}
for a, b in product(range(1, 7), repeat=2):
key = (a, (a + b) % 2 == 0)
joint[key] = joint.get(key, 0) + Fraction(1, 36)
pX = {a: sum(v for (x, e), v in joint.items() if x == a) for a in range(1, 7)}
pY = {e: sum(v for (x, ee), v in joint.items() if ee == e) for e in (True, False)}
print(all(joint[(a, e)] == pX[a] * pY[e] for a in range(1, 7) for e in (True, False)))True
Test yourself
How do you get the marginal distribution of from a joint table?
- Take the diagonal
- Sum each row over all values of
- Multiply rows and columns
- Take the maximum of each row
The density on the triangle describes…
- independent variables
- dependent variables
- uniform marginals
- an invalid density