OT1D: Discrete Optimal Transport in 1D by Linear Programming
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:
- SciPy: it is at least 5x faster than scipy.stats.wasserstein_distance, and up to 30x faster on sorted input.
- 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.
Related projects
- 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/17] | Testing and evaluating OT1D |
Usage
The main function of the OT1D library is the following:
z = OT1D(x, y, A=None, B=None, p=1, sorting=True, threads=1, 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 toNone, all the samples have the same mass.B: the (nonnegative) weights of the second measure. If equal toNone, all the samples have the same mass.AandBmust be given together. If their total masses differ, aRuntimeWarningis issued and only the smaller total mass is transported (partial value and plan).p: the order of the Wasserstein distance (p=1orp=2).sorting: if equal toTrue, the function sorts the support points given as input; if equal toFalse, the input must already be sorted.threads: the number of threads used by the parallel sorting algorithm (default1). Threads only speed up the sorting phase, so they help only on large inputs withsorting=True.plan: if equal toTrue, the function also returns an optimal transportation plan, as a triplez, T, W: the rowT[k] = (i, j)means that the massW[k]moves fromx[i]toy[j](see the exampleinterpolate.py).
The first four parameters can be given as numpy arrays (preferred) or Python lists.
All the functions release the GIL while running the C++ code, so several calls can run in parallel from different Python threads (for instance, with a ThreadPoolExecutor); in this case, keep threads=1.
Sorting at the speed of light
In addition, we expose the following in-place parallel sorting function:
parasort(x, A=None, threads=1)
The parameters of the function are:
x: the support points of a given measure.A: the weights of the given measure. If equal toNone, 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.
On x86-64 CPUs, the Python wrapper sorts with the vectorized quicksort of x86-simd-sort (BSD 3-Clause License), the same library used by np.sort. The best instruction set supported by the CPU is selected at runtime: AVX-512 (for instance, Intel Skylake-X and later, AMD Zen 4 and later), AVX2 (almost every x86-64 CPU since 2013), or none, falling back to pdqsort and parasort. With AVX-512 a single thread sorts 3 to 8 times faster than pdqsort, and with AVX2 about 2.5 times faster, so threads matters only for weighted measures (or with a plan) of at least one million points. You can check which one is used with OT1D.simd(), and select another one with OT1D.set_simd('avx2') (or 'avx512', 'none'), for instance to compare timings. To build without them, set the environment variable OT1D_NO_SIMD=1 before installing.
In C++, the header OT1D.hpp uses pdqsort and parasort by default. To use the SIMD sorting, compile include/ot1d_avx2.cpp and/or include/ot1d_avx512.cpp (C++17) with the corresponding flags, compile your code with -DOT1D_USE_AVX2 and/or -DOT1D_USE_AVX512, and link these object files last, AVX2 first (see the target test-simd in the Makefile).
Prerequisites for compilation
To compile the source code and the Python wrapper, you only need:
- A C++ compiler compliant with the
C++11standard (C++17for the optional SIMD sorting). - 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.
Use of AI tools
The algorithms in this library were designed and originally implemented by hand by the author. For release 0.4.0, the author used Claude Code (the Anthropic CLI) with the Opus 5.5 model to debug and test the library. In that process, Claude found and fixed several bugs, restructured parts of the C++ header OT1D.hpp while fixing them, and wrote the test suites. Every change was reviewed and committed by the author, who remains solely responsible for the code.
#code-written-by-human-hands
Author and maintainer
Stefano Gualandi, stefano.gualandi@gmail.com.
Metadata
Release files for OT1D 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ot1d-0.4.1.tar.gz | 249.1 kB | Without provenance | Details for ot1d-0.4.1.tar.gz |
Release files / ot1d-0.4.1.tar.gz
| Download URL | ot1d-0.4.1.tar.gz |
|---|---|
| Size | 249.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a73cd6c29297ceea46f3d2285e29937593a0b60fc4529c1d80b5ea77a40e7c9e
|
|
BLAKE2b-256 checksum How to use checksums |
1788a6fae698786967e7ea3a5dea4d4c2d64a6972b618f77abeb3b1e554dbe33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.12
|
Security for OT1D 0.4.1
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.