Dive into Deep Learning · §37.4
Test Functions and Optimal Transport
integral probability metrics · kernel MMD · witness functions · Wasserstein-1 · Kantorovich–Rubinstein · Sinkhorn
Motivation: The Shifted-Gaussian Diagnostic
Compare overlapping shifted Gaussians P = \mathcal{N}(0, 1) and Q = \mathcal{N}(d, 1) separated by d = 8 standard deviations:
Five Comparisons on Shifted Gaussians
Evaluating all five comparisons and their central-difference derivatives in d reveals how quickly bounded ratios and local kernels lose sight of distant data:
d KL slope JS slope TV slope MMD^2 slope W1 slope
0.5 0.125 5.0e-01 0.030 1.2e-01 0.197 3.9e-01 0.047 1.8e-01 0.500 1.0e+00
1.0 0.500 1.0e+00 0.111 2.0e-01 0.383 3.5e-01 0.177 3.3e-01 1.000 1.0e+00
2.0 2.000 2.0e+00 0.337 2.2e-01 0.683 2.4e-01 0.562 4.0e-01 2.000 1.0e+00
4.0 8.000 4.0e+00 0.633 6.9e-02 0.954 5.4e-02 1.074 1.1e-01 4.000 1.0e+00
8.0 32.000 8.0e+00 0.693 2.0e-04 1.000 1.3e-04 1.155 7.2e-05 8.000 1.0e+00
Unifying Test-Function Comparisons
An Integral Probability Metric (IPM) compares P and Q across a symmetric class \mathcal{G} of test functions (“auditors”) g:
\textrm{IPM}_{\mathcal{G}}(P, Q) = \sup_{g \in \mathcal{G}}\Big(\mathbb{E}_{x \sim P}[g(x)] - \mathbb{E}_{y \sim Q}[g(y)]\Big)
Sample Efficiency vs. Far-Sighted Gradients
Building Maximum Mean Discrepancy
With feature map \boldsymbol{\varphi}(x) and mean embedding \mathbf{m}_P = \mathbb{E}_{x \sim P}[\boldsymbol{\varphi}(x)], Cauchy–Schwarz solves the supremum over \|\mathbf{w}\|_2 \le 1 analytically:
\textrm{MMD}(P, Q) = \sup_{\|\mathbf{w}\|_2 \le 1} \mathbf{w}^\top(\mathbf{m}_P - \mathbf{m}_Q) = \|\mathbf{m}_P - \mathbf{m}_Q\|_2
Closed-Form Pairwise Expectations
Expanding \|\mathbf{m}_P - \mathbf{m}_Q\|_2^2 via k(x, x') = \langle \boldsymbol{\varphi}(x), \boldsymbol{\varphi}(x') \rangle yields three pairwise kernel expectations:
\textrm{MMD}^2(P, Q) = \mathbb{E}_{P}[k(x, x')] + \mathbb{E}_{Q}[k(y, y')] - 2\mathbb{E}_{P, Q}[k(x, y)]
Infinite-Dimensional Feature Spaces
Any finite polynomial kernel (such as k(x, x') = xx' + x^2 x'^2) is blind to higher moments. Choosing the RBF (Gaussian) kernel k(x, x') = \exp\!\big(-\frac{\|x - x'\|^2}{2\ell^2}\big) with length scale \ell > 0 compares all moments simultaneously:
Two-Sample Testing without a Critic
Given finite samples \{x_i\}_{i=1}^n \sim P and \{y_j\}_{j=1}^m \sim Q, omitting diagonal self-pairs (i = j, which would bias within-sample similarities upward by k(x_i, x_i)/n = 1/n) yields an unbiased U-statistic estimator:
linear, N(0,1) vs N(0,4) MMD^2 = -0.0033 (population 0.0000)
quadratic, N(0,1) vs N(0,4) MMD^2 = +10.2618 (population 9.0000)
RBF, N(0,1) vs N(0,4) MMD^2 = +0.1180 (population 0.0942)
RBF, N(0,1) vs N(0,1) MMD^2 = +0.0001 (population 0.0000)
RBF, N(0,1) vs N(0.5,1) MMD^2 = +0.0351 (population 0.0471)
Why Fixed-Bandwidth MMD Is Nearsighted
Which test function g^\star \in \mathcal{G} attains the maximum gap in \textrm{MMD}(P, Q)?
witness changes sign at x = 0.35
E_P g - E_Q g = 0.0360
Moving Probability Mass
To keep a steady gradient at every distance d, we must incorporate the ground distance \|x - y\| of the space. A transport plan (or coupling) \gamma \in \Pi(P, Q) is a joint distribution over (x, y) whose marginals are P and Q:
W_1(P, Q) = \inf_{\gamma \in \Pi(P, Q)} \mathbb{E}_{(x, y) \sim \gamma}\big[\|x - y\|\big]
Monge–Kantorovich Geometry
On the real line, reshaping a left-heavy distribution P into a right-heavy distribution Q on atoms \{0, 1, 2, 3, 4, 5\} moves mass across intervals whose vertical gaps |F_P(t) - F_Q(t)| sum to W_1 = 1.7:
Primal Linear Program to Dual IPM
LP strong duality converts the primal optimal-transport infimum over couplings \gamma \in \Pi(P, Q) into a dual IPM supremum over 1-Lipschitz critics:
W_1(P, Q) = \inf_{\gamma \in \Pi(P,Q)}\mathbb{E}_\gamma[\|x - y\|] = \sup_{\|g\|_{\textrm{Lip}} \le 1}\big(\mathbb{E}_P[g(x)] - \mathbb{E}_Q[g(y)]\big)
Four-Step Proof on the Real Line
In one dimension, Kantorovich–Rubinstein duality and primal optimal transport both collapse to the area between the cumulative distribution functions F_P(t) = \Pr(X \le t) and F_Q(t) = \Pr(Y \le t):
W_1(P, Q) = \int_{-\infty}^\infty \big|F_P(t) - F_Q(t)\big|\,dt
Six-Point Worked Example
On the 6-point benchmark, summing the five CDF gaps (0.25 + 0.35 + 0.50 + 0.35 + 0.25 = 1.7), solving the 36-variable linear program, and evaluating the monotone quantile coupling all return W_1 = 1.7000:
W1 from the CDF gaps : 1.7000
W1 from the LP : 1.7000
cost of the monotone plan: 1.7000
LP plan = monotone plan : False, mass the LP plan moves leftward: 0.00
Strict Convexity & Gaussian Closed Form
Replacing |x - y| with squared distance \|x - y\|^2 defines Wasserstein-2 (W_2):
W_2^2\big(\mathcal{N}(\mu_1, \sigma_1^2), \mathcal{N}(\mu_2, \sigma_2^2)\big) = (\mu_1 - \mu_2)^2 + (\sigma_1 - \sigma_2)^2
Fast Differentiable Transport
In dimensions n \ge 2, no 1D CDF formula exists and solving an N^2-variable linear program costs \mathcal{O}(N^3 \log N). Entropic optimal transport (Cuturi, 2013) adds an entropy penalty of strength \varepsilon > 0 to encourage smooth plans:
\min_{\boldsymbol{\gamma} \in \Pi(\mathbf{p}, \mathbf{q})} \sum_{i,j=1}^N \gamma_{ij} C_{ij} + \varepsilon \sum_{i,j=1}^N \gamma_{ij}(\log \gamma_{ij} - 1)
Alternating Marginal Projections
To enforce \boldsymbol{\gamma}_\varepsilon^\top\mathbf{1} = \mathbf{q} and \boldsymbol{\gamma}_\varepsilon\mathbf{1} = \mathbf{p} on \boldsymbol{\gamma}_\varepsilon = \textrm{diag}(\mathbf{a})\mathbf{K}\textrm{diag}(\mathbf{b}), Sinkhorn–Knopp alternates two \mathcal{O}(N^2) matrix-vector divisions:
\mathbf{b} \leftarrow \frac{\mathbf{q}}{\mathbf{K}^\top \mathbf{a}}, \qquad \mathbf{a} \leftarrow \frac{\mathbf{p}}{\mathbf{K}\mathbf{b}}
Sharpness vs. Iteration Budget
With squared cost C_{ij} = (x_i - x_j)^2 (W_2^2 = 3.50000 across 10 monotone routes), lowering \varepsilon from 2.0 to 0.1 sharpens the heatmap into the exact staircase while increasing iterations from 50 to \sim 150{,}000:
squared cost, LP optimum : 3.50000
epsilon = 2.0: 47 iterations, entropic cost = 4.06489, routes above 1e-3 = 25
epsilon = 0.5: 186 iterations, entropic cost = 3.56963, routes above 1e-3 = 14
epsilon = 0.1: 154325 iterations, entropic cost = 3.50002, routes above 1e-3 = 10
Statistical & Computational Trade-Offs
Check Your Understanding
Test your understanding of IPMs and optimal transport:
Check Your Understanding
Answers:
Key Takeaways
| Method / Distance | Mathematical Definition | Sample Complexity / Computation | Geometric Range & Deep Learning Role |
|---|---|---|---|
| IPM Template | \sup_{g \in \mathcal{G}}(\mathbb{E}_P[g] - \mathbb{E}_Q[g]) | Governed by richness of auditor class \mathcal{G} | Unifies TV (|g|\le 1/2), MMD (RKHS ball), and W_1 (\textrm{Lip}_1) |
| Kernel MMD | \|\mathbf{m}_P - \mathbf{m}_Q\|_2 = (\mathbb{E}[k_{PP} + k_{QQ} - 2k_{PQ}])^{1/2} | \mathcal{O}(N^{-1/2}) in any dimension n; closed-form U-statistic | Characteristic for RBF kernel; ideal for two-sample tests, flat for d \gg \ell |
| Wasserstein-1 (W_1) | \inf_{\gamma \in \Pi(P,Q)} \mathbb{E}_\gamma\|x-y\| = \sup_{\|g\|_{\textrm{Lip}}\le 1}(\mathbb{E}_P g - \mathbb{E}_Q g) | 1D: \int |F_P - F_Q|\,dt; n-D: \mathcal{O}(N^{-1/n}) empirical rate | Constant unit slope at all distances d; foundation of WGAN |
| Wasserstein-2 (W_2) | (\inf_{\gamma \in \Pi(P,Q)} \mathbb{E}_\gamma\|x-y\|^2)^{1/2} | Unique monotone plan in 1D; closed-form Gaussian Fréchet distance | Penalizes crossing paths; powers FID evaluation & flow-matching OT |
| Sinkhorn Entropic OT | \min_{\gamma \in \Pi(\mathbf{p},\mathbf{q})} \langle \boldsymbol{\gamma}, \mathbf{C} \rangle + \varepsilon \langle \boldsymbol{\gamma}, \log \boldsymbol{\gamma} - \mathbf{1} \rangle | \boldsymbol{\gamma}_\varepsilon = \textrm{diag}(\mathbf{a})e^{-\mathbf{C}/\varepsilon}\textrm{diag}(\mathbf{b}); \mathcal{O}(N^2) per step | Fast, GPU-differentiable transport; trades blur (\varepsilon) for iteration count |