# Wasserstein metric

Short description: Distance function defined between probability distributions

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

Intuitively, if each distribution is viewed as a unit amount of earth (soil) piled on $\displaystyle{ 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[1] (Russian, 1969). However the metric was first defined by Leonid Kantorovich in The Mathematical Method of Production Planning and Organization[2] (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 German origin).

## Definition

Let $\displaystyle{ (M,d) }$ be a metric space for which every Borel probability measure on $\displaystyle{ M }$ is a Radon measure (a so-called Radon space). For $\displaystyle{ p\ge1 }$, let $\displaystyle{ P_p(M) }$ denote the collection of all probability measures $\displaystyle{ \mu }$ on $\displaystyle{ M }$ with finite $\displaystyle{ p^{\text{th}} }$ moment, that is, there exists some $\displaystyle{ x_0 }$ in $\displaystyle{ M }$ such that:

$\displaystyle{ \int_M d(x, x_0)^{p} \, \mathrm{d} \mu (x) \lt \infty. }$

The $\displaystyle{ p^\text{th} }$ Wasserstein distance between two probability measures $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ in $\displaystyle{ P_p(M) }$ is defined as

$\displaystyle{ W_p (\mu, \nu):=\left( \inf_{\gamma \in \Gamma (\mu, \nu)} \int_{M \times M} d(x, y)^p \, \mathrm{d} \gamma (x, y) \right)^{1/p}, }$

where $\displaystyle{ \Gamma(\mu,\nu) }$ denotes the collection of all measures on $\displaystyle{ M \times M }$ with marginals $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ on the first and second factors respectively. (The set $\displaystyle{ \Gamma(\mu,\nu) }$ is also called the set of all couplings of $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$.)

The above distance is usually denoted $\displaystyle{ W_p(\mu,\nu) }$ (typically among authors who prefer the "Wasserstein" spelling) or $\displaystyle{ \ell_p(\mu,\nu) }$ (typically among authors who prefer the "Vaserstein" spelling). The remainder of this article will use the $\displaystyle{ W_p }$ notation.

The Wasserstein metric may be equivalently defined by

$\displaystyle{ W_{p} (\mu, \nu) = \left( \inf \operatorname{\mathbf{E}} \big[ d( X , Y )^p \big] \right)^{1/p}, }$

where $\displaystyle{ \mathbf{E}[Z] }$ denotes the expected value of a random variable $\displaystyle{ Z }$ and the infimum is taken over all joint distributions of the random variables $\displaystyle{ X }$ and $\displaystyle{ Y }$ with marginals $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ respectively.

## Intuition and connection to optimal transport

Two one-dimensional distributions $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$, plotted on the x and y axes, and one possible joint distribution that defines a transport plan between them. The joint distribution/transport plan is not unique

One way to understand the above definition is to consider the optimal transport problem. That is, for a distribution of mass $\displaystyle{ \mu(x) }$ on a space $\displaystyle{ X }$, we wish to transport the mass in such a way that it is transformed into the distribution $\displaystyle{ \nu(x) }$ on the same space; transforming the 'pile of earth' $\displaystyle{ \mu }$ to the pile $\displaystyle{ \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 $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ are probability distributions containing a total mass of 1. Assume also that there is given some cost function

$\displaystyle{ c(x,y) \mapsto [0,\infty) }$

that gives the cost of transporting a unit mass from the point $\displaystyle{ x }$ to the point $\displaystyle{ y }$. A transport plan to move $\displaystyle{ \mu }$ into $\displaystyle{ \nu }$ can be described by a function $\displaystyle{ \gamma(x,y) }$ which gives the amount of mass to move from $\displaystyle{ x }$ to $\displaystyle{ y }$. You can imagine the task as the need to move a pile of earth of shape $\displaystyle{ \mu }$ to the hole in the ground of shape $\displaystyle{ \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

\displaystyle{ \begin{align} \int \gamma(x,y) \,\mathrm{d} y = \mu(x) & \qquad \text{(the amount of earth moved out of point } x \text{ needs to equal the amount that was there to begin with)} \\ \int \gamma(x,y) \,\mathrm{d} x = \nu(y) & \qquad \text{(the amount of earth moved into point } y \text{ needs to equal the depth of the hole that was there at the beginning)} \end{align} }

That is, that the total mass moved out of an infinitesimal region around $\displaystyle{ x }$ must be equal to $\displaystyle{ \mu(x) \mathrm{d}x }$ and the total mass moved into a region around $\displaystyle{ y }$ must be $\displaystyle{ \nu(y)\mathrm{d}y }$. This is equivalent to the requirement that $\displaystyle{ \gamma }$ be a joint probability distribution with marginals $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$. Thus, the infinitesimal mass transported from $\displaystyle{ x }$ to $\displaystyle{ y }$ is $\displaystyle{ \gamma(x,y) \, \mathrm{d} x \, \mathrm{d} y }$, and the cost of moving is $\displaystyle{ 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 $\displaystyle{ \gamma }$ is

$\displaystyle{ \iint c(x,y) \gamma(x,y) \, \mathrm{d} x \, \mathrm{d} y = \int c(x,y) \, \mathrm{d} \gamma(x,y) }$

The plan $\displaystyle{ \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 $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$; letting $\displaystyle{ \Gamma }$ denote the set of all such measures as in the first section, the cost of the optimal plan is

$\displaystyle{ 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 $\displaystyle{ W_1 }$ distance.

## Examples

### Point masses (degenerate distributions)

Let $\displaystyle{ \mu_{1} = \delta_{a_{1}} }$ and $\displaystyle{ \mu_{2} = \delta_{a_{2}} }$ be two degenerate distributions (i.e. Dirac delta distributions) located at points $\displaystyle{ a_{1} }$ and $\displaystyle{ a_{2} }$ in $\displaystyle{ \mathbb{R} }$. There is only one possible coupling of these two measures, namely the point mass $\displaystyle{ \delta_{(a_{1}, a_{2})} }$ located at $\displaystyle{ (a_{1}, a_{2}) \in \mathbb{R}^{2} }$. Thus, using the usual absolute value function as the distance function on $\displaystyle{ \mathbb{R} }$, for any $\displaystyle{ p \geq 1 }$, the $\displaystyle{ p }$-Wasserstein distance between $\displaystyle{ \mu_{1} }$ and $\displaystyle{ \mu_2 }$ is

$\displaystyle{ W_p (\mu_1, \mu_2) = | a_1 - a_2 | . }$

By similar reasoning, if $\displaystyle{ \mu_{1} = \delta_{a_{1}} }$ and $\displaystyle{ \mu_{2} = \delta_{a_{2}} }$ are point masses located at points $\displaystyle{ a_{1} }$ and $\displaystyle{ a_{2} }$ in $\displaystyle{ \mathbb{R}^{n} }$, and we use the usual Euclidean norm on $\displaystyle{ \mathbb{R}^{n} }$ as the distance function, then

$\displaystyle{ W_p (\mu_1, \mu_2) = \| a_1 - a_2 \|_2 . }$

### Normal distributions

Let $\displaystyle{ \mu_1 = \mathcal{N}(m_1, C_1) }$ and $\displaystyle{ \mu_2 = \mathcal{N}(m_2, C_2) }$ be two non-degenerate Gaussian measures (i.e. normal distributions) on $\displaystyle{ \mathbb{R}^n }$, with respective expected values $\displaystyle{ m_1 }$ and $\displaystyle{ m_2 \in \mathbb{R}^n }$ and symmetric positive semi-definite covariance matrices $\displaystyle{ C_{1} }$ and $\displaystyle{ C_2 \in \mathbb{R}^{n \times n} }$. Then,[3] with respect to the usual Euclidean norm on $\displaystyle{ \mathbb{R}^{n} }$, the 2-Wasserstein distance between $\displaystyle{ \mu_{1} }$ and $\displaystyle{ \mu_{2} }$ is

$\displaystyle{ 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) . }$

This result generalises the earlier example of the Wasserstein distance between two point masses (at least in the case $\displaystyle{ 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 $\displaystyle{ \mu_1, \mu_2 \in P_p(\mathbb{R}) }$ be probability measures on $\displaystyle{ \mathbb{R} }$, and denote their cumulative distribution functions by $\displaystyle{ F_1(x) }$ and $\displaystyle{ 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 $\displaystyle{ q }$ of $\displaystyle{ \mu_1 }$ moves to quantile $\displaystyle{ q }$ of $\displaystyle{ \mu_2 }$. Thus, the $\displaystyle{ p }$-Wasserstein distance between $\displaystyle{ \mu_1 }$ and $\displaystyle{ \mu_2 }$ is

$\displaystyle{ 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 $\displaystyle{ F_1^{-1} }$ and $\displaystyle{ F_2^{-1} }$ are the quantile functions (inverse CDFs). In the case of $\displaystyle{ p=1 }$, a change of variables leads to the formula

$\displaystyle{ 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.[4] 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,[5] and to shape analysis.[6]

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

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

## Properties

### Metric structure

It can be shown that Wp satisfies all the axioms of a metric on Pp(M). Furthermore, convergence with respect to Wp is equivalent to the usual weak convergence of measures plus convergence of the first pth moments.[9]

### 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,

$\displaystyle{ 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.

Compare this with the definition of the Radon metric:

$\displaystyle{ \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 is bounded by some constant C, then

$\displaystyle{ 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.

### Equivalence of W2 and a negative-order Sobolev norm

Under suitable assumptions, the Wasserstein distance $\displaystyle{ W_2 }$ of order two is Lipschitz equivalent to a negative-order homogeneous Sobolev norm.[10] More precisely, if we take $\displaystyle{ M }$ to be a connected Riemannian manifold equipped with a positive measure $\displaystyle{ \pi }$, then we may define for $\displaystyle{ f \colon M \to \mathbb{R} }$ the seminorm

$\displaystyle{ \| f \|_{\dot{H}^{1}(\pi)}^{2} = \int_{M} | \nabla f(x) |^{2} \, \pi(\mathrm{d} x) }$

and for a signed measure $\displaystyle{ \mu }$ on $\displaystyle{ M }$ the dual norm

$\displaystyle{ \| \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 $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ on $\displaystyle{ M }$ satisfy the upper bound

$\displaystyle{ W_{2} (\mu, \nu) \leq 2 \| \mu - \nu \|_{\dot{H}^{-1}(\mu)} . }$

In the other direction, if $\displaystyle{ \mu }$ and $\displaystyle{ \nu }$ each have densities with respect to the standard volume measure on $\displaystyle{ M }$ that are both bounded above some $\displaystyle{ 0 \lt C \lt \infty }$, and $\displaystyle{ M }$ has non-negative Ricci curvature, then

$\displaystyle{ \| \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.[11]

## References

1. "Markov processes over denumerable products of spaces, describing large systems of automata". Problemy Peredači Informacii 5 (3): 64–72. 1969.
2. "Mathematical Methods of Organizing and Planning Production". Management Science 6 (4): 366–422. 1939. doi:10.1287/mnsc.6.4.366.
3. "The distance between two random vectors with given dispersion matrices". Linear Algebra and Its Application 48: 257–263. October 1982. doi:10.1016/0024-3795(82)90112-4. ISSN 0024-3795.
4. "Wasserstein Generative Adversarial Networks". International Conference on Machine Learning 214-223: 214–223. July 2017.
5. "Chiral mixtures". Journal of Mathematical Physics 43 (8): 4147–4157. 2002. doi:10.1063/1.1484559. Bibcode2002JMP....43.4147P.
6. "From shape similarity to shape complementarity: toward a docking theory". Journal of Mathematical Chemistry 35 (3): 147–158. 2004. doi:10.1023/B:JOMC.0000033252.59423.6b.
7. "Determining clinically relevant features in cytometry data using persistent homology". PLOS Computational Biology 18 (3): e1009931. March 2022. doi:10.1371/journal.pcbi.1009931. PMID 35312683. Bibcode2022PLSCB..18E9931M.
8. Frederick, Christina; Yang, Yunan (2022-05-06). "Seeing through rock with help from optimal transport". Snapshots of Modern Mathematics from Oberwolfach. doi:10.14760/SNAP-2022-004-EN.
9. "An elementary proof of the triangle inequality for the Wasserstein metric". Proceedings of the American Mathematical Society 136 (1): 333–339. 2008. doi:10.1090/S0002-9939-07-09020-X.
10. "Comparison between W2 distance and −1 norm, and localization of Wasserstein distance". ESAIM: Control, Optimisation and Calculus of Variations 24 (4): 1489–1501. October 2018. doi:10.1051/cocv/2017050. ISSN 1292-8119.  (See Theorems 2.1 and 2.5.)
11. "The Monge–Kantorovich problem: achievements, connections, and perspectives". Russian Mathematical Surveys 67 (5): 785–890. October 2012. doi:10.1070/RM2012v067n05ABEH004808. Bibcode2012RuMaS..67..785B.