Repository navigation
[MRG] Fix circular OT costs and derivatives when all shifted CDF entries are negative #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xihaian251
wants to merge
4
commits into
PythonOT:master
Choose a base branch
from
xihaian251:phase34-circle-cdf-wrap-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5070a4c
fix: wrap all-negative CDFs in circular Sinkhorn costs
0696d9b
test: cover batch composition and document circular CDF fix
d18bfe0
test: assert full batch invariance for circular OT helpers
62f6937
Merge branch 'master' into phase34-circle-cdf-wrap-fix
rflamary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||||||
| """Regression for the all-negative CDF branch in circular optimal transport.""" | ||||||||||||
|
|
||||||||||||
| 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) | ||||||||||||
|
Comment on lines
+55
to
+56
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hard coding, you can get the result from ot.emd2, e.g.
Suggested change
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| 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 | ||||||||||||
|
|
||||||||||||
| 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) | ||||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move these tests to the file
test_circle_solver.py