Kolmogorov–Smirnov Two-Sample Test

medium
Unit 17 · Hypothesis Testing II0/3 to milestone

The KS test compares two whole distributions, not just their means. The statistic D is the largest vertical gap between the two empirical CDFs.

Implement:

  • ecdf(sample, x): the fraction of the sample <= x, for each value in x
  • ks_statistic(a, b): D, evaluated at every value in the pooled sample
  • ks_pvalue(D, n, m): the asymptotic p-value. With ne=nmn+mn_e = \frac{nm}{n + m} and λ=(ne+0.12+0.11/ne) D\lambda = (\sqrt{n_e} + 0.12 + 0.11/\sqrt{n_e})\,D, it's Q(λ)=2∑k=1100(−1)k−1e−2k2λ2Q(\lambda) = 2\sum_{k=1}^{100}(-1)^{k-1}e^{-2k^2\lambda^2}, clipped to [0, 1]
  • ks_test(a, b): (D, p)
#hypothesis-testing#drift-detection#distributions2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Kolmogorov–Smirnov Two-Sample Test · TechGradify