# Kolmogorov-Smirnov Test

The Kolmogorov–Smirnov test (K–S test or KS test) is a nonparametric test of the equality of continuous, one-dimensional probability distributions that can be used to compare a sample with a reference probability distribution (one-sample K–S test), or to compare two samples (two-sample K–S test). The Kolmogorov–Smirnov statistic quantifies a distance between the empirical distribution function of the sample and the cumulative distribution function of the reference distribution, or between the empirical distribution functions of two samples. The null distribution of this statistic is calculated under the null hypothesis that the sample is drawn from the reference distribution (in the one-sample case) or that the samples are drawn from the same distribution (in the two-sample case). In each case, the distributions considered under the null hypothesis are continuous distributions but are otherwise unrestricted.

The two-sample K–S test is one of the most useful and general nonparametric methods for comparing two samples, as it is sensitive to differences in both location and shape of the empirical cumulative distribution functions of the two samples.

## Kolmogorov–Smirnov statistic

The empirical distribution function $F_n$ for $n$ iid observations $X_i$ is defined as:

[$]F_n(x)={1 \over n}\sum_{i=1}^n I_{[-\infty,x]}(X_i)[$]

where $I_{[-\infty,x]}(X_i)$ is the indicator function, equal to 1 if $X_i \le x$ and equal to 0 otherwise.

The Kolmogorov–Smirnov statistic for a given cumulative distribution function $F(x)$

[$]D_n= \sup_x |F_n(x)-F(x)|[$]

where $\sup_x$ is the supremum of the set of distances. By the Glivenko–Cantelli theorem, if the sample comes from distribution $F(x)$, then $D_n$ converges to 0 almost surely in the limit when $n$ goes to infinity. Kolmogorov strengthened this result, by effectively providing the rate of this convergence (see below). In practice, the statistic requires a relatively large number of data points to properly reject the null hypothesis.

## Kolmogorov distribution

The Kolmogorov distribution is the distribution of the random variable:

[$]K=\sup_{t\in[0,1]}|B(t)|[$]

where $B(t)$ is the Brownian bridge. The cumulative distribution function of $K$ is given by[1]:

[$]P(K\leq x)=1-2\sum_{k=1}^\infty (-1)^{k-1} e^{-2k^2 x^2}=\frac{\sqrt{2\pi}}{x}\sum_{k=1}^\infty e^{-(2k-1)^2\pi^2/(8x^2)}.[$]

Under null hypothesis that the sample comes from the hypothesized distribution $F(x)$,

[$]\sqrt{n}D_n\xrightarrow{D}\sup_t |B(F(t))|[$]

where $B(t)$ is the Brownian bridge and the convergence is in distribution. If $F$ is continuous then under the null hypothesis $\sqrt{n}D_n$ converges to the Kolmogorov distribution, which does not depend on $F$.

The goodness-of-fit test or the Kolmogorov–Smirnov test is constructed by using the critical values of the Kolmogorov distribution. The null hypothesis is rejected at level $\alpha$ if $\sqrt{n}D_{n} \gt K_{\alpha}$ where $K_{\alpha}$ is found from $P(K\leq K_{\alpha})=1-\alpha.\,$. The value of $K_{\alpha}$ is given in the table below for each level of $\alpha$[2]:

 $\alpha$ 0.1 0.05 0.025 0.01 0.005 0.001 $K_{\alpha}$ 1.22 1.36 1.48 1.63 1.73 1.95

## Two-sample Kolmogorov–Smirnov test

The Kolmogorov–Smirnov test may also be used to test whether two underlying one-dimensional probability distributions differ. In this case, the Kolmogorov–Smirnov statistic is :

[$]D_{n,n'}=\sup_x |F_{1,n}(x)-F_{2,n'}(x)|,[$]

where $F_{1,n}$ and $F_{2,n'}$ are the empirical distribution functions of the first and the second sample respectively, and $\sup$ is the supremum function.

The null hypothesis is rejected at level $\alpha$ if

[$]D_{n,n'} \gt K_{\alpha}\sqrt{\frac{n + n'}{n n'}}.[$]

Note that the two-sample test checks whether the two data samples come from the same distribution. This does not specify what that common distribution is (e.g. whether it's normal or not normal).

## Setting confidence limits for the shape of a distribution function

While the Kolmogorov–Smirnov test is usually used to test whether a given $F(x)$ is the underlying probability distribution of $F_n(x)$, the procedure may be inverted to give confidence limits on $F(x)$ itself. If one chooses a critical value of the test statistic, $D_{\alpha}$, such that $P(D_n \gt D_{\alpha}) = \alpha$, then a band of width $± D_{\alpha}$ around $F_n(x)$ will entirely contain $F(x)$ with probability $1-\alpha$.

## References

1. Marsaglia G, Tsang WW, Wang J (2003). "Evaluating Kolmogorov’s Distribution". Journal of Statistical Software 8 (18): 1–4.
2. Table of critical values for the two-sample test

## Wikipedia References

• Wikipedia contributors. "Kolmogorov–Smirnov test". Wikipedia. Wikipedia. Retrieved 31 May 2019.