Foundations to Frontier
Tests
A. Mathematical Foundations › A12 Geometry for ML

Optimal Transport and the Wasserstein Distance

How far apart are two distributions? Optimal transport answers by asking for the cheapest way to move one pile of mass onto the other. This topic covers Monge's and Kantorovich's formulations, the Wasserstein distances (with closed forms in 1-D and for Gaussians), Kantorovich–Rubinstein duality, and why Wasserstein distances give useful gradients where KL and Jensen–Shannon don't (the idea behind Wasserstein GANs). It also covers entropic regularization and the Sinkhorn algorithm. In the research exercise you assign a drone swarm to a formation exactly with an auction algorithm, compare it with greedy assignment, and verify the collision-avoidance property of optimal-transport trajectories.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Moving piles of sand

Picture two piles of sand with different shapes. To reshape the first pile into the second, you move each grain somewhere, and moving a grain far costs more than moving it close. The cheapest total effort is the earth mover’s distance, also called the Wasserstein distance.

Unlike other ways of comparing distributions, it notices how far things moved. Two piles 1 m apart are closer than two piles 1 km apart, even though neither pair overlaps at all.

Optimal transport: flying a swarm into formation
total squared distance: optimal 6985 m², greedy 14556 m² (+108.4%) · closest approach in flight with OT: 7.90 m
Grey dots: start; blue rings: formation slots; green: drones now (orange when closer than 3 m to another). Minimizing the total squared distance is an optimal transport problem between two point clouds. Its solution has no "crossing" pairs that a swap could improve, which is why the simultaneous straight-line flights stay well separated (Turpin, Michael and Kumar, 2014). Greedy nearest-first grabs the easy matches early and leaves long, crossing paths for the last drones.
Why engineers care

Optimal transport assigns robots and drones to targets, matches stars between catalogues, compares sensor histograms, aligns point clouds and trains generative models. Its ability to measure distance between non-overlapping distributions is what made Wasserstein GANs train stably, and it underlies modern flow-matching generative models.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The earth mover's distance measures…
  2. 2. Shifting a distribution 2 units to the right changes its Wasserstein-1 distance from the original by…
  3. 3. Assigning drones to formation slots with minimum total travel is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Monge and Kantorovich

  • Monge (1781): find a map T pushing μ to ν that minimizes ∫c(x,T(x)) dμ\int c(x, T(x))\,d\mu. A map may not exist, for example when splitting one point mass into two.
  • Kantorovich (1942): allow mass to split. Find a coupling (transport plan) π\pi with marginals μ and ν:
OTc(μ,ν)=min⁡π∈Π(μ,ν)∫c(x,y) dπ(x,y).\mathrm{OT}_c(\mu, \nu) = \min_{\pi\in\Pi(\mu, \nu)}\int c(x, y)\,d\pi(x, y).

For discrete distributions this is a linear program over the matrix P with row sums a and column sums b. With n equal masses on each side, an optimal plan is a permutation: the assignment problem.

Wasserstein distances

Wp(μ,ν)=(OT∥x−y∥p)1/pW_p(\mu, \nu) = \big(\mathrm{OT}_{\|x - y\|^p}\big)^{1/p}. In 1-D, sort and match: W1=∫∣F(x)−G(x)∣ dxW_1 = \int|F(x) - G(x)|\,dx, the area between the CDFs.

📝 Quick check (Level 1)
0/3 answered
  1. 1. W₁ between samples {0, 1, 2} and {3, 4, 5}:
  2. 2. Kantorovich's formulation optimizes over…
  3. 3. In 1-D, W₁ equals…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Closed forms

  • Gaussians: W22=∥m1−m2∥2+tr⁡(Σ1+Σ2−2(Σ21/2Σ1Σ21/2)1/2)W_2^2 = \|m_1 - m_2\|^2 + \operatorname{tr}\big(\Sigma_1 + \Sigma_2 - 2(\Sigma_2^{1/2}\Sigma_1\Sigma_2^{1/2})^{1/2}\big). In 1-D this reduces to (m1−m2)2+(σ1−σ2)2(m_1 - m_2)^2 + (\sigma_1 - \sigma_2)^2. This is the Fréchet Inception Distance (FID) used to evaluate image generators.
  • Duality (Kantorovich–Rubinstein): W1(μ,ν)=sup⁡∥f∥Lip≤1Eμf−EνfW_1(\mu, \nu) = \sup_{\|f\|_{\mathrm{Lip}}\le1}\mathbb E_\mu f - \mathbb E_\nu f. WGANs (Arjovsky, Chintala and Bottou, 2017) train a critic f constrained to be 1-Lipschitz.

Why not KL or JS?

For point masses at 0 and δ, KL = ∞ and JS = ln 2 for every δ ≠ 0, so their gradient in δ is zero. W1=∣δ∣W_1 = |\delta| decreases smoothly as the distributions approach. Generators whose samples miss the data support get no signal from JS, but do from W.

Entropic OT and Sinkhorn

Add an entropy penalty: min⁡P⟨P,C⟩−εH(P)\min_P\langle P, C\rangle - \varepsilon H(P). The solution is P=diag⁡(u) Kdiag⁡(v)P = \operatorname{diag}(u)\,K\operatorname{diag}(v) with Gibbs kernel K=e−C/εK = e^{-C/\varepsilon}. Sinkhorn’s algorithm alternately rescales rows and columns to match the marginals (Cuturi, 2013). It is fast, parallel and differentiable. As ε→0\varepsilon\to0 it approaches exact OT, but it converges more slowly. Work in the log domain for stability.

🐍 Exercise 1: Wasserstein distances and Sinkhorn
Compute W₁ in 1-D by sorting and via CDFs, W₂ between Gaussians, entropic OT with log-domain Sinkhorn, and exact OT by brute-force assignment. Compare them, and contrast W₁ with KL and JS for shifted point masses.
📝 Quick check (Level 2)
0/3 answered
  1. 1. W₂ between N(0, 1) and N(0, 9) in 1-D:
  2. 2. For two point masses at different locations, JS divergence is…
  3. 3. Sinkhorn's algorithm alternately…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

1-D OT is sorting

For convex costs c(x−y)c(x - y) on the line, the monotone (quantile) coupling T=G−1∘FT = G^{-1}\circ F is optimal. Any crossing pair x1<x2x_1 < x_2 with T(x1)>T(x2)T(x_1) > T(x_2) can be uncrossed without increasing cost, by convexity. With that coupling, W1=∫01∣F−1(u)−G−1(u)∣ du=∫∣F−G∣ dxW_1 = \int_0^1|F^{-1}(u) - G^{-1}(u)|\,du = \int|F - G|\,dx (swap the order of integration). ∎

Cyclical monotonicity

An optimal plan’s support is c-cyclically monotone: no permutation of matched pairs lowers the cost. With 2-cycles, c(xi,yi)+c(xj,yj)≤c(xi,yj)+c(xj,yi)c(x_i, y_i) + c(x_j, y_j)\le c(x_i, y_j) + c(x_j, y_i). For squared Euclidean cost this becomes ⟨xi−xj,yi−yj⟩≥0\langle x_i - x_j, y_i - y_j\rangle\ge0. Matched displacements never “cross”, which is why simultaneous straight-line flights of an optimal assignment keep their separation (Turpin, Michael and Kumar, 2014).

Brenier’s theorem

For c=∥x−y∥2c = \|x - y\|^2 and μ absolutely continuous, the optimal plan is a map T=∇φT = \nabla\varphi with φ convex (Brenier, 1991). This links OT to convex analysis, PDEs (Monge–Ampère) and modern flow-matching generative models.

Exercise 3.1: W₂ between N(1, 4) and N(4, 16)

The standard deviations are 2 and 4, so W2=(4−1)2+(4−2)2=13≈3.606W_2 = \sqrt{(4 - 1)^2 + (4 - 2)^2} = \sqrt{13}\approx3.606.

📝 Quick check (Level 3)
0/3 answered
  1. 1. For squared Euclidean cost, optimal maps are…
  2. 2. Cyclical monotonicity with 2-cycles says an optimal matching has…
  3. 3. In 1-D, the optimal coupling for convex cost is…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Assigning a drone swarm to a formation

40 drones parked at least 6 m apart on a 100 m field must fly straight lines into a ring of radius 30 m, all arriving together. We solve for the assignment that minimizes total squared distance exactly, with Bertsekas’ auction algorithm and ε-scaling (Bertsekas, 1988).

Method total squared distance longest flight closest approach
Optimal transport (auction) 11 456 m² 30.2 m 4.41 m
Greedy nearest-first 12 971 m² (+13%) 38.5 m 0.39 m
Sinkhorn, ε = 0.002 11 703 m² — —

No pairwise swap improves the auction solution. Its trajectories keep at least min⁡(start,goal spacing)/2\min(\text{start}, \text{goal spacing})/\sqrt2 apart, as the CAPT guarantee requires. Greedy leaves near-collisions. Drone light shows, multi-rover task allocation and satellite constellation reconfiguration use this kind of assignment.

🐍 Exercise 2 (research): optimal transport for swarm formation
Implement the auction algorithm, greedy assignment and Sinkhorn for squared-distance costs. Assign 40 drones to a ring, check optimality by pairwise swaps, compare costs, and measure the closest approach during simultaneous straight-line flight.

Optimal transport in AI, robotics and aerospace

  • Generative models: Wasserstein GANs, OT-based flow matching and Schrödinger bridges; FID for evaluation.
  • Robotics: multi-robot task assignment and formation control (CAPT); point-cloud registration; comparing occupancy maps.
  • Aerospace: constellation reconfiguration; matching detections to catalogues; distribution shift between simulated and real sensor data.
  • ML at large: domain adaptation, the sliced Wasserstein distance for speed, and Wasserstein barycenters for averaging shapes (Peyré and Cuturi, 2019).

References

  1. C. Villani (2008). Optimal Transport: Old and New. Springer.
  2. G. Peyré, M. Cuturi (2019). “Computational optimal transport.” Foundations and Trends in ML, 11.
  3. M. Cuturi (2013). “Sinkhorn distances: lightspeed computation of optimal transport.” NeurIPS.
  4. M. Arjovsky, S. Chintala, L. Bottou (2017). “Wasserstein generative adversarial networks.” ICML.
  5. Y. Brenier (1991). “Polar factorization and monotone rearrangement of vector-valued functions.” Comm. Pure Appl. Math., 44.
  6. D. Bertsekas (1988). “The auction algorithm: a distributed relaxation method for the assignment problem.” Annals of OR, 14.
  7. M. Turpin, N. Michael, V. Kumar (2014). “CAPT: concurrent assignment and planning of trajectories for multiple robots.” IJRR, 33.
  8. Y. Lipman, R. Chen, H. Ben-Hamu, M. Nickel, M. Le (2023). “Flow matching for generative modeling.” ICLR.