Skip to content

Commit

Permalink
Updates on the semi-positive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GardevoirX committed Sep 18, 2024
1 parent 939e0a4 commit 0bc1715
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/skmatter/utils/_sparsekde.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 36 in src/skmatter/utils/_sparsekde.py

View check run for this annotation

Codecov / codecov/patch

src/skmatter/utils/_sparsekde.py#L36

Added line #L36 was not covered by tests
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))

Expand Down
4 changes: 2 additions & 2 deletions tests/test_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 0bc1715

Please sign in to comment.