Rust Optimal Transport

This library provides solvers for performing regularized and unregularized Optimal Transport in Rust.

Inspired by Python Optimal Transport, this library provides the following solvers: - Network simplex algorithm for linear program / Earth Movers Distance - Entropic regularization OT solvers including Sinkhorn Knopp and Greedy Sinkhorn - Unbalanced Sinkhorn Knopp

Installation

The library has been tested on macOS. It requires a C++ compiler for building the EMD solver and relies on the following Rust libraries:

Cargo installation

Edit your Cargo.toml with the following to use rust-optimal-transport in your project.

toml [dependencies] rust-optimal-transport = "0.1"

Features

If you would like to enable LAPACK backend (currently supporting OpenBLAS):

toml [dependencies] rust-optimal-transport = { version = "0.1", features = ["blas"] }

This will link against an installed instance of OpenBLAS on your system. For more details see the ndarray-linalg crate.

Examples

Short examples

```rust use rustoptimaltransport as ot; use ot::prelude::*;

```

```rust // Generate data let n_samples = 100;

// Mean, Covariance of the source distribution let musource = array![0., 0.]; let covsource = array![[1., 0.], [0., 1.]];

// Mean, Covariance of the target distribution let mutarget = array![4., 4.]; let covtarget = array![[1., -0.8], [-0.8, 1.]];

// Samples of a 2D gaussian distribution let source = ot::utils::sample2Dgauss(nsamples, &musource, &covsource).unwrap(); let target = ot::utils::sample2Dgauss(nsamples, &mutarget, &covtarget).unwrap();

// Uniform weights on the source and target distributions let mut sourceweights = Array1::::fromelem(n, 1. / (n as f64)); let mut targetweights = Array1::::fromelem(n, 1. / (n as f64));

// Compute ground cost matrix - Squared Euclidean distance let mut cost = dist(&source, &target, SqEuclidean); let max_cost = cost.max().unwrap();

// Normalize cost matrix for numerical stability cost = &cost / *max_cost;

// Compute optimal transport matrix as the Earth Mover's Distance let otmatrix = match EarthMovers::new( &mut sourceweights, &mut targetweights, &mut groundcost ).solve()?;

```

Acknowledgements

This library is inspired by Python Optimal Transport. The original authors and contributors of that project are listed at POT.