#282Kolmogorov-Smirnov StatisticMedium

Kolmogorov-Smirnov Statistic

Background

The Kolmogorov-Smirnov (KS) statistic detects drift in a continuous feature without assuming a parametric form. It compares two empirical CDFs and reports the maximum gap between them - a single scalar that is easy to monitor and threshold.

Problem statement

Implement ks_statistic(sample_a, sample_b):

KS  =  supxFA(x)FB(x)\text{KS} \;=\; \sup_x \bigl|\,F_A(x) - F_B(x)\,\bigr|

where FA,FBF_A, F_B are the empirical CDFs of the two samples. Returns a float in [0,1][0, 1].

Input

  • sample_a, sample_b - array-like of floats.

Output

  • float in [0,1][0, 1] - maximum absolute CDF difference.

Examples

Input: sample_a=[1,2,3,4,5], sample_b=[1,2,3,4,5]
Output: 0.0
Input: sample_a=[1,2,3], sample_b=[10,11,12]
Output: 1.0      # the two CDFs are 100% apart at any point in between

Constraints

  • Empty input on either side raises ValueError.
  • The standard algorithm: pool and sort all observations, walk through computing CDFs, take max abs diff.

Notes

  • vs PSI. PSI is a sum (KL-flavored) over binned probabilities; KS is a sup over CDFs. KS is bin-free; PSI is bin-dependent but more interpretable in industry.
  • Two-sided. This is the standard two-sided KS; the one-sided variant only checks one direction.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference identical inputs give zero
  • Example disjoint inputs give one
  • Sample partial overlap