Overview
The anomaly detection system identifies patients with abnormal clinical measurements that may require immediate attention. It uses standardized z-score analysis to flag outliers and supports early warning simulation.OutlierDetector Class
TheOutlierDetector implements a simple but effective statistical approach based on mean absolute z-scores.
Initialization
random_state(int): Random seed for reproducibility (default: 42)
Training the Detector
center: Mean of each feature (used for centering)scale: Standard deviation of each feature (used for scaling)
Anomaly Scoring
Compute anomaly scores for new observations:X(pd.DataFrame): Feature matrix to score
Anomaly Detection
Detect outliers using a quantile-based threshold:X(pd.DataFrame): Feature matrix to evaluatethreshold_quantile(float): Quantile for threshold (default: 0.9)
anomaly_score: Numeric score for each sampleis_anomaly: Boolean flag (True if score ≥ threshold)
Early Warning System
The early warning module simulates real-time alert generation based on anomaly scores.Simulating Alerts
scores(pd.Series): Anomaly scores over timetimestamps(pd.DatetimeIndex): Time of each measurementthreshold(float): Alert threshold
alert_count: Number of alerts triggeredfirst_alert_latency_s: Seconds until first alert (inf if no alerts)
Evaluating Detection Latency
Measure how quickly the system detects known events:scores(pd.Series): Anomaly scoresground_truth_events(pd.Series): Binary labels (1 = event occurred)timestamps(pd.DatetimeIndex): Time of each measurement
- Returns
nanif no events exist - Returns
infif events exist but were not detected
Complete Workflow Example
Tuning the Detector
Threshold Quantile
- 0.90: Flag top 10% as anomalies (moderate sensitivity)
- 0.95: Flag top 5% (higher specificity, fewer false alarms)
- 0.85: Flag top 15% (higher sensitivity, more alerts)
Alert Threshold
Clinical Interpretation
High Anomaly Scores (> 2.0)
Patient measurements deviate significantly from baseline:- Review vital signs immediately
- Consider bedside assessment
- Check for measurement errors
Medium Scores (1.0 - 2.0)
Moderate deviation:- Monitor trend over time
- Schedule follow-up measurement
- Note in patient record
Low Scores (< 1.0)
Within expected range:- Continue routine monitoring
- No immediate action required
Source References
- Detector implementation:
anomaly_detection/detectors.py:7-26 - Early warning system:
anomaly_detection/early_warning.py:7-26