You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a compact Matlab code to compute the 1- and 2-Wasserstein distances for one dimensional probability distributions.
Refer to Wikipedia for a general definition of the Wasserstein distance $w_p$. This implementation is based on the fact that for given distributions $u$ and $v$ the 1-Wasserstein distance can be written as
Here $U_\text{cdf}$ and $V_\text{cdf}$ refer to the cumulative density functions of $u$ and $v$ and $U_\text{icdf}$ and $V_\text{icdf}$ to the corresponding pseudo inverse cumulative density functions defined by
The code assumes (unlike for instance the bioconductor function) that $u$ and $v$ are discrete and uniform probability distribution. In this case there are samples $u_1, u_2, \dots, u_N$ such that any $u$ distributed random variable $X_u$ satisfies $P(X_u = u_k)=1/N$ for all $k$. These samples are the inputs of the function, let's assume without loss of generality that they are increasingly sorted then the cumulative distribution function and its pseudo inverse are given by the step functions
The code has been tested in Matlab R2017a and checked against the scipy Wasserstein distance (in case of the 1-Wasserstein distance) and the earth mover distance code on Matlab Central File Exchange. The latter code allows for computations of the general p-Wasserstein distance but is computationally more complex than the present code for the described situation.
Some references
Carrillo, J.A. and G. Toscani. „Wasserstein Metric and Large-Time Asymptotics of Nonlinear Diffusion Equations“. In New Trends in Mathematical Physics, 234–44. Naples, Italy: WORLD SCIENTIFIC, 2005. https://doi.org/10.1142/9789812702319_0022.