OutlierDetector
Z-score based anomaly detector that identifies outliers using standardized distances from the mean.Constructor
Random seed for reproducibility (currently unused but provided for consistency)
Attributes
Mean values for each feature, computed during fit
Standard deviations for each feature (with 1e-6 added for numerical stability), computed during fit
Methods
fit
Fit the detector by computing feature means and standard deviations.Training data with samples as rows and features as columns
Returns self for method chaining
score_samples
Compute anomaly scores for samples.Data to score. Must have same features as training data.
Series of anomaly scores (mean absolute z-score across features). Higher scores indicate more anomalous samples.
detect
Detect anomalies based on quantile threshold.Data to analyze for anomalies
Quantile threshold for anomaly detection. Scores at or above this quantile are flagged as anomalies.
DataFrame with columns:
anomaly_score: Numeric anomaly scores (float)is_anomaly: Boolean flag indicating if score exceeds threshold (bool)
simulate_early_warning
Simulate an early warning system by counting alerts and measuring latency.Time-ordered anomaly scores
Timestamps corresponding to each score (must be same length as scores)
Score threshold for triggering alerts
Dictionary containing:
alert_count: Total number of alerts triggered (float)first_alert_latency_s: Seconds from start to first alert (float, inf if no alerts)
evaluate_detection_latency
Evaluate detection latency by measuring time from ground truth event to first alert.Anomaly scores for each time point
Binary series (0 or 1) indicating when true events occurred
Timestamps for each observation (must be same length as scores and events)
Latency in seconds from first ground truth event to first alert. Returns:
- Positive float: Seconds between first event and first subsequent alert
nan: No ground truth events foundinf: No alerts triggered after the first event