WRITE · TRACE · UNDERSTAND

Measure binned drift with smoothing and fixed boundaries

Implement psi(reference,current,epsilon=1e-6) for equal nonempty nonnegative finite bin counts with positive totals.

Read lesson: Measure binned drift with smoothing and fixed boundaries →

Your experiment

Implement psi(reference,current,epsilon=1e-6) for equal nonempty nonnegative finite bin counts with positive totals. Add epsilon to every bin count before normalizing each histogram; return sum((q-p)*log(q/p)). Require finite epsilon>0.

Python runs in your browser. Your draft stays on this device.

What to look for

Smoothing reduces instability but changes the estimate, especially on tiny windows. PSI thresholds are not universal laws; calibrate alerts against reference variation and observed model outcomes.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Measure binned drift with smoothing and fixed boundaries works

The idea

The population stability index, PSI, measures how much a distribution has moved between a reference window, such as the training data, and a current window. Both windows are counted into the same fixed bins. For each bin psi compares the two proportions p and q with (q - p) * log(q / p) and adds the results.

Each term is zero when the proportions match and positive otherwise, because the difference and the logarithm always have the same sign. The total is therefore 0 for identical histograms and grows with the shift, and swapping the two windows gives the same value. Adding epsilon to every count first keeps an empty bin from causing a division by zero or a logarithm of zero.

How it works

  1. Check the inputs: equal bin counts, finite non-negative values, positive totals and a finite epsilon > 0.
  2. Add epsilon to every bin count in both histograms.
  3. Divide each histogram by its own new total to get the proportions p and q.
  4. For every bin compute (q - p) * log(q / p).
  5. Return the sum: 0 for identical distributions, larger for bigger shifts.

What it costs

Time
O(b) for b bins
Space
O(b)

One pass over the b bins does all the work; the cost does not depend on how many rows were counted into the histograms.

Where it is used

  • Credit risk teams track the PSI of scorecard scores between the development sample and recent applicants.
  • A common rule of thumb in that field reads PSI below 0.1 as stable and above 0.25 as a major shift.
  • The open-source Evidently library offers PSI as one of its data drift tests for production features.

Watch out for

  • Recomputing bin edges on the current window: the bins must stay fixed from the reference, or the shift is hidden.
  • Leaving empty bins unsmoothed, which gives a logarithm of zero or a division by zero.
  • Comparing raw counts instead of proportions, so windows of different sizes look like drift.