Two-sample Kolmogorov-Smirnov statistic for drift alerts
PSI needs bins. The Kolmogorov-Smirnov statistic does not: it is the largest vertical gap between the empirical CDFs of two samples.
Write ks_statistic(a, b). For every value v that appears in either sample, compute F_a(v) = fraction of a that is <= v and F_b(v) likewise. Return max |F_a(v) - F_b(v)|. Ties and repeated values must be handled exactly.
def ks_statistic(a, b)Understand the idea
The Kolmogorov-Smirnov statistic compares two samples without any bins. Draw each sample's empirical CDF, the fraction of values at or below v, and find the biggest vertical gap between the two curves. 0 means identical shapes; 1 means no overlap at all. Like comparing two staircases and measuring where their steps are furthest apart. The gap can only change at observed values, so checking every value from either sample is enough. Worked example: a = [1, 2, 3], b = [2, 3, 4]. At v = 1: F_a = 1/3, F_b = 0, gap 0.333. At v = 2: 2/3 vs 1/3, gap 0.333. At v = 3: 1 vs 2/3, gap 0.333. At v = 4: 1 vs 1, gap 0. KS = 0.333. Repeated values are handled by counting values <= v, which np.searchsorted with side='right' does on a sorted array. Rule: KS = max over all observed v of |F_a(v) - F_b(v)|, with F(v) = count(x <= v) / n. Sort both samples first. Drift monitors use KS on numeric features.