Skip to content

Commit 9857029

Browse files
author
Jaime Céspedes Sisniega
authored
Merge pull request #175 from IFCA/fix-msprt
Fix mSPRT setting theta=0
2 parents 18e9c67 + 90bd322 commit 9857029

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

frouros/callbacks/streaming/msprt.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,14 @@ def tau(self, value: Optional[float]) -> None:
9797
raise TypeError("tau must be a float or None")
9898
self._tau = value
9999

100-
def on_fit_end(self, **kwargs) -> None:
101-
"""On fit end method."""
102-
self.incremental_mean.num_values = len(kwargs["X"])
103-
104100
def on_update_end(self, value: Union[int, float], **kwargs) -> None:
105101
"""On update end method.
106102
107103
:param value: value to update detector
108104
:type value: int
109105
"""
110106
self.incremental_mean.update(value=value)
111-
self.p_value, likelihood = self._calculate_p_value(value=value)
107+
self.p_value, likelihood = self._calculate_p_value()
112108

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

137-
def _calculate_p_value(self, value: float) -> Tuple[float, float]:
133+
def _calculate_p_value(self) -> Tuple[float, float]:
138134
likelihood = self._likelihood_normal_mixing_distribution(
139135
mean=self.incremental_mean.get(),
140136
sigma=self.sigma,
141137
sigma_squared=self.sigma_squared,
142138
tau_squared=self.tau_squared,
143139
two_sigma_squared=self.two_sigma_squared,
144-
value=value,
145140
n=self.detector.num_instances, # type: ignore
146141
)
147142
p_value = min(
@@ -157,15 +152,14 @@ def _likelihood_normal_mixing_distribution(
157152
sigma_squared: float,
158153
tau_squared: float,
159154
two_sigma_squared: float,
160-
value: float,
161155
n: int,
162156
) -> float:
163157
n_tau_squared = n * tau_squared
164158
sigma_squared_plus_n_tau_squared = sigma_squared + n_tau_squared
165159
likelihood = (sigma / np.sqrt(sigma_squared_plus_n_tau_squared)) * np.exp(
166160
n
167161
* n_tau_squared
168-
* (mean - value) ** 2
169-
/ (two_sigma_squared * (sigma_squared_plus_n_tau_squared))
162+
* mean**2 # (mean - theta) ** 2, theta = 0 (H_0 value, no distance)
163+
/ (two_sigma_squared * sigma_squared_plus_n_tau_squared)
170164
)
171165
return likelihood

frouros/tests/integration/test_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_streaming_warning_samples_buffer_on_concept_drift(
242242
" expected_p_value,"
243243
" expected_likelihood",
244244
[
245-
(MMDStreaming, 40, 0.08821576, 0.00494882, 202.06836342),
245+
(MMDStreaming, 31, 0.34147982, 0.0487643, 20.50680424),
246246
],
247247
)
248248
def test_streaming_msprt_multivariate_different_distribution(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "frouros"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
description = "A Python library for drift detection in Machine Learning problems"
55
authors = [
66
{name = "Jaime Céspedes Sisniega", email = "cespedes@ifca.unican.es"}

0 commit comments

Comments
 (0)