forked from Ledger-Lenz/Ledgerlens-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_drift_detectors.py
More file actions
284 lines (237 loc) · 10.4 KB
/
Copy pathtest_drift_detectors.py
File metadata and controls
284 lines (237 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Tests for detection/drift_detectors.py — ADWIN and Page-Hinkley streaming detectors."""
import numpy as np
import pytest
from detection.drift_detectors import (
ADWIN_DELTA,
PAGE_HINKLEY_DELTA,
PAGE_HINKLEY_THRESHOLD,
DRIFT_ACTIVE_COOLDOWN_OBSERVATIONS,
ADWINDriftDetector,
DriftDetectorRegistry,
PageHinkleyDetector,
_combine_stats,
)
# ---------------------------------------------------------------------------
# _combine_stats
# ---------------------------------------------------------------------------
class TestCombineStats:
def test_empty_combination(self):
n, total, var = _combine_stats(0, 0.0, 0.0, 0, 0.0, 0.0)
assert n == 0
assert total == 0.0
assert var == 0.0
def test_single_bucket_plus_empty(self):
n, total, var = _combine_stats(5, 10.0, 2.0, 0, 0.0, 0.0)
assert n == 5
assert total == 10.0
def test_two_equal_buckets(self):
# Two buckets of 3 samples each, each with total 6 → mean 2
n, total, var = _combine_stats(3, 6.0, 0.0, 3, 6.0, 0.0)
assert n == 6
assert total == 12.0
# Variance should be 0 since both have same mean
assert var == pytest.approx(0.0)
def test_variance_combination(self):
# Bucket1: n=2, values [0, 2] → total=2, var=2 (since mean=1)
# Bucket2: n=2, values [4, 6] → total=10, var=2 (since mean=5)
# Combined: values [0, 2, 4, 6], mean=3, SS=20, var=20
n, total, var = _combine_stats(2, 2.0, 2.0, 2, 10.0, 2.0)
assert n == 4
assert total == 12.0
# Combined variance: 2+2 + (1-5)^2 * 2*2/4 = 4 + 16*1 = 20
assert var == pytest.approx(20.0)
# ---------------------------------------------------------------------------
# ADWINDriftDetector
# ---------------------------------------------------------------------------
class TestADWINDriftDetector:
def test_initial_state(self):
detector = ADWINDriftDetector()
assert detector.width == 0
assert detector.total == 0.0
assert detector.variance == 0.0
assert detector.estimation == 0.0
assert detector.drift_detected is False
assert detector.n_detections == 0
assert detector.last_detection_at is None
def test_delta_validation(self):
with pytest.raises(ValueError, match="delta"):
ADWINDriftDetector(delta=0.0)
with pytest.raises(ValueError, match="delta"):
ADWINDriftDetector(delta=1.0)
with pytest.raises(ValueError, match="delta"):
ADWINDriftDetector(delta=-0.1)
def test_basic_updates_no_drift_on_stationary(self):
detector = ADWINDriftDetector(delta=0.01)
for i in range(100):
result = detector.update(5.0)
assert result is False # No drift in stationary stream
assert detector.width == 100
assert detector.estimation == pytest.approx(5.0)
assert detector.n_detections == 0
def test_drift_detection_on_mean_shift(self):
detector = ADWINDriftDetector(delta=0.002)
# Feed stationary data first
for _ in range(200):
detector.update(10.0)
assert detector.n_detections == 0
# Then feed shifted data — should eventually fire
fired = False
for _ in range(500):
if detector.update(20.0):
fired = True
break
assert fired, "ADWIN should detect a large mean shift"
def test_drift_detection_increases_counter(self):
detector = ADWINDriftDetector(delta=0.002)
for _ in range(200):
detector.update(10.0)
for _ in range(500):
if detector.update(20.0):
break
assert detector.n_detections >= 1
assert detector.last_detection_at is not None
def test_estimation_after_drift_reflects_new_mean(self):
detector = ADWINDriftDetector(delta=0.002)
# Build up window of mean=10
for _ in range(200):
detector.update(10.0)
# Shift to mean=20
fired = False
for _ in range(500):
if detector.update(20.0):
fired = True
break
assert fired
# After drift, estimation should be closer to 20 than 10
assert detector.estimation > 14.0
def test_update_with_nan_does_not_crash(self):
"""Feeding NaN should not raise (handled at registry level, but ADWIN itself
receives float so it should be handled upstream)."""
detector = ADWINDriftDetector()
# ADWIN itself accepts any float — NaN comes through as a bucket value
# but won't crash the algorithm, just produces weird stats
detector.update(float("nan"))
assert detector.width == 1
# ---------------------------------------------------------------------------
# PageHinkleyDetector
# ---------------------------------------------------------------------------
class TestPageHinkleyDetector:
def test_initial_state(self):
detector = PageHinkleyDetector()
assert detector.statistic == 0.0
assert detector.n_observations == 0
assert detector.drift_detected is False
assert detector.n_detections == 0
assert detector.last_detection_at is None
def test_stationary_stream_no_drift(self):
detector = PageHinkleyDetector(delta=0.005, threshold=50.0)
rng = np.random.RandomState(42)
for _ in range(500):
result = detector.update(rng.normal(0, 1))
# On stationary N(0,1) with a large threshold, should not fire
if result:
pytest.fail("Page-Hinkley should not fire on stationary N(0,1)")
assert detector.n_detections == 0
def test_drift_detection_on_large_shift(self):
detector = PageHinkleyDetector(delta=0.005, threshold=30.0)
# Stationary phase
for _ in range(200):
detector.update(0.0)
# Large shift
fired = False
for _ in range(500):
if detector.update(5.0):
fired = True
break
assert fired, "Page-Hinkley should detect a sustained +5 mean shift"
def test_reset_on_fire(self):
detector = PageHinkleyDetector(delta=0.005, threshold=20.0)
for _ in range(100):
detector.update(0.0)
for _ in range(300):
if detector.update(5.0):
break
# After firing, cumulative/min should be reset to 0
assert detector._cumulative == 0.0
assert detector._min_cumulative == 0.0
def test_n_observations_increments(self):
detector = PageHinkleyDetector()
for _ in range(50):
detector.update(1.0)
assert detector.n_observations == 50
def test_custom_alpha(self):
"""Alpha < 1.0 applies forgetting to the cumulative sum."""
detector = PageHinkleyDetector(delta=0.005, threshold=50.0, alpha=0.9)
for _ in range(100):
detector.update(0.0)
assert detector.n_observations == 100
# No assertion on drift, just that alpha doesn't crash
# ---------------------------------------------------------------------------
# DriftDetectorRegistry
# ---------------------------------------------------------------------------
class TestDriftDetectorRegistry:
@pytest.fixture
def registry(self):
return DriftDetectorRegistry(["f1", "f2", "f3"])
def test_initial_state(self, registry):
assert registry.last_drifted_features == []
assert registry.last_event_at is None
assert not registry.is_active()
def test_observe_stationary_no_fire(self, registry):
for _ in range(50):
result = registry.observe({"f1": 1.0, "f2": 2.0, "f3": 3.0})
assert result == []
def test_observe_ignores_unknown_features(self, registry):
"""Unknown keys are silently skipped."""
result = registry.observe({"f1": 1.0, "unknown": 999.0})
assert len(result) <= 1 # Only f1 might fire
def test_observe_ignores_non_numeric(self, registry):
"""Non-numeric values are silently skipped."""
result = registry.observe({"f1": "not_a_number", "f2": 1.0})
# f1 is skipped, f2 is processed
assert all(e["feature"] != "f1" for e in result)
def test_observe_skips_nan(self, registry):
"""NaN values are silently skipped."""
result = registry.observe({"f1": float("nan"), "f2": 1.0})
assert not any(e["feature"] == "f1" for e in result)
def test_state_returns_expected_keys(self, registry):
state = registry.state()
assert "drift_active" in state
assert "last_drifted_features" in state
assert "last_event_at" in state
assert "config" in state
assert "features" in state
for fname in ("f1", "f2", "f3"):
assert fname in state["features"]
assert "adwin" in state["features"][fname]
assert "page_hinkley" in state["features"][fname]
def test_is_active_without_detection(self, registry):
assert not registry.is_active()
def test_is_active_immediately_after_detection(self, registry):
# Force detection by inducing a shift on one feature
detector = registry._adwin["f1"]
# Pre-fill the ADWIN window so it can trigger
for _ in range(200):
detector.update(0.0)
# Feed through registry: f1 is stationary, but we'll force via direct ADWIN access
# Registry's observe won't fire on stationary data
# Instead, verify is_active returns False when there's no detection
for _ in range(100):
registry.observe({"f1": 0.0, "f2": 0.0, "f3": 0.0})
assert not registry.is_active()
# ---------------------------------------------------------------------------
# Module-level configuration
# ---------------------------------------------------------------------------
class TestModuleConfig:
def test_adwin_delta_is_float(self):
assert isinstance(ADWIN_DELTA, float)
assert 0 < ADWIN_DELTA < 1
def test_page_hinkley_threshold_is_positive(self):
assert isinstance(PAGE_HINKLEY_THRESHOLD, float)
assert PAGE_HINKLEY_THRESHOLD > 0
def test_page_hinkley_delta_is_small(self):
assert isinstance(PAGE_HINKLEY_DELTA, float)
assert 0 < PAGE_HINKLEY_DELTA < 0.1
def test_cooldown_observations_is_positive(self):
assert isinstance(DRIFT_ACTIVE_COOLDOWN_OBSERVATIONS, int)
assert DRIFT_ACTIVE_COOLDOWN_OBSERVATIONS > 0