Given two vectors a
and b
, compute the Wasserstein distance of
order p
between their empirical distributions.
wasserstein1d(a, b, p = 1, wa = NULL, wb = NULL)
A single number, the Wasserstein distance for the specified data.
two vectors.
a positive number. The order of the Wasserstein distance.
optional vectors of non-negative weights for a
and b
.
Dominic Schuhmacher dschuhm1@uni-goettingen.de
The Wasserstein distance of order p
is defined as the p
-th root of the total cost incurred when transporting a pile of mass into another pile of mass in an optimal way, where the cost of transporting a unit of mass from \(x\) to \(y\) is given as the p
-th power \(\|x-y\|^p\) of the Euclidean distance.
In the present function the vector a
represents the locations on the real line of \(m\) deposits of mass \(1/m\) and the vector b
the locations of \(n\) deposits of mass \(1/n\). If the user specifies weights wa
and wb
, these default masses are replaced by wa/sum(wa)
and wb/sum(wb)
, respectively.
In terms of the empirical distribution function \(F(t) = \sum_{i=1}^m w^{(a)}_i 1\{a_i \leq t\}\) of locations \(a_i\) with normalized weights \(w^{(a)}_i\), and the corresponding function \(G(t) = \sum_{j=1}^n w^{(b)}_j 1\{b_j \leq t\}\) for b
, the Wasserstein distance in 1-d is given as
$$W_p(F,G) = \left(\int_0^1 |F^{-1}(u)-G^{-1}(u)|^p \; du \right)^{1/p},$$
where \(F^{-1}\) and \(G^{-1}\) are generalized inverses. If \(p=1\), we also have $$W_1(F,G) = \int_{-\infty}^{\infty} |F(x)-G(x)| \; dx.$$
wasserstein
x <- rnorm(200)
y <- rnorm(150,2)
wasserstein1d(x,y)
Run the code above in your browser using DataLab