Test Functions and Transport

Dive into Deep Learning · §37.4

Test Functions and Optimal Transport
integral probability metrics · kernel MMD · witness functions · Wasserstein-1 · Kantorovich–Rubinstein · Sinkhorn

Does Tail Overlap Rescue Density-Ratio Losses?

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:

  • Smooth tails do not rescue JS, TV, or local MMD: By d = 8, \textrm{JS} \approx \log 2 and \textrm{TV} \approx 1 have saturated, collapsing \partial\textrm{JS}/\partial d, \partial\textrm{TV}/\partial d, and \partial\textrm{MMD}^2/\partial d (\ell = 1) to \sim 10^{-4}.
  • Wasserstein-1 (W_1) stays steady: Maintains unit slope \partial W_1/\partial d = 1.00 at every distance d without needing an explicit model density q(x).

Evaluating Values and Gradients at Separation d

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

Integral Probability Metrics: A Panel of Auditors

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)

  • Three choices of \mathcal{G}: Bounded |g| \le \tfrac12 gives TV; RKHS unit ball \|\mathbf{w}\|_2 \le 1 gives MMD (closed-form); 1-Lipschitz \textrm{Lip}_1 gives Wasserstein W_1 (WGAN).

Two Competing Requirements on Test Class G

Sample Efficiency vs. Far-Sighted Gradients

  • Failure 1 (\mathcal{G} too rich / spiky): For |g(x)| \le \tfrac12, two independent N-samples from \mathcal{N}(0, 1) share no points almost surely, so g(x) = \mathbf{1}_{x \in \{X_i\}} - \tfrac12 gives empirical \textrm{TV}(\hat{P}_N, \hat{Q}_N) = 1.00 even when P = Q.
  • Failure 2 (\mathcal{G} bounded in height): Any class with |g(x)| \le M caps the gap at 2M, saturating with zero gradient on distant modes.
  • 1-Lipschitz resolution: Bounded slope |\nabla g| \le 1 prevents sample memorization and grows linearly with distance d.

From Linear and Quadratic Features to Mean Maps

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

  • Feature richness: Linear \varphi(x) = x is blind to variance on \mathcal{N}(0,1) vs. \mathcal{N}(0,4), whereas \boldsymbol{\varphi}(x) = (x, x^2) separates them (\|\mathbf{m}_P - \mathbf{m}_Q\|_2 = 3), and the characteristic RBF kernel embeds all moments j = 0, \ldots, \infty.

The Kernel Trick Eliminates the Supremum

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)]

  • No critic to train: Within-group similarities minus twice cross-group similarity cancel to 0 iff P = Q (for characteristic kernels).

Characteristic Kernels and Closed-Form RBF MMD

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:

  • Infinite feature expansion: Factoring e^{-(x-x')^2/(2\ell^2)} = e^{-x^2/(2\ell^2)}e^{-x'^2/(2\ell^2)}\sum_{j=0}^\infty \frac{(xx'/\ell^2)^j}{j!} reveals an inner product over infinitely many damped monomial features \varphi_j(x) = \frac{x^j}{\ell^j\sqrt{j!}}e^{-x^2/(2\ell^2)}.
  • Characteristic property: Because these features separate all distributions, \textrm{MMD}(P, Q) = 0 \iff P = Q, making RBF-MMD a true metric on probability distributions (Gretton et al., 2012).
  • Closed form on Gaussians (\ell = 1): Using \mathbb{E}[e^{-Z^2/(2\ell^2)}] = (1 + s^2/\ell^2)^{-1/2}e^{-\mu^2/(2(\ell^2+s^2))} for Z \sim \mathcal{N}(\mu, s^2):
    • On \mathcal{N}(0, 1) vs. \mathcal{N}(0, 4), x - x' \sim \mathcal{N}(0, 2), y - y' \sim \mathcal{N}(0, 8), and x - y \sim \mathcal{N}(0, 5), giving \textrm{MMD}^2 = \frac{1}{\sqrt{3}} + \frac{1}{\sqrt{9}} - \frac{2}{\sqrt{6}} \approx 0.0942.
    • On shifted Gaussians \mathcal{N}(0, 1) vs. \mathcal{N}(d, 1), \textrm{MMD}^2 = \frac{2}{\sqrt{3}}(1 - e^{-d^2/6}), which saturates as d \gg \ell.

Unbiased U-Statistic Estimation from Samples

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)

The Witness Function and Tail Gradient Vanishing

Why Fixed-Bandwidth MMD Is Nearsighted

Which test function g^\star \in \mathcal{G} attains the maximum gap in \textrm{MMD}(P, Q)?

  • Closed-form witness function: By Cauchy–Schwarz, \mathbf{w}^\star \propto \mathbf{m}_P - \mathbf{m}_Q, so the optimal auditor (the witness) is: g^\star(x) \propto \mathbb{E}_{X \sim P}[k(x, X)] - \mathbb{E}_{Y \sim Q}[k(x, Y)]
  • Sign crossing: Positive where P dominates and negative where Q dominates (crossing 0 at x = 0.25 for \mathcal{N}(0, 1) vs. \mathcal{N}(0.5, 1)).
  • Vanishing tail gradients: A generator pushes samples uphill along \nabla g^\star(y), but beyond a few length scales \ell in the tails, g^\star(x) flattens to 0.

witness changes sign at x = 0.35
E_P g - E_Q g = 0.0360

Optimal Transport and Wasserstein-1 Distance

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]

  • Physical meaning (Monge, 1781): \gamma(x, y) specifies how much earth moves from source x to pit y; W_1(P, Q) is the minimum total mass \times distance work required to reshape P into Q.
  • Exact distance recovery at every separation d:
    • Disjoint point masses (\delta_0 vs. \delta_d): The unique coupling puts all mass on (0, d), giving W_1(\delta_0, \delta_d) = |d| with gradient \pm 1.
    • Shifted Gaussians (\mathcal{N}(0, 1) vs. \mathcal{N}(d, 1)): Translating y = x + d costs d, and by Jensen’s inequality \mathbb{E}_\gamma|x - y| \ge |\mathbb{E}_P[X] - \mathbb{E}_Q[Y]| = |d|, so W_1 = |d| with constant slope 1 for all d > 0.

Visualizing Transport Plans and 1D CDF Areas

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:

image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Kantorovich–Rubinstein Duality: 1-Lipschitz Critics

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)

  • Weak duality in one line: Since g(x) - g(y) \le \|x - y\| pointwise for \|g\|_{\textrm{Lip}} \le 1, taking \mathbb{E}_\gamma gives \mathbb{E}_P[g] - \mathbb{E}_Q[g] \le \mathbb{E}_\gamma[\|x - y\|].

Exact 1D Optimal Transport: Area Between CDFs

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

  • Step 1 (Lower bound on any plan): Writing |X - Y| = \int \mathbf{1}(t \text{ between } X \text{ and } Y)\,dt and taking \mathbb{E}_\gamma shows that net mass crossing t is at least |\gamma(X \le t < Y) - \gamma(Y \le t < X)| = |F_P(t) - F_Q(t)|.
  • Step 2 (Monotone quantile coupling attains the area): Let U \sim \mathrm{Unif}(0, 1) and pair quantiles X = F_P^{-1}(U), Y = F_Q^{-1}(U). Because X and Y never send mass in opposite directions across any t, its cost equals \int |F_P(t) - F_Q(t)|\,dt.
  • Step 3 & 4 (Optimal 1-Lipschitz dual critic): By Fubini’s theorem, \mathbb{E}_P[g] - \mathbb{E}_Q[g] = \int_{-\infty}^\infty g'(t)(F_Q(t) - F_P(t))\,dt, which attains \int |F_P(t) - F_Q(t)|\,dt at slope g'(t) = \textrm{sign}(F_Q(t) - F_P(t)) \in \{\pm 1\}. \blacksquare

Verifying 1D Transport: CDF Area vs. 36-Var LP

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

Squared Cost W₂ and Fréchet Inception Distance

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

  • Strict 1D uncrossing: For x < x' and y < y', uncrossing saves 2(x' - x)(y' - y) > 0, making the monotone quantile coupling strictly unique.
  • Fréchet Inception Distance (FID): Multivariate Gaussian W_2^2 evaluates image generators in feature space.

Entropic Regularization and the Gibbs Kernel

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)

  • Lagrangian diagonal-scaling solution: Introducing multipliers \xi_i for row sums \sum_j \gamma_{ij} = p_i and \zeta_j for column sums \sum_i \gamma_{ij} = q_j and setting \frac{\partial \mathcal{L}}{\partial \gamma_{ij}} = C_{ij} + \varepsilon \log \gamma_{ij} - \xi_i - \zeta_j = 0 yields: \gamma_{ij} = e^{\xi_i/\varepsilon} e^{-C_{ij}/\varepsilon} e^{\zeta_j/\varepsilon} \iff \boldsymbol{\gamma}_\varepsilon = \textrm{diag}(\mathbf{a})\,\mathbf{K}\,\textrm{diag}(\mathbf{b}), \qquad K_{ij} = e^{-C_{ij}/\varepsilon}
  • The optimal regularized coupling is simply the elementwise Gibbs kernel \mathbf{K} = e^{-\mathbf{C}/\varepsilon} rescaled by positive row and column vectors \mathbf{a}, \mathbf{b} > \mathbf{0}.

Sinkhorn Matrix Scaling and the ε Trade-Off

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}}

  • Differentiable on GPUs: Gradients backpropagate directly through unrolled Sinkhorn steps.
  • The \varepsilon trade-off: Large \varepsilon converges fast but blurs \boldsymbol{\gamma}_\varepsilon; small \varepsilon \to 0 recovers the exact LP, but the step count grows fast (over 800\times from \varepsilon = 0.5 to 0.1).

Sinkhorn Convergence Across ε on the 6-Point Grid

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

Fixed Kernels vs. Critics: Curse of Dimension

Statistical & Computational Trade-Offs

  • Fixed-Kernel MMD: Closed-form \mathcal{O}(N^2) U-statistic with dimension-free sample error \mathcal{O}(N^{-1/2}) in any dimension n, but tail gradients vanish for d \gg \ell.
  • Wasserstein-1 (W_1): Unit gradient at all distances d, but empirical sample-to-sample W_1 suffers the curse of dimensionality \mathcal{O}(N^{-1/n}) for n > 2 (requiring neural critic restriction in WGAN).

Concept Check: Empirical TV, 1D W₁, and Critics

Check Your Understanding

Test your understanding of IPMs and optimal transport:

  1. Empirical Total Variation failure: You draw two independent samples of N = 1{,}000 points each from \mathcal{N}(0, 1). What is the exact Total Variation distance between the two empirical distributions \hat{P}_N and \hat{Q}_N, and what does the unbiased RBF-\textrm{MMD}^2 estimator report instead?
  2. 1D Wasserstein vs. TV under rescaling: For P = (0.6, 0.3, 0.1) and Q = (0.2, 0.5, 0.3) supported on atoms \{0, 1, 2\}, compute \textrm{TV}(P, Q) and W_1(P, Q). What happens to both quantities if the atoms are stretched to \{0, 2, 4\}?
  3. Optimal critic for variance shift: For P = \mathcal{N}(0, 1) and Q = \mathcal{N}(0, 4), the 1D monotone coupling maps X \mapsto Y = 2X. Use this coupling to compute W_1(P, Q) and identify the optimal 1-Lipschitz critic g^\star(x).

Concept Check: Empirical TV, 1D W₁, and Critics (cont.)

Check Your Understanding

Answers:

  1. Empirical TV: Because continuous draws share zero points almost surely, the indicator of the first sample gives \textrm{TV}(\hat{P}_N, \hat{Q}_N) = 1.0, whereas unbiased RBF-\textrm{MMD}^2 cancels first-order noise and reports \approx 0.000 \pm 0.002.
  2. 1D W_1 on 3 atoms: \textrm{TV} = 0.4. The CDFs are F_P = (0.6, 0.9, 1.0) and F_Q = (0.2, 0.7, 1.0), with gaps |F_P - F_Q| = (0.4, 0.2) across unit intervals, so W_1 = 0.4(1) + 0.2(1) = 0.6. Stretching atoms to \{0, 2, 4\} doubles interval widths (\Delta x = 2), doubling W_1 \to 1.2 while \textrm{TV} = 0.4 stays invariant.
  3. Gaussian variance shift: Under Y = 2X, |X - Y| = |X|, so W_1 = \mathbb{E}_{X \sim \mathcal{N}(0,1)}[|X|] = \sqrt{2/\pi} \approx 0.798. Because F_P(t) < F_Q(t) for t < 0 and F_P(t) > F_Q(t) for t > 0, the optimal critic has slope g'(t) = -\textrm{sign}(t), giving g^\star(x) = -|x| (pulling wide samples inward).

Summary: Integral Probability Metrics & Transport

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