Hubbry Logo
Kolmogorov–Smirnov testKolmogorov–Smirnov testMain
Open search
Kolmogorov–Smirnov test
Community hub
Kolmogorov–Smirnov test
logo
8 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Kolmogorov–Smirnov test
Kolmogorov–Smirnov test
from Wikipedia

Illustration of the Kolmogorov–Smirnov statistic. The red line is a model CDF, the blue line is an empirical CDF, and the black arrow is the KS statistic.

In statistics, the Kolmogorov–Smirnov test (also K–S test or KS test) is a nonparametric test of the equality of continuous (or discontinuous, see Section 2.2), one-dimensional probability distributions. It can be used to test whether a sample came from a given reference probability distribution (one-sample K–S test), or to test whether or not two samples came from the same distribution (two-sample K–S test). It is named after Andrey Kolmogorov and Nikolai Smirnov, who developed it in the 1930s.[1]

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 the one-sample case, the distribution considered under the null hypothesis may be continuous (see Section 2), purely discrete or mixed (see Section 2.2). In the two-sample case (see Section 3), 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 (see Test with estimated parameters). 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.[2] 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.

One-sample Kolmogorov–Smirnov statistic

[edit]

The empirical distribution function Fn for n independent and identically distributed (i.i.d.) ordered observations Xi is defined as

where is the indicator function, equal to 1 if and equal to 0 otherwise.

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

where supx is the supremum of the set of distances. Intuitively, the statistic takes the largest absolute difference between the two distribution functions across all x values.

By the Glivenko–Cantelli theorem, if the sample comes from the distribution F(x), 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 (see Kolmogorov distribution). Donsker's theorem provides a yet stronger result.

In practice, the statistic requires a relatively large number of data points (in comparison to other goodness of fit criteria such as the Anderson–Darling test statistic) to properly reject the null hypothesis.

Kolmogorov distribution

[edit]
Illustration of the Kolmogorov distribution's PDF

The Kolmogorov distribution is the distribution of the random variable

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

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,[4] while a table of the distribution was published by Nikolai Smirnov.[5] Recurrence relations for the distribution of the test statistic in finite samples are available.[4]

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

in distribution, where B(t) 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.[6]

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:

  • [7] and [8] for continuous null distributions with code in C and Java to be found in.[7]
  • [9] for purely discrete, mixed or continuous null distribution implemented in the KSgeneral package [10] 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.[9]

Test with estimated parameters

[edit]

If either the form or the parameters of F(x) are determined from the data Xi 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,[11] and later publications also include the Gumbel distribution.[12] 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.

Using estimated parameters, the question 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 (data is normal, so using the standard deviation for scale) 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.[citation needed]

Discrete and mixed null distribution

[edit]

Under the assumption that is non-decreasing and right-continuous, with countable (possibly infinite) 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,[9] implemented in C++ and in the KSgeneral package [10] 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 [13] as part of the dgof package of the R language. Major statistical packages among which SAS PROC NPAR1WAY,[14] Stata ksmirnov[15] implement the KS test under the assumption that is continuous, which is more conservative if the null distribution is actually not continuous (see [16] [17] [18]).

Two-sample Kolmogorov–Smirnov test

[edit]
Illustration of the two-sample Kolmogorov–Smirnov statistic. Red and blue lines each correspond to an empirical distribution function, and the black arrow is the two-sample KS statistic.

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

where 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[19] 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 (e.g. ), 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 (e.g. whether it's normal or not normal). Again, tables of critical values have been published. A shortcoming of the univariate 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. Some argue[20][21] that the Cucconi test, originally proposed for simultaneously comparing location and scale, can be much more powerful than the Kolmogorov–Smirnov test when comparing two distribution functions.

Two-sample KS tests have been applied in economics to detect asymmetric effects and to study natural experiments.[22]

Setting confidence limits for the shape of a distribution function

[edit]

While the Kolmogorov–Smirnov test is usually used to test whether a given F(x) is the underlying probability distribution of Fn(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(Dn > Dα) = α, then a band of width ±Dα around Fn(x) will entirely contain F(x) with probability 1 − α.

The Kolmogorov–Smirnov statistic in more than one dimension

[edit]

A distribution-free multivariate Kolmogorov–Smirnov goodness of fit test has been proposed by Justel, Peña and Zamar (1997).[23] 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 KS statistics. In d dimensions, there are 2d − 1 such orderings. One such variation is due to Peacock[24] (see also Gosset[25] for a 3D version) and another to Fasano and Franceschini[26] (see Lopes et al. for a comparison and computational details).[27] Critical values for the test statistic can be obtained by simulations, but depend on the dependence structure in the joint distribution.

Implementations

[edit]

The Kolmogorov–Smirnov test is implemented in many software programs. Most of these implement both the one and two sampled test.

  • Mathematica has KolmogorovSmirnovTest.
  • MATLAB's Statistics Toolbox has kstest and kstest2 for one-sample and two-sample Kolmogorov–Smirnov tests, respectively.
  • The R package "KSgeneral"[10] 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 ks.test {stats} in its "stats" package.
  • SAS implements the test in its PROC NPAR1WAY procedure.
  • In Python, the SciPy package implements the test in the scipy.stats.kstest function.[28]
  • SYSTAT (SPSS Inc., Chicago, IL)
  • Java has an implementation of this test provided by Apache Commons.[29]
  • KNIME has a node implementing this test based on the above Java implementation.[30]
  • Julia has the package HypothesisTests.jl with the function ExactOneSampleKSTest(x::AbstractVector{<:Real}, d::UnivariateDistribution).[31]
  • StatsDirect (StatsDirect Ltd, Manchester, UK) implements all common variants.
  • Stata (Stata Corporation, College Station, TX) implements the test in ksmirnov (Kolmogorov–Smirnov equality-of-distributions test) command.[32]
  • PSPP implements the test in its KOLMOGOROV-SMIRNOV (or using KS shortcut function).
  • The Real Statistics Resource Pack for Excel runs the test as KSCRIT and KSPROB.[33]

See also

[edit]

References

[edit]

Further reading

[edit]
[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
The Kolmogorov–Smirnov test (KS test) is a nonparametric statistical test used to determine whether a sample of follows a specified continuous (one-sample KS test) or whether two independent samples are drawn from the same continuous distribution (two-sample KS test). The test evaluates the maximum vertical distance between the empirical cumulative distribution function (ECDF) of the sample(s) and the theoretical (CDF), providing a measure of discrepancy that is sensitive to differences anywhere in the distribution. Developed independently by Russian mathematicians Andrey Nikolaevich Kolmogorov and Nikolai Vasilyevich Smirnov, the one-sample version was introduced by Kolmogorov in 1933 as a method to assess the goodness-of-fit of empirical distributions to theoretical ones. Smirnov extended the framework in 1939 to the two-sample case, allowing comparison of distributions from two populations without assuming a specific parametric form. The , denoted as D, is defined for the one-sample case as D=supxFn(x)F(x),D = \sup_x |F_n(x) - F(x)|, where FnF_n is the ECDF and FF is the reference CDF, and for the two-sample case as D=supxF1,n(x)F2,m(x),D = \sup_x |F_{1,n}(x) - F_{2,m}(x)|, with F1,nF_{1,n} and F2,mF_{2,m} as the ECDFs of the two samples. Under the of no difference, the distribution of D is independent of the underlying continuous distribution, enabling exact p-values via tabulated critical values or asymptotic approximations for large samples. Key advantages of the KS test include its distribution-free nature under the , applicability to any continuous distribution without prior from the , and its ability to detect deviations in , scale, or across the entire support of the distribution. However, it assumes continuous distributions and can be less powerful against specific alternatives compared to parametric tests; it is also sensitive to the presence of ties in discrete and requires the reference distribution to be fully specified. Critical values and significance levels have been extensively tabulated, with asymptotic results facilitating computation for sample sizes n > 40. In practice, the KS test is widely applied in goodness-of-fit testing, such as verifying normality or uniformity in datasets. For instance, it is commonly used to assess whether observed data conform to an expected model before applying parametric methods. It has applications in fields like , hydrology, biological sciences, and credit risk assessment, where the two-sample KS test measures the maximum separation between the cumulative distributions of good (non-defaulting) and bad (defaulting) borrowers, with higher KS values (e.g., 40-60%) indicating stronger discriminatory power. Extensions and modifications, including weighted versions and multivariate analogs, have been developed to address limitations in higher dimensions or specific scenarios.

Introduction

Purpose and variants

The Kolmogorov–Smirnov test serves as a nonparametric primarily used to evaluate goodness-of-fit in the one-sample case, where it determines whether a sample's empirical aligns with a specified theoretical distribution, and in the two-sample case, where it assesses whether two independent samples arise from the same underlying distribution. This test quantifies discrepancies between without requiring parametric assumptions about the form of the distributions, making it suitable for broad applications in distributional inference. As a nonparametric procedure, the test imposes minimal assumptions on the data-generating process, relying only on the continuity of the distributions for the validity of its under the ; it does not presuppose membership in any specific parametric family, such as normality or exponentiality. This distribution-free property under the null enables its use across diverse datasets where parametric forms are unknown or suspect. Key variants include the standard one-sample test, which compares a single empirical distribution to a fully specified reference, and the two-sample test, which compares two empirical distributions directly; a notable adaptation is the , which modifies the one-sample version to account for estimated parameters, such as mean and variance in normality testing. Under the , the distributions are identical, while the alternative posits a difference in their cumulative distribution functions. Developed in the early , the test emerged as a foundational tool for distribution-free , with the one-sample form introduced by Kolmogorov in 1933 and the two-sample extension by Smirnov in 1939.

Historical development

The Kolmogorov–Smirnov test emerged in the early as a nonparametric method for assessing the fit between empirical and theoretical distributions. In 1933, introduced the one-sample variant in his paper "Sulla determinazione empirica di una legge di distribuzione," where he defined the test for continuous distributions and established its , laying the groundwork for modern goodness-of-fit testing. This contribution was contemporaneous with and influenced by the Glivenko-Cantelli theorem, proved by Valery Glivenko in the same year, which demonstrates the almost sure of the to the true as sample size increases. Nikolai V. Smirnov built upon Kolmogorov's foundation with key extensions in the late 1930s and 1940s. In 1939, Smirnov developed the two-sample test in his work "On the Estimation of the Discrepancy Between Empirical Curves of Distribution for Two Independent Samples," enabling comparisons between distributions from two independent samples without assuming a specific form. He further advanced the framework in 1948 by publishing tables for estimating the goodness of fit of empirical distributions, which provided practical computational aids for applying the test in finite samples. These developments formalized the test's variants, shifting it from abstract probability theory toward applied statistics. Post-World War II, the test gained broader acceptance as statistical computing advanced, with its incorporation into software packages by the 1960s enhancing accessibility for researchers. Refinements in the , notably by F. J. Massey, included detailed tables of critical values for the goodness-of-fit statistic, improving the test's usability for small to moderate sample sizes. By the 1970s, the Kolmogorov–Smirnov test had transitioned into a standard practical tool, widely applied in for verifying process distributions and in astronomy for comparing observational data sets to theoretical models.

One-sample test

Statistic definition

The one-sample Kolmogorov–Smirnov test assesses whether a random sample is drawn from a specified continuous distribution by comparing its empirical cumulative distribution function (ECDF) to the theoretical (CDF). Let X1,,XnX_1, \dots, X_n be a random sample from a distribution with hypothesized CDF FF. The ECDF for the sample is defined as Fn(x)=1ni=1n1{Xix},F_n(x) = \frac{1}{n} \sum_{i=1}^n \mathbf{1}\{X_i \leq x\}, where 1{}\mathbf{1}\{\cdot\} is the . The test statistic DnD_n is the supremum of the between the ECDF and the hypothesized CDF, Dn=supxFn(x)F(x),D_n = \sup_x |F_n(x) - F(x)|, which quantifies the maximum vertical distance between the ECDF and the theoretical CDF. This statistic, introduced by Kolmogorov in , measures overall discrepancies in the distribution without assuming a parametric form beyond the specified FF. The is that the sample arises from the distribution with CDF FF, with the alternative that it does not. To compute DnD_n, sort the observations to obtain the order statistics X(1)X(n)X_{(1)} \leq \dots \leq X_{(n)}, then evaluate the differences at the points just before and after each X(i)X_{(i)}, as the supremum occurs at these jumps in the ECDF. Specifically, compute Dn+=max1in(inF(X(i))),Dn=max1in(F(X(i))i1n),D_n^+ = \max_{1 \leq i \leq n} \left( \frac{i}{n} - F(X_{(i)}) \right), \quad D_n^- = \max_{1 \leq i \leq n} \left( F(X_{(i)}) - \frac{i-1}{n} \right), and Dn=max(Dn+,Dn)D_n = \max(D_n^+, D_n^-). For illustration, consider a sample of 1000 observations generated from a standard and tested against the normal CDF (α = 0.05, critical value ≈ 0.043). The computed Dn0.024D_n \approx 0.024 does not exceed the critical value, failing to reject the of normality. In contrast, the same sample size from a yields Dn0.535D_n \approx 0.535, leading to rejection.

Asymptotic distribution

Under the null hypothesis that a sample of size nn is drawn from the continuous distribution function FF, the one-sample Kolmogorov–Smirnov statistic DnD_n satisfies nDndsupt[0,1]B(t),\sqrt{n} \, D_n \xrightarrow{d} \sup_{t \in [0,1]} |B(t)|,
Add your contribution
Related Hubs
User Avatar
No comments yet.