diff --git a/RELEASES.md b/RELEASES.md index 58391f283..9ab2d0ee8 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,6 +22,7 @@ #### Closed issues +- Clamp roundoff-negative eigenvalues in non-Torch backend matrix square roots so rank-deficient positive semi-definite covariances remain finite (Issue #889) - 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/backend.py b/ot/backend.py index b74377041..9ec2ad18a 100644 --- a/ot/backend.py +++ b/ot/backend.py @@ -1577,7 +1577,7 @@ def pinv(self, a, hermitian=False): def sqrtm(self, a): L, V = np.linalg.eigh(a) - L = np.sqrt(L) + L = np.sqrt(np.maximum(L, 0)) # Q[...] = V[...] @ diag(L[...]) Q = np.einsum("...jk,...k->...jk", V, L) # R[...] = Q[...] @ V[...].T @@ -2040,7 +2040,7 @@ def pinv(self, a, hermitian=False): def sqrtm(self, a): L, V = jnp.linalg.eigh(a) - L = jnp.sqrt(L) + L = jnp.sqrt(jnp.maximum(L, 0)) # Q[...] = V[...] @ diag(L[...]) Q = jnp.einsum("...jk,...k->...jk", V, L) # R[...] = Q[...] @ V[...].T @@ -3126,7 +3126,7 @@ def pinv(self, a, hermitian=False): def sqrtm(self, a): L, V = cp.linalg.eigh(a) - L = cp.sqrt(L) + L = cp.sqrt(cp.maximum(L, 0)) # Q[...] = V[...] @ diag(L[...]) Q = cp.einsum("...jk,...k->...jk", V, L) # R[...] = Q[...] @ V[...].T @@ -3608,7 +3608,7 @@ def pinv(self, a, hermitian=False): def sqrtm(self, a): L, V = tf.linalg.eigh(a) - L = tf.sqrt(L) + L = tf.sqrt(tf.maximum(L, 0)) # Q[...] = V[...] @ diag(L[...]) Q = tf.einsum("...jk,...k->...jk", V, L) # R[...] = Q[...] @ V[...].T diff --git a/test/test_backend.py b/test/test_backend.py index 0b784aa62..461ceb541 100644 --- a/test/test_backend.py +++ b/test/test_backend.py @@ -812,6 +812,20 @@ def test_func_backends(nx): ) +def test_sqrtm_rank_deficient_is_finite(nx): + rng = np.random.RandomState(0) + factors = rng.randn(10, 5) + covariance = factors @ factors.T / 5 + + covariance_backend = nx.from_numpy(covariance) + square_root = nx.to_numpy(nx.sqrtm(covariance_backend)) + + assert np.isfinite(square_root).all() + np.testing.assert_allclose( + square_root @ square_root, covariance, rtol=1e-5, atol=1e-6 + ) + + def test_arange_type_as(nx): # `arange` used to silently ignore `type_as` for dtype (and device, in # some backends), unlike `zeros`/`ones`/`full`, which do respect it. diff --git a/test/test_gaussian.py b/test/test_gaussian.py index 3199e2403..f01bc40b4 100644 --- a/test/test_gaussian.py +++ b/test/test_gaussian.py @@ -156,13 +156,12 @@ def test_empirical_bures_wasserstein_mapping_hd(nx, bias): np.testing.assert_allclose(Ct, Cst, rtol=1.0, atol=1.0) -def test_empirical_bures_wasserstein_mapping_numerical_error_warning(): +def test_empirical_bures_wasserstein_mapping_ill_conditioned_is_finite(): rng = np.random.RandomState(42) Xs = rng.rand(766, 800) * 5 Xt = rng.rand(295, 800) * 2 - with pytest.warns(): - A, b = ot.gaussian.empirical_bures_wasserstein_mapping(Xs, Xt, reg=1e-8) - assert not is_all_finite(A, b) + A, b = ot.gaussian.empirical_bures_wasserstein_mapping(Xs, Xt, reg=1e-8) + assert is_all_finite(A, b) def test_bures_wasserstein_distance(nx):