Repository navigation
[MRG] Fix circular OT costs and derivatives when all shifted CDF entries are negative - #938
Open
xihaian251 wants to merge 4 commits into
Open
xihaian251 wants to merge 4 commits into
xihaian251 wants to merge 4 commits into
Conversation
added 3 commits
November 24, 2026 14:40
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.
Check cost and both one-sided derivatives in the batch-composition regression, and correct the circular optimal transport wording.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #938 +/- ##
=======================================
Coverage 97.09% 97.10%
=======================================
Files 130 131 +1
Lines 27273 27325 +52
=======================================
+ Hits 26481 26533 +52
Misses 792 792 🚀 New features to boost your workflow:
|
clbonet
requested changes
Nov 30, 2026
clbonet
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for the PR and the fix! It improves the solver for the examples you highlighted.
I have a few minor comments:
- You could move the tests alongside the other tests for the circle solver.
- For the expected results in the tests, you can use
ot.emd2instead of hardcoding the values. - Could you also fix the reference numbers in the docstrings?
- Can you also add an argument
numItermaxas inot.sinkhorn, with a warning that it did not converge when we reach the maximum number of iterations? This would partially resolve Issue #936
Comment on lines
+55
to
+56
| # SciPy linprog / exact Fraction breakpoint enumeration value. | ||
| np.testing.assert_allclose(got, 0.03158514371890842, atol=1e-9, rtol=1e-9) |
Collaborator
There was a problem hiding this comment.
Instead of hard coding, you can get the result from ot.emd2, e.g.
Suggested change
| # SciPy linprog / exact Fraction breakpoint enumeration value. | |
| np.testing.assert_allclose(got, 0.03158514371890842, atol=1e-9, rtol=1e-9) | |
| M = np.minimum(np.abs(x[:, None] - y[None, :]), 1 - np.abs(x[:, None] - y[None, :]))**2 | |
| tgt = ot.emd2(a, b, M) | |
| np.testing.assert_allclose(got, tgt, atol=1e-9, rtol=1e-9) |
| @@ -0,0 +1,93 @@ | |||
| """Regression for the all-negative CDF branch in circular optimal transport.""" | |||
Collaborator
There was a problem hiding this comment.
Move these tests to the file test_circle_solver.py
|
|
||
|
|
||
| def ot_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p): | ||
| r"""Computes the the cost (Equation (6.2) of [1]) |
Collaborator
There was a problem hiding this comment.
Update the number of the reference [1] -> [44]
|
|
||
|
|
||
| def derivative_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p=2): | ||
| r"""Computes the left and right derivative of the cost (Equation (6.3) and (6.4) of [1]) |
Collaborator
There was a problem hiding this comment.
Update the number of the reference [1] -> [44]
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[MRG] Fix circular OT costs and derivatives when all shifted CDF entries are negative
User impact
ot.lp.solver_circlecan silently return a badly wrong optimal cost andderivative for valid float64 inputs. In a deterministic public-API example,
master returns
0.8049522235041142; an independent SciPy LP solver and anexact
Fractionbreakpoint enumeration both give0.03158514371890842.The solver terminates normally: there is no warning or exception.
Failure mechanism
Both
ot_cost_on_circleandderivative_cost_on_circleshift target CDFs bythe fractional part of
theta:When
thetais a tiny negative number,theta - floor(theta)can round to1.0. If accumulated weights end slightly below one after finite summation,all shifted CDF entries are negative. The current guard
then skips the required wrap.
searchsorteduses negative CDFs and evaluatesthe wrong quantile mapping. Because the condition is batch-global, adding an
unrelated batch with a non-negative CDF can change the value of a failing batch.
Deterministic public-API reproducer
Helper-level batch-composition violation
Separately, with fixed
theta=-1e-17, legal normalized inputsx=(0, 1/8, 1/4),y=(0, 1/8, 1/4, 7/8),a=(2,3,1)/6,b=(1,1,3,2)/7:1.05766369047619050.143229166666666690.143229166666666690.143229166666666690.143229166666666690.143229166666666691.05766369047619050.14322916666666669This is a helper-level counterexample with fixed theta; the public-API
counterexample above is separate and does not rely on helper manipulation.
Left and right derivatives show the same composition dependence on master and
are invariant after the patch.
Independent oracles and exact certificate
linprogon the full circular cost matrix.Fractionbreakpoint enumeration of the periodic quantile objective.with δ=2⁻⁵³ and
cu=cv=(1/2, 1−δ), no wrap gives13510798882111489/18014398509481984; wrapping gives0.Across 1200 deterministic cases, maximum absolute error versus LP drops from
0.773367to1.397e-9. A remaining small optimizer gap is documented and isnot claimed to be eliminated by this patch.
Minimal fix
One guard in each function:
Ordinary mixed-sign and all-non-negative paths are unchanged. In 200 ordinary
cases, 199 outputs are bitwise identical; the changed case is a master error.
Tests
CDF continuity, and full batch-composition invariance of cost,
dCpanddCm.only the derivative fix fails the batch-invariance test. Both fixes are
necessary.
pytest test/test_circle_wrap_regression.py test/test_circle_solver.py test/sliced/test_spherical_sliced.py -q: 87 passed on the final commit.0696d9bgave 3637 passed, 468 skipped,6 xfailed and one local distutils/MSVC
test_helpersenvironment failurethat also fails on frozen master. It was not rerun on the final test-only
commit
d18bfe0.pre-commit run --all-filesandgit diff --check: passed.Relation to #936
Issue #936, reported by @Luminousyyh, concerns non-termination from midpoint
stagnation. Its proposed one-line midpoint exit is a separate fix and is
not part of this PR. This PR does not solve that infinite-loop problem,
does not claim
Closes/Fixes #936, and does not claim all finite-precisionerrors are eliminated. It only fixes the distinct silent wrong-value bug in
the all-negative CDF wrap branch.