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 . This implementation is based on the fact that for given distributions u and v the 1-Wasserstein distance can be written as
and the 2-Wasserstein distance as
Here and refer to the cumulative density functions of u and v and and 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 such that any u distributed random variable satisfies 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.
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.
Kantorovich, L. V. „On the Translocation of Masses“. Journal of Mathematical Sciences 133, No. 4 (March 2006): 1381–82. https://doi.org/10.1007/s10958-006-0049-2.