TL;DR
Picture one probability distribution as a pile of sand and another as the shape you want to build from it. The Wasserstein distance is the least total effort needed to move the sand, where each grain costs its mass times the distance it travels. It is a true distance between distributions: it is zero only when they match, it is symmetric, and it obeys the triangle inequality.
Where you'll see it: in training Wasserstein GANs, in comparing histograms of measurements, and in matching the feature distributions of two data sources in machine learning.
Intuition
Moving a pile of sand
A density or a histogram describes how a quantity is spread out. Two such shapes can differ in two ways: they can put their mass in different places, or they can put the same mass in nearly the same places. The Wasserstein question is: what is the cheapest way to turn one shape into the other?
Imagine each shape is a pile of sand. Moving one grain one metre costs one unit of effort. The Wasserstein distance is the total effort of the cheapest way to reshape pile A into pile B. If B is A pushed a little to the right, the effort is small. If B is A pushed far to the right, the effort is large. The measure respects closeness in the space the mass lives in, and it does so without any extra rule.
Why not compare heights?
A common way to compare two densities is to look at their heights point by point, as KL divergence does. That works when the two shapes overlap. When they do not overlap at all, the height comparison breaks down: wherever one shape has mass the other has none, the comparison says "infinitely different", even if the two piles are one centimetre apart. The Wasserstein distance measures the horizontal gap instead, so it stays finite and shrinks as the piles approach each other.
- P = N(0, 1)
- Q = N(2, 1)
The math
Couplings
A coupling of two distributions and is a joint distribution on pairs whose two margins (the distributions of the first and second coordinate alone) are and . If is drawn from , then has law and has law . Read as a transport plan: it says how much of the mass at is sent to the target . Write for the set of all couplings. Every coupling sends out exactly what holds and fills exactly what needs.
Definition
Let be a metric space, and let and be probability distributions on . For , the Wasserstein distance of order is
The expectation is the average cost of one plan, where the cost of moving from to is the distance raised to the power . The infimum (the greatest lower bound, here simply the cost of the cheapest plan) picks the cheapest plan among all couplings, and the outer power brings the answer back to units of distance. The case is the earth mover's distance, . The case is the one most often used in theory, because it has the cleanest formulas.
Discrete distributions are a linear program
Suppose puts mass on points , and puts mass on points . A coupling is then a table with row sums (every source sends out what it holds) and column sums (every target receives what it needs). With cost ,
where the minimum runs over the tables that satisfy the row and column constraints. The objective and the constraints are all linear, so this is a linear program, and its best solution sits at a corner of the feasible set. That is why Wasserstein distances between histograms can be computed exactly.
The one-dimensional formula
On the real line with , the best plan is the sorted matching: the lowest source goes to the lowest target, and so on. The distance then has a formula in terms of the cumulative distribution functions and :
At each point , the quantity is the net amount of mass that must cross to turn into . Integrating over all adds up those crossings, and each crossing is paid for by the distance it spans. This is the most useful formula in practice, because you can check it by hand.
For general , the same idea works through the quantile function , the inverse of the CDF:
The metric axioms
Wasserstein distance satisfies the axioms of a metric on the set of distributions. It is zero only when . It is symmetric, because swapping the roles of and transposes every plan. The triangle inequality comes from gluing: given a plan from to and a plan from to , you can glue them along their shared middle distribution to get a plan from to , and the cost of that plan is the sum of the two costs.
The dual form for
For there is a second formula that uses no plans at all. A function is 1-Lipschitz if for all and , so it never changes faster than the distance does (see Lipschitz Continuity). The Kantorovich–Rubinstein duality says
Each allowed test function gives a lower bound on the distance: its average under minus its average under . The supremum (the least upper bound, here the best value over all allowed ) is exactly . This form is the one used to train GANs, because it turns the distance into an optimisation over a single function.
Worked examples
puts mass at and at . puts mass at and at . Use .
- Sorted matching: send the mass at to (distance 1) and the mass at to (distance 1). The cost is .
- The other pairing sends to and to . Its cost is , which is worse.
- Check with the CDF formula. is before , on , and from on. is before , on , and from on. The difference is on , on , on , and elsewhere. Its integral is .
Interpretation: the plan and the formula agree on 1. Since the formula gives the exact distance, no plan can do better, so .
Measurements are sorted into three bins at positions 1, 2 and 3 (low, medium, high), with . Histogram is and histogram is .
- Use the CDF formula with the bins as points. After bin 1, and . After bin 2, and .
- Between positions 1 and 2, the mass that must cross is . The gap is 1, so this stretch contributes .
- Between positions 2 and 3, the mass that must cross is , contributing .
- Total: .
- Check with an explicit plan. Move from bin 1 to bin 2 (cost ), move from bin 1 to bin 3 (cost ), and move from bin 2 to bin 3 (cost ). Bin 1 sends , which it holds. Bin 2 sends , which it holds. Bin 2 receives , which is what it needs. Bin 3 receives , which is what it needs. The total cost is .
flowchart LR S1["source bin 1 (0.6)"] -->|"0.3 at cost 1"| T2["target bin 2 (0.3)"] S1 -->|"0.3 at cost 2"| T3["target bin 3 (0.7)"] S2["source bin 2 (0.4)"] -->|"0.4 at cost 1"| T3
Interpretation: units of mass cross the boundary between bins 1 and 2, and cross the boundary between bins 2 and 3. The cheapest plan pays for each crossing once, and the explicit plan costs exactly what the formula predicted.
Let and . The means are and , and the standard deviations are and . For two univariate Gaussians there is a closed form for :
- Mean difference: .
- Standard deviation difference: .
- Add: .
- Take the square root: .
Interpretation: part of the distance comes from shifting the centre by 2 and part from changing the spread by 1. The two pieces combine like the legs of a right triangle, so the answer is not the simple sum .
Suppose the real data all sit at one point, . A generative model outputs a single value , so its distribution is , a point mass at . Compare the two losses. Write for the data.
- KL divergence. If , the outcome has probability 1 under and probability 0 under . The divergence is therefore infinite. Every positive gets the same infinite loss, so a KL-trained model cannot tell from , and it gets no signal about which way to move.
- Wasserstein distance. The only coupling of two point masses moves all the mass from to , so .
- Dual check. Take . It is 1-Lipschitz, because . The dual objective is , which equals the value from step 2, so this attains the supremum.
- So the loss is : at and at . A gradient step lowers it by moving toward .
Interpretation: the Wasserstein loss says how far off the model is and which way to move. The KL loss says "infinitely wrong" everywhere except at an exact match. Arjovsky, Chintala and Bottou made this kind of argument when they proposed Wasserstein training for GANs.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Distributions with disjoint supports, such as two point masses | KL divergence is infinite, so it gives no gradient | Wasserstein distance, which stays finite and shrinks as the supports approach |
| Large numbers of points | Exact transport becomes slow, since it is a large linear program | Entropic regularisation with Sinkhorn iterations (Sinkhorn distances) |
| Samples in high dimensions | Distances between empirical samples converge slowly and are biased upward | Use it with care, reduce the dimension, or use sliced versions that average one-dimensional distances |
| A poor choice of ground metric | The value depends entirely on , so bin spacing or a colour scale can change the answer | Choose so that it matches what "moving" means for the problem |
Two practical points follow from the definition. First, the distance inherits the geometry of the space. Moving mass between two nearby colours costs little, while moving it between unrelated categories costs whatever you declared the distance to be. Second, the choice of matters. For long moves are penalised more heavily than for . In one dimension both have the cheap formulas above; in higher dimensions there is usually no closed form, and you rely on linear programs or approximations.
History
Gaspard Monge posed the transport problem in 1781, asking how to move earth from cuttings into embankments at the lowest total cost. Leonid Kantorovich recast it as a linear problem in the early 1940s, which is why the distance is sometimes called the Monge–Kantorovich distance. The Russian mathematician Leonid Vaserstein used the same distance in a 1969 paper on Markov processes. The name "Wasserstein" is the spelling that became common in the West. In the late 1990s, computer vision researchers, led by Rubner, Tomasi and Guibas, popularised the distance as the "earth mover's distance" for comparing colour histograms. Cédric Villani's 2009 monograph Optimal Transport: Old and New made the theory central to modern analysis. In 2017, Arjovsky, Chintala and Bottou used the Kantorovich–Rubinstein dual to train GANs, which brought the distance into deep learning. It caught on because it is a true metric, it is finite when other comparisons are not, and it has both a clean theory and a concrete algorithm.
Recap
The Wasserstein distance is the least total cost of turning one distribution into another, where the cost of a move is mass times distance. It is defined as an infimum over couplings, each of which is a joint distribution with the right margins, and the cost is an expectation. For discrete histograms it is a linear program. On the line it is the area between the two CDFs. For it also equals a supremum over 1-Lipschitz test functions. Unlike KL divergence, it stays finite when supports do not overlap, and it is a true metric on distributions. It leads to the Wasserstein GAN, and to the general theory of optimal transport, which treats transport between any two measures.
Check yourself
QuestionRecall: what two conditions must a coupling of P and Q satisfy?Show answer
Its first margin must be and its second margin must be . In the discrete case, every source's row sum equals the mass puts there, and every target's column sum equals the mass asks for.
QuestionComputation: let and , so has mean 0 and standard deviation 3. Find .Show answer
The means are equal, so the mean term is 0. The standard deviations are 1 and 3, so the spread term is . Then , and .
QuestionWhy: take two distributions with disjoint supports. Why is KL divergence unhelpful here, while the Wasserstein distance is still useful?Show answer
KL divergence is infinite wherever one distribution has mass and the other has none. That happens for any disjoint pair, however close the supports are, so the divergence carries no information about how far apart they are. The Wasserstein distance measures how far the mass has to travel. That is finite, and it shrinks as the supports move closer, so it gives a useful signal.
QuestionApplied: a GAN's generator starts out producing samples whose distribution has almost no overlap with the real data. Which loss gives training a usable signal, and what must the critic satisfy?Show answer
The Wasserstein loss, computed through the dual form for , still changes smoothly as the generator moves toward the data. The critic (the network that plays the role of the test function ) must be 1-Lipschitz. The original Wasserstein GAN enforced this by clipping weights, and later work used a gradient penalty.
Sources
- Wikipedia, "Wasserstein metric": https://en.wikipedia.org/wiki/Wasserstein_metric
- Wikipedia, "Optimal transport": https://en.wikipedia.org/wiki/Optimal_transport
- Cédric Villani, Optimal Transport: Old and New (Springer, 2009): https://doi.org/10.1007/978-3-540-71050-9
- Arjovsky, Chintala, Bottou, Wasserstein GAN (2017): https://arxiv.org/abs/1701.07875