Wasserstein metric

In mathematics, the Wasserstein distance or Kantorovich–Rubinstein metric is a distance function defined between probability distributions on a given metric space $$M$$. It is named after Leonid Vaseršteĭn.

Intuitively, if each distribution is viewed as a unit amount of earth (soil) piled on $$M$$, the metric is the minimum "cost" of turning one pile into the other, which is assumed to be the amount of earth that needs to be moved times the mean distance it has to be moved. This problem was first formalised by Gaspard Monge in 1781. Because of this analogy, the metric is known in computer science as the earth mover's distance.

The name "Wasserstein distance" was coined by R. L. Dobrushin in 1970, after learning of it in the work of Leonid Vaseršteĭn on Markov processes describing large systems of automata (Russian, 1969). However the metric was first defined by Leonid Kantorovich in The Mathematical Method of Production Planning and Organization (Russian original 1939) in the context of optimal transport planning of goods and materials. Some scholars thus encourage use of the terms "Kantorovich metric" and "Kantorovich distance". Most English-language publications use the German spelling "Wasserstein" (attributed to the name "Vaseršteĭn" (Васерштейн) being of Yiddish origin).

Definition
Let $$(M,d)$$ be a metric space that is a Polish space. For $$p \in [1, +\infty]$$, the Wasserstein $$p$$-distance between two probability measures $$\mu$$ and $$\nu$$ on $$M$$ with finite $$p$$-moments is $$W_p(\mu, \nu) = \inf_{\gamma \in \Gamma(\mu, \nu)} \left(\mathbf{E}_{(x, y) \sim \gamma} d(x, y)^p \right)^{1/p},$$ where $$\Gamma(\mu, \nu)$$ is the set of all couplings of $$\mu$$ and $$\nu$$; $$W_\infty(\mu, \nu)$$ is defined to be $$\lim_{p\rightarrow +\infty} W_p(\mu, \nu)$$ and corresponds to a supremum norm. A coupling $$\gamma$$ is a joint probability measure on $$M \times M$$ whose marginals are $$\mu$$ and $$\nu$$ on the first and second factors, respectively. That is, for all measurable $$A\subset M$$ a coupling fulfills $$\int_A\int_M \gamma(x, y) \,\mathrm{d}y \,\mathrm{d}x= \mu(A),$$ $$\int_A \int_M \gamma(x, y) \,\mathrm{d}x \,\mathrm{d}y= \nu(A).$$

Intuition and connection to optimal transport
One way to understand the above definition is to consider the optimal transport problem. That is, for a distribution of mass $$\mu(x)$$ on a space $$X$$, we wish to transport the mass in such a way that it is transformed into the distribution $$\nu(x)$$ on the same space; transforming the 'pile of earth' $$\mu$$ to the pile $$\nu$$. This problem only makes sense if the pile to be created has the same mass as the pile to be moved; therefore without loss of generality assume that $$\mu$$ and $$\nu$$ are probability distributions containing a total mass of 1. Assume also that there is given some cost function

$$c(x,y) \geq 0$$

that gives the cost of transporting a unit mass from the point $$x$$ to the point $$y$$. A transport plan to move $$\mu$$ into $$\nu$$ can be described by a function $$\gamma(x,y)$$ which gives the amount of mass to move from $$x$$ to $$y$$. You can imagine the task as the need to move a pile of earth of shape $$\mu$$ to the hole in the ground of shape $$\nu$$ such that at the end, both the pile of earth and the hole in the ground completely vanish. In order for this plan to be meaningful, it must satisfy the following properties:

the amount of earth moved out of point $$x$$ must equal the amount that was there to begin with; that is,

$$\int \gamma(x,y) \,\mathrm{d} y = \mu(x),$$

and the amount of earth moved into point $$y$$ must equal the depth of the hole that was there at the beginning; that is,

$$\int \gamma(x,y) \,\mathrm{d} x = \nu(y).$$ 

That is, that the total mass moved out of an infinitesimal region around $$x$$ must be equal to $$\mu(x) \mathrm{d}x$$ and the total mass moved into a region around $$y$$ must be $$\nu(y)\mathrm{d}y$$. This is equivalent to the requirement that $$\gamma$$ be a joint probability distribution with marginals $$\mu$$ and $$\nu$$. Thus, the infinitesimal mass transported from $$x$$ to $$y$$ is $$\gamma(x,y) \, \mathrm{d} x \, \mathrm{d} y$$, and the cost of moving is $$c(x,y) \gamma(x,y) \, \mathrm{d} x \, \mathrm{d} y$$, following the definition of the cost function. Therefore, the total cost of a transport plan $$\gamma$$ is $$ \iint c(x,y) \gamma(x,y) \, \mathrm{d} x \, \mathrm{d} y = \int c(x,y) \, \mathrm{d} \gamma(x,y). $$

The plan $$\gamma$$ is not unique; the optimal transport plan is the plan with the minimal cost out of all possible transport plans. As mentioned, the requirement for a plan to be valid is that it is a joint distribution with marginals $$\mu$$ and $$\nu$$; letting $$\Gamma$$ denote the set of all such measures as in the first section, the cost of the optimal plan is $$ C = \inf_{\gamma \in \Gamma(\mu, \nu)} \int c(x,y) \, \mathrm{d} \gamma(x,y). $$ If the cost of a move is simply the distance between the two points, then the optimal cost is identical to the definition of the $$W_1$$ distance.

Deterministic distributions
Let $$\mu_{1} = \delta_{a_{1}}$$ and $$\mu_{2} = \delta_{a_{2}}$$ be two degenerate distributions (i.e. Dirac delta distributions) located at points $$a_{1}$$ and $$a_{2}$$ in $$\mathbb{R}$$. There is only one possible coupling of these two measures, namely the point mass $$\delta_{(a_{1}, a_{2})}$$ located at $$(a_{1}, a_{2}) \in \mathbb{R}^{2}$$. Thus, using the usual absolute value function as the distance function on $$\mathbb{R}$$, for any $$p \geq 1$$, the $$p$$-Wasserstein distance between $$\mu_{1}$$ and $$\mu_2$$ is $$W_p (\mu_1, \mu_2) = | a_1 - a_2 | .$$ By similar reasoning, if $$\mu_{1} = \delta_{a_{1}}$$ and $$\mu_{2} = \delta_{a_{2}}$$ are point masses located at points $$a_{1}$$ and $$a_{2}$$ in $$\mathbb{R}^{n}$$, and we use the usual Euclidean norm on $$\mathbb{R}^{n}$$ as the distance function, then $$W_p(\mu_1, \mu_2) = \| a_1 - a_2 \|_2 .$$

One dimension
If $$P$$ is an empirical measure with samples $$X_1, \ldots, X_n$$ and $$Q$$ is an empirical measure with samples $$Y_1, \ldots, Y_n$$, the distance is a simple function of the order statistics: $$W_p(P, Q) = \left( \frac{1}{n}\sum_{i=1}^n \|X_{(i)} - Y_{(i)}\|^p \right)^{1/p}.$$

Higher dimensions
If $$P$$ and $$Q$$ are empirical distributions, each based on $$n$$ observations, then

$$W_p(P, Q) = \inf_\pi \left( \frac{1}{n} \sum_{i=1}^n \|X_i - Y_{\pi(i)}\|^p \right)^{1/p},$$

where the infimum is over all permutations $$\pi$$ of $$n$$ elements. This is a linear assignment problem, and can be solved by the Hungarian algorithm in cubic time.

Normal distributions
Let $$\mu_1 = \mathcal{N}(m_1, C_1)$$ and $$\mu_2 = \mathcal{N}(m_2, C_2)$$ be two non-degenerate Gaussian measures (i.e. normal distributions) on $$\mathbb{R}^n$$, with respective expected values $$m_1$$ and $$m_2 \in \mathbb{R}^n$$ and symmetric positive semi-definite covariance matrices $$C_{1}$$ and $$C_2 \in \mathbb{R}^{n \times n}$$. Then, with respect to the usual Euclidean norm on $$\mathbb{R}^{n}$$, the 2-Wasserstein distance between $$\mu_{1}$$ and $$\mu_{2}$$ is $$W_{2} (\mu_1, \mu_2)^2 = \| m_1 - m_2 \|_2^2 + \mathop{\mathrm{trace}} \bigl( C_1 + C_2 - 2 \bigl( C_2^{1/2} C_1 C_2^{1/2} \bigr)^{1/2} \bigr) .$$ where $$C^{1/2}$$ denotes the principal square root of $$C$$. Note that the second term (involving the trace) is precisely the (unnormalised) Bures metric between $$C_1$$ and $$C_2$$. This result generalises the earlier example of the Wasserstein distance between two point masses (at least in the case $$p = 2$$), since a point mass can be regarded as a normal distribution with covariance matrix equal to zero, in which case the trace term disappears and only the term involving the Euclidean distance between the means remains.

One-dimensional distributions
Let $$\mu_1, \mu_2 \in P_p(\mathbb{R})$$ be probability measures on $$\mathbb{R}$$, and denote their cumulative distribution functions by $$F_1(x)$$ and $$F_2(x)$$. Then the transport problem has an analytic solution: Optimal transport preserves the order of probability mass elements, so the mass at quantile $$q$$ of $$\mu_1$$ moves to quantile $$q$$ of $$\mu_2$$. Thus, the $$p$$-Wasserstein distance between $$\mu_1$$ and $$\mu_2$$ is $$W_p(\mu_1, \mu_2) = \left(\int_0^1 \left| F_1^{-1}(q) - F_2^{-1}(q) \right|^p \, \mathrm{d} q\right)^{1/p},$$ where $$F_1^{-1}$$ and $$F_2^{-1}$$ are the quantile functions (inverse CDFs). In the case of $$p=1$$, a change of variables leads to the formula $$W_1(\mu_1, \mu_2) = \int_{\mathbb{R}} \left| F_1(x) - F_2(x) \right| \, \mathrm{d} x. $$

Applications
The Wasserstein metric is a natural way to compare the probability distributions of two variables X and Y, where one variable is derived from the other by small, non-uniform perturbations (random or deterministic).

In computer science, for example, the metric W1 is widely used to compare discrete distributions, e.g. the color histograms of two digital images; see earth mover's distance for more details.

In their paper 'Wasserstein GAN', Arjovsky et al. use the Wasserstein-1 metric as a way to improve the original framework of generative adversarial networks (GAN), to alleviate the vanishing gradient and the mode collapse issues. The special case of normal distributions is used in a Frechet inception distance.

The Wasserstein metric has a formal link with Procrustes analysis, with application to chirality measures, and to shape analysis.

In computational biology, Wasserstein metric can be used to compare between persistence diagrams of cytometry datasets.

The Wasserstein metric also has been used in inverse problems in geophysics.

The Wasserstein metric is used in integrated information theory to compute the difference between concepts and conceptual structures.

The Wasserstein metric and related formulations have also been used to provide a unified theory for shape observable analysis in high energy and collider physics datasets.

Metric structure
It can be shown that Wp satisfies all the axioms of a metric on the Wasserstein space Pp(M) consisting of all Borel probability measures on M having finite pth moment. Furthermore, convergence with respect to Wp is equivalent to the usual weak convergence of measures plus convergence of the first pth moments.

Dual representation of W1
The following dual representation of W1 is a special case of the duality theorem of Kantorovich and Rubinstein (1958): when μ and ν have bounded support,

$$W_1 (\mu, \nu) = \sup \left\{ \left. \int_{M} f(x) \, \mathrm{d} (\mu - \nu) (x) \, \right| \text{ continuous } f : M \to \mathbb{R}, \operatorname{Lip} (f) \leq 1 \right\},$$

where Lip(f) denotes the minimal Lipschitz constant for f. This form shows that W1 is an integral probability metric.

Compare this with the definition of the Radon metric:

$$\rho (\mu, \nu) := \sup \left\{ \left. \int_M f(x) \, \mathrm{d} (\mu - \nu) (x) \, \right| \text{ continuous } f : M \to [-1, 1] \right\}.$$

If the metric d of the metric space (M,d) is bounded by some constant C, then

$$2 W_1 (\mu, \nu) \leq C \rho (\mu, \nu),$$

and so convergence in the Radon metric (identical to total variation convergence when M is a Polish space) implies convergence in the Wasserstein metric, but not vice versa.

Proof
The following is an intuitive proof which skips over technical points. A fully rigorous proof is found in.

Discrete case: When $$M$$ is discrete, solving for the 1-Wasserstein distance is a problem in linear programming: $$\begin{cases} \min_\gamma \sum_{x, y} c(x, y) \gamma(x, y) \\ \sum_y \gamma(x, y) = \mu(x) \\ \sum_x \gamma(x, y) = \nu(y) \\ \gamma \geq 0 \end{cases} $$ where $$c: M \times M \to [0, \infty)$$ is a general "cost function".

By carefully writing the above equations as matrix equations, we obtain its dual problem: $$\begin{cases} \max_{f, g} \sum_x \mu(x)f(x) + \sum_y \nu(y)g(y)\\ f(x) + g(y) \leq c(x, y)	 \end{cases} $$ and by the duality theorem of linear programming, since the primal problem is feasible and bounded, so is the dual problem, and the minimum in the first problem equals the maximum in the second problem. That is, the problem pair exhibits strong duality.

For the general case, the dual problem is found by converting sums to integrals: $$\begin{cases} \sup_{f, g} \mathbb E_{x\sim \mu}[f(x)] + \mathbb E_{y\sim \nu}[g(y)]\\ f(x) + g(y) \leq c(x, y)	 \end{cases} $$ and the strong duality still holds. This is the Kantorovich duality theorem. Cédric Villani recounts the following interpretation from Luis Caffarelli: Suppose you want to ship some coal from mines, distributed as $$\mu$$, to factories, distributed as $$\nu$$. The cost function of transport is $$c$$. Now a shipper comes and offers to do the transport for you. You would pay him $$f(x)$$ per coal for loading the coal at $$x$$, and pay him $$g(y)$$ per coal for unloading the coal at $$y$$.

For you to accept the deal, the price schedule must satisfy $$f(x) + g(y) \leq c(x, y)$$. The Kantorovich duality states that the shipper can make a price schedule that makes you pay almost as much as you would ship yourself. This result can be pressed further to yield:$$

The two infimal convolution steps are visually clear when the probability space is $$\R$$.

For notational convenience, let $$\square$$ denote the infimal convolution operation.

For the first step, where we used $$f = \text{cone} \mathbin{\square} (-g)$$, plot out the curve of $$-g$$, then at each point, draw a cone of slope 1, and take the lower envelope of the cones as $$f$$, as shown in the diagram, then $$f$$ cannot increase with slope larger than 1. Thus all its secants have slope $$\bigg| \frac{f(x) - f(y)}{x-y}\bigg| \leq 1$$.

For the second step, picture the infimal convolution $$\text{cone} \mathbin{\square} (-f)$$, then if all secants of $$f$$ have slope at most 1, then the lower envelope of $$\text{cone} \mathbin{\square} (-f) $$ are just the cone-apices themselves, thus $$\text{cone} \mathbin{\square} (-f)=-f$$.

1D Example. When both $$\mu, \nu$$ are distributions on $$\R$$, then integration by parts give $$\mathbb{E}_{x\sim \mu}[f(x)] - \mathbb E_{y\sim \nu}[f(y)] = \int f'(x) (F_\nu(x) - F_\mu(x)) \, \mathrm{d}x,$$ thus $$f(x) = K \cdot \operatorname{sign}(F_\nu(x) - F_\mu(x)).$$

Fluid mechanics interpretation of W2
Benamou & Brenier found a dual representation of $$W_2$$ by fluid mechanics, which allows efficient solution by convex optimization.

Given two probability densities $$p, q$$ on $$\R^n$$, $$W_2(p, q)=\min_{\bold{v}} \int_0^1 \int_{\R^n} \|\bold{v}(\bold{x}, t)\|^2 \rho(\bold{x}, t) \, d \bold{x} \, dt$$where $$\bold{v}$$ ranges over velocity fields driving the continuity equation with boundary conditions on the fluid density field: $$ \dot{\rho}+\nabla\cdot(\rho\bold{v})=0 \quad \rho(\cdot, 0)=p,\; \rho(\cdot, 1)=q $$ That is, the mass should be conserved, and the velocity field should transport the probability distribution $$p$$ to $$q$$ during the time interval $$[0, 1]$$.

Equivalence of W2 and a negative-order Sobolev norm
Under suitable assumptions, the Wasserstein distance $$W_2$$ of order two is Lipschitz equivalent to a negative-order homogeneous Sobolev norm. More precisely, if we take $$M$$ to be a connected Riemannian manifold equipped with a positive measure $$\pi$$, then we may define for $$f \colon M \to \mathbb{R}$$ the seminorm $$\| f \|_{\dot{H}^{1}(\pi)}^{2} = \int_{M} \|\nabla f(x)\|^{2} \, \pi(\mathrm{d} x)$$ and for a signed measure $$\mu$$ on $$M$$ the dual norm $$\| \mu \|_{\dot{H}^{-1}(\pi)} = \sup \bigg\{ | \langle f, \mu \rangle | \,\bigg|\, \| f \|_{\dot{H}^{1}(\pi)} \leq 1 \bigg\} .$$ Then any two probability measures $$\mu$$ and $$\nu$$ on $$M$$ satisfy the upper bound $$W_{2} (\mu, \nu) \leq 2\, \| \mu - \nu \|_{\dot{H}^{-1}(\pi)} .$$ In the other direction, if $$\mu$$ and $$\nu$$ each have densities with respect to the standard volume measure on $$M$$ that are both bounded above by some $$0 < C < \infty$$, and $$M$$ has non-negative Ricci curvature, then $$\| \mu - \nu \|_{\dot{H}^{-1}(\pi)} \leq \sqrt{C}\, W_{2} (\mu, \nu) .$$

Separability and completeness
For any p ≥ 1, the metric space (Pp(M), Wp) is separable, and is complete if (M, d) is separable and complete.

Wasserstein distance for p = &infin;
It is also possible to consider the Wasserstein metric for $$p = \infty$$. In this case, the defining formula becomes: $$ W_{\infty}(\mu,\nu) = \lim_{p \rightarrow +\infty} W_p(\mu,\nu) = \inf_{\gamma \in \Gamma(\mu, \nu) } \gamma\operatorname{-essup} d(x,y), $$ where $$\gamma\operatorname{-essup} d(x,y)$$ denotes the essential supremum of $$d(x,y)$$ with respect to measure $$\gamma$$. The metric space (P∞(M), W∞) is complete if (M, d) is separable and complete. Here, P∞ is the space of all probability measures with bounded support.