KS shift (Kolmogorov–Smirnov shift)
KS shift is the KS statistic pointed at a monitoring question: how far has a feature’s distribution moved between the baseline window and the current one.
\[ D = \sup_x |F_{\text{baseline}}(x) - F_{\text{current}}(x)| \]
It needs no bins, unlike PSI, and it reads in units anyone can hold in their head. That readability is what produces the rubric you see everywhere: below 0.1 is fine, 0.1 to 0.2 is worth watching, above 0.2 is drift. The rubric is the problem.
The number has a floor, and the floor moves
Draw both windows from the same distribution, so that no drift exists at all, and read D anyway. The helper ks_stat computes D directly: it sorts both samples, evaluates each empirical CDF at every pooled observation with searchsorted, and returns the largest absolute gap, the same quantity that scipy.stats.ks_2samp reports as its statistic.
import numpy as np
def ks_stat(a, b):
a, b = np.sort(a), np.sort(b)
g = np.concatenate([a, b])
return float(np.max(np.abs(np.searchsorted(a, g, "right") / a.size
- np.searchsorted(b, g, "right") / b.size)))
rng = np.random.default_rng(3)
print("%9s %6s %9s %9s %9s %12s" % ("n / side", "reps", "median D", "95th pct", "max D",
"share > 0.10"))
for n, reps in ((100, 4000), (1000, 2000), (10000, 600), (100000, 200)):
d = np.array([ks_stat(rng.normal(size=n), rng.normal(size=n)) for _ in range(reps)])
print("%9d %6d %9.4f %9.4f %9.4f %11.1f%%"
% (n, reps, np.median(d), np.quantile(d, 0.95), d.max(), (d > 0.10).mean() * 100))
n / side reps median D 95th pct max D share > 0.10
100 4000 0.1100 0.1900 0.3300 62.5%
1000 2000 0.0360 0.0590 0.0940 0.0%
10000 600 0.0121 0.0192 0.0289 0.0%
100000 200 0.0036 0.0063 0.0087 0.0%
At 100 rows a side, the typical reading with nothing wrong is 0.1100, already inside the band the rubric calls a moderate shift, and 62.5% of windows cross the 0.10 line that is supposed to mean no meaningful drift. At 100,000 rows a side the largest of two hundred no-drift readings is 0.0087, so 0.10 was never crossed in these no-drift draws. This says nothing about the maximum under an alternative: a sufficiently large population shift can readily exceed 0.10 at that sample size.
This is not a quirk of the simulation. D is a supremum over a curve estimated from finite samples, so it has a positive expected value under the null and that value decays like \( 1/\sqrt{n} \). A fixed threshold is a statement about sample size wearing the costume of a statement about the data.
What to compare it against instead
The floor is not mysterious. The asymptotic 5% critical value for the two-sample statistic is \( 1.36\sqrt{(n+m)/(nm)} \), which for equal windows is \( 1.36\sqrt{2/n} \). Hold a real shift constant and read it at three window sizes. This block and the next one reuse ks_stat from the first block and run in the same session.
rng = np.random.default_rng(23)
shift = 0.05
rows = []
for n, reps in ((200, 3000), (2000, 1500), (20000, 400)):
floor = np.quantile([ks_stat(rng.normal(size=n), rng.normal(size=n))
for _ in range(reps)], 0.95)
d = np.mean([ks_stat(rng.normal(size=n), rng.normal(shift, 1, n))
for _ in range(reps)])
rows.append((n, d, float(floor)))
print("%9s %10s %13s %10s %14s" % ("n / side", "measured D", "no-drift 95th",
"D / floor", "1.36*sqrt(2/n)"))
for n, d, floor in rows:
print("%9d %10.4f %13.4f %10.2f %14.4f" % (n, d, floor, d / floor, 1.36 * np.sqrt(2 / n)))
n / side measured D no-drift 95th D / floor 1.36*sqrt(2/n)
200 0.0888 0.1300 0.68 0.1360
2000 0.0357 0.0425 0.84 0.0430
20000 0.0231 0.0131 1.76 0.0136
The same 0.05 standard-deviation shift reads as 0.0888 at 200 rows and 0.0231 at 20,000. The larger window gives the smaller number, because the small-window reading was mostly noise. Only the bottom row separates signal from floor, at a ratio of 1.76, and its D is 0.0231 — a number the fixed rubric files under no meaningful drift.
For statistical evidence, compare D against \( 1.36\sqrt{2/n} \) for the window you actually have, or hold the window size fixed across days so that yesterday’s number and today’s are the same measurement. A fixed effect-size threshold can still be an intentional practical tolerance; pair it with uncertainty and appropriate multiple-testing or sequential control rather than treating it as a universal significance threshold.
One feature at a time
KS is defined on one dimension. A production monitor therefore runs it per feature and alerts on the worst one, which leaves a specific kind of change with nowhere to land.
rng = np.random.default_rng(101)
n = 4000
floor = 1.36 * np.sqrt(2 / n)
base = rng.normal(size=(n, 2))
a, b = rng.normal(size=n), rng.normal(size=n)
cur = np.column_stack([a, 0.8 * a + 0.6 * b])
print("Noise floor at n = %d per side: %.4f" % (n, floor))
for i in (0, 1):
d = ks_stat(base[:, i], cur[:, i])
print("feature %d D = %.4f %.2f x the floor" % (i + 1, d, d / floor))
print("correlation baseline %+.4f current %+.4f" % (np.corrcoef(base.T)[0, 1],
np.corrcoef(cur.T)[0, 1]))
d = ks_stat(base[:, 0] * base[:, 1], cur[:, 0] * cur[:, 1])
print("product D = %.4f %.2f x the floor" % (d, d / floor))
Noise floor at n = 4000 per side: 0.0304
feature 1 D = 0.0203 0.67 x the floor
feature 2 D = 0.0170 0.56 x the floor
correlation baseline +0.0135 current +0.7954
product D = 0.3060 10.06 x the floor
Both features are still standard normal, and both per-feature readings sit below the noise floor. The correlation between them went from +0.0135 to +0.7954. Both nondegenerate normal distributions have support across the same real plane, but the frequencies of feature combinations have changed. Models using interactions can be affected even when both per-feature gauges remain below their thresholds; the direction and size of the performance effect still require evaluation.
Multiplying the two features exposes it at ten times the floor, but only because someone knew which pair to multiply. With forty features there are 780 pairs and no reason to prefer any of them. That is the boundary of the method rather than a tuning problem: KS shift is a good univariate gauge, and joint structure needs a test that was built for it, such as MMD, energy distance, or a classifier two-sample test.
Worth keeping separate: a moved feature is covariate drift, and covariate drift is neither necessary nor sufficient for the model to get worse. KS shift measures the input, not the damage.
References
- SciPy documentation,
scipy.stats.ks_2samp. - Gretton, A., Borgwardt, K. M., Rasch, M. J., Schölkopf, B., & Smola, A. (2012). A kernel two-sample test. Journal of Machine Learning Research, 13, 723–773.
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
