summaryrefslogtreecommitdiff
path: root/ot
diff options
context:
space:
mode:
authorRomain Tavenard <romain.tavenard@univ-rennes2.fr>2019-06-27 10:23:32 +0200
committerRomain Tavenard <romain.tavenard@univ-rennes2.fr>2019-06-27 10:23:32 +0200
commit0d333e004636f5d25edea6bb195e8e4d9a95ba98 (patch)
tree75b40d64101c0f503e6ed8d3101db1c095ffbd72 /ot
parent1140141938c678d267f688dbb9106d3422d633c5 (diff)
Improved tests and docs for wasserstein_1d
Diffstat (limited to 'ot')
-rw-r--r--ot/__init__.py5
-rw-r--r--ot/lp/__init__.py13
-rw-r--r--ot/lp/emd_wrap.pyx3
3 files changed, 11 insertions, 10 deletions
diff --git a/ot/__init__.py b/ot/__init__.py
index f0e526c..5bd9bb3 100644
--- a/ot/__init__.py
+++ b/ot/__init__.py
@@ -22,7 +22,7 @@ from . import smooth
from . import stochastic
# OT functions
-from .lp import emd, emd2, emd_1d, emd2_1d
+from .lp import emd, emd2, emd_1d, emd2_1d, wasserstein_1d, wasserstein2_1d
from .bregman import sinkhorn, sinkhorn2, barycenter
from .da import sinkhorn_lpl1_mm
@@ -32,5 +32,6 @@ from .utils import dist, unif, tic, toc, toq
__version__ = "0.5.1"
__all__ = ["emd", "emd2", 'emd_1d', "sinkhorn", "sinkhorn2", "utils", 'datasets',
- 'bregman', 'lp', 'tic', 'toc', 'toq', 'gromov', 'emd_1d', 'emd2_1d',
+ 'bregman', 'lp', 'tic', 'toc', 'toq', 'gromov',
+ 'emd_1d', 'emd2_1d', 'wasserstein_1d', 'wasserstein2_1d',
'dist', 'unif', 'barycenter', 'sinkhorn_lpl1_mm', 'da', 'optim']
diff --git a/ot/lp/__init__.py b/ot/lp/__init__.py
index 719032b..76c9ec0 100644
--- a/ot/lp/__init__.py
+++ b/ot/lp/__init__.py
@@ -530,13 +530,13 @@ def emd2_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,
def wasserstein_1d(x_a, x_b, a=None, b=None, p=1., dense=True, log=False):
- """Solves the Wasserstein distance problem between 1d measures and returns
+ """Solves the p-Wasserstein distance problem between 1d measures and returns
the OT matrix
.. math::
- \gamma = arg\min_\gamma \left(\sum_i \sum_j \gamma_{ij}
- |x_a[i] - x_b[j]|^p \right)^{1/p}
+ \gamma = arg\min_\gamma \left( \sum_i \sum_j \gamma_{ij}
+ |x_a[i] - x_b[j]|^p \\right)^{1/p}
s.t. \gamma 1 = a,
\gamma^T 1= b,
@@ -617,15 +617,14 @@ def wasserstein_1d(x_a, x_b, a=None, b=None, p=1., dense=True, log=False):
dense=dense, log=log)
-def wasserstein2_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1.,
- dense=True, log=False):
- """Solves the Wasserstein distance problem between 1d measures and returns
+def wasserstein2_1d(x_a, x_b, a=None, b=None, p=1., dense=True, log=False):
+ """Solves the p-Wasserstein distance problem between 1d measures and returns
the loss
.. math::
\gamma = arg\min_\gamma \left( \sum_i \sum_j \gamma_{ij}
- |x_a[i] - x_b[j]|^p \right)^{1/p}
+ |x_a[i] - x_b[j]|^p \\right)^{1/p}
s.t. \gamma 1 = a,
\gamma^T 1= b,
diff --git a/ot/lp/emd_wrap.pyx b/ot/lp/emd_wrap.pyx
index 7134136..42b848f 100644
--- a/ot/lp/emd_wrap.pyx
+++ b/ot/lp/emd_wrap.pyx
@@ -13,6 +13,7 @@ cimport numpy as np
from ..utils import dist
cimport cython
+cimport libc.math as math
import warnings
@@ -159,7 +160,7 @@ def emd_1d_sorted(np.ndarray[double, ndim=1, mode="c"] u_weights,
elif metric == 'cityblock' or metric == 'euclidean':
m_ij = abs(u[i] - v[j])
elif metric == 'minkowski':
- m_ij = abs(u[i] - v[j]) ** p
+ m_ij = math.pow(abs(u[i] - v[j]), p)
else:
m_ij = dist(u[i].reshape((1, 1)), v[j].reshape((1, 1)),
metric=metric)[0, 0]