From 0bc1715bd81d1087668e99462c2da21e741809b9 Mon Sep 17 00:00:00 2001 From: GardevoirX Date: Wed, 18 Sep 2024 16:46:19 +0200 Subject: [PATCH] Updates on the semi-positive tests --- src/skmatter/utils/_sparsekde.py | 17 +++++++++++------ tests/test_neighbors.py | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/skmatter/utils/_sparsekde.py b/src/skmatter/utils/_sparsekde.py index 3d96d2f16..ba366ca00 100644 --- a/src/skmatter/utils/_sparsekde.py +++ b/src/skmatter/utils/_sparsekde.py @@ -21,21 +21,26 @@ def effdim(cov): -------- >>> import numpy as np >>> from skmatter.utils import effdim - >>> cov = np.array([[1, 1, 0], [1, 1, 0], [0, 0, 1]]) + >>> cov = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]], dtype=np.float64) >>> print(round(effdim(cov), 3)) - 1.89 + 2.214 References ---------- https://ieeexplore.ieee.org/document/7098875 """ eigval = np.linalg.eigvals(cov) + if (lowest_eigval := np.min(eigval)) <= -np.max(cov.shape) * np.finfo( + cov.dtype + ).eps: + raise np.linalg.LinAlgError( + f"Matrix is not positive definite." + f"Lowest eigenvalue {lowest_eigval} is " + f"above numerical threshold." + ) + eigval[eigval < 0.0] = 0.0 eigval /= sum(eigval) eigval *= np.log(eigval) - if (lowest_eigval := np.min(eigval)) <= -np.max(cov.shape)*np.finfo(cov.dtype).eps: - raise np.linalg.LinAlgError(f"Matrix is not positive definite. Lowest eigenvalue " - f"{lowest_eigval} is above numerical threshold.") - eigval[eigval <= 0.] = 0.0 return np.exp(-sum(eigval)) diff --git a/tests/test_neighbors.py b/tests/test_neighbors.py index 14024a60a..fd6b4c0af 100644 --- a/tests/test_neighbors.py +++ b/tests/test_neighbors.py @@ -98,8 +98,8 @@ def test_covariance_periodic(self): class EffdimTests(unittest.TestCase): @classmethod def setUpClass(cls): - cls.cov = np.array([[1, 1, 0], [1, 1, 0], [0, 0, 1]]) - cls.expected_effdim = 1.8898815748423097 + cls.cov = np.array([[1, 1, 0], [1, 1.5, 0], [0, 0, 1]], dtype=np.float64) + cls.expected_effdim = 2.24909102090124 def test_effdim(self): self.assertTrue(np.allclose(effdim(self.cov), self.expected_effdim))