Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#### Closed issues

- Fix `ot.lp.dmmot_monge_1dgrid_loss` returning dual potentials that violate dual feasibility when an input distribution has exact zero-mass bins. Such potentials could not be used as valid subgradients of the d-MMOT value. Zero-mass potentials are now lowered to a safe bound, leaving the primal value, dual objective and all strictly positive inputs unchanged
- 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)
Expand Down
10 changes: 10 additions & 0 deletions ot/lp/dmmot.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ def dmmot_monge_1dgrid_loss(A, verbose=False, log=False):
except Exception:
pass

# Zero-mass bins do not contribute to the dual objective, but the greedy
# fill-in can leave them at potentials that violate dual feasibility.
# Lowering them to a globally safe bound restores feasibility without
# changing the dual objective or the primal cost.
if np.any(A == 0):
n_bins, n_hists = A.shape
safe_lower_bound = (n_hists - 1) * (n_bins - 1)
for j in range(n_hists):
dual[j][A[:, j] == 0] = -safe_lower_bound

dualobj = sum([np.dot(A[:, i], arr) for i, arr in enumerate(dual)])
obj = nx.from_numpy(obj)

Expand Down
48 changes: 48 additions & 0 deletions test/test_dmmot.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,51 @@ def test_dmmot_monge_1dgrid_optimize(nx):
err_msg="Test failed: \
Expected vectors of all zeros",
)


def test_dmmot_zero_mass_dual_feasibility():
"""Zero-mass bins must not leave the returned dual infeasible."""
A = np.array([[1.0, 1.0], [0.0, 0.0]])
obj, log = ot.lp.dmmot_monge_1dgrid_loss(A, log=True)
u, v = np.asarray(log["dual"][0]), np.asarray(log["dual"][1])
slack = np.array([[0.0, 1.0], [1.0, 0.0]]) - u[:, None] - v[None, :]

np.testing.assert_allclose(obj, 0.0, atol=1e-12)
np.testing.assert_allclose(log["dual objective"], obj, atol=1e-12)
assert np.all(slack >= -1e-12)


def test_dmmot_positive_and_near_zero_inputs_unchanged():
"""The zero-bin completion must not alter strictly positive or near-zero bins."""
A = np.array([[0.7, 0.3], [0.3, 0.7]])
obj, log = ot.lp.dmmot_monge_1dgrid_loss(A, log=True)
np.testing.assert_allclose(obj, 0.4, atol=1e-12)
np.testing.assert_allclose(
np.column_stack(log["dual"]), [[0, 0], [-1, 1]], atol=1e-12
)

A_near = np.array([[0.2, 0.3], [0.3, 0.3], [0.5, 0.4], [1e-12, 2e-12]])
_, log_near = ot.lp.dmmot_monge_1dgrid_loss(A_near, log=True)
np.testing.assert_allclose(
np.column_stack(log_near["dual"]),
[[0, 0], [1, -1], [2, -2], [3, -3]],
atol=1e-12,
)


def test_dmmot_multiple_zero_marginals_dual_feasibility():
"""The tighter safe bound remains feasible with zeros in several marginals."""
A = np.array([[1.0, 0.5, 0.0], [0.0, 0.5, 1.0]])
obj, log = ot.lp.dmmot_monge_1dgrid_loss(A, log=True)
u, v, w = [np.asarray(x) for x in log["dual"]]
C = np.array(
[
[[max(t) - min(t) for t in [(i, j, k)]] for k in range(2)]
for i in range(2)
for j in range(2)
]
).reshape(2, 2, 2)

slack = C - u[:, None, None] - v[None, :, None] - w[None, None, :]
np.testing.assert_allclose(log["dual objective"], obj, atol=1e-12)
assert np.all(slack >= -1e-12)
Loading