Sub topic 8
Scoring drift with the Population Stability Index
This chapter assumes you know basic NumPy and histograms, and that you have seen the KS and chi-square tests from the previous chapter. Those tests return a p-value. With large samples, a p-value flags tiny shifts that do not matter. The Population Stability Index (PSI) returns an effect size instead: one number for how far a feature has moved.
What PSI measures
PSI compares two distributions after splitting the feature into bins. For each bin, take the share of reference rows and the share of current rows. Then sum over all bins:
PSI = sum of (current% - reference%) x ln(current% / reference%)
Each term is zero when the two shares match. Each term is positive when they differ, because the difference and the log ratio always have the same sign. PSI is therefore never negative, and it grows as the distributions separate.
Computing PSI in Python
import numpy as np
def psi(reference, current, bins=10, eps=1e-6):
edges = np.quantile(reference, np.linspace(0, 1, bins + 1))
edges[0], edges[-1] = -np.inf, np.inf
edges = np.unique(edges)
ref_pct = np.histogram(reference, bins=edges)[0] / len(reference)
cur_pct = np.histogram(current, bins=edges)[0] / len(current)
ref_pct = np.clip(ref_pct, eps, None)
cur_pct = np.clip(cur_pct, eps, None)
return float(np.sum((cur_pct - ref_pct) * np.log(cur_pct / ref_pct)))
rng = np.random.default_rng(0)
print(psi(rng.normal(0, 1, 5000), rng.normal(0.3, 1, 5000)))The function makes four choices:
- Bin edges come from the reference data only. Each reference bin holds about 10% of the rows, so any bin where the current share departs from 10% is a visible change.
- The outer edges are set to minus and plus infinity. Current values outside the reference range still land in the first or last bin instead of being dropped.
np.uniqueremoves duplicate edges. A feature with many repeated values, such as a column that is mostly zeros, produces fewer bins than requested.epsclips empty bins. An empty bin would make the logarithm undefined or infinite.
For a mean shift of 0.3 standard deviations, expect a score near 0.09. Theory gives about 0.09 for this case. Binning and sampling noise move the result slightly.
Reading the score
Credit-risk teams popularized these rules of thumb:
| PSI | Usual reading |
|---|---|
| Below 0.1 | Stable |
| 0.1 to 0.25 | Moderate shift, worth a look |
| Above 0.25 | Major shift, investigate |
Treat these cut-offs as conventions, not laws. A PSI of 0.12 on a feature the model ignores matters less than 0.05 on its most important input. The thresholds chapter later in this tutorial covers tuning them.
Common errors
- Using too few rows. PSI is biased upward on small samples because random variation fills some bins and empties others. With 10 bins and a few hundred rows, a stable feature can show a PSI above 0.05. Compare against a PSI computed between two reference-sized samples with no real shift.
- Recomputing edges from the current window. Then both windows split evenly by construction and the score stays near zero.
- Treating PSI as a test. It has no p-value. You choose the threshold.
- Forgetting categorical features. Use each category as a bin. Add an "unseen" bin for new categories, since a new category is a strong signal.
Exercise
You compute PSI with 10 bins on a feature where 70% of reference values are exactly 0. The function returns fewer than 10 bins. Why, and is the result still usable?
Show answer
Quantile edges at several of the lowest percentiles all equal 0, so np.unique collapses them into one edge. The result is still usable, because the large zero bin is compared as a single bin. Resolution is lower for the nonzero values, so consider binning only the nonzero values separately and tracking the share of zeros as its own metric.