Wasserstein Distance Calculator (Earth Mover's Distance)
Compute the Wasserstein-1 distance — the Earth Mover's Distance — between two 1-D distributions or raw samples. It uses the exact CDF-area formula, shows the shaded area that equals the distance, and stays finite where KL divergence blows up. All in your browser, matched to SciPy.
How it works
The Wasserstein-1 distance measures the cost of the optimal transport plan that turns one distribution into another — the minimum of (mass moved) × (distance moved), summed over every unit of probability. It comes from the Kantorovich–Rubinstein formulation of optimal transport, and in the vision literature the same quantity is Rubner, Tomasi & Guibas's Earth Mover's Distance. In one dimension the general linear program collapses to an exact closed form based on the cumulative distribution functions (CDFs):
W₁(P, Q) = ∫ |F_P(x) − F_Q(x)| dx
For a shared, ordered support x₁ < x₂ < … < xₙ with normalised masses, the integral becomes a finite sum over the gaps between support points:
- Normalise each vector so it sums to 1 (Wasserstein-1 needs equal total mass). This lets you paste raw counts as well as probabilities.
- Build the cumulative masses P(i) = Σ₍ⱼ≤ᵢ₎ p̂ⱼ and Q(i) = Σ₍ⱼ≤ᵢ₎ q̂ⱼ at each support point.
- Sum |P(i) − Q(i)| · (x₍ᵢ₊₁₎ − xᵢ) over the intervals. With unit-spaced bins (Δx = 1) this reduces to Σ |P(i) − Q(i)|.
- For raw samples, give every value mass 1/n and apply the same integral over the pooled sorted support. When the two samples are equal length it equals the order-statistic mean, (1/n) Σ |A₍ᵢ₎ − B₍ᵢ₎|.
The tool verifies the answer a second, independent way. The same distance equals the integral of the gap between the inverse CDFs (the quantile functions), W₁ = ∫₀¹ |F_P⁻¹(u) − F_Q⁻¹(u)| du— the comonotonic transport plan. The two integrals are equal by Fubini's theorem, so the calculator computes both and shows them reconciling. A third built-in signal is the bound W₁ ≥ |E[P] − E[Q]|: the mean can never move further than the total transport. Unlike KL divergence, W₁ is a true metric — symmetric and satisfying the triangle inequality — and it stays finite even when the two supports do not overlap, which is exactly why it is the drift metric of choice in model monitoring and Wasserstein GANs.
Worked examples
Frequently asked questions
Sources & references
- Wasserstein metric — definition and the 1-D CDF closed form (Kantorovich–Rubinstein)
- Rubner, Tomasi & Guibas (1998), “A Metric for Distributions…” (Earth Mover's Distance)
- Arjovsky, Chintala & Bottou (2017), “Wasserstein GAN”, arXiv:1701.07875
- SciPy — scipy.stats.wasserstein_distance (reference 1-D implementation)
The formula and worked examples on this page were last reconciled against SciPy on 2026-07-16. The closed form is a mathematical identity, not a published rate, so it does not change over time — the date records when the numbers were re-checked.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.