Drift Detection

Sliding window anomaly detection using z-score analysis.

Usage

#![allow(unused)]
fn main() {
use entrenar::monitor::{DriftDetector, DriftStatus};

let mut detector = DriftDetector::new(100); // 100-value window

// During training
for value in metrics {
    match detector.check(value) {
        DriftStatus::Normal => {},
        DriftStatus::Warning(z) => println!("Warning: z-score = {:.2}", z),
        DriftStatus::Drift(z) => {
            println!("DRIFT DETECTED: z-score = {:.2}", z);
            // Take corrective action
        }
    }
}
}

Severity Levels

Z-ScoreSeverityAction
< 3.0NormalContinue
3.0 - 4.0WarningLog and monitor
4.0 - 5.0HighAlert
> 5.0CriticalStop training

Sliding Window Baseline

The detector maintains a sliding window for adaptive baselines:

#![allow(unused)]
fn main() {
use entrenar::monitor::SlidingWindowBaseline;

let mut baseline = SlidingWindowBaseline::new(100);

// Add values
baseline.update(0.5);
baseline.update(0.48);

// Check if value is anomalous
if let Some(anomaly) = baseline.detect_anomaly(0.9, 3.0) {
    println!("Anomaly: {:?}", anomaly.severity);
}
}

Z-Score Calculation

z = (x - μ) / σ

where:
  x = current value
  μ = window mean
  σ = window standard deviation