This library provides solvers for performing regularized and unregularized Optimal Transport in Rust.
Heavily 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
The library has been tested on macOS. It requires a C++ compiler for building the EMD solver and relies on the following Rust libraries:
Edit your Cargo.toml with the following to add ROT as a dependency for your project (uses git url pending publishing on Cargo)
NOTE: Update to the latest commit with cargo update
.
toml
[dependencies]
rust-optimal-transport = { git = "https://github.com/kachark/rust-optimal-transport", branch = "main" }
```rust use rustoptimaltransport as ot;
use ot::lp::emd; ```
```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::distributions::sample2Dgauss(nsamples, &musource, &covsource).unwrap(); let target = ot::utils::distributions::sample2Dgauss(nsamples, &mutarget, &covtarget).unwrap();
// Uniform distribution on the source and target samples
let mut sourcemass = Array1::
// Compute ground cost matrix - Squared Euclidean distance let mut groundcost = ot::utils::metrics::dist(&source, &target, ot::utils::metrics::MetricType::SqEuclidean); let maxcost = ground_cost.max().unwrap();
// Normalize cost matrix for numerical stability groundcost = &groundcost / *max_cost;
// Compute optimal transport matrix as the Earth Mover's Distance let otmatrix = match emd(&mut sourcemass, &mut targetmass, &mut groundcost, None, None) { Ok(result) => result, Err(error) => panic!("{:?}", error) };
```
This library is inspired by Python Optimal Transport. The original authors and contributors of that project are listed at POT.