research34 min studyLesson 96 in Mathematics

Optimal transport and Wasserstein distance

Compare probability measures through admissible couplings and compute a transport distance without confusing cost with a metric.

On this page

What you’ll understand

  • Construct a coupling with the correct two marginals.
  • Distinguish an arbitrary transport cost from the Wasserstein metric.
  • Compute a one-dimensional discrete example and explain what regularized solvers change.

Two probability distributions can place their mass at different locations even when they share the same total mass. Optimal transport compares them by asking how one mass allocation can be reassigned to the other while respecting both marginals. The chosen ground distance makes geometry visible in the comparison. We will solve a tiny example exactly, state the moment and metric assumptions behind Wasserstein distance, and distinguish the transport problem from a generic gradient-descent animation or a regularized numerical surrogate.

A coupling records a joint allocation

For probability measures μ and ν, a coupling is a probability measure π on pairs (x,y) whose first marginal is μ and second marginal is ν. A discrete coupling is a nonnegative matrix with prescribed row and column sums. Its entry πᵢⱼ tells how much probability mass is assigned from source xᵢ to target yⱼ. A coupling can split a source's mass across multiple destinations. Restricting to a deterministic transport map is a different problem and may exclude feasible assignments when source atoms cannot be divided into the required target masses.

After choosing a cost c(x,y), minimize its average over admissible couplings. For finite supports this is a linear program: the variables are masses, constraints conserve each marginal, and the objective is linear in those variables. The independent product coupling always supplies a feasible probability coupling, although its cost can be infinite on an unbounded space. Existence of a finite-cost optimizer in general spaces needs hypotheses such as appropriate tightness and lower semicontinuity. A colorful set of arrows is not enough; checking marginal constraints is the first certificate of admissibility.

Π(μ,ν)={π:π(⋅×X)=μ, π(X×⋅)=ν},inf⁡π∈Π(μ,ν)∫c(x,y) dπ\Pi(\mu,\nu)=\{\pi:\pi(\cdot\times X)=\mu,\ \pi(X\times\cdot)=\nu\},\qquad \inf_{\pi\in\Pi(\mu,\nu)}\int c(x,y)\,d\pi
The displayed notation assumes both measures live on the same space X. Couplings preserve the full marginal measures, not just their total masses.↔ Scroll wide equations horizontally.

The ground metric and root are part of Wasserstein distance

Let X be a complete separable metric space with distance d, and take p≥1. On probability measures with finite pth moment relative to one, hence any, base point, W_p is the pth root of the least average d(x,y)^p. In this setting it is a genuine metric. Symmetry and zero distance follow from the ground metric; the triangle inequality uses compatible couplings and an Lp inequality. Replacing d^p by an arbitrary cost or forgetting the final root can destroy the metric properties one intended to use.

On the real line, matching ordered quantiles is optimal for the convex power cost. For empirical measures with equal numbers of equally weighted atoms, sort both lists and pair corresponding entries. This is a special low-dimensional shortcut, not a general permission to pair arbitrary labeled points in higher dimensions. W₁ additionally has a dual formulation involving functions with Lipschitz constant at most one under standard metric-space hypotheses. That dual view connects transport to test functions, but a neural network critic only approximates it if the function-class and Lipschitz restrictions are controlled.

Wp(μ,ν)=(inf⁡π∈Π(μ,ν)∫d(x,y)p dπ)1/p,1≤p<∞W_p(\mu,\nu)=\left(\inf_{\pi\in\Pi(\mu,\nu)}\int d(x,y)^p\,d\pi\right)^{1/p},\qquad 1\le p<\infty
On P_p(X), the finite-pth-moment probabilities over a complete separable metric space, this formula defines a metric. An arbitrary cost need not do so.↔ Scroll wide equations horizontally.

Computation introduces a second approximation question

Entropic regularization changes the objective by adding an entropy-related penalty scaled by a positive parameter. This can enable efficient scaling algorithms for positive discrete kernels, but the resulting objective is not automatically the original Wasserstein distance. It can assign a nonzero self-cost, depending on the convention. Debiased constructions address one issue without making every finite-parameter approximation identical to unregularized transport. Separate three errors: approximating distributions by finite samples, modifying the optimization problem, and solving that modified problem only to finite numerical accuracy. Each requires its own diagnostic.

The geometric choice also matters before any algorithm runs. Changing units changes d and hence the numerical transport distance. In feature spaces, an arbitrary embedding may assign distances that fail to represent the distinctions a scientific question needs. The optimization lab below follows gradient descent on x²/2 with adjustable step size and initial value. It demonstrates optimization stability only. It has no coupling constraints, transport matrix, Wasserstein gradient flow, or entropic kernel, so use it to question the solver step rather than to infer a transport answer.

μ=1m∑i=1mδai,ν=1m∑i=1mδbi,Wpp(μ,ν)=1m∑i=1m∣a(i)−b(i)∣p\mu=\frac1m\sum_{i=1}^m\delta_{a_i},\quad\nu=\frac1m\sum_{i=1}^m\delta_{b_i},\quad W_p^p(\mu,\nu)=\frac1m\sum_{i=1}^m|a_{(i)}-b_{(i)}|^p
For equally weighted empirical measures on the real line, sorted atoms a_(i) and b_(i) give the optimal matching for p≥1.↔ Scroll wide equations horizontally.

Two atoms with an exact coupling

On the real line, compare μ=(δ₀+δ₂)/2 and ν=(δ₁+δ₃)/2 using squared distance. Compute W₂ and contrast the crossed pairing.

  1. The sorted pairing sends mass 1/2 from 0 to 1 and mass 1/2 from 2 to 3. Its coupling matrix is diag(1/2,1/2), with every required row and column sum equal to 1/2.

  2. Its mean squared cost is (1/2)(1−0)²+(1/2)(3−2)²=1. The Wasserstein distance is the square root, W₂=1.

  3. The crossed pairing sends 0 to 3 and 2 to 1. Its mean squared cost is (1/2)9+(1/2)1=5, giving square root √5.

  4. Every feasible two-by-two coupling has diagonal entries a and off-diagonal entries 1/2−a for 0≤a≤1/2. Its cost is 5−8a, minimized at a=1/2, proving the sorted answer directly.

The result

The admissible sorted coupling is optimal, with squared cost one and Wasserstein distance one.

Explore the model

Interactive lab

Interactive lab · optimization

Solver stability is a separate question

Predict the first update at η=1, then adjust step size η and initial x₀ for xₙ=(1−η)ⁿx₀ on f(x)=x²/2. Compare the immediate zero with contraction at η=0.5 and instability above two. This illustrates an optimizer's stability, not a transport solver, coupling, Wasserstein gradient, or entropic objective.

Solver stability is a separate questionGradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Current readings: Position x0: 2; Objective x²/2: 2; Iteration behavior: Stable step. All parameters are adjustable with the labeled controls below.−4−202402.174.356.528.7Parameter xf(x) = x²/2n = 0
ObjectiveDiscrete iterate
Position x0
2
Objective x²/2
2
Iteration behavior
Stable step
n = 0 · t = 0 s

Gradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA stable optimization step does not establish correct marginal constraints or identify the intended transport metric.

Check your understanding

What is required for the displayed W_p formula to define the usual finite-valued Wasserstein metric?

Try it yourself

For μ=δ₀ and ν=(δ₋₁+δ₁)/2 on R, compute W₁ and W₂. Is a deterministic map from the single source atom sufficient?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The marginal constraints force half the mass to each target.

Compare with the solution

The only coupling sends mass 1/2 to −1 and 1/2 to 1. Both mean absolute distance and mean squared distance are one, so W₁=W₂=1. A deterministic map from the single source point sends all mass to one location and cannot produce the split target; a coupling can.

Take it with you

  • A feasible coupling must preserve both marginals and may split atomic mass.
  • Wasserstein distance includes a ground metric, p≥1, finite moments, and a final root.
  • Sampling, regularization, and solver error are separate approximations.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.