Skip to content
Merged
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
14 changes: 4 additions & 10 deletions frouros/callbacks/streaming/msprt.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,14 @@ def tau(self, value: Optional[float]) -> None:
raise TypeError("tau must be a float or None")
self._tau = value

def on_fit_end(self, **kwargs) -> None:
"""On fit end method."""
self.incremental_mean.num_values = len(kwargs["X"])

def on_update_end(self, value: Union[int, float], **kwargs) -> None:
"""On update end method.

:param value: value to update detector
:type value: int
"""
self.incremental_mean.update(value=value)
self.p_value, likelihood = self._calculate_p_value(value=value)
self.p_value, likelihood = self._calculate_p_value()

self.logs.update(
{
Expand All @@ -134,14 +130,13 @@ def _calculate_tau_squared(
tau_squared = sigma_squared * minus_b_cdf / (1 / b * norm.pdf(b) - minus_b_cdf)
return tau_squared

def _calculate_p_value(self, value: float) -> Tuple[float, float]:
def _calculate_p_value(self) -> Tuple[float, float]:
likelihood = self._likelihood_normal_mixing_distribution(
mean=self.incremental_mean.get(),
sigma=self.sigma,
sigma_squared=self.sigma_squared,
tau_squared=self.tau_squared,
two_sigma_squared=self.two_sigma_squared,
value=value,
n=self.detector.num_instances, # type: ignore
)
p_value = min(
Expand All @@ -157,15 +152,14 @@ def _likelihood_normal_mixing_distribution(
sigma_squared: float,
tau_squared: float,
two_sigma_squared: float,
value: float,
n: int,
) -> float:
n_tau_squared = n * tau_squared
sigma_squared_plus_n_tau_squared = sigma_squared + n_tau_squared
likelihood = (sigma / np.sqrt(sigma_squared_plus_n_tau_squared)) * np.exp(
n
* n_tau_squared
* (mean - value) ** 2
/ (two_sigma_squared * (sigma_squared_plus_n_tau_squared))
* mean**2 # (mean - theta) ** 2, theta = 0 (H_0 value, no distance)
/ (two_sigma_squared * sigma_squared_plus_n_tau_squared)
)
return likelihood
2 changes: 1 addition & 1 deletion frouros/tests/integration/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_streaming_warning_samples_buffer_on_concept_drift(
" expected_p_value,"
" expected_likelihood",
[
(MMDStreaming, 40, 0.08821576, 0.00494882, 202.06836342),
(MMDStreaming, 31, 0.34147982, 0.0487643, 20.50680424),
],
)
def test_streaming_msprt_multivariate_different_distribution(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "frouros"
version = "0.2.5"
version = "0.2.6"
description = "A Python library for drift detection in Machine Learning problems"
authors = [
{name = "Jaime Céspedes Sisniega", email = "cespedes@ifca.unican.es"}
Expand Down