-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanomaly_detector.py
More file actions
42 lines (32 loc) · 1.24 KB
/
anomaly_detector.py
File metadata and controls
42 lines (32 loc) · 1.24 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
import numpy as np
from collections import deque
import logging
# Configure Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class AnomalyDetector:
"""
Real-time statistical anomaly detection using rolling Z-Score analysis.
Designed to detect data drift in time-series metrics.
"""
def __init__(self, window_size=20, threshold=2.5):
self.window_size = window_size
self.threshold = threshold
self.data_window = deque(maxlen=window_size)
def update(self, value):
"""
Ingest new data point and calculate deviation.
Returns: (is_anomaly, message, z_score)
"""
self.data_window.append(value)
if len(self.data_window) < 5:
return False, "Initializing baseline...", 0.0
mean = np.mean(self.data_window)
std_dev = np.std(self.data_window)
if std_dev == 0:
return False, "Stable", 0.0
z_score = (value - mean) / std_dev
if abs(z_score) > self.threshold:
logger.warning(f"Anomaly Detected: Value {value} | Deviation {z_score:.2f}")
return True, f"CRITICAL: Deviation {z_score:.2f} sigma", z_score
return False, "Normal", z_score