TExcelTAD
Home › Probability Theory › Marginals and Independence

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:

pX(x)=∑yp(x,y),fX(x)=∫−∞∞f(x,y) dyp_X(x) = \sum_y p(x, y), \qquad f_X(x) = \int_{-\infty}^{\infty} f(x, y)\,dy

The name comes from writing the row and column sums in the margins of the joint table.

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

XX and YY are independent exactly when the joint distribution is the product of the marginals:

p(x,y)=pX(x) pY(y)for all x,yorf(x,y)=fX(x) fY(y)p(x, y) = p_X(x)\,p_Y(y) \quad \text{for all } x, y \qquad \text{or} \qquad f(x, y) = f_X(x)\,f_Y(y)

For densities, a quick test: if f(x,y)f(x, y) factors as g(x)h(y)g(x)h(y) and the region where f>0f > 0 is a rectangle, the variables are independent. The density f(x,y)=4xyf(x, y) = 4xy on the unit square is independent; f(x,y)=2f(x, y) = 2 on the triangle 0≤x≤y≤10 \le x \le y \le 1 is constant, but the triangle is not a rectangle: knowing Y=0.1Y = 0.1 forces X≤0.1X \le 0.1.

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

python · try it yourself
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 XX and YY are independent, so are g(X)g(X) and h(Y)h(Y) for any functions g,hg, h. And for independent variables, E[XY]=E[X]E[Y]E[XY] = E[X]E[Y], 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 Pr⁡[car]\Pr[\text{car}] and Pr⁡[car∣sun]\Pr[\text{car} \mid \text{sun}]?

Answer

0.220.22 and 0.050.05.

Solution

Column sum: 0.02+0.05+0.15=0.220.02 + 0.05 + 0.15 = 0.22. Row sun sums to 0.4, so 0.02/0.4=0.050.02/0.4 = 0.05.

Exercise 2: A density test

Are XX and YY independent if f(x,y)=6e−2x−3yf(x, y) = 6e^{-2x - 3y} for x,y≥0x, y \ge 0?

Answer

Yes: XX is exponential with rate 2 and YY exponential with rate 3.

Solution

f=(2e−2x)(3e−3y)f = (2e^{-2x})(3e^{-3y}) on the quadrant, which is a product region, so the factorisation shows independence.

Exercise 3: Checking a table (code)

Build the joint table of XX = first die and YY = (sum of two dice is even). Are they independent?

Answer

Yes: whatever the first die, the sum is even with probability 1/21/2.

Solution
python · try it yourself
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 XX from a joint table?

  1. Take the diagonal
  2. Sum each row over all values of YY
  3. Multiply rows and columns
  4. Take the maximum of each row

The density f(x,y)=2f(x, y) = 2 on the triangle 0≤x≤y≤10 \le x \le y \le 1 describes…

  1. independent variables
  2. dependent variables
  3. uniform marginals
  4. an invalid density