# 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" (Russian: Васерштейн) 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)^{\frac {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. Here, 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. This means that for all measurable $A\subset M$, it fulfills $\gamma (A\times M)=\mu (A)$ and $\gamma (M\times A)=\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.

## Examples
### Point masses
#### 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}.$

#### Empirical distributions
##### 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)^{\frac {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)^{\frac {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} } \left(C_{1}+C_{2}-2\left(C_{2}^{\frac {1}{2}}C_{1}C_{2}^{\frac {1}{2}}\right)^{\frac {1}{2}}\right).$ where $C^{\frac {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)^{\frac {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 W$_1$ 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.

## Properties
### Metric structure

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

### Dual representation of W$_1$

The following dual representation of W$_1$ 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 W$_1$ 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
$2W_{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 }\displaystyle \sum _{x,y}c(x,y)\gamma (x,y)\\[6pt]\displaystyle \sum _{y}\gamma (x,y)=\mu (x)\\[6pt]\displaystyle \sum _{x}\gamma (x,y)=\nu (y)\\[6pt]\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}\displaystyle \sum _{x}\mu (x)f(x)+\sum _{y}\nu (y)g(y)\\[6pt]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)]\\[6pt]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:
Theorem (Kantorovich-Rubenstein duality) — When the probability space $\Omega$ is a metric space, then
for any fixed $K>0$, $W_{1}(\mu ,\nu )={\frac {1}{K}}\sup _{\|f\|_{L}\leq K}\mathbb {E} _{x\sim \mu }[f(x)]-\mathbb {E} _{y\sim \nu }[f(y)]$ where $\|\cdot \|_{L}$ is the Lipschitz norm.
It suffices to prove the case of $K=1$.
Start with
$W_{1}(\mu ,\nu )=\sup _{f(x)+g(y)\leq d(x,y)}\mathbb {E} _{x\sim \mu }[f(x)]+\mathbb {E} _{y\sim \nu }[g(y)].$ Then, for any choice of $g$, one can push the term higher by setting $f(x)=\inf _{y}d(x,y)-g(y)$, making it an infimal convolution of $-g$ with a cone. This implies $f(x)-f(y)\leq d(x,y)$ for any $x,y$, that is, $\|f\|_{L}\leq 1$.
Thus,
${\begin{aligned}W_{1}(\mu ,\nu )&=\sup _{g}\sup _{f(x)+g(y)\leq d(x,y)}\mathbb {E} _{x\sim \mu }[f(x)]+\mathbb {E} _{y\sim \nu }[g(y)]\\[6pt]&=\sup _{g}\sup _{\|f\|_{L}\leq 1,f(x)+g(y)\leq d(x,y)}\mathbb {E} _{x\sim \mu }[f(x)]+\mathbb {E} _{y\sim \nu }[g(y)]\\[6pt]&=\sup _{\|f\|_{L}\leq 1}\sup _{g,f(x)+g(y)\leq d(x,y)}\mathbb {E} _{x\sim \mu }[f(x)]+\mathbb {E} _{y\sim \nu }[g(y)].\end{aligned}}$ Next, for any choice of $\|f\|_{L}\leq 1$, $g$ can be optimized by setting $g(y)=\inf _{x}d(x,y)-f(x)$. Since $\|f\|_{L}\leq 1$, this implies $g(y)=-f(y)$.
The two infimal convolution steps are visually clear when the probability space is $\mathbb {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 $\mathbb {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 W$_2$

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 $\mathbb {R} ^{n}$, $W_{2}^{2}(p,q)=\min _{\mathbf {v}}\int _{0}^{1}\int _{\mathbb {R} ^{n}}\|{\mathbf {v}}({\mathbf {x}},t)\|^{2}\rho ({\mathbf {x}},t)\,d{\mathbf {x}}\,dt$ where ${\mathbf {v}}$ ranges over velocity fields driving the continuity equation with boundary conditions on the fluid density field: ${\dot {\rho }}+\nabla \cdot (\rho {\mathbf {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 W$_2$ 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 (P$_p$(M), W$_p$) is separable, and is complete if (M, d) is separable and complete.

## Wasserstein distance for p = ∞

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.