induwara.lk
induwara.lkAI · Optimal Transport

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.

By Induwara AshinsanaUpdated Jul 16, 2026
Wasserstein-1 distance
Input mode

Two or more non-negative masses. Comma, space, or newline separated. Auto-normalised to sum to 1.

Same number of bins as P, over the same shared support.

One strictly-increasing number per bin — the x-axis locations. Leave blank for unit spacing 0, 1, 2, …

Presets
Wasserstein-1 (W₁)
2.0000
Earth Mover's Distance
Quantile cross-check
2.0000
Reconciles — as it must
Mean shift E[Q] − E[P]
2.0000
W₁ ≥ |Δmean| = 2.0000
Support points
4
Sums to 1 as entered
Decimals

On average, probability mass moved 2.0000 units along the axis. Unlike KL divergence, W₁ stays finite even when the supports don't overlap, and it grows with how far the mass moved — the mean shifted to the right (up) by 2.0000.

Cross-check. The area between the CDFs gives 2.0000; the independent quantile integral gives 2.0000. They reconcile, as Fubini's theorem requires. The proven bound W₁ ≥ |E[P] − E[Q]| = 2.0000 holds.

CDF overlay — shaded area = W₁

03
F_P (baseline) F_Q (comparison) area = W₁

Per-interval transport working

IntervalF_PF_Q|ΔCDF|Δx|ΔCDF|·Δx
[0, 1]0.50000.00000.50001.00000.5000
[1, 2]1.00000.00001.00001.00001.0000
[2, 3]1.00000.50000.50001.00000.5000
W₁ total2.0000

Method: W₁ = Σ |F_P(zₖ) − F_Q(zₖ)| · Δzₖ (area between the CDFs), the exact 1-D optimal-transport cost — the same value as scipy.stats.wasserstein_distance. No data leaves this page.

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:

  1. Normalise each vector so it sums to 1 (Wasserstein-1 needs equal total mass). This lets you paste raw counts as well as probabilities.
  2. Build the cumulative masses P(i) = Σ₍ⱼ≤ᵢ₎ p̂ⱼ and Q(i) = Σ₍ⱼ≤ᵢ₎ q̂ⱼ at each support point.
  3. Sum |P(i) − Q(i)| · (x₍ᵢ₊₁₎ − xᵢ) over the intervals. With unit-spaced bins (Δx = 1) this reduces to Σ |P(i) − Q(i)|.
  4. 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

Disjoint distributions, unit bins

P = [0.5, 0.5, 0, 0], Q = [0, 0, 0.5, 0.5], support 0..3

  1. CDF P = 0.5, 1.0, 1.0 ; CDF Q = 0, 0, 0.5 (last bin excluded)
  2. i=0: |0.5 − 0| · 1 = 0.5
  3. i=1: |1.0 − 0| · 1 = 1.0
  4. i=2: |1.0 − 0.5| · 1 = 0.5
  5. W₁ = 0.5 + 1.0 + 0.5 = 2.0 — mass moved 2 bins; KL here = +∞

Raw samples, equal length (+1 shift)

A = [1, 2, 3, 4], B = [2, 3, 4, 5]

  1. Order statistics differ by exactly 1 at every rank
  2. W₁ = (1/4)(|1−2| + |2−3| + |3−4| + |4−5|)
  3. = (1/4)(1 + 1 + 1 + 1) = 1.0
  4. A uniform +1 shift ⇒ distance 1.0 (matches scipy.stats.wasserstein_distance)

Custom bins — fractional + large spacing (edge case)

support = [0, 0.5, 1000], P = [0.5, 0.5, 0], Q = [0, 0.5, 0.5]

  1. CDF P = 0.5, 1.0 ; CDF Q = 0, 0.5
  2. gap 0→0.5 (Δx = 0.5): |0.5 − 0| · 0.5 = 0.25
  3. gap 0.5→1000 (Δx = 999.5): |1.0 − 0.5| · 999.5 = 499.75
  4. W₁ = 0.25 + 499.75 = 500.0
  5. Check: the 0.5 mass at position 0 must travel 1000 → 0.5 × 1000 = 500 ✓

Frequently asked questions

Sources & references

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

Rate this tool
Be the first to rate

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.