Search

Search papers and lessons

Optimal Transport

Level 4 · 15 min read

Optimal transport finds the cheapest way to move one distribution of mass onto another when every move has a cost, and the best plan's total cost is the basis of the Wasserstein distance.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Optimization
  • Sign in to see your progress

TL;DR

Optimal transport asks for the cheapest way to move one distribution of mass onto another, when every move has a cost. The answer is a plan: a record of how much mass leaves each place and arrives at each other place. The total cost of the best plan is the central object of the theory. When the cost is a power of distance (see Metric Space), that total gives the Wasserstein distance.

Where you'll see it: in comparing probability distributions, in training generative models such as GANs, in averaging several distributions into one, and in assigning jobs to workers at the lowest total cost.

Intuition

Earth from cuttings to embankments

In 1781 the French mathematician Gaspard Monge asked a practical question. A road builder must dig earth out of cuttings and pile it into embankments. Each cutting holds a fixed volume, each embankment needs a fixed volume, and moving earth costs its volume times the distance it travels. Which assignment of earth to places makes the total cost smallest?

Start with a tiny version. Two piles of 1 unit each sit at positions 0 and 10. Two holes, each needing 1 unit, sit at positions 1 and 11. Pairing the pile at 0 with the hole at 1, and the pile at 10 with the hole at 11, costs 1+1=21 + 1 = 2. The crossed pairing costs 11+9=2011 + 9 = 20. The sensible pairing wins, and the cost measures how well a plan respects the geography.

Why allow splitting?

Monge's original problem requires every grain at a point xx to go to one destination T(x)T(x). That is too strict. If one pile must feed two holes, no single destination works, because a pile cannot send each grain to two places. The fix, due to Leonid Kantorovich, is to let the plan split. Instead of saying where each grain goes, the plan says how much mass flows from each place to each other place. Those flows form a table, and finding the cheapest table is a linear problem, which we know how to solve.

flowchart LR
  A[Monge: a map T sends each point to one destination] --> B[Kantorovich: a plan splits mass between destinations]
  B --> C[Linear program, so solvable and convex]
  C --> D[Dual prices on sources and sinks]
  C --> E[Wasserstein distance when the cost is a power of distance]

The math

Setting

Let μ\mu and ν\nu be probability distributions on spaces XX and YY, which here represent the sources and the targets. Let c(x,y)≥0c(x, y) \ge 0 be the cost of moving one unit of mass from xx to yy. Write Π(μ,ν)\Pi(\mu, \nu) for the set of couplings: joint distributions γ\gamma on X×YX \times Y whose first marginal is μ\mu and whose second marginal is ν\nu. This is the same notion as in the Wasserstein lesson, where the two distributions were called PP and QQ.

Two formulations

Definition: Monge problem

Find a map T:X→YT : X \to Y that pushes μ\mu forward to ν\nu, written T#μ=νT_{\#}\mu = \nu, meaning that the mass landing in any set BB equals ν(B)\nu(B), and that minimises

∫Xc(x,T(x)) dμ(x).\int_X c\big(x, T(x)\big)\, d\mu(x).
Definition: Kantorovich problem

Find a coupling γ∈Π(μ,ν)\gamma \in \Pi(\mu, \nu) that minimises

∫X×Yc(x,y) dγ(x,y).\int_{X \times Y} c(x, y)\, d\gamma(x, y).

Monge's version asks for a function, and Kantorovich's asks for a plan. Every map gives a plan, namely the coupling of μ\mu with T(μ)T(\mu) that puts all mass of xx at T(x)T(x). So the Kantorovich value is never larger than the Monge value. The relaxation is strict in general: when μ\mu is a single point and ν\nu has two atoms, no map pushes μ\mu onto ν\nu, but a plan still exists.

The Kantorovich problem is linear in γ\gamma. The objective is linear, and the marginal constraints are linear equalities. For discrete distributions, with μ=∑iaiδxi\mu = \sum_i a_i \delta_{x_i} and ν=∑jbjδyj\nu = \sum_j b_j \delta_{y_j} (where δx\delta_x is the distribution with all its mass at the point xx), the problem becomes the linear program

min⁡γ≥0∑i,jcij γijsubject to∑jγij=ai,∑iγij=bj,\min_{\gamma \ge 0} \sum_{i,j} c_{ij}\,\gamma_{ij} \quad \text{subject to} \quad \sum_j \gamma_{ij} = a_i, \quad \sum_i \gamma_{ij} = b_j,

with cij=c(xi,yj)c_{ij} = c(x_i, y_j). Each γij\gamma_{ij} is how much mass leaves source ii for target jj. Linear programs are convex, so a best plan can be found, and a dual problem can be written down as shown below.

Duality and prices

The dual of the Kantorovich problem is

sup⁡{∫Xφ dμ+∫Yψ dν  :  φ(x)+ψ(y)≤c(x,y) for all x,y}.\sup\left\{ \int_X \varphi\, d\mu + \int_Y \psi\, d\nu \;:\; \varphi(x) + \psi(y) \le c(x, y) \text{ for all } x, y \right\}.

Here φ\varphi and ψ\psi are functions on the sources and the targets, and the constraint is the one in the dual of a linear program (see Lagrangian Duality). The economic reading is clean. Think of −φ(x)-\varphi(x) as the price of a unit of supply at xx, and ψ(y)\psi(y) as the price of a unit of demand at yy. The constraint ψ(y)−(−φ(x))≤c(x,y)\psi(y) - (-\varphi(x)) \le c(x, y) says no trader can buy at xx, ship to yy and sell there for more than the shipping cost. Under mild conditions the dual value equals the Kantorovich value, so the optimal plan and the optimal prices certify each other.

For the Wasserstein distance with p=1p = 1, the dual reduces to the Kantorovich–Rubinstein form from the earlier lesson. Taking ψ=−φ\psi = -\varphi with c=dc = d turns the constraint into φ(x)−φ(y)≤d(x,y)\varphi(x) - \varphi(y) \le d(x,y), which says φ\varphi is 1-Lipschitz (see Lipschitz Continuity): it never changes faster than the distance between the points.

The one-dimensional solution

On the real line with a cost of the form c(x,y)=h(x−y)c(x, y) = h(x - y) for a convex function hh (see Convex Function), the optimal plan is monotone: the lowest source goes to the lowest target, and so on. If μ\mu has no atoms, the optimal map is

T(x)=Fν−1(Fμ(x)),T(x) = F_\nu^{-1}\big(F_\mu(x)\big),

where FμF_\mu is the cumulative distribution function of μ\mu and Fν−1F_\nu^{-1} is the inverse of the CDF of ν\nu, its quantile function. This is the reason sorting solves one-dimensional transport: sorting is what the composition of the two CDFs does.

Quadratic cost and Brenier's theorem

With c(x,y)=∥x−y∥2c(x, y) = \lVert x - y \rVert^2 on Rd\mathbb{R}^d and μ\mu spread out (it has a density), the Monge problem has a unique solution. Yann Brenier proved in 1991 that this solution is the Gradient of a convex function: T=∇ϕT = \nabla \phi for some convex ϕ\phi. So the best way to move one density onto another is to follow the slope of a convex landscape. This gives both a uniqueness result and a structural description of the optimal map.

Entropic regularisation

Exact transport becomes slow when there are many points. Marco Cuturi's 2013 idea was to add a small entropy penalty to the cost:

min⁡γ∈Π(μ,ν)∑i,jcij γij+ε∑i,jγij(log⁡γij−1),ε>0.\min_{\gamma \in \Pi(\mu,\nu)} \sum_{i,j} c_{ij}\,\gamma_{ij} + \varepsilon \sum_{i,j} \gamma_{ij}\big(\log \gamma_{ij} - 1\big), \qquad \varepsilon \gt 0.

The added term is minus the Entropy of the plan, scaled by ε\varepsilon, so minimising it spreads the mass out. The penalised problem has a unique solution of the form γij=uiKijvj\gamma_{ij} = u_i K_{ij} v_j, where Kij=e−cij/εK_{ij} = e^{-c_{ij}/\varepsilon}. Sinkhorn's algorithm finds uu and vv by alternately rescaling: u←a/(Kv)u \leftarrow a / (K v) and then v←b/(K⊤u)v \leftarrow b / (K^\top u), where division is entrywise. Each step is one matrix-vector product, so the method is fast. As ε\varepsilon shrinks toward zero, the solution approaches the exact plan.

Worked examples

Example
Tiny: one pile, two holes (Monge fails, Kantorovich works)

Let μ\mu put all its mass, 1, at the point 00. Let ν\nu put 12\tfrac12 at −1-1 and 12\tfrac12 at 11. Use c(x,y)=∣x−y∣c(x, y) = \lvert x - y \rvert.

  1. Monge: a map TT must send the whole mass at 00 to a single point T(0)T(0). Then T#μT_{\#}\mu is a single point mass, which cannot equal ν\nu, which has two atoms. No Monge map exists.
  2. Kantorovich: take the plan γ\gamma with γ(0,−1)=12\gamma(0, -1) = \tfrac12 and γ(0,1)=12\gamma(0, 1) = \tfrac12. Its first marginal is the point mass at 00, and its second marginal is ν\nu. So γ\gamma is a valid coupling.
  3. Its cost is 12∣0−(−1)∣+12∣0−1∣=12+12=1\tfrac12 \lvert 0 - (-1) \rvert + \tfrac12 \lvert 0 - 1 \rvert = \tfrac12 + \tfrac12 = 1.

Interpretation: the plan splits the single pile between two holes, which Monge's map could not do. Since the plan is optimal here, the Kantorovich value is 1, and the Monge problem has no answer at all.

Example
Standard: monotone sorting for a quadratic cost

Let μ\mu be uniform on {1,2,3}\{1, 2, 3\}, so each point has mass 13\tfrac13. Let ν\nu be uniform on {2,4,6}\{2, 4, 6\}. Use c(x,y)=(x−y)2c(x, y) = (x - y)^2.

  1. Sorted matching pairs 1→21 \to 2, 2→42 \to 4 and 3→63 \to 6. The squared costs are 11, 44 and 99.
  2. Its total cost is 13(1+4+9)=143\tfrac13(1 + 4 + 9) = \tfrac{14}{3}.
  3. Check every pairing. Matching (1,2,3)(1, 2, 3) to the targets (2,6,4)(2, 6, 4) costs 1+16+1=181 + 16 + 1 = 18. Matching to (4,2,6)(4, 2, 6) costs 9+0+9=189 + 0 + 9 = 18. Matching to (4,6,2)(4, 6, 2) costs 9+16+1=269 + 16 + 1 = 26. Matching to (6,2,4)(6, 2, 4) costs 25+0+1=2625 + 0 + 1 = 26. Matching to (6,4,2)(6, 4, 2) costs 25+4+1=3025 + 4 + 1 = 30.
  4. The smallest sum over the six pairings is 1414, the sorted one. So the optimal cost is 143≈4.67\tfrac{14}{3} \approx 4.67.

Interpretation: with equal masses and a convex cost, the best plan is a one-to-one matching, and sorting finds it without checking every pairing. The check in step 3 confirms the claim on this small case.

Example
Standard: two Gaussians and the monotone map

Let μ=N(0,1)\mu = \mathcal{N}(0, 1) and ν=N(0,σ2)\nu = \mathcal{N}(0, \sigma^2) on the real line, with σ=2\sigma = 2. Use c(x,y)=(x−y)2c(x, y) = (x - y)^2.

  1. The CDF of μ\mu turns each xx into a uniform number u=Fμ(x)u = F_\mu(x). The quantile function of ν\nu turns uu into σ\sigma times the standard normal quantile at uu. Composing them gives T(x)=σxT(x) = \sigma x.
  2. Check that TT pushes μ\mu forward to ν\nu. If X∼N(0,1)X \sim \mathcal{N}(0, 1), then 2X∼N(0,4)2X \sim \mathcal{N}(0, 4), which is ν\nu.
  3. The cost is E[(X−2X)2]=E[X2]=1\mathbb{E}\big[(X - 2X)^2\big] = \mathbb{E}[X^2] = 1.
  4. This agrees with the closed form from the Wasserstein lesson, W22=(μ1−μ2)2+(σ1−σ2)2=0+(1−2)2=1W_2^2 = (\mu_1 - \mu_2)^2 + (\sigma_1 - \sigma_2)^2 = 0 + (1 - 2)^2 = 1.

Interpretation: the optimal way to widen a standard bell curve into one twice as wide is to stretch every point away from the centre by a factor of 2. The map is linear, as Brenier's theorem predicts for Gaussians.

Example
Realistic: assigning workers to jobs at minimum total hours

Three workers A, B and C can each do one of three jobs, J1, J2 and J3. The hours each assignment takes are

J1J2J3A428B439C316\begin{array}{c|ccc} & \text{J1} & \text{J2} & \text{J3} \\ \hline \text{A} & 4 & 2 & 8 \\ \text{B} & 4 & 3 & 9 \\ \text{C} & 3 & 1 & 6 \end{array}

Every worker takes one job and every job is taken, so the source and target masses are each 13\tfrac13 on three points. Find the assignment with the fewest total hours.

  1. Enumerate the six one-to-one assignments and add the hours. Assigning A to J1, B to J2 and C to J3 gives 4+3+6=134 + 3 + 6 = 13.
  2. A to J1, B to J3 and C to J2 gives 4+9+1=144 + 9 + 1 = 14.
  3. A to J2, B to J1 and C to J3 gives 2+4+6=122 + 4 + 6 = 12.
  4. A to J2, B to J3 and C to J1 gives 2+9+3=142 + 9 + 3 = 14.
  5. A to J3, B to J1 and C to J2 gives 8+4+1=138 + 4 + 1 = 13.
  6. A to J3, B to J2 and C to J1 gives 8+3+3=148 + 3 + 3 = 14.
  7. The minimum is 1212, with A on J2, B on J1 and C on J3. As an optimal transport plan with masses 13\tfrac13, this is γ=13\gamma = \tfrac13 on those three pairs, with cost 13×12=4\tfrac13 \times 12 = 4 hours per worker on average.

Interpretation: the plan is a permutation of the workers, so this assignment problem is optimal transport with equal masses. Real assignment problems have thousands of workers and jobs, which is why the exact linear program and the Sinkhorn approximation both matter in practice.

Tradeoffs and failure modes

SituationProblemBetter choice
Very many pointsAn exact linear program or assignment solver becomes slowEntropic regularisation with Sinkhorn iterations, which trades a little accuracy for speed
You need a map, not a planA plan splits mass, so one source can feed many targetsUse Brenier's map for a quadratic cost on densities, or learn a map from samples
The cost is not a true distanceResults depend heavily on an arbitrary cost, and the Wasserstein guarantees do not applyPick the cost from the problem, and use WpW_p only when cc is a power of a metric
High-dimensional continuous measuresNo closed form, and sample estimates converge slowly and are biasedProject onto random directions and solve one-dimensional problems, the idea behind sliced transport
Unequal total massesThe standard formulation needs equal massesUnbalanced transport, which lets mass be created or destroyed at a penalty

Two cautions apply throughout. The answer depends on the cost cc, so a poor cost gives a precise but meaningless answer. And the regularised solution from Sinkhorn is biased: with a large ε\varepsilon the plan is too spread out, and with a small ε\varepsilon the iterations need more steps to converge.

History

Monge introduced the transport problem in 1781 in Mémoire sur la théorie des déblais et des remblais. For nearly two centuries it stayed a question about the geometry of earthworks. Leonid Kantorovich recast it as a linear problem in the early 1940s, which made it amenable to the methods that became linear programming. He shared the 1975 Nobel Memorial Prize in Economic Sciences with Tjalling Koopmans for work on the optimal allocation of resources. Yann Brenier's 1991 theorem gave the monotone gradient structure for quadratic costs. Cédric Villani's 2009 monograph Optimal Transport: Old and New made the theory central to analysis and geometry, and he received the Fields Medal in 2010, mainly for his work on Landau damping and the Boltzmann equation.

The applied turn came later. Rubner, Tomasi and Guibas used the earth mover's distance for comparing image colour histograms in the late 1990s. Marco Cuturi's 2013 entropic approach made the computations practical for large problems. Arjovsky, Chintala and Bottou used the Kantorovich–Rubinstein dual to train GANs in 2017. Since then optimal transport has spread into machine learning, statistics and economics, where it describes matching markets. It caught on because one object, the optimal plan, answers both "how far apart are these two distributions?" and "how do I move one into the other?".

Recap

Optimal transport asks how to move one distribution onto another at least total cost. Monge's version seeks a map, which can fail to exist. Kantorovich's version seeks a plan, a coupling of the two distributions, and it is a linear program, so it is convex and solvable. Its dual gives prices on sources and targets that certify the optimum. In one dimension the best plan is sorting, and for a quadratic cost on densities it is the gradient of a convex function. Adding an entropy penalty gives Sinkhorn's algorithm, which is fast enough for large problems. When the cost is a power of a metric, the optimal value is a Wasserstein distance. The theory feeds into generative models and into the study of the convex landscapes that optimal maps follow.

Check yourself

QuestionRecall: what is the difference between a Monge map and a Kantorovich plan?Show answer

A Monge map TT sends each source point to exactly one destination, so all the mass at a point goes to one place. A Kantorovich plan γ\gamma is a coupling that may split the mass at a point between several destinations. Every map gives a plan, so the Kantorovich value is at most the Monge value, and plans can exist when no map does.

QuestionComputation: let μ\mu be uniform on {0,1}\{0, 1\} and ν\nu be uniform on {2,5}\{2, 5\}. Use c(x,y)=(x−y)2c(x, y) = (x - y)^2. Find the optimal cost.Show answer

Sorted matching pairs 0→20 \to 2 and 1→51 \to 5, with squared costs 44 and 1616. The total is 12(4+16)=10\tfrac12(4 + 16) = 10. The crossed pairing 0→50 \to 5, 1→21 \to 2 costs 12(25+1)=13\tfrac12(25 + 1) = 13. Since there are only two one-to-one matchings, the sorted one is optimal with cost 10.

QuestionWhy: why is the Kantorovich problem easier to solve than the Monge problem?Show answer

In the Kantorovich problem the objective is linear in the plan γ\gamma, and the marginal conditions are linear constraints. A linear problem over a convex set is convex, so local solutions are global ones and standard solvers apply. The Monge problem asks for a function TT satisfying a nonlinear constraint, T#μ=νT_{\#}\mu = \nu, which can have no solution at all.

QuestionApplied: for a cost c(x,y)=2c(x, y) = 2 between a source xx and a target yy, suppose a dual pair assigns φ(x)=0.5\varphi(x) = 0.5 and ψ(y)=1.8\psi(y) = 1.8. Is this pair feasible for the dual, and why does it matter?Show answer

It is not feasible. The dual constraint requires φ(x)+ψ(y)≤c(x,y)\varphi(x) + \psi(y) \le c(x, y), but 0.5+1.8=2.30.5 + 1.8 = 2.3 is larger than 22. In price terms, a trader could be paid 0.50.5 to take a unit away from xx, ship it to yy for a cost of 22, and sell it there for 1.81.8. The net gain is 0.5+1.8−2=0.30.5 + 1.8 - 2 = 0.3, which is an arbitrage. A feasible dual pair cannot contain such an arbitrage, and only feasible pairs give a lower bound on the optimal cost.

Sources