Histogram distances and alerts
Quick answer
Histogram distances and two-sample tests compare reference and current feature distributions. Population Stability Index (PSI) summarizes binned proportion differences; the Kolmogorov–Smirnov (KS) statistic measures the largest empirical CDF difference. Neither directly measures model accuracy. Examples use Python, SciPy 1.17.0 and scikit-learn 1.8.0 with small synthetic inputs. ByteMind execution is unverified.
PSI with fixed bins
import mathp=[.8,.2];q=[.5,.5]print(round(sum((b-a)*math.log(b/a) for a,b in zip(p,q)),6))Expected output (locally tested): 0.415888. PSI increases when these binned proportions move apart.
How it works
Freeze reference bins, including underflow and overflow, and use identical bins for current observations. For normalized nonzero proportions p and q, PSI=sum((q-p)*ln(q/p)). Apply documented pseudocounts to counts before renormalization when zero bins occur; PSI depends on binning and smoothing. No universal PSI threshold exists. Total variation for discrete bins is half the sum of absolute differences. For continuous independent samples KS D=max_x|F_ref(x)-F_current(x)|; scipy.stats.ks_2samp returns D and a p-value. Ties, serial dependence and sampling changes can invalidate standard p-value assumptions. Report sample counts and effect size as well as p. For two tests in one window a Bonferroni threshold 0.05/2 controls familywise error under valid null p-values; repeated windows require a separate policy. Season-matched references and a persistence rule may reduce alert fatigue but are not a formal sequential error guarantee.
Native KS statistic
from scipy.stats import ks_2sampr=ks_2samp([0.,1.,2.],[3.,4.,5.])print(float(r.statistic))Expected output (locally tested): 1.0. The samples are separated; the maximum empirical CDF gap is one.
Common mistakes
Do not refit bins per window, silently omit missingness, treat a p-value as probability of drift, or reuse a single-test threshold for many tests. Investigate changes rather than automatically retrain.
Practice and next steps
Compare [80,20] and [50,50] with total variation, then add pseudocounts and calculate PSI. Repeat with unchanged histograms and verify zero distance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Histogram distances and alerts — knowledge check
Apply histogram distances and alerts to small examples and debugging scenarios.
What must remain fixed for comparable PSI?
What is the KS statistic?
What can make standard KS p-values unreliable?
What is a Bonferroni cutoff for two tests at 0.05?
Does large PSI prove lower model accuracy?