2 releases

0.1.1 Jun 29, 2019
0.1.0 Jun 28, 2019

#10 in #earth

MIT license

32KB
748 lines

EMD

Build Status

A simple Rust library for computing the Earth Mover's Distance (or "Wasserstein distance" or "Kantorovich–Rubinstein distance").

This is a wrapper of Gary Doran's pyemd.

Basic usage

Include the following in Cargo.toml:

emd = "0.1.1"

Then:

extern crate emd;
#[macro_use(array)]
extern crate ndarray;

use emd::*;
use ndarray::*;

let x = array![0., 1.];
let y = array![5., 3.];
assert_eq!(distance(&x.view(), &y.view()), 3.5);

Check out the docs for more.

Maintainers

gchers, ehentgen


lib.rs:

Earth Mover's Distance (EMD)

This is a wrapper around pyemd[1] for computing the EMD (or Wasserstein) metric. By default, it uses Euclidean distance as a cost function, but this can be changed by using the generic distance function distance_generic().

Examples

It allows computing the distance between two vectors (e.g., histograms):

extern crate emd;
#[macro_use(array)]
extern crate ndarray;

use emd::*;
use ndarray::*;

let x = array![0., 1.];
let y = array![5., 3.];

assert_eq!(distance(&x.view(), &y.view()), 3.5);

Or between matrices (e.g., multivariate histograms). Note that in this case the two matrices must have the same number of columns.

extern crate emd;
#[macro_use(array)]
extern crate ndarray;

use emd::*;
use ndarray::*;

let x = array![[4., 3.], [3., 6.], [2., 3.]];
let y = array![[2., 3.], [1., 3.]];

assert_eq!(distance_multi(&x.view(), &y.view()), 2.035183758487996);

Alternatively, one can compute EMD for matrices (and, by extension, vectors) with a chosen distance as cost function by using distance_generic().

References

[1] https://github.com/garydoranjr/pyemd

Dependencies

~2MB
~42K SLoC