Kolmogorov–Smirnov test
In statistics, the Kolmogorov–Smirnov 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, or to compare two samples. It is named after Andrey Kolmogorov and Nikolai Smirnov.
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 or that the samples are drawn from the same distribution. In the one-sample case, the distribution considered under the null hypothesis may be continuous, purely discrete or mixed. In the two-sample case, the distribution considered under the null hypothesis is a continuous distribution but is 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.
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. 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. However, these other tests have their own disadvantages. For instance the Shapiro–Wilk test is known not to work well in samples with many identical values.
Kolmogorov–Smirnov statistic
The empirical distribution function Fn for n independent and identically distributed ordered observations Xi is defined aswhere is the indicator function, equal to 1 if and equal to 0 otherwise.
The Kolmogorov–Smirnov statistic for a given cumulative distribution function F is
where supx is the supremum of the set of distances. By the Glivenko–Cantelli theorem, if the sample comes from distribution F, then Dn converges to 0 almost surely in the limit when goes to infinity. Kolmogorov strengthened this result, by effectively providing the rate of this convergence. Donsker's theorem provides a yet stronger result.
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 variablewhere B is the Brownian bridge. The cumulative distribution function of K is given by
which can also be expressed by the Jacobi theta function. Both the form of the Kolmogorov–Smirnov test statistic and its asymptotic distribution under the null hypothesis were published by Andrey Kolmogorov, while a table of the distribution was published by Nikolai Smirnov. Recurrence relations for the distribution of the test statistic in finite samples are available.
Under null hypothesis that the sample comes from the hypothesized distribution F,
in distribution, where B is the Brownian bridge.
If F is continuous then under the null hypothesis converges to the Kolmogorov distribution, which does not depend on F. This result may also be known as the Kolmogorov theorem. The accuracy of this limit as an approximation to the exact cdf of when is finite is not very impressive: even when, the corresponding maximum error is about ; this error increases to when and to a totally unacceptable when. However, a very simple expedient of replacing by
in the argument of the Jacobi theta function reduces these errors to
,, and respectively; such accuracy would be usually considered more than adequate for all practical applications.
The goodness-of-fit test or the Kolmogorov–Smirnov test can be constructed by using the critical values of the Kolmogorov distribution. This test is asymptotically valid when. It rejects the null hypothesis at level if
where Kα is found from
The asymptotic power of this test is 1.
Fast and accurate algorithms to compute the cdf or its complement for arbitrary and, are available from:
- and for continuous null distributions with code in C and Java to be found in.
- for purely discrete, mixed or continuous null distribution implemented in the KSgeneral package of the R project for statistical computing, which for a given sample also computes the KS test statistic and its p-value. Alternative C++ implementation is available from.
Test with estimated parameters
Using estimated parameters, the questions arises which estimation method should be used. Usually this would be the maximum likelihood method, but e.g. for the normal distribution MLE has a large bias error on sigma. Using a moment fit or KS minimization instead has a large impact on the critical values, and also some impact on test power. If we need to decide for Student-T data with df = 2 via KS test whether the data could be normal or not, then a ML estimate based on H0 would give much larger KS distance, than a fit with minimum KS. In this case we should reject H0, which is often the case with MLE, because the sample standard deviation might be very large for T-2 data, but with KS minimization we may get still a too low KS to reject H0. In the Student-T case, a modified KS test with KS estimate instead of MLE, makes the KS test indeed slightly worse. However, in other cases, such a modified KS test leads to slightly better test power.
Discrete and mixed null distribution
Under the assumption that is non-decreasing and right-continuous, with countable number of jumps, the KS test statistic can be expressed as:From the right-continuity of, it follows that and and hence, the distribution of depends on the null distribution, i.e., is no longer distribution-free as in the continuous case. Therefore, a fast and accurate method has been developed to compute the exact and asymptotic distribution of when is purely discrete or mixed, implemented in C++ and in the KSgeneral package of the R language. The functions
disc_ks_test
, mixed_ks_test
and cont_ks_test
compute also the KS test statistic and p-values for purely discrete, mixed or continuous null distributions and arbitrary sample sizes. The KS test and its p-values for discrete null distributions and small sample sizes are also computed in as part of the dgof package of the R language. Major statistical packages among which SAS PROC NPAR1WAY
, Stata ksmirnov
implement the KS test under the assumption that is continuous, which is more conservative if the null distribution is actually not continuous.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 iswhere and are the empirical distribution functions of the first and the second sample respectively, and is the supremum function.
For large samples, the null hypothesis is rejected at level if
Where and are the sizes of first and second sample respectively. The value of is given in the table below for the most common levels of
0.20 | 0.15 | 0.10 | 0.05 | 0.025 | 0.01 | 0.005 | 0.001 | |
1.073 | 1.138 | 1.224 | 1.358 | 1.48 | 1.628 | 1.731 | 1.949 |
and in general by
so that the condition reads
Here, again, the larger the sample sizes, the more sensitive the minimal bound: For a given ratio of sample sizes, the minimal bound scales in the size of either of the samples according to its inverse square root.
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. Again, tables of critical values have been published. A shortcoming of the Kolmogorov–Smirnov test is that it is not very powerful because it is devised to be sensitive against all possible types of differences between two distribution functions. and showed evidence that the Cucconi test, originally proposed for simultaneously comparing location and scale, is much more powerful than the Kolmogorov–Smirnov test when comparing two distribution functions.
Setting confidence limits for the shape of a distribution function
While the Kolmogorov–Smirnov test is usually used to test whether a given F is the underlying probability distribution of Fn, the procedure may be inverted to give confidence limits on F itself. If one chooses a critical value of the test statistic Dα such that P = α, then a band of width ±Dα around Fn will entirely contain F with probability 1 − α.The Kolmogorov–Smirnov statistic in more than one dimension
A distribution-free multivariate Kolmogorov–Smirnov goodness of fit test has been proposed by Justel, Peña and Zamar. 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 or 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 2d−1 such orderings. One such variation is due to Peacock
and another to Fasano and Franceschini. Critical values for the test statistic can be obtained by simulations, but depend on the dependence structure in the joint distribution.
In one dimension, the Kolmogorov–Smirnov statistic is identical to the so-called star discrepancy D, so another native KS extension to higher dimensions would be simply to use D also for higher dimensions. Unfortunately, the star discrepancy is hard to calculate in high dimensions.
Implementations
The Kolmogorov-Smirnov test is implemented in many software programs:- Mathematica has
- MATLAB has in its Statistics Toolbox.
- The R package "KSgeneral" computes the KS test statistics and its p-values under arbitrary, possibly discrete, mixed or continuous null distribution.
- R's statistics base-package implements the test as in its "stats" package.
- SAS implements the test in its PROC NPAR1WAY procedure.
- Python has an implementation of this test provided by SciPy by Statistical functions
- SYSTAT
- Java has an implementation of this test provided by Apache Commons
- KNIME has a node implementing this test based on the above Java implementation
- StatsDirect implements .
- Stata implements the test in ksmirnov command.
- PSPP implements the test in its function.
- Excel runs the test as KSCRIT and KSPROB