Implement prediction distribution monitoring for a deployed ML model. Track the distribution of model predictions over time and detect drift by comparing against a reference distribution using statistical tests.
import numpy as np
def compute_psi(reference: np.ndarray, current: np.ndarray, bins: int = 10) -> float:
"""Population Stability Index for distribution monitoring."""
breakpoints = np.linspace(np.min(reference), np.max(reference), bins + 1)
breakpoints[0] = -np.inf
breakpoints[-1] = np.inf
ref_counts = np.histogram(reference, bins=breakpoints)[0]
cur_counts = np.histogram(current, bins=breakpoints)[0]
ref_pct = ref_counts / len(reference)
cur_pct = cur_counts / len(current)
# Avoid division by zero
ref_pct = np.clip(ref_pct, 1e-4, None)
cur_pct = np.clip(cur_pct, 1e-4, None)
psi = np.sum((cur_pct - ref_pct) * np.log(cur_pct / ref_pct))
return float(psi)
def monitor_predictions(reference: np.ndarray, current: np.ndarray,
psi_threshold: float = 0.2) -> dict:
psi = compute_psi(reference, current)
drift_detected = psi > psi_threshold
ref_mean = float(np.mean(reference))
cur_mean = float(np.mean(current))
ref_std = float(np.std(reference))
cur_std = float(np.std(current))
return {
"psi": round(psi, 4),
"drift_detected": drift_detected,
"reference_stats": {"mean": ref_mean, "std": ref_std},
"current_stats": {"mean": cur_mean, "std": cur_std},
}