Skip to main content

Chapter 2: Data Drift Detection

Types of Drift​

TypeDescriptionDetection
Covariate DriftInput data distribution changesStatistical tests on features
Concept DriftRelationship between X and Y changesMonitor prediction accuracy
Label DriftTarget distribution changesCompare prediction distributions

Statistical Tests for Drift​

from scipy import stats
import numpy as np

def detect_drift(reference_data: np.ndarray, current_data: np.ndarray, threshold: float = 0.05):
"""Detect drift using Kolmogorov-Smirnov test."""
drift_results = {}

for i, feature in enumerate(feature_names):
statistic, p_value = stats.ks_2samp(
reference_data[:, i],
current_data[:, i]
)
drift_results[feature] = {
'statistic': statistic,
'p_value': p_value,
'drift_detected': p_value < threshold
}

return drift_results

Drift Monitoring Dashboard​

# Grafana dashboard config
panels:
- title: "Feature Drift Score"
type: timeseries
targets:
- expr: model_drift_score{feature="age"}
- expr: model_drift_score{feature="income"}
thresholds:
- value: 0.1
color: yellow
- value: 0.2
color: red

Next: Chapter 3: Alerting and Incident Response