PSI in diff() silently skips bins/categories that are zero on one side, under-reporting the largest shifts
Summary
The PSI reported by Profiler.diff() silently drops every bin (numeric) or category (categorical) that is zero on one side only. Those are exactly the bins that carry the largest PSI contribution — a population shifting into a new range, or a new category appearing — so the reported PSI stays well below the 0.1 / 0.2 thresholds documented in _calculate_psi while the population has clearly changed.
numerical_column_stats.py::_calculate_psi(line ~822 onmain@ 5062fb3):if iter_psi and iter_psi != float("inf"): psi_value += iter_psi. When one side is 0,iter_psiis±infand is skipped.categorical_column_profile.pydiff (line ~324):if (percent_other != 0) and (percent_self != 0):— one-sided categories are skipped outright, even though_preprocess_for_categorical_psi_calculationhas just filled them in with 0.
The same diff() output's chi2-test does react (p = 0), so the information is there; it is only the PSI that is misleading.
Reproduce (dataprofiler 0.14.0, Python 3.12, no data labeler)
import numpy as np, pandas as pd, dataprofiler as dp, math
rng = np.random.default_rng(0)
opts = dp.ProfilerOptions()
opts.set({"structured_options.multiprocess.is_enabled": False, "data_labeler.is_enabled": False})
# numeric: A uniform on [0,10); B has half of its mass on [10,20)
A = pd.DataFrame({"x": rng.uniform(0, 10, 20000)})
B = pd.DataFrame({"x": np.concatenate([rng.uniform(0, 10, 10000), rng.uniform(10, 20, 10000)])})
pa = dp.Profiler(A, options=opts, samples_per_update=len(A))
pb = dp.Profiler(B, options=opts, samples_per_update=len(B))
print(pa.diff(pb)["data_stats"][0]["statistics"]["psi"]) # 1.119
# categorical: B introduces a new category 'd' at 25%
Ac = pd.DataFrame({"ch": rng.choice(list("abc"), 20000, p=[.5, .3, .2])})
Bc = pd.DataFrame({"ch": rng.choice(list("abcd"), 20000, p=[.375, .225, .15, .25])})
pac = dp.Profiler(Ac, options=opts, samples_per_update=len(Ac))
pbc = dp.Profiler(Bc, options=opts, samples_per_update=len(Bc))
print(pac.diff(pbc)["data_stats"][0]["statistics"]["psi"]) # 0.069 -> "no significant change"
Reference values with the usual epsilon smoothing (eps = 1e-4, same 10 equal-width bins over the union range for the numeric case): 3.80 for the numeric example and 2.03 for the categorical one. The categorical case is the worrying one: a brand-new category holding 25% of the population is reported as PSI 0.07.
Suggested fix
Follow the common PSI practice of replacing a zero share with a small epsilon (e.g. 1e-4, or 0.5 / n) before taking the log, instead of skipping the bin. If skipping is kept for backward compatibility, the docstring thresholds should say that one-sided bins are excluded, and ideally the diff should expose how many bins/categories were skipped.
Related: #1028 (categorical PSI via _calculate_psi) — if the categorical path is unified with the numeric one, it would be good to fix the zero-bin handling in the shared function at the same time.
Thanks for the detailed report, and for maintaining DataProfiler.
I reproduced this on main (5062fb3) with exact counts instead of random data. Two categorical profiles over 2000 rows each, A with a=50%, b=30%, c=20% and B with a=37.5%, b=22.5%, c=15%, d=25%, report psi 0.0719 from diff(). With the usual epsilon of 1e-4 on zero shares, the same two distributions give 2.03. So a category that goes from 0% to 25% of the population leaves the PSI under the 0.1 "no significant change" threshold.
The reason is the check in categorical_column_profile.py (if (percent_other != 0) and (percent_self != 0)), and the matching iter_psi != float("inf") skip in NumericStatsMixin._calculate_psi. Both drop the bins that carry the most change.
I noticed that the existing tests pin the current behaviour on purpose. test_categorical_column_profile.py has a "psi new category in another profile" case that expects 0.0990, and another that expects 0.1681. So I would rather not change the default output without your view. Three options I could implement:
- Smooth zero shares with a small epsilon by default. This is the standard PSI practice, but it changes those expected values, so it would be a behaviour change for existing users.
- Keep the default as it is, and add an opt-in argument (for example
psi_zero_epsilon=None, where None keeps today's behaviour) for both the categorical and numeric paths. - Keep the numbers, correct the docstring to say one-sided bins are excluded, and report how many bins or categories were skipped in the
diff()output.
Which of these would you accept? I am happy to send a pull request for whichever one you prefer, with tests that fail before and pass after, and I will not start until I hear from you. I will also leave the larger question from the issue about unifying the categorical path with _calculate_psi (#1028) for a separate change.
Thanks for taking this on, and for the exact-count reproduction — 0.0719 → 2.03 matches what I saw with random draws (0.069), so we are looking at the same thing.
One caveat first: I opened the issue but I am not a maintainer, so treat this as my input, not an approval — the Capital One team has the final say on what they will merge.
Of your three options I like option 2 best. Default None keeps today's numbers, so the 0.0990 / 0.1681 tests stay valid, one argument covers both the categorical path and NumericStatsMixin._calculate_psi, and the new tests can use your exact-count fixture (2.03 with eps=1e-4; the numeric example in the issue gives 3.80). Option 1 is where the default should probably end up, but flipping it is a behaviour change and should be the maintainers' call. Option 3 on its own does not fix the under-reporting — a new category at 25% would still read as 0.07.
What we actually do in production is closer to a fourth variant: option 2 plus disclosure of the one-sided bins. The reason: once zero shares are smoothed, a single category that is present on one side and absent on the other dominates the sum. With a 5% baseline share and eps=1e-4 that one term contributes ≈ 0.05 × ln(0.05 / 1e-4) ≈ 0.31 by itself, which flips a PSI from "no change" to "moderate / significant" while the other categories have not moved. We hit exactly this case in monitoring — a PSI went from 0.09 to 0.32 because one small category went to zero, not because the population shifted — and the rule we settled on is: keep the epsilon-smoothed PSI as the number, but always surface which categories/bins were one-sided and their share on the non-zero side, so the reader can tell "a new segment appeared" from "the whole distribution moved". Skipping (today's behaviour) hides the biggest shift; smoothing without disclosure lets one small bin masquerade as a structural change. Disclosure is what makes either number safe to act on.
So my suggestion for the PR:
psi_zero_epsilon=None(opt-in) on both paths, applied identically: replace a zero share withepsbefore the log, no renormalisation. Sincediff()in both profilers already accepts anoptions: dict, that may be the natural place for the knob, but I would defer to the maintainers on where it lives.- Alongside
psi, report the one-sided bins/categories — e.g. apsi_one_sidedentry listing the bin edges / category names and their share on the non-zero side. Returned regardless of whether epsilon is set, so with the default it also tells users what the current PSI excluded (which covers the useful half of your option 3 with more information than a count). - Fix the
_calculate_psidocstring so the 0.1 / 0.2 thresholds state that one-sided bins are excluded whenpsi_zero_epsilon is None.
Out of scope for this PR, but worth separate issues later: making the default eps a 1/n-style floor instead of a fixed 1e-4 (a fixed value penalises very differently at n=2k vs n=2M), and a sampling-noise reference for PSI under the null at the given n and bin count. Agree that unifying the categorical path with _calculate_psi (#1028) stays separate.
Maintainers — happy to hear if you would rather go a different way.