Earth Mover’s Distance

In statistics, the earth mover’s distance (EMD) is a measure of the distance between two probability distributions over a region .

In general, we use the EMD to quantify the cost of transporting things from to , and minimize that cost, which is essentially the Optimal Transport problem.

This talk is an interesting idea: Detecting Anomalies Using Statistical Distances | SciPy 2018 | Charles Masson

Also See also Sinkhorn Distance.

Compute EMD

We can compute the EMD by framing it as an Optimal Transport problem, The EMD can be computed by solving an instance of transportation problem, using any algorithm for minimum-cost flow problem, e.g. the network simplex algorithm.

The Hungarian algorithm can be used to get the solution if the domain

As a special case, if is a one-dimensional array of “bins” of size n, the EMD can be efficiently computed by scanning the array and keeping track of how much dirt needs to be transported between consecutive bins. Here the bins are zero-indexed:

import torch
def EMD(p, q):
    x = p - q
    y = torch.cumsum(x, dim=0)
    return y.abs().sum()

Optimal Transport Problem

This took me so long to understand how it is calculated, see example calculation here. actually this is way better.

Why EMD vs. simpler metrics like Euclidean Distance? See this Stackoverflow thread.

  • Because EMD is more designed to measure distances between distributions
  • L1/L2 distance don’t model these points are distributions (when we use K-Means Clustering, we try to find a centroid such that the variance of the cluster is minimized)
  • Ex: Visualize the distances between . Euclidean Distance would treat them as equivalent, but EMD would not because there is a transport cost associated.

Notes from this youtube video

Geodesics in the Wasserstein Metric space can preserve the shape of the function, whereas a function distance would not preserve the shape.

It’s using the concept of work.


I first heard about this through the Poker AI project, but apparently it is super used in Poker AI project, but apparently it is super used in Computer Vision: this paper has been cited 5293 times

Also applied in the world of Natural Language Processing

EMD in linear time :

Computing distance between two points is easy. Computing distance between two areas is more difficult. How would you compute the distance between the US and the UK?

  • Using the centroids of the two countries?
  • Closest distance?
  • Average distance using chunks of the countries?
  • Computing distance between two distributions?

We discretize these distributions with a histogram.

EMD is commonly used in computer vision for comparing colour distribution or texture histograms of images for content based image retrieval.

  • Wasserstein GAN