From af5fc562be10b791975a1b4576abed26f0771727 Mon Sep 17 00:00:00 2001 From: POT Phase XXIII Research Date: Sun, 22 Nov 2026 13:32:57 +0800 Subject: [PATCH] Restore dual feasibility at zero-mass bins in d-MMOT --- RELEASES.md | 1 + ot/lp/dmmot.py | 10 ++++++++++ test/test_dmmot.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/RELEASES.md b/RELEASES.md index b0e12bb7c..cf2baf52d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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) diff --git a/ot/lp/dmmot.py b/ot/lp/dmmot.py index f40a99b6f..dfb6474b5 100644 --- a/ot/lp/dmmot.py +++ b/ot/lp/dmmot.py @@ -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) diff --git a/test/test_dmmot.py b/test/test_dmmot.py index 67ac985dd..d1f4e5c97 100644 --- a/test/test_dmmot.py +++ b/test/test_dmmot.py @@ -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)