Repository navigation
[MRG] Fix circular OT costs and derivatives when all shifted CDF entries are negative - #938
Open
xihaian251 wants to merge 3 commits into
Open
xihaian251 wants to merge 3 commits into
xihaian251 wants to merge 3 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.09%
=======================================
Files 130 131 +1
Lines 27229 27281 +52
=======================================
+ Hits 26437 26489 +52
Misses 792 792 🚀 New features to boost your workflow:
|
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.