Chapter 2: Data Drift Detection
Types of Drift
| Type | Description | Detection |
|---|---|---|
| Covariate Drift | Input data distribution changes | Statistical tests on features |
| Concept Drift | Relationship between X and Y changes | Monitor prediction accuracy |
| Label Drift | Target distribution changes | Compare 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