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
and the 2-Wasserstein distance as
Here
The code assumes (unlike for instance the bioconductor function) that
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.