
In
statistics
Statistics (from German language, German: ', "description of a State (polity), state, a country") is the discipline that concerns the collection, organization, analysis, interpretation, and presentation of data. In applying statistics to a s ...
, the Kolmogorov–Smirnov test (also K–S test or KS test) is a
nonparametric test
Nonparametric statistics is a type of statistical analysis that makes minimal assumptions about the underlying distribution of the data being studied. Often these models are infinite-dimensional, rather than finite dimensional, as in parametric sta ...
of the equality of continuous (or discontinuous, see
Section 2.2), one-dimensional
probability distribution
In probability theory and statistics, a probability distribution is a Function (mathematics), function that gives the probabilities of occurrence of possible events for an Experiment (probability theory), experiment. It is a mathematical descri ...
s. It can be used to test whether a
sample
Sample or samples may refer to:
* Sample (graphics), an intersection of a color channel and a pixel
* Sample (material), a specimen or small quantity of something
* Sample (signal), a digital discrete sample of a continuous analog signal
* Sample ...
came from a given reference probability distribution (one-sample K–S test), or to test whether two samples came from the same distribution (two-sample K–S test). Intuitively, it provides a method to qualitatively answer the question "How likely is it that we would see a collection of samples like this if they were drawn from that probability distribution?" or, in the second case, "How likely is it that we would see two sets of samples like this if they were drawn from the same (but unknown) probability distribution?".
It is named after
Andrey Kolmogorov
Andrey Nikolaevich Kolmogorov ( rus, Андре́й Никола́евич Колмого́ров, p=ɐnˈdrʲej nʲɪkɐˈlajɪvʲɪtɕ kəlmɐˈɡorəf, a=Ru-Andrey Nikolaevich Kolmogorov.ogg, 25 April 1903 – 20 October 1987) was a Soviet ...
and
Nikolai Smirnov.
The Kolmogorov–Smirnov statistic quantifies a
distance
Distance is a numerical or occasionally qualitative measurement of how far apart objects, points, people, or ideas are. In physics or everyday usage, distance may refer to a physical length or an estimation based on other criteria (e.g. "two co ...
between the
empirical distribution function
In statistics, an empirical distribution function ( an empirical cumulative distribution function, eCDF) is the Cumulative distribution function, distribution function associated with the empirical measure of a Sampling (statistics), sample. Th ...
of the sample and the
cumulative distribution function
In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x.
Ever ...
of the reference distribution, or between the empirical distribution functions of two samples. The
null distribution
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
of this statistic is calculated under the
null hypothesis
The null hypothesis (often denoted ''H''0) is the claim in scientific research that the effect being studied does not exist. The null hypothesis can also be described as the hypothesis in which no relationship exists between two sets of data o ...
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
The goodness of fit of a statistical model describes how well it fits a set of observations. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question. Such measur ...
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
The Shapiro–Wilk test is a Normality test, test of normality. It was published in 1965 by Samuel Sanford Shapiro and Martin Wilk.
Theory
The Shapiro–Wilk test tests the null hypothesis that a statistical sample, sample ''x''1, ..., ''x'n'' ...
or
Anderson–Darling test
The Anderson–Darling test is a statistical test of whether a given sample of data is drawn from a given probability distribution. In its basic form, the test assumes that there are no parameters to be estimated in the distribution being tested, i ...
. 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
The
empirical distribution function
In statistics, an empirical distribution function ( an empirical cumulative distribution function, eCDF) is the Cumulative distribution function, distribution function associated with the empirical measure of a Sampling (statistics), sample. Th ...
''F''
''n'' for ''n''
independent and identically distributed
Independent or Independents may refer to:
Arts, entertainment, and media Artist groups
* Independents (artist group), a group of modernist painters based in Pennsylvania, United States
* Independentes (English: Independents), a Portuguese artist ...
(i.i.d.) ordered observations ''X
i'' is defined as
where
is the
indicator function
In mathematics, an indicator function or a characteristic function of a subset of a set is a function that maps elements of the subset to one, and all other elements to zero. That is, if is a subset of some set , then the indicator functio ...
, equal to 1 if
and equal to 0 otherwise.
The Kolmogorov–Smirnov
statistic
A statistic (singular) or sample statistic is any quantity computed from values in a sample which is considered for a statistical purpose. Statistical purposes include estimating a population parameter, describing a sample, or evaluating a hypot ...
for a given
cumulative distribution function
In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x.
Ever ...
''F''(''x'') is
where sup
''x'' is the
supremum
In mathematics, the infimum (abbreviated inf; : infima) of a subset S of a partially ordered set P is the greatest element in P that is less than or equal to each element of S, if such an element exists. If the infimum of S exists, it is unique, ...
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
In the theory of probability, the Glivenko–Cantelli theorem (sometimes referred to as the fundamental theorem of statistics), named after Valery Ivanovich Glivenko and Francesco Paolo Cantelli, describes the asymptotic behaviour of the empirica ...
, if the sample comes from the distribution ''F''(''x''), then ''D''
''n'' converges to 0
almost surely
In probability theory, an event is said to happen almost surely (sometimes abbreviated as a.s.) if it happens with probability 1 (with respect to the probability measure). In other words, the set of outcomes on which the event does not occur ha ...
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
In probability theory, Donsker's theorem (also known as Donsker's invariance principle, or the functional central limit theorem), named after Monroe D. Donsker, is a functional extension of the central limit theorem for empirical distribution fun ...
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
The Anderson–Darling test is a statistical test of whether a given sample of data is drawn from a given probability distribution. In its basic form, the test assumes that there are no parameters to be estimated in the distribution being tested, i ...
statistic) to properly reject the null hypothesis.
Kolmogorov distribution

The Kolmogorov distribution is the distribution of the
random variable
A random variable (also called random quantity, aleatory variable, or stochastic variable) is a Mathematics, mathematical formalization of a quantity or object which depends on randomness, random events. The term 'random variable' in its mathema ...
where ''B''(''t'') is the
Brownian bridge
A Brownian bridge is a continuous-time gaussian process ''B''(''t'') whose probability distribution is the conditional probability distribution of a standard Wiener process ''W''(''t'') (a mathematical model of Brownian motion) subject to the con ...
. The
cumulative distribution function
In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x.
Ever ...
of ''K'' is given by
which can also be expressed by the
Jacobi theta function
In mathematics, theta functions are special functions of several complex variables. They show up in many topics, including Abelian varieties, moduli spaces, quadratic forms, and solitons. Theta functions are parametrized by points in a tube do ...
. Both the form of the Kolmogorov–Smirnov test statistic and its asymptotic distribution under the null hypothesis were published by
Andrey Kolmogorov
Andrey Nikolaevich Kolmogorov ( rus, Андре́й Никола́евич Колмого́ров, p=ɐnˈdrʲej nʲɪkɐˈlajɪvʲɪtɕ kəlmɐˈɡorəf, a=Ru-Andrey Nikolaevich Kolmogorov.ogg, 25 April 1903 – 20 October 1987) was a Soviet ...
,
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''(''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.
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
Power may refer to:
Common meanings
* Power (physics), meaning "rate of doing work"
** Engine power, the power put out by an engine
** Electric power, a type of energy
* Power (social and political), the ability to influence people or events
Math ...
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
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
Monte Carlo ( ; ; or colloquially ; , ; ) is an official administrative area of Monaco, specifically the Ward (country subdivision), ward of Monte Carlo/Spélugues, where the Monte Carlo Casino is located. Informally, the name also refers to ...
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
In probability theory and statistics, a normal distribution or Gaussian distribution is a type of continuous probability distribution for a real-valued random variable. The general form of its probability density function is
f(x) = \frac ...
and the exponential distribution
In probability theory and statistics, the exponential distribution or negative exponential distribution is the probability distribution of the distance between events in a Poisson point process, i.e., a process in which events occur continuousl ...
have been published, and later publications also include the Gumbel distribution
In probability theory and statistics, the Gumbel distribution (also known as the type-I generalized extreme value distribution) is used to model the distribution of the maximum (or the minimum) of a number of samples of various distributions.
Thi ...
. The Lilliefors test
Lilliefors test is a normality test based on the Kolmogorov–Smirnov test. It is used to test the null hypothesis that data come from a normally distributed population, when the null hypothesis does not specify ''which'' normal distribution; i.e. ...
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.
Discrete and mixed null distribution
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,[ implemented in C++ and in the KSgeneral package ][ of the ]R language
R, or r, is the eighteenth letter of the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''ar'' (pronounced ), plural ''ars''.
The lette ...
. 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
Stata (, , alternatively , occasionally stylized as STATA) is a general-purpose Statistics, statistical software package developed by StataCorp for data manipulation, visualization, statistics, and automated reporting. It is used by researchers ...
ksmirnov
implement the KS test under the assumption that is continuous, which is more conservative if the null distribution is actually not continuous (see
).
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
where and are the empirical distribution function
In statistics, an empirical distribution function ( an empirical cumulative distribution function, eCDF) is the Cumulative distribution function, distribution function associated with the empirical measure of a Sampling (statistics), sample. Th ...
s 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
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 (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 that the Cucconi test
In statistics, the Cucconi test is a nonparametric test for jointly comparing central tendency and variability (detecting location and scale changes) in two samples. Many rank tests have been proposed for the two-sample location-scale problem. Ne ...
, 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.
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 distribution-free multivariate Kolmogorov–Smirnov goodness of fit test has been proposed by Justel, Peña and Zamar (1997). 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
Multivariate statistics is a subdivision of statistics encompassing the simultaneous observation and analysis of more than one outcome variable, i.e., ''multivariate random variables''.
Multivariate statistics concerns understanding the differe ...
. This is not straightforward because the maximum difference between two joint cumulative distribution function
In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x.
Ever ...
s 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 2''d'' − 1 such orderings. One such variation is due to Peacock
(see also Gosset
for a 3D version)
and another to Fasano and Franceschini (see Lopes et al. for a comparison and computational details). Critical values for the test statistic can be obtained by simulations, but depend on the dependence structure in the joint distribution.
Implementations
The Kolmogorov–Smirnov test is implemented in many software programs. Most of these implement both the one and two sampled test.
* Mathematica
Wolfram (previously known as Mathematica and Wolfram Mathematica) is a software system with built-in libraries for several areas of technical computing that allows machine learning, statistics, symbolic computation, data manipulation, network ...
ha
KolmogorovSmirnovTest
* MATLAB
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
's Statistics Toolbox ha
kstest
an
for one-sample and two-sample Kolmogorov–Smirnov tests, respectively.
* 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 a]
ks.test
in its "stats" package.
* SAS implements the test in its PROC NPAR1WAY procedure.
* In Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
, the SciPy
SciPy (pronounced "sigh pie") is a free and open-source Python library used for scientific computing and technical computing.
SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, fast Fourier ...
package implements the test in the scipy.stats.kstest function.
* SYSTAT (SPSS Inc., Chicago, IL)
* Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
has an implementation of this test provided by Apache Commons The Apache Commons is a project of the Apache Software Foundation, formerly under the Jakarta Project. The purpose of the Commons is to provide reusable, Open-source software, open source Java (software platform), Java software. The Commons is compo ...
.
* KNIME
KNIME (), the Konstanz Information Miner, is a data analytics, reporting and integrating platform. KNIME integrates various components for machine learning and data mining through its modular data pipelining "Building Blocks of Analytics" con ...
has a node implementing this test based on the above Java implementation.
*Julia
Julia may refer to:
People
*Julia (given name), including a list of people with the name
*Julia (surname), including a list of people with the name
*Julia gens, a patrician family of Ancient Rome
*Julia (clairvoyant) (fl. 1689), lady's maid of Qu ...
has the packag
HypothesisTests.jl
with the function ExactOneSampleKSTest(x::AbstractVector, d::UnivariateDistribution).
* StatsDirect (StatsDirect Ltd, Manchester, UK) implement
all common variants
* Stata
Stata (, , alternatively , occasionally stylized as STATA) is a general-purpose Statistics, statistical software package developed by StataCorp for data manipulation, visualization, statistics, and automated reporting. It is used by researchers ...
(Stata Corporation, College Station, TX) implements the test in ksmirnov (Kolmogorov–Smirnov equality-of-distributions test) command.
* PSPP
PSPP is a free software application for analysis of sampled data, intended as a free alternative for IBM SPSS Statistics. It has a graphical user interface and conventional command-line interface. It is written in C and uses GNU Scientific L ...
implements the test in it
KOLMOGOROV-SMIRNOV
(or using KS shortcut function).
* The Real Statistics Resource Pack for Excel runs the test as KSCRIT and KSPROB.
See also
*Lepage test In statistics, the Lepage test is an exact distribution-free test (nonparametric test) for jointly monitoring the location (central tendency) and scale ( variability) in two-sample treatment versus control comparisons. It is a rank test for the two ...
*Cucconi test
In statistics, the Cucconi test is a nonparametric test for jointly comparing central tendency and variability (detecting location and scale changes) in two samples. Many rank tests have been proposed for the two-sample location-scale problem. Ne ...
*Kuiper's test
Kuiper's test is used in statistics to test whether a data sample comes from a given distribution (one-sample Kuiper test), or whether two data samples came from the same unknown distribution (two-sample Kuiper test). It is named after Dutch math ...
*Shapiro–Wilk test
The Shapiro–Wilk test is a Normality test, test of normality. It was published in 1965 by Samuel Sanford Shapiro and Martin Wilk.
Theory
The Shapiro–Wilk test tests the null hypothesis that a statistical sample, sample ''x''1, ..., ''x'n'' ...
*Anderson–Darling test
The Anderson–Darling test is a statistical test of whether a given sample of data is drawn from a given probability distribution. In its basic form, the test assumes that there are no parameters to be estimated in the distribution being tested, i ...
* Cramér–von Mises test
*Wasserstein metric
In mathematics, the Wasserstein distance or Kantorovich– Rubinstein metric is a distance function defined between probability distributions on a given metric space M. It is named after Leonid Vaseršteĭn.
Intuitively, if each distribution ...
References
Further reading
*
*
*
*
*
*
External links
*
Short introduction
JavaScript implementation of one- and two-sided tests
Online calculator with the KS test
* Open-source C++ code to compute th
and perform th
*Paper o
Evaluating Kolmogorov's Distribution
contains C implementation. This is the method used in Matlab
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
.
*Paper o
Computing the Two-Sided Kolmogorov–Smirnov Distribution
computing the cdf of the KS statistic in C or Java.
*Pape
powerlaw: A Python Package for Analysis of Heavy-Tailed 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 a
PyPi
{{DEFAULTSORT:Kolmogorov-Smirnov Test
Statistical distance
Nonparametric statistics
Normality tests