# [R] Transport and Earth Mover's Distance

Schuhmacher, Dominic dominic.schuhmacher at mathematik.uni-goettingen.de
Tue Mar 7 14:31:57 CET 2017

```Dear Lorenzo,

The code you have attached (if you replace 00 by 50) generates two sets of 50 points each that are uniformly distributed on the unit square [0,1]^2 and then computes the Wasserstein-1 distance between them assuming that each point has mass 1/50. Do the following to get a plot of the optimal point assignment (the average of the line lengths is the Wasserstein-1 distance).

x <- pp(matrix(runif(100),50,2))
y <- pp(matrix(runif(100),50,2))
match <- transport(x,y,p=1)
plot(x,y,match)
wasserstein(x,y,p=1,match)

Currently the transport package only matches "objects" with the same total mass, i.e. point patterns (class pp) with equal numbers of points, weighted point patterns (class wpp) where e.g. the mass at each point is given by 1/(no. of points), and pixel grids (class pgrid) which are supposed to have the same total mass summed over all pixel values.

If you have no particular need for binning, check out the function pppdist in the R-package spatstat, which offers a more flexible way to deal with point patterns of different size. If you need to bin, check out transport.pgrid, but be aware that it does not do "incomplete transport", but will normalize both pixel grids to unit total mass if these masses are different.

Best regards,
Dominic

> Am 07.03.2017 um 13:29 schrieb Lorenzo Isella <lorenzo.isella at gmail.com>:
>
> Dear All,
> From time to time I need to resort to the calculation of the earth
> mover' distance (see
>
> https://en.wikipedia.org/wiki/Earth_mover's_distance and
> https://en.wikipedia.org/wiki/Wasserstein_metric .
>
> In the past I used the package
>
> https://r-forge.r-project.org/projects/earthmovdist/
>
> which apparently is no longer available, but there is plenty of choice
> in R.
>
> From the transport package, I found this example
>
> set.seed(27)
> x <- pp(matrix(runif(100),50,2))
> y <- pp(matrix(runif(100),00,2))
> wasserstein(x,y,p=1)
>
> but it is not 100% clear to me how to interpret it.
> Are x and y meant as histograms where the the center of each bin is
> provided and the total mass in the bins is automatically normalized to
> 1?
>
> Essentially, my situation is that I have two univariate samples of unequal
> size. I would like to bin them and calculate the earth mover's
> distance between them.
>
> I am not sure if this is what the example above does.
> Cheers
>
> Lorenzo
>
>

------------------------------------
Prof. Dr. Dominic Schuhmacher
Institute for Mathematical Stochastics
University of Goettingen
Goldschmidtstrasse 7
D-37077 Goettingen
Germany

Phone: +49 (0)551 39172107
E-mail: dominic.schuhmacher at mathematik.uni-goettingen.de

```

More information about the R-help mailing list