From 5070a4cb1246d51fcc2a210c06442ad3a3d9286f Mon Sep 17 00:00:00 2001 From: Beihai Date: Tue, 24 Nov 2026 14:40:17 +0800 Subject: [PATCH 1/3] fix: wrap all-negative CDFs in circular Sinkhorn costs When the fractional shift rounds to 1 and accumulated marginals fall slightly below 1, every shifted CDF can be negative. The existing guard required both positive and negative entries, so the wrap was skipped and the returned objective and derivatives were wrong. Wrap whenever a negative CDF exists; add a regression test with an independent LP/exact value. --- ot/lp/solver_circle.py | 4 +-- test/test_circle_wrap_regression.py | 56 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/test_circle_wrap_regression.py diff --git a/ot/lp/solver_circle.py b/ot/lp/solver_circle.py index 2fe34b97a..73947aeec 100644 --- a/ot/lp/solver_circle.py +++ b/ot/lp/solver_circle.py @@ -106,7 +106,7 @@ def derivative_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p=2): v_values[mask_n] += nx.floor(theta)[mask_n] + 1 v_values[mask_p] += nx.floor(theta)[mask_p] - if nx.any(mask_n) and nx.any(mask_p): + if nx.any(mask_n): v_cdf_theta[mask_n] += 1 v_cdf_theta2 = nx.copy(v_cdf_theta) @@ -201,7 +201,7 @@ def ot_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p): v_values[mask_n] += nx.floor(theta)[mask_n] + 1 v_values[mask_p] += nx.floor(theta)[mask_p] - if nx.any(mask_n) and nx.any(mask_p): + if nx.any(mask_n): v_cdf_theta[mask_n] += 1 # Put negative values at the end diff --git a/test/test_circle_wrap_regression.py b/test/test_circle_wrap_regression.py new file mode 100644 index 000000000..cf1ada6c2 --- /dev/null +++ b/test/test_circle_wrap_regression.py @@ -0,0 +1,56 @@ +"""Regression for the all-negative CDF branch in circular Sinkhorn costs.""" + +import numpy as np + +from ot.lp.solver_circle import binary_search_circle, ot_cost_on_circle + + +def _case(): + x = np.array( + [ + 0.16941984065866045, 0.1836324134419436, 0.44653045879629283, + 0.4820980124097798, 0.4917087310586765, 0.8126037842950916, + 0.8510944169432116, 0.9797740750713527, + ] + ) + y = np.array( + [ + 0.27379110343532287, 0.3658703056370922, 0.471481879564172, + 0.4991513312359799, 0.634675836166241, 0.7040614541610074, + 0.8349212395014908, + ] + ) + a = np.array( + [ + 0.11527813452574018, 0.13388447349681654, 0.1598842189785102, + 0.06981890027127201, 0.16354173746775985, 0.1749747944285811, + 0.037675572756166016, 0.14494216807515392, + ] + ) + b = np.array( + [ + 0.043465847478736226, 0.07830347135588803, 0.15577476895197248, + 0.058202097877683455, 0.2908482240722164, 0.2924309055386121, + 0.08097468472489115, + ] + ) + return x, y, a, b + + +def test_tiny_negative_theta_is_continuous(): + x, y, a, b = _case() + uc = np.cumsum(a)[None, :] + vc = np.cumsum(b)[None, :] + assert uc[0, -1] < 1.0 and vc[0, -1] < 1.0 # float accumulation in this case + theta0 = np.zeros((1, len(y))) + theta_neg = np.full((1, len(y)), -1e-16) + c0 = ot_cost_on_circle(theta0, x[None, :], y[None, :], uc, vc, 2)[0] + cneg = ot_cost_on_circle(theta_neg, x[None, :], y[None, :], uc, vc, 2)[0] + np.testing.assert_allclose(cneg, c0, atol=1e-10, rtol=1e-10) + + +def test_binary_search_matches_independent_optimum(): + x, y, a, b = _case() + got = np.asarray(binary_search_circle(x, y, a, b, p=2, eps=1e-6))[0] + # SciPy linprog / exact Fraction breakpoint enumeration value. + np.testing.assert_allclose(got, 0.03158514371890842, atol=1e-9, rtol=1e-9) From 0696d9b76a3f8ce29d6fc910e36a8c1e1b524616 Mon Sep 17 00:00:00 2001 From: Beihai Date: Tue, 24 Nov 2026 15:23:21 +0800 Subject: [PATCH 2/3] test: cover batch composition and document circular CDF fix --- RELEASES.md | 1 + test/test_circle_wrap_regression.py | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/RELEASES.md b/RELEASES.md index b0e12bb7c..1218dbd29 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,6 +22,7 @@ #### Closed issues +- Fix wrong circular optimal-transport costs and derivatives when every shifted target CDF is negative, which could occur when a tiny negative shift rounds its fractional part to one and accumulated weights fall just below one. The wrap is now applied whenever a negative CDF exists, restoring batch-composition invariance. - Fix dead links in the README (three example gallery pages and the reference [39]), the user guide and the contributing guide (PR #928) - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) diff --git a/test/test_circle_wrap_regression.py b/test/test_circle_wrap_regression.py index cf1ada6c2..56a44c9cc 100644 --- a/test/test_circle_wrap_regression.py +++ b/test/test_circle_wrap_regression.py @@ -54,3 +54,36 @@ def test_binary_search_matches_independent_optimum(): got = np.asarray(binary_search_circle(x, y, a, b, p=2, eps=1e-6))[0] # SciPy linprog / exact Fraction breakpoint enumeration value. np.testing.assert_allclose(got, 0.03158514371890842, atol=1e-9, rtol=1e-9) + + +def test_batch_composition_invariance(): + """An all-negative batch must not depend on another batch's sign mask.""" + from ot.lp.solver_circle import derivative_cost_on_circle + + theta = -1e-17 + xA = np.array([0.0, 0.125, 0.25]) + y = np.array([0.0, 0.125, 0.25, 0.875]) + aA = np.array([2.0, 3.0, 1.0]) / 6 + bA = np.array([1.0, 1.0, 3.0, 2.0]) / 7 + xB = np.array([0.5, 0.625, 0.75]) + aB = np.ones(3) / 3 + bB = np.ones(4) / 4 + + def value(x, a, b): + X = x[None, :] if x.ndim == 1 else x + A = a[None, :] if a.ndim == 1 else a + B = b[None, :] if b.ndim == 1 else b + Y = np.repeat(y[None, :], X.shape[0], axis=0) + B = np.repeat(B, X.shape[0], axis=0) if B.shape[0] == 1 else B + th = np.full((X.shape[0], Y.shape[1]), theta) + c = np.asarray(ot_cost_on_circle(th, X, Y, np.cumsum(A, 1), np.cumsum(B, 1), 2)) + dp, dm = derivative_cost_on_circle(th, X, Y, np.cumsum(A, 1), np.cumsum(B, 1), 2) + return c, dp, dm + + a_alone = value(xA, aA, bA)[0][0] + a_with_b = value(np.stack([xA, xB]), np.stack([aA, aB]), np.stack([bA, bB]))[0][0] + a_with_ba = value(np.stack([xB, xA]), np.stack([aB, aA]), np.stack([bB, bA]))[0][1] + a_repeated = value(np.stack([xA, xA]), np.stack([aA, aA]), np.stack([bA, bA]))[0][0] + np.testing.assert_allclose(a_with_b, a_alone, rtol=1e-12, atol=1e-12) + np.testing.assert_allclose(a_with_ba, a_alone, rtol=1e-12, atol=1e-12) + np.testing.assert_allclose(a_repeated, a_alone, rtol=1e-12, atol=1e-12) From d18bfe01ab40baacd7742d4cb5b1e8a14a18ce87 Mon Sep 17 00:00:00 2001 From: Beihai Date: Tue, 24 Nov 2026 16:33:07 +0800 Subject: [PATCH 3/3] test: assert full batch invariance for circular OT helpers Check cost and both one-sided derivatives in the batch-composition regression, and correct the circular optimal transport wording. --- test/test_circle_wrap_regression.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/test_circle_wrap_regression.py b/test/test_circle_wrap_regression.py index 56a44c9cc..4b990380a 100644 --- a/test/test_circle_wrap_regression.py +++ b/test/test_circle_wrap_regression.py @@ -1,4 +1,4 @@ -"""Regression for the all-negative CDF branch in circular Sinkhorn costs.""" +"""Regression for the all-negative CDF branch in circular optimal transport.""" import numpy as np @@ -80,10 +80,14 @@ def value(x, a, b): dp, dm = derivative_cost_on_circle(th, X, Y, np.cumsum(A, 1), np.cumsum(B, 1), 2) return c, dp, dm - a_alone = value(xA, aA, bA)[0][0] - a_with_b = value(np.stack([xA, xB]), np.stack([aA, aB]), np.stack([bA, bB]))[0][0] - a_with_ba = value(np.stack([xB, xA]), np.stack([aB, aA]), np.stack([bB, bA]))[0][1] - a_repeated = value(np.stack([xA, xA]), np.stack([aA, aA]), np.stack([bA, bA]))[0][0] + def at(result, index): + cost, d_cp, d_cm = result + return np.array([cost[index], d_cp[index, 0], d_cm[index, 0]]) + + a_alone = at(value(xA, aA, bA), 0) + a_with_b = at(value(np.stack([xA, xB]), np.stack([aA, aB]), np.stack([bA, bB])), 0) + a_with_ba = at(value(np.stack([xB, xA]), np.stack([aB, aA]), np.stack([bB, bA])), 1) + a_repeated = at(value(np.stack([xA, xA]), np.stack([aA, aA]), np.stack([bA, bA])), 0) np.testing.assert_allclose(a_with_b, a_alone, rtol=1e-12, atol=1e-12) np.testing.assert_allclose(a_with_ba, a_alone, rtol=1e-12, atol=1e-12) np.testing.assert_allclose(a_repeated, a_alone, rtol=1e-12, atol=1e-12)