Skip to main content

OT1D: Discrete Optimal Transport in 1D by Linear Programming

build-update Actions Status Downloads Downloads Downloads

The OT1D library offers a simple but efficient implementation of an algorithm to compute the Kantorovich-Wasserstein distance between two empirical measures defined in dimension 1, that is, measures whose support points lie in R. We designed the algorithm by directly exploiting the complementary slackness conditions of Linear Programming. The implementation focuses on efficiency rather than genericity, and we try to be as efficient as possible in several notable cases. We implemented the core algorithm in standard C++11, and we provide a Python 3 wrapper, which can be installed with:

pip3 install ot1d

The OT1D library provides an implementation of Optimal Transport in 1D that is faster than:

  1. SciPy: it is at least 5x faster than scipy.stats.wasserstein_distance, and up to 30x faster on sorted input.
  2. POT: it is at least 3x faster than ot.lp.wasserstein_1d, and up to 45x faster on sorted input.

The actual speedup depends on your computer (e.g., the number of cores), your OS, and your compiler. To run a performance test on your computer, see below, or run the Python script OT1D_test.py. For some strange reason, the speedups on Mac laptops are larger than on other architectures.

REMARK: If you find instances where OT1D is slower, please let us know.

DotLIB

This tiny library is part of dotlib, a larger project to develop Optimal Transport algorithms based on efficient Linear Programming implementations.

  • not1d by Pietro Peterlongo: a partial port of OT1D to Nim, which can also be used from Python. It computes the distance (without the transportation plan) on a single thread, using a radix sort.

Basic Usage: Colab Notebook

The simplest way to test this library is to run the following notebook on Colab:

Date Notebook Link
[2026/11/15] Testing and evaluating OT1D Open In Colab

Usage

The main function of the OT1D library is the following:

z = OT1D(x, y, A=None, B=None, p=1, sorting=True, threads=8, plan=False)

The parameters of the function are:

  • x: the support points of the first measure.
  • y: the support points of the second measure.
  • A: the (nonnegative) weights of the first measure. If equal to None, all the samples have the same mass.
  • B: the (nonnegative) weights of the second measure. If equal to None, all the samples have the same mass. A and B must be given together. If their total masses differ, a RuntimeWarning is issued and only the smaller total mass is transported (partial value and plan).
  • p: the order of the Wasserstein distance (p=1 or p=2).
  • sorting: if equal to True, the function sorts the support points given as input; if equal to False, the input must already be sorted.
  • threads: the number of threads used by the parallel sorting algorithm.
  • plan: if equal to True, the function also returns an optimal transportation plan, as a triple z, T, W: the row T[k] = (i, j) means that the mass W[k] moves from x[i] to y[j] (see the example interpolate.py).

The first four parameters can be given as numpy arrays (preferred) or Python lists.

Sorting at the speed of light

In addition, we expose the following in-place parallel sorting function:

parasort(x, A=None, threads=8)

The parameters of the function are:

  • x: the support points of a given measure.
  • A: the weights of the given measure. If equal to None, only the support points are sorted.
  • threads: the number of threads used by the parallel sorting algorithm.

The first two parameters must be numpy arrays of floats, since they are sorted in place.

Details

Given two empirical distributions, the Kantorovich-Wasserstein distance is given by the optimal solution of a linear program, known as the transportation problem. While this is a general linear program, when the costs are defined among points belonging to the real line, the problem can be solved with an algorithm having worst-case time complexity of O(n log n). This can be shown by first writing the dual linear program, and then the complementary slackness conditions.

The key step of the algorithm is sorting the two arrays of support points x and y. We sort the arrays by using a customized parallel sorting algorithm implemented in C++, which combines the very fast pdqsort with parasort. See the linked webpages for the licenses of these two libraries.

Prerequisites for compilation

To compile the source code and the Python wrapper, you only need:

  • A C++ compiler compliant with the C++11 standard.
  • cython
  • numpy

You might need to install the Python development headers, which on Linux can be installed with:

apt install python3-dev  # Ubuntu

Installation

To install OT1D, you can run the following command:

pip3 install ot1d

Testing

To test the library, you can run the following command:

python3 basic_test.py

A basic usage example is the following:

import numpy as np
from OT1D import OT1D, parasort

np.random.seed(13)

N = 1000000

# Uniform samples
x = np.random.uniform(1, 2, N)
y = np.random.uniform(0, 1, N)

z = OT1D(x, y, p=2, sorting=True, threads=16)

print('Wasserstein distance of order 2, W2(x,y) =', z)

and the output should be similar to:

Wasserstein distance of order 2, W2(x,y) = 1.0002549459050794

Testing for correctness

The repository includes two test suites, which compare every function against an independent reference implementation, and check that the transportation plans are feasible and optimal:

make test                         # C++ tests (also: make sanitize)
cd python && python3 test_wrapper.py  # Python wrapper tests (requires scipy)

Testing for performance

The following results can be reproduced by running the following command (you need to have installed scipy and pot):

python3 OT1D_test.py

whose output should be similar to the following (but it depends on your platform). The speedup is the ratio between the runtime of each library and the runtime of OT1D. TEST 5 checks that the Wasserstein distance of order 2 computed by OT1D matches the one computed by POT (SciPy only computes W1), on instances from 1 to 5 million points; note that POT returns W2 squared, so the script compares OT1D with its square root.

--------------- TEST 3: Unsorted input (average runtime) --------------------
For OT1D using 8 threads

running test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Testing W1, samples of deltas, n=m
Scipy: average time = 0.18 speedup = 9.7
POT  : average time = 0.168 speedup = 9.0
OT1D : average time = 0.019 speedup = 1.0

Testing W2, samples of deltas, n=m
POT  : average time = 0.124 speedup = 6.6
OT1D : average time = 0.019 speedup = 1.0

Testing W1, samples with weights
Scipy: average time = 0.193 speedup = 5.1
POT  : average time = 0.165 speedup = 4.3
OT1D : average time = 0.038 speedup = 1.0

Testing W2, samples with weights
POT  : average time = 0.12 speedup = 3.1
OT1D : average time = 0.039 speedup = 1.0

--------------- TEST 4: Sorted input (average runtime) --------------------
For OT1D using 8 threads

running test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parallel sorting: time = 0.027 sec

Testing W1, samples of deltas, n=m
Scipy: average time = 0.077 speedup = 30.1
POT  : average time = 0.116 speedup = 45.5
OT1D : average time = 0.003 speedup = 1.0

Testing W2, samples of deltas, n=m
POT  : average time = 0.09 speedup = 35.5
OT1D : average time = 0.003 speedup = 1.0

Testing W1, samples with weights
Scipy: average time = 0.087 speedup = 6.7
POT  : average time = 0.117 speedup = 9.0
OT1D : average time = 0.013 speedup = 1.0

Testing W2, samples with weights
POT  : average time = 0.087 speedup = 6.5
OT1D : average time = 0.013 speedup = 1.0

--------------- TEST 5: W2 against POT (correctness) --------------------
Comparing OT1D with sqrt(POT), since POT returns W2^2

m =       1, n =       1, weights = False | POT: z = 4.98342038 time = 0.0002 | OT1D: z = 4.98342038 time = 0.0000
m =       1, n =       1, weights = True  | POT: z = 4.98342038 time = 0.0001 | OT1D: z = 4.98342038 time = 0.0000
m =       2, n =       3, weights = False | POT: z = 3.46826482 time = 0.0001 | OT1D: z = 3.46826482 time = 0.0000
m =       2, n =       3, weights = True  | POT: z = 4.10324470 time = 0.0001 | OT1D: z = 4.10324470 time = 0.0000
m =      10, n =      10, weights = False | POT: z = 2.86894031 time = 0.0001 | OT1D: z = 2.86894031 time = 0.0000
m =      10, n =      10, weights = True  | POT: z = 2.93894066 time = 0.0001 | OT1D: z = 2.93894066 time = 0.0000
m =      17, n =       5, weights = False | POT: z = 3.18112337 time = 0.0001 | OT1D: z = 3.18112337 time = 0.0000
m =      17, n =       5, weights = True  | POT: z = 3.40960305 time = 0.0001 | OT1D: z = 3.40960305 time = 0.0000
m =    1000, n =    1000, weights = False | POT: z = 3.18595988 time = 0.0002 | OT1D: z = 3.18595988 time = 0.0000
m =    1000, n =    1000, weights = True  | POT: z = 3.23862020 time = 0.0002 | OT1D: z = 3.23862020 time = 0.0001
m =    1000, n =     777, weights = False | POT: z = 3.12133828 time = 0.0002 | OT1D: z = 3.12133828 time = 0.0000
m =    1000, n =     777, weights = True  | POT: z = 3.08777081 time = 0.0002 | OT1D: z = 3.08777081 time = 0.0001
m =  100000, n =  100000, weights = False | POT: z = 3.16226321 time = 0.0168 | OT1D: z = 3.16226321 time = 0.0061
m =  100000, n =  100000, weights = True  | POT: z = 3.15995238 time = 0.0175 | OT1D: z = 3.15995238 time = 0.0067
m =  100000, n =   65537, weights = False | POT: z = 3.15278684 time = 0.0136 | OT1D: z = 3.15278684 time = 0.0041
m =  100000, n =   65537, weights = True  | POT: z = 3.15374537 time = 0.0181 | OT1D: z = 3.15374537 time = 0.0058
m = 5000000, n = 5000000, weights = False | POT: z = 3.16297776 time = 1.9577 | OT1D: z = 3.16297776 time = 0.1855
m = 5000000, n = 5000000, weights = True  | POT: z = 3.16249183 time = 1.9836 | OT1D: z = 3.16249183 time = 0.4073

All W2 values match POT: max relative error = 1.42e-11

Please contact us by email if you encounter any issues.

Author and maintainer

Stefano Gualandi, stefano.gualandi@gmail.com.

Metadata

Release files for OT1D 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for OT1D 0.4.0
File Size Uploaded Provenance Actions
ot1d-0.4.0.tar.gz 201.1 kB Without provenance Details for ot1d-0.4.0.tar.gz

Release files / ot1d-0.4.0.tar.gz

Download URL ot1d-0.4.0.tar.gz
Size 201.1 kB
Tags Source
SHA-256 checksum
How to use checksums
04b2f9d8d4ad32544ddb6d6a3afd7e890674d0651fc00f6454e83aff6ca351a0
BLAKE2b-256 checksum
How to use checksums
ba274a0b7206500ae6ea77081ddb2f0ea5d4ce60057c01c05ccaba98b81387a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.11

Security for OT1D 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page