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
2 changes: 2 additions & 0 deletions src/pyrecest/filters/gaussian_hypothesis_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def normalize_log_weights(log_weights: list[float] | np.ndarray) -> np.ndarray:
values = np.asarray(log_weights, dtype=float).reshape(-1)
if values.size == 0:
raise ValueError("log_weights must not be empty")
if np.any(np.isnan(values)):
raise ValueError("log_weights must not contain NaN values")

positive_infinite = np.isposinf(values)
if np.any(positive_infinite):
Expand Down
16 changes: 16 additions & 0 deletions tests/filters/test_gaussian_hypothesis_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def test_multiple_positive_infinite_log_weights_share_mass(self):

self.assertTrue(np.allclose(weights, np.array([0.5, 0.0, 0.5])))

def test_nan_log_weights_are_rejected(self):
with self.assertRaisesRegex(ValueError, "NaN"):
normalize_log_weights(np.array([0.0, np.nan]))

with self.assertRaisesRegex(ValueError, "NaN"):
moment_match_gaussian_hypotheses(
[
WeightedGaussianHypothesis(
np.array([0.0]), np.array([[1.0]]), log_weight=0.0
),
WeightedGaussianHypothesis(
np.array([1.0]), np.array([[1.0]]), log_weight=np.nan
),
]
)

def test_moment_matching_respects_dominant_infinite_weight(self):
mean, covariance, weights = moment_match_gaussian_hypotheses(
[
Expand Down
Loading