Sub topic 8
Scoring Drift with the Population Stability Index
The Population Stability Index (PSI) reduces the difference between two distributions to a single number. It is popular in monitoring because you can put it on a dashboard, compare it across features, and set one threshold for many features. This chapter explains how PSI is computed and how to read it. It also covers where it misleads you.
This chapter assumes you can write basic Python and NumPy. It also assumes you know what a reference window is and how the KS and chi-square tests work, from the earlier chapters.
What PSI measures
Split the range of a feature into bins. For each bin, take the share of reference rows and the share of current rows that fall in it. PSI is:
PSI = sum over bins of (current% - reference%) * ln(current% / reference%)
Each bin contributes a non-negative amount. The difference and the log ratio always have the same sign, so their product is never negative. A bin whose share is unchanged contributes zero. The total is zero only when every bin matches.
PSI is the symmetric form of the Kullback-Leibler divergence. Swapping reference and current gives the same value. That makes it a distance-like score. It is not a hypothesis test, so it has no p-value.
Computing PSI for a numeric feature
The key design choice is to take the bin edges from the reference data only. The current data is then counted into those same bins. If you recomputed edges on each window, the bins would move with the data and hide the shift you are trying to see.
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)))Each line has a purpose:
- Quantile edges give each reference bin about the same number of rows, 10% each with ten bins. Equal-width bins would leave the tails nearly empty and make the score unstable.
- Setting the outer edges to minus and plus infinity makes sure that current values outside the reference range still land in the first or last bin. Without this,
np.histogramwould silently drop them. np.uniqueremoves duplicate edges. A feature with many ties, such as a column that is 60% zeros, produces repeated quantiles, and duplicate edges would create empty bins. You end up with fewer bins than requested.- Clipping to
epsavoidslog(0)and division by zero when a bin is empty in one window.
The shifted example prints a value below 0.1. A 0.3 standard deviation shift is visible to the KS test at this sample size, but PSI rates it as small.
Reading the score
A widely used rule of thumb gives three bands:
| PSI | Common reading |
|---|---|
| below 0.1 | No meaningful shift |
| 0.1 to 0.25 | Moderate shift, worth a look |
| above 0.25 | Large shift, investigate |
Sample size sets a noise floor
Even when nothing has changed, PSI is not zero. Random sampling puts slightly different counts in each bin. For B bins, the expected PSI between two samples from the same distribution is roughly:
(B - 1) * (1 / nreference + 1 / ncurrent)
With 10 bins and 5,000 rows in each window, that is about 0.004, which is negligible. With 200 rows in each window, it is about 0.09, which is almost the whole "no shift" band. The simulation below checks this.
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(1)
n = 200
scores = [psi(rng.normal(0, 1, n), rng.normal(0, 1, n)) for _ in range(500)]
print(
f"mean={np.mean(scores):.3f}, "
f"95th percentile={np.percentile(scores, 95):.3f}, "
f"formula={9 * (2 / n):.3f}"
)The simulated mean lands near the formula's value. Two practical consequences follow:
- Small windows, such as hourly traffic for a low-volume model, produce PSI values that look like drift but are noise. Use larger windows, fewer bins, or both.
- The same threshold does not mean the same thing at different sample sizes. Compare each feature's PSI with its own no-drift baseline, which you can estimate by simulating or by bootstrapping the reference data.
Choosing the number of bins
Ten bins is the usual default. More bins make PSI more sensitive to small local changes, but they raise the noise floor because B appears in the formula. Fewer bins are more stable, but they can miss a change inside one wide bin. A reasonable rule is to keep each bin's expected count at 50 rows or more in both windows.
Quantile bins also hide one pattern. If the shift is only in the extreme tails, the outer bins absorb it, and PSI can stay small. For tail-sensitive features, such as transaction amounts, check the tails separately.
PSI for categorical features
For a categorical feature, each category is its own bin. No edges are needed. Use the same formula on category shares, and handle categories that appear in only one window.
from collections import Counter
import numpy as np
def psi_categorical(reference, current, eps=1e-6):
ref_counts, cur_counts = Counter(reference), Counter(current)
categories = sorted(set(ref_counts) | set(cur_counts))
ref_pct = np.array([ref_counts[c] / len(reference) for c in categories])
cur_pct = np.array([cur_counts[c] / len(current) for c in categories])
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)))
reference = ["a"] * 500 + ["b"] * 300 + ["c"] * 200
current = ["a"] * 400 + ["b"] * 300 + ["c"] * 300
print(f"PSI={psi_categorical(reference, current):.3f}")This prints PSI=0.063, which is in the no-shift band, even though category c grew from 20% to 30% of rows. A chi-square test on the same counts would flag the change at these sample sizes. The two tools answer different questions, so they can disagree.
A new category that never appeared in the reference is a special case. Its reference share is clipped to eps, so its term is about current% * ln(current% / 1e-6). A category with just 1% of current rows adds roughly 0.12 on its own. This is useful as a signal, but it also means the value of eps changes the score. Fix eps once and keep it the same across features and over time.
PSI compared with KS and chi-square
| Property | KS and chi-square | PSI |
|---|---|---|
| Output | Statistic and p-value | One non-negative score |
| Sensitivity to sample size | p-value shrinks as n grows, so large windows flag tiny shifts | Score does not trend with n, but noise floor is higher for small n |
| Comparable across features | Poorly, because p-values depend on n | Reasonably, with the same bins and window sizes |
| Depends on binning | KS does not, chi-square does | Always |
The practical point is that with millions of rows, a KS p-value is almost always tiny, so it carries little information. PSI gives a size of shift rather than a yes-or-no verdict. Many teams therefore alert on PSI and use the tests for diagnosis.
Common errors
- Recomputing edges on the current window. The bins then follow the data and the score understates drift. Compute edges from the reference once and store them.
- Forgetting infinite outer edges. Values outside the reference range vanish from the counts, and the percentages no longer sum to one.
- Using PSI on a feature with few distinct values. After
np.unique, you may have only two or three bins. Treat such a feature as categorical. - Comparing PSI values built with different bin counts or window sizes. The scores are not on the same scale.
- Reading PSI as impact on accuracy. A feature can have a high PSI and be irrelevant to the model. The chapter on drift versus performance covers this.
Exercise
A feature has 60% exact zeros in the reference data, and you call psi(reference, current, bins=10). The function returns a value, but you suspect it uses fewer than 10 bins. Which line causes this, and what would you do to a feature like this?
Show answer
The quantile edges repeat at 0, and np.unique collapses the duplicate edges, so fewer bins remain. For a feature with a large spike at one value, handle the zeros as their own category (a "zero" bin) and apply PSI to the nonzero values with quantile bins, or treat the feature as categorical.
Exercise
You monitor a model that scores 150 requests per hour and compute PSI on 10 bins each hour against a large reference set. Roughly what PSI would you expect when nothing has changed, and what does that mean for a 0.1 alert threshold?
Show answer
The noise floor is about (B - 1) / n_current = 9 / 150 = 0.06, because the reference set is large and contributes little. Normal sampling noise already uses most of the 0.1 budget, so hourly alerts at 0.1 would fire often. Use a larger window (for example daily), fewer bins, or a threshold calibrated to the simulated no-drift distribution.