Comparing Distributions

⚡ Key Takeaways

Comparing distributions means quantifying how similar or different two probability distributions are, using graphical tools (Q–Q plots, ECDF overlays), summary-statistic comparisons (mean, variance, quantiles), formal hypothesis tests (two-sample t, Kolmogorov–Smirnov, Mann–Whitney), and distance/divergence measures (total variation, KL divergence, Wasserstein). Key result: different tools answer different questions — "are these the same distribution?" vs. "are their means equal?" vs. "how far apart are they?" — and choosing the wrong one is the central practical error. Applies across statistics, machine learning, econometrics, and the experimental sciences. Foundational figures: Kolmogorov, Smirnov, Mann, Whitney, and Wasserstein (1969 metric).

💡 The Core Insight

There is no single "distance" between distributions — there is a toolbox, and each tool privileges a different feature. Mean comparisons ask about the first moment; variance comparisons about the second; the Kolmogorov–Smirnov statistic about the largest vertical gap between cumulative distribution functions (CDFs); KL divergence about information loss; Wasserstein distance about the physical cost of moving probability mass. The art of comparing distributions is matching the question ("equal centers?" "same shape?" "indistinguishable laws?") to the right instrument, while respecting whether your data are paired or independent, continuous or discrete, and how heavy the tails are.

Formal Statement

Let XX and YY be random variables with cumulative distribution functions F(x)=P(X≤x)F(x) = P(X \le x) and G(y)=P(Y≤y)G(y) = P(Y \le y). We have independent samples x1,…,xnx_1,\dots,x_n from FF and y1,…,ymy_1,\dots,y_m from GG.

DefinitionEquality of Distributions

Two distributions are equal, written F=GF = G, if their CDFs agree everywhere:

1
F(t)=G(t)for all t∈R.\quad F(t) = G(t) \quad \text{for all } t \in \mathbb{R}.

Derivation hint: the CDF uniquely determines a probability law on R\mathbb{R}, so equality of CDFs is equivalent to equality of distributions; this is the strongest sense in which two distributions can "be the same."

DefinitionEmpirical Cumulative Distribution Function (ECDF)

The ECDF of a sample estimates the true CDF by the fraction of observations at or below each point:

2
F^n(t)=1n∑i=1n1{xi≤t},\quad \hat{F}_n(t) = \frac{1}{n}\sum_{i=1}^{n} \mathbf{1}\{x_i \le t\},

where 1{⋅}\mathbf{1}\{\cdot\} is the indicator function (1 if true, 0 if false). Derivation hint: F^n(t)\hat F_n(t) is an average of i.i.d. Bernoulli(F(t))(F(t)) indicators, so by the law of large numbers F^n(t)→F(t)\hat F_n(t) \to F(t); uniform convergence is the Glivenko–Cantelli theorem.

DefinitionKolmogorov–Smirnov Statistic

The two-sample KS statistic is the supremum (largest) vertical distance between the two ECDFs:

3
Dn,m=sup⁡t∈R∣F^n(t)−G^m(t)∣.\quad D_{n,m} = \sup_{t \in \mathbb{R}} \left| \hat{F}_n(t) - \hat{G}_m(t) \right|.

Derivation hint: because both ECDFs are step functions that jump only at observed data points, the supremum is attained at one of the n+mn+m sample values, making Dn,mD_{n,m} computable by scanning the pooled sorted data.

DefinitionTotal Variation Distance

For distributions with densities ff and gg (or PMFs), the total variation distance is

4
TV(F,G)=sup⁡A∣PF(A)−PG(A)∣=12∫∣f(x)−g(x)∣ dx,\quad \mathrm{TV}(F,G) = \sup_{A} \left| P_F(A) - P_G(A) \right| = \tfrac{1}{2}\int |f(x) - g(x)|\,dx,

where the supremum runs over all measurable events AA. Derivation hint: the optimal event is A∗={x:f(x)>g(x)}A^* = \{x : f(x) > g(x)\}; substituting and using ∫(f−g)=0\int(f-g)=0 over the whole space yields the 12∫∣f−g∣\tfrac12\int|f-g| form.

DefinitionKullback–Leibler Divergence

The KL divergence from GG to FF measures expected information lost when GG is used to approximate FF:

5
DKL(F ∥ G)=∫f(x) log⁡f(x)g(x) dx  ≥  0.\quad D_{\mathrm{KL}}(F \,\|\, G) = \int f(x)\,\log\frac{f(x)}{g(x)}\,dx \;\ge\; 0.

Derivation hint: nonnegativity follows from Jensen's inequality applied to the convex function −log⁡-\log, since EF[−log⁡(g/f)]≥−log⁡EF[g/f]=−log⁡1=0\mathbb{E}_F[-\log(g/f)] \ge -\log \mathbb{E}_F[g/f] = -\log 1 = 0.

DefinitionWasserstein (Earth Mover's) Distance

The pp-Wasserstein distance is the minimal cost of transporting mass from FF to GG:

6
Wp(F,G)=(inf⁡γ∈Γ(F,G)∫∣x−y∣p dγ(x,y))1/p,\quad W_p(F,G) = \left( \inf_{\gamma \in \Gamma(F,G)} \int |x-y|^p \, d\gamma(x,y) \right)^{1/p},

where Γ(F,G)\Gamma(F,G) is the set of joint distributions (couplings) with marginals FF and GG. Derivation hint: for p=1p=1 on R\mathbb{R}, the optimal coupling matches quantiles, giving the closed form W1=∫01∣F−1(u)−G−1(u)∣ du=∫−∞∞∣F(t)−G(t)∣ dtW_1 = \int_0^1 |F^{-1}(u) - G^{-1}(u)|\,du = \int_{-\infty}^{\infty}|F(t)-G(t)|\,dt.

TheoremGlivenko–Cantelli

The ECDF converges uniformly to the true CDF almost surely:

7
sup⁡t∣F^n(t)−F(t)∣→a.s.0as n→∞.\quad \sup_{t}\left|\hat{F}_n(t) - F(t)\right| \xrightarrow{\text{a.s.}} 0 \quad \text{as } n \to \infty.

Derivation hint: pointwise a.s. convergence from the SLLN plus monotonicity of CDFs lets a finite grid control the whole real line; refining the grid drives the uniform error to zero.

TheoremKolmogorov Limiting Distribution

Under F=GF = G (continuous), the scaled KS statistic has a distribution-free limit:

8
nmn+m  Dn,m→dK,P(K≤t)=1−2∑k=1∞(−1)k−1e−2k2t2.\quad \sqrt{\tfrac{nm}{n+m}}\; D_{n,m} \xrightarrow{d} K, \qquad P(K \le t) = 1 - 2\sum_{k=1}^{\infty}(-1)^{k-1}e^{-2k^2 t^2}.

Derivation hint: the empirical process n(F^n−F)\sqrt{n}(\hat F_n - F) converges to a Brownian bridge whose supremum has the Kolmogorov distribution; the result is distribution-free because the time-change t↦F(t)t \mapsto F(t) removes dependence on FF.

Why It Holds / Derivation

ProofThe KS Test Is Distribution-Free Under the Null

Claim: Under H0:F=GH_0: F = G with FF continuous, the null distribution of Dn,mD_{n,m} does not depend on the underlying FF. 1. Probability integral transform. If X∼FX \sim F with FF continuous, then U=F(X)U = F(X) is Uniform(0,1)(0,1). Apply FF to every observation in both samples. 2. ECDFs transform consistently. Since FF is monotone increasing, xi≤t  ⟺  F(xi)≤F(t)x_i \le t \iff F(x_i) \le F(t). Writing ui=F(xi)u_i = F(x_i) and s=F(t)s = F(t), the indicator 1{xi≤t}\mathbf{1}\{x_i \le t\} equals 1{ui≤s}\mathbf{1}\{u_i \le s\}. 3. The supremum is invariant. As tt ranges over R\mathbb{R}, s=F(t)s=F(t) ranges over (0,1)(0,1), so

Dn,m=sup⁡t∣F^n(t)−G^m(t)∣=sup⁡s∈(0,1)∣F^nU(s)−G^mU(s)∣,D_{n,m} = \sup_t |\hat F_n(t) - \hat G_m(t)| = \sup_{s\in(0,1)} |\hat F^U_n(s) - \hat G^U_m(s)|,

where the right side uses ECDFs of the transformed uniform samples. 4. Conclusion. Under H0H_0 both transformed samples are i.i.d. Uniform(0,1)(0,1), irrespective of the original FF. Hence the distribution of Dn,mD_{n,m} depends only on nn and mm — not on FF. ■\blacksquare

∎
ProofKL Divergence Is Nonnegative (Gibbs' Inequality)

1. Write DKL(F∥G)=EF ⁣[−log⁡g(X)f(X)]D_{\mathrm{KL}}(F\|G) = \mathbb{E}_F\!\left[-\log\frac{g(X)}{f(X)}\right] where X∼fX \sim f. 2. The function ϕ(u)=−log⁡u\phi(u) = -\log u is convex on (0,∞)(0,\infty). 3. By Jensen's inequality, EF[ϕ(g/f)]≥ϕ(EF[g/f])\mathbb{E}_F[\phi(g/f)] \ge \phi(\mathbb{E}_F[g/f]). 4. Compute the inner expectation: EF[g(X)/f(X)]=∫f⋅(g/f) dx=∫g dx=1\mathbb{E}_F[g(X)/f(X)] = \int f \cdot (g/f)\,dx = \int g\,dx = 1. 5. Therefore DKL≥ϕ(1)=−log⁡1=0D_{\mathrm{KL}} \ge \phi(1) = -\log 1 = 0, with equality iff g/f=1g/f = 1 a.e., i.e. F=GF = G. ■\blacksquare

∎
Figure
Two ECDFs and the Kolmogorov–Smirnov gap
value t CDF 1.0 0.0 D = max gap F̂ₙ(t) Ĝₘ(t)
mathematician · Andrey Kolmogorov | 1903–1987

Kolmogorov axiomatized probability theory in 1933 and, the same year, introduced the goodness-of-fit statistic that compares an ECDF to a hypothesized CDF via their maximal gap. His insight that this gap's limiting distribution is governed by the Brownian bridge — and is free of the underlying distribution — made distribution comparison rigorous and universal. Nikolai Smirnov extended the idea to the two-sample case in 1939, giving the tool used to this day for asking "do these two samples come from the same law?"

Building Intuition

🧠 Intuition

Picture each distribution as a pile of sand on a number line. Comparing means asks only where the balance point of each pile sits — you could shovel sand around wildly without moving it. Comparing variances asks how spread out each pile is. The KS statistic stacks the sand into a running cumulative wall and asks: what is the biggest height difference between the two walls anywhere? Wasserstein distance asks the most physical question of all: if I literally had to shovel one pile into the shape of the other, what is the minimum total "dirt × distance" work? Notice these can disagree dramatically: two piles with the same balance point can have a huge KS gap (different shapes), and two piles a KS test calls "barely different" can be far apart in Wasserstein if the difference lives in the tails. The choice of tool is a choice of what "different" means.

Worked Examples

ExampleTwo-Sample t-Test for Equal Means

Given: A new fertilizer (group A) and control (group B). Yields (kg/plot): A: nA=5n_A = 5, mean xˉA=52\bar x_A = 52, sd sA=4s_A = 4. B: nB=5n_B = 5, mean xˉB=47\bar x_B = 47, sd sB=3s_B = 3.

Goal: Test H0:μA=μBH_0:\mu_A = \mu_B at α=0.05\alpha = 0.05 (two-sided), assuming equal variances.

Step 1 — Pooled variance.

sp2=(nA−1)sA2+(nB−1)sB2nA+nB−2=4(16)+4(9)8=1008=12.5.s_p^2 = \frac{(n_A-1)s_A^2 + (n_B-1)s_B^2}{n_A+n_B-2} = \frac{4(16)+4(9)}{8} = \frac{100}{8} = 12.5.

Step 2 — Standard error.

SE=sp1nA+1nB=12.5 0.4=3.536×0.632=2.236.\mathrm{SE} = s_p\sqrt{\tfrac{1}{n_A}+\tfrac{1}{n_B}} = \sqrt{12.5}\,\sqrt{0.4} = 3.536 \times 0.632 = 2.236.

Step 3 — t-statistic.

t=xˉA−xˉBSE=52−472.236=2.236.t = \frac{\bar x_A - \bar x_B}{\mathrm{SE}} = \frac{52-47}{2.236} = 2.236.

Step 4 — Critical value. With df=8df = 8, t0.025,8=2.306t_{0.025,8} = 2.306.

Step 5 — Decision. Since ∣t∣=2.236<2.306|t| = 2.236 < 2.306, we fail to reject H0H_0.

t=2.24,df=8,p≈0.056>0.05  ⇒  no significant difference in mean yield (kg/plot)\boxed{t = 2.24,\quad df = 8,\quad p \approx 0.056 > 0.05 \;\Rightarrow\; \text{no significant difference in mean yield (kg/plot)}}
ExampleTwo-Sample KS Test for Whole-Distribution Difference

Given: Two small samples (sorted). A: {1,2,4,7}\{1, 2, 4, 7\} (n=4n=4). B: {3,5,6,8}\{3, 5, 6, 8\} (m=4m=4).

Goal: Test H0:FA=FBH_0: F_A = F_B using Dn,mD_{n,m}.

Step 1 — Build ECDFs over the pooled sorted points {1,2,3,4,5,6,7,8}\{1,2,3,4,5,6,7,8\}. Each step is 1/4=0.251/4 = 0.25.

ttF^A\hat F_AF^B\hat F_B∣F^A−F^B∣|\hat F_A-\hat F_B|
10.250.000.25
20.500.000.50
30.500.250.25
40.750.250.50
50.750.500.25
60.750.750.00
71.000.750.25
81.001.000.00

Step 2 — Maximum gap. D4,4=0.50D_{4,4} = 0.50 (attained at t=2t=2 and t=4t=4).

Step 3 — Critical value. For n=m=4n=m=4 at α=0.05\alpha=0.05, the exact critical value is Dcrit=3/4=0.75D_{\mathrm{crit}} = 3/4 = 0.75.

Step 4 — Decision. Since 0.50<0.750.50 < 0.75, we fail to reject H0H_0.

D4,4=0.50<0.75  ⇒  samples consistent with the same distribution (small n has weak power)\boxed{D_{4,4} = 0.50 < 0.75 \;\Rightarrow\; \text{samples consistent with the same distribution (small } n \text{ has weak power)}}

Common Misconceptions

⚠️ Common mistake

Equal means do NOT imply equal distributions. A two-sample t-test that "finds no difference" tests only the first moment. Two groups can have identical means and still differ wildly in variance, skew, or modality. Always ask which feature your test is sensitive to before declaring distributions "the same."

⚠️ Common mistake

A non-significant test is not evidence of equality. Failing to reject H0:F=GH_0:F=G means you lacked power to detect a difference, not that the distributions match. With small samples (as in the KS example above), the test can miss even large real differences. "Absence of evidence is not evidence of absence."

⚠️ Common mistake

KL divergence is not a distance. It is asymmetric, DKL(F∥G)≠DKL(G∥F)D_{\mathrm{KL}}(F\|G) \ne D_{\mathrm{KL}}(G\|F), fails the triangle inequality, and blows up to +∞+\infty whenever GG assigns zero probability where FF does not. Do not use it as if it were a metric — use TV or Wasserstein for that.

Counterexample

Identical mean and variance, totally different shape. Let F=N(0,1)F = \mathcal{N}(0,1) and let GG be the two-point mixture placing mass 12\tfrac12 at −1-1 and 12\tfrac12 at +1+1. Both have mean 00 and variance 11. A t-test and an F-test (variance test) both report "no difference." Yet GG is discrete and bimodal while FF is unimodal continuous — a KS or Wasserstein comparison detects a large gap. Moment-matching can perfectly disguise a structural difference.

Connections

connection

Comparing distributions sits at the crossroads of several areas. The KS test rests on the Empirical Distribution Function and its convergence (Glivenko–Cantelli), while its limiting law is governed by the Brownian Bridge from stochastic processes. Mean comparisons descend from the Central Limit Theorem and Hypothesis Testing. Divergence measures like KL connect to Information Theory (entropy, cross-entropy) and to Maximum Likelihood Estimation, where minimizing KL to the empirical distribution is maximizing likelihood. Wasserstein distance links to Optimal Transport and underlies modern generative modeling. Rank-based comparisons (Mann–Whitney) belong to Nonparametric Statistics.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1908 — William Gosset ("Student") publishes the t-distribution, enabling small-sample mean comparison.
- 1933 — Andrey Kolmogorov introduces the one-sample goodness-of-fit statistic based on the maximal ECDF–CDF gap.
- 1933 — Glivenko and Cantelli prove uniform convergence of the ECDF to the true CDF.
- 1939 — Nikolai Smirnov extends Kolmogorov's idea to the two-sample setting, creating the KS two-sample test.
- 1947 — Henry Mann and Donald Whitney publish the rank-based U test for stochastic ordering of two distributions.
- 1948 — Claude Shannon's information theory provides the conceptual home for cross-entropy and (soon after) KL divergence.
- 1951 — Solomon Kullback and Richard Leibler formalize the directed divergence between distributions.
- 1969 — Leonid Vaserstein (Wasserstein) introduces the metric, rooted in Monge–Kantorovich optimal transport, that now bears his name.
- 2017 — Arjovsky, Chintala, and Bottou introduce Wasserstein GANs, propelling optimal-transport distribution comparison into deep learning.

Subtleties & Limits

open-problem

Several deep issues remain practically and theoretically active. (1) High-dimensional comparison: the KS test has no canonical multivariate generalization, and estimating Wasserstein or KL distances in high dimensions suffers a curse of dimensionality (sample requirements grow exponentially). Entropic regularization (Sinkhorn distances) and kernel methods (Maximum Mean Discrepancy) are partial answers, but optimal sample complexity is still studied. (2) Calibrated equivalence testing: standard tests are built to detect differences, not to certify similarity; rigorous "two one-sided test" (TOST) equivalence frameworks exist but choosing the equivalence margin remains a modeling judgment. (3) Distribution-free guarantees under dependence: most theory assumes i.i.d. data, yet real time-series and networked data violate this, and the limiting distributions of comparison statistics under general dependence are incompletely characterized.

See Also

Empirical Distribution Function · Hypothesis Testing · Kolmogorov–Smirnov Test · Mann–Whitney U Test · Optimal Transport · Information Theory · Brownian Bridge · Goodness-of-Fit Testing · Central Limit Theorem