JA EN
LearnProbability & Statistics
·★ MEMBER·PAPER·10 min read

Optimal Transport — The Mathematics of Moving Distributions

Optimal transport measures the distance between two probability distributions as the cheapest way to haul sand from one pile into the shape of another. From the definition of the Wasserstein distance to the Sinkhorn algorithm that makes it practical, and on to WGAN, FID and Flow Matching — with numpy code and the failure modes that bite in production.

ModalitytextTaskmath

Sinkhorn Distances: Lightspeed Computation of Optimal Transport

Primary source — what this article is built on

undefined2026-08-27

Sinkhorn Distances: Lightspeed Computation of Optimal TransportarXiv:1306.0895Paper page·PDF
Wasserstein GANarXiv:1701.07875Paper page·PDF

Where an ordinary distance quietly fails

Most of machine learning boils down to pulling a model's distribution toward the data's distribution. To pull, you first need a single number saying how far apart they currently are. The usual choice is KL divergence — and KL has one situation it handles badly: when the two distributions do not overlap at all.

Say the real data is a thin spike at x=0x=0 and the model is a thin spike at x=θx=\theta. Whether θ\theta is 0.010.01 or 100100, KL is infinite on the sole grounds that the overlap is zero (Jensen–Shannon would sit flat at log2\log 2). So moving θ\theta changes nothing — the gradient is zero, even though any human would say θ=0.01\theta=0.01 is the closer one. That intuition uses where the spike stands, while KL only compares probabilities vertically, point by point, never sideways along the axis.

Optimal transport fills exactly that gap. It defines distance as the minimum cost of hauling one distribution until it takes the shape of the other. Because you are hauling, horizontal distance ends up inside the number.

The metaphor: a moving company's quote

You run an earthworks outfit. Sand is piled at several depots, and several construction sites each need a fixed amount. The bill is "amount moved × distance moved." There are countless ways to finish the job, but exactly one cheapest total, and that cheapest total is the "distance" between where the sand is and where it is wanted.

This is not only a metaphor but the literal origin. In 1781 Gaspard Monge posed it as the problem of cuttings and embankments — moving earth.

Intuition: you choose a transport table, not a matching

Monge's version had a flaw. It looks for a one-to-one assignment — "all the sand at depot ii goes to site σ(i)\sigma(i)" — so if you have three depots and two sites, no such assignment exists. Leonid Kantorovich's 1942 fix is almost anticlimactic: let the sand be split. What you choose then is a table.

Site 1 Site 2 Row sum
Depot 1 3 t 1 t 4 t
Depot 2 0 t 6 t 6 t
Col sum 3 t 7 t

Entry (i,j)(i,j) of the table PP is how much moves from depot ii to site jj. Only three constraints apply: every entry non-negative, each row sums to that depot's stock, each column sums to that site's demand. The set of tables satisfying this is called the transport polytope.

The total bill is "amount × unit price" summed over every cell, so both the objective and the constraints are linear in the entries. Optimal transport is therefore a linear program. The moment splitting is allowed, a solution is guaranteed to exist and an entire existing toolbox becomes available — that is why Kantorovich's relaxation was the turning point.

Mechanism: defining the Wasserstein distance

All of the above condenses into one line.

Wp(μ,ν)=(minPU(a,b) i,jPijd(xi,yj)p)1/pW_p(\mu,\nu)=\left(\min_{P\in U(a,b)}\ \sum_{i,j}P_{ij}\,d(x_i,y_j)^p\right)^{1/p}
(1)

μ\mu and ν\nu are the two distributions being compared, xix_i and yjy_j are the locations of their points, and aia_i and bjb_j are the probability mass sitting there (the sand). U(a,b)U(a,b) is the transport polytope from above, and dd is distance on the ground, usually Euclidean. The exponent pp sets how the bill scales: p=1p=1 charges proportionally to distance, p=2p=2 charges the square, which strongly punishes long hauls. The outer 1/p1/p power converts the result back into units of length.

In plain words the formula says: consider every possible transport table, take the one with the smallest total bill, and call that bill the distance. The p=1p=1 case has its own name, the Earth Mover's Distance — a name that is already the explanation.

Why brute force is hopeless

The naive plan is to try every way of moving. But just assigning nn points to nn points one-to-one gives n!n! options — about 2.4×10182.4 \times 10^{18} at n=20n=20.

FIG 1The number of brute-force assignments, n!, climbs even faster than O(2ⁿ) at the top of this chart. Flip the y-axis to linear and it becomes physical: optimal transport is unreachable without a purpose-built algorithm

Even the dedicated exact solvers (network simplex, auction algorithms) cost roughly n3n^3 with a log factor for nn points. Fine up to a few thousand points — far too slow for a generative model comparing two minibatches at every training step.

In one dimension, sorting is all you need

There is one exception. If the points lie on a line, the optimal plan requires no thought: match them in sorted order. If two hauling routes ever crossed, untangling the crossing would strictly shorten the total distance.

Wpp(μ,ν)=01F1(u)G1(u)pduW_p^p(\mu,\nu)=\int_0^1 \left|F^{-1}(u)-G^{-1}(u)\right|^p du

Here F1F^{-1} and G1G^{-1} are the quantile functions — the value below which a fraction uu of the mass sits. The formula says: line up matching quantiles, measure the gap, average. With equal sample counts you just sort both arrays and subtract elementwise, which is O(nlogn)O(n\log n). In Python, scipy.stats.wasserstein_distance is the one-liner.

This "one dimension is dirt cheap" fact pays off later. Project a high-dimensional cloud onto many random directions, take the 1-D Wasserstein distance along each, and average — that is the Sliced Wasserstein distance, an approximation people genuinely ship.

Back to the two spikes. The distance between a spike at and one at is the cost of hauling all the mass a distance , so it is exactly . Differentiate with respect to and the sign tells you which way to move. Precisely where KL and JS went flat, Wasserstein returns a gradient with both magnitude and direction. That obser

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Sinkhorn Distances: Lightspeed Computation of Optimal Transport. arXiv:1306.0895Paper page·PDF
  2. Wasserstein GAN. arXiv:1701.07875Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment