Frequent Links
Kolmogorov–Smirnov test
In statistics, the Kolmogorov–Smirnov test (K–S test or KS test) is a nonparametric test of the equality of continuous, onedimensional probability distributions that can be used to compare a sample with a reference probability distribution (onesample K–S test), or to compare two samples (twosample 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 samples are drawn from the same distribution (in the twosample case) or that the sample is drawn from the reference distribution (in the onesample case). In each case, the distributions considered under the null hypothesis are continuous distributions but are otherwise unrestricted.
The twosample 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.
The Kolmogorov–Smirnov test can be modified to serve as a goodness of fit test. In the special case of testing for normality of the distribution, samples are standardized and compared with a standard normal distribution. This is equivalent to setting the mean and variance of the reference distribution equal to the sample estimates, and it is known that using these to define the specific reference distribution changes the null distribution of the test statistic: see below. Various studies have found that, even in this corrected form, the test is less powerful for testing normality than the Shapiro–Wilk test or Anderson–Darling test.^{[1]} However, other tests have their own disadvantages. For instance the Shapiro–Wilk test is known not to work well with many ties (many identical values).
Contents
Kolmogorov–Smirnov statistic
The empirical distribution function F_{n} for n iid observations X_{i} is defined as
 <math>F_n(x)={1 \over n}\sum_{i=1}^n I_{[\infty,x]}(X_i)</math>
where <math>I_{[\infty,x]}(X_i)</math> is the indicator function, equal to 1 if <math>X_i \le x</math> and equal to 0 otherwise.
The Kolmogorov–Smirnov statistic for a given cumulative distribution function F(x) is
 <math>D_n= \sup_x F_n(x)F(x)</math>
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 <math>n</math> goes to infinity. Kolmogorov strengthened this result, by effectively providing the rate of this convergence (see below). Donsker's theorem provides yet a stronger result.
In practice, the statistic requires a relatively large number of data points^{[quantify]} to properly reject the null hypothesis.
Kolmogorov distribution
The Kolmogorov distribution is the distribution of the random variable
 <math>K=\sup_{t\in[0,1]}B(t)</math>
where B(t) is the Brownian bridge. The cumulative distribution function of K is given by^{[2]}
 <math>\operatorname{Pr}(K\leq x)=12\sum_{k=1}^\infty (1)^{k1} e^{2k^2 x^2}=\frac{\sqrt{2\pi}}{x}\sum_{k=1}^\infty e^{(2k1)^2\pi^2/(8x^2)}.</math>
Both the form of the Kolmogorov–Smirnov test statistic and its asymptotic distribution under the null hypothesis were published by Andrey Kolmogorov,^{[3]} while a table of the distribution was published by Nikolai Vasilyevich Smirnov.^{[4]} Recurrence relations for the distribution of the test statistic in finite samples are available.^{[3]}
Under null hypothesis that the sample comes from the hypothesized distribution F(x),
 <math>\sqrt{n}D_n\xrightarrow{n\to\infty}\sup_t B(F(t))</math>
in distribution, where B(t) is the Brownian bridge.
If F is continuous then under the null hypothesis <math>\sqrt{n}D_n</math> converges to the Kolmogorov distribution, which does not depend on F. This result may also be known as the Kolmogorov theorem; see Kolmogorov's theorem for disambiguation.
The goodnessoffit test or the Kolmogorov–Smirnov test is constructed by using the critical values of the Kolmogorov distribution. The null hypothesis is rejected at level <math>\alpha</math> if
 <math>\sqrt{n}D_n>K_\alpha,\,</math>
where K_{α} is found from
 <math>\operatorname{Pr}(K\leq K_\alpha)=1\alpha.\,</math>
The asymptotic power of this test is 1.
Test with estimated parameters
If either the form or the parameters of F(x) are determined from the data X_{i} the critical values determined in this way are invalid. In such cases, Monte Carlo or other methods may be required, but tables have been prepared for some cases. Details for the required modifications to the test statistic and for the critical values for the normal distribution and the exponential distribution have been published,^{[5]} and later publications also include the Gumbel distribution.^{[6]} The Lilliefors test represents a special case of this for the normal distribution. The logarithm transformation may help to overcome cases where the Kolmogorov test data does not seem to fit the assumption that it came from the normal distribution.
Discrete null distribution
The Kolmogorov–Smirnov test must be adapted for discrete variables.^{[7]} The form of the test statistic remains the same as in the continuous case, but the calculation of its value is more subtle. We can see this if we consider computing the test statistic between a continuous distribution <math>f(x)</math> and a step function <math>g(x)</math> that has a discontinuity at <math>x_{\rm i}</math>. In other words, the limit <math>\lim_{x\rightarrow x_{\rm i}} g(x)</math> does not exist. Thus, when computing the statistic
<math>\sup_x g(x)f(x) = \max_{\rm i} \left[ \max\left( g(x_{\rm i})f(x_{\rm i}),\lim_{x\rightarrow x_{\rm i}} g(x)  f(x_{\rm i1}) \right) \right],</math>
it is unclear how to replace the limit, unless we know the limiting value of the underlying distribution.
The discretized KS test is implemented in the ks.test() function in the dgof package of the R project for statistical computing.^{[7]}
Twosample Kolmogorov–Smirnov test
The Kolmogorov–Smirnov test may also be used to test whether two underlying onedimensional probability distributions differ. In this case, the Kolmogorov–Smirnov statistic is
 <math>D_{n,n'}=\sup_x F_{1,n}(x)F_{2,n'}(x),</math>
where <math>F_{1,n}</math> and <math>F_{2,n'}</math> are the empirical distribution functions of the first and the second sample respectively, and <math>sup</math> is the supremum function.
The null hypothesis is rejected at level <math>\alpha</math> if
 <math>D_{n,n'}>c(\alpha)\sqrt{\frac{n + n'}{n n'}}.</math>^{[8]}
The value of <math>c({\alpha})</math> is given in the table below for each level of <math>\alpha</math>^{[8]}
<math>\alpha</math>  0.10  0.05  0.025  0.01  0.005  0.001 
<math>c({\alpha})</math>  1.22  1.36  1.48  1.63  1.73  1.95 
Note that the twosample 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). Again, tables of critical values have been published.^{[5]}^{[8]} These critical values have one thing in common with the Anderson–Darling and Chisquares, namely the fact that higher values tend to be more rare.^{[9]}
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_{α} such that P(D_{n} > D_{α}) = α, then a band of width ±D_{α} around F_{n}(x) will entirely contain F(x) with probability 1 − α.
The Kolmogorov–Smirnov statistic in more than one dimension
A distributionfree multivariate Kolmogorov–Smirnov goodness of fit test has been proposed by Justel, Peña and Zamar (1997).^{[10]} The test uses a statistic which is built using Rosenblatt's transformation, and an algorithm is developed to compute it in the bivariate case. An approximate test that can be easily computed in any dimension is also presented.
The Kolmogorov–Smirnov test statistic needs to be modified if a similar test is to be applied to multivariate data. This is not straightforward because the maximum difference between two joint cumulative distribution functions is not generally the same as the maximum difference of any of the complementary distribution functions. Thus the maximum difference will differ depending on which of <math>\Pr(x < X \and y < Y)</math> or <math>\Pr(X < x \and Y > y)</math> or any of the other two possible arrangements is used. One might require that the result of the test used should not depend on which choice is made.
One approach to generalizing the Kolmogorov–Smirnov statistic to higher dimensions which meets the above concern is to compare the cdfs of the two samples with all possible orderings, and take the largest of the set of resulting K–S statistics. In d dimensions, there are 2^{d}−1 such orderings. One such variation is due to Peacock^{[11]} and another to Fasano and Franceschini^{[12]} (see Lopes et al. for a comparison and computational details).^{[13]} Critical values for the test statistic can be obtained by simulations, but depend on the dependence structure in the joint distribution.
See also
 Cramér–von Mises test
 Dvoretzky–Kiefer–Wolfowitz inequality
 Jarque–Bera test
 Kuiper's test
 pvalue
 Siegel–Tukey test
 Total variation distance of probability measures
 Lilliefors test
Footnotes
 ↑ Stephens, M. A. (1974). "EDF Statistics for Goodness of Fit and Some Comparisons". Journal of the American Statistical Association (American Statistical Association) 69 (347): 730–737. JSTOR 2286009. doi:10.2307/2286009.
 ↑ Marsaglia G, Tsang WW, Wang J (2003). "Evaluating Kolmogorov’s Distribution". Journal of Statistical Software 8 (18): 1–4.
 ↑ ^{3.0} ^{3.1} Kolmogorov A (1933). "Sulla determinazione empirica di una legge di distribuzione". G. Ist. Ital. Attuari 4: 83–91.
 ↑ Smirnov N (1948). "Table for estimating the goodness of fit of empirical distributions". Annals of Mathematical Statistics 19: 279–281. doi:10.1214/aoms/1177730256.
 ↑ ^{5.0} ^{5.1} Pearson, E. S. and Hartley, H. O., ed. (1972). Biometrika Tables for Statisticians 2. Cambridge University Press. pp. 117–123, Tables 54, 55. ISBN 0521069378.
 ↑ Shorack, Galen R.; Wellner, Jon A. (1986). Empirical Processes with Applications to Statistics. Wiley. p. 239. ISBN 047186725X.
 ↑ ^{7.0} ^{7.1} Arnold, Taylor B.; Emerson, John W. (2011). "Nonparametric GoodnessofFit Tests for Discrete Null Distributions" (PDF). The R Journal 3 (2): 34–39.
 ↑ ^{8.0} ^{8.1} ^{8.2} Table of critical values for the twosample test
 ↑ Mehta, S. (2014) Statistics Topics ISBN 9781499273533
 ↑ Justel, A.; Peña, D.; Zamar, R. (1997). "A multivariate Kolmogorov–Smirnov test of goodness of fit". Statistics & Probability Letters 35 (3): 251–259. doi:10.1016/S01677152(97)000205.
 ↑ Peacock J.A. (1983). "Twodimensional goodnessoffit testing in astronomy". Monthly Notices of the Royal Astronomical Society 202: 615–627. Bibcode:1983MNRAS.202..615P. doi:10.1093/mnras/202.3.615.
 ↑ Fasano, G., Franceschini, A. (1987). "A multidimensional version of the Kolmogorov–Smirnov test". Monthly Notices of the Royal Astronomical Society 225: 155–170. Bibcode:1987MNRAS.225..155F. ISSN 00358711. doi:10.1093/mnras/225.1.155.
 ↑ Lopes, R.H.C., Reid, I., Hobson, P.R. (April 23–27, 2007). The twodimensional Kolmogorov–Smirnov test (PDF). XI International Workshop on Advanced Computing and Analysis Techniques in Physics Research. Amsterdam, the Netherlands.
References
 Eadie, W.T.; D. Drijard, F.E. James, M. Roos and B. Sadoulet (1971). Statistical Methods in Experimental Physics. Amsterdam: NorthHolland. pp. 269–271. ISBN 0444101179.
 Stuart, Alan; Ord, Keith; Arnold, Steven [F.] (1999). Classical Inference and the Linear Model. Kendall's Advanced Theory of Statistics 2A (Sixth ed.). London: Arnold. pp. 25.37–25.43. ISBN 0340662301. MR 1687411.
 Corder, G. W.; Foreman, D. I. (2014). Nonparametric Statistics: A StepbyStep Approach. Wiley. ISBN 9781118840313.
 Stephens, M. A. (1979). "Test of fit for the logistic distribution based on the empirical distribution function". Biometrika 66 (3): 591–595. doi:10.1093/biomet/66.3.591.
External links
 Hazewinkel, Michiel, ed. (2001), "Kolmogorov–Smirnov test", Encyclopedia of Mathematics, Springer, ISBN 9781556080104
 Short introduction
 KS test explanation
 JavaScript implementation of one and twosided tests
 Online calculator with the KS test
 Opensource C++ code to compute the Kolmogorov distribution and perform the KS test
 Paper on Evaluating Kolmogorov’s Distribution; contains C implementation. This is the method used in Matlab.
 Paper powerlaw: A Python Package for Analysis of HeavyTailed Distributions; Jeff Alstott, Ed Bullmore, Dietmar Plenz. Among others, it also performs the Kolmogorov–Smirnov test. Source code and installers of powerlaw package are available at PyPi.
