The guide
The Kolmogorov-Smirnov test reduces a sample to one gap, and at small n the exact p-value matters
One number, the biggest vertical gap between two cumulative curves, drives the test. How it becomes a p-value, and whether the reference was fixed in advance, decide if it can be trusted.
- 0.202523 D for the twelve readings, set by D- alone
- 0.638159 exact p-value at n = 12
- 0.708636 asymptotic p-value for the same D
- 28/2145 exact p-value for seven against eight, no ties
- 29/715 exact p-value for the tied example
Read the full guide Fold the guide
Take twelve numbers and ask whether they could have come from a standard normal curve. A histogram invites an argument about bin widths. The Kolmogorov-Smirnov test sidesteps bins altogether.
Sort the sample and walk along it. At each value, count the share of the sample at or below that point. That running share is the empirical distribution function, a staircase that climbs by one twelfth at every observation. Lay the reference cumulative curve over it. The test statistic, D, is the largest vertical distance between the two. The same idea compares two samples: build two staircases and take the largest gap between them. The test is named for Andrey Kolmogorov and Nikolai Smirnov, who developed it in the 1930s.
One number from a staircase
A staircase has two kinds of gap, and a careless calculation sees only one. Just after a step jumps, the staircase can sit above the curve. That is D+, the largest value of i/n minus F(x_i), where F is the reference function. Just before a step jumps, the staircase can sit below the curve. That is D-, the largest value of F(x_i) minus (i-1)/n. The handbook of the US National Institute of Standards and Technology writes the statistic as the larger of those two expressions, and SciPy reports the statistic as D+, D- or their maximum, depending on the alternative asked for.
Here is the worked example that the page loads on first use. The twelve readings are 0.12, -0.45, 0.88, 1.31, -1.02, 0.35, -0.27, 0.66, 1.74, -0.81, 0.09 and 0.52, and the reference is the standard normal. D+ is 0.040930. D- is 0.202523, reached just before the step at 0.09, where the normal curve has climbed to 0.5359 and the staircase has only reached four twelfths. So D is 0.202523, and D- alone sets it. Compute D from D+ only and you report 0.0409, about a fifth of the true gap, which makes a poor fit look like a good one.
The test on this page is two-sided: it looks for a gap in either direction. The one-sided variants use D+ or D- alone, and the page does not offer them.
From a gap to a p-value
A gap of 0.2025 means little until you know how large a gap chance produces at twelve observations. If the sample really came from the reference, the distribution of D does not depend on which continuous reference it is. That is the attraction of the test: one table serves every continuous curve. The distribution comes in two forms.
The old form is a limit. Multiply D by the square root of n and, as n grows, the result settles toward the Kolmogorov distribution, whose upper tail is twice the alternating sum of exp(-2 k squared x squared) over k from 1. For the twelve readings x is the square root of 12 times 0.202523, which is 0.7016, and the sum gives 0.708636. It is easy to program, and it is the formula that many quick scripts and printed tables rest on.
The other form is the exact distribution of D at the actual sample size. Computing it was long treated as too hard. Marsaglia, Tsang and Wang published a method in 2003 that raises a matrix to the power n, and Simard and L’Ecuyer later described an algorithm that uses different approximations in different regions of n and x. R’s ks.test documentation says its exact one-sample p-values follow the Marsaglia, Tsang and Wang method. The page runs that matrix method for n up to 100, rescaling the matrix so its powers never overflow, and it gets 0.638159. An independent 50-digit computation and SciPy’s kstwo agree with it to about one part in ten billion.
The two answers differ by 0.070477, about eleven percent of the exact value, and the formula is the larger. It makes the sample look more ordinary than it is. Wikipedia’s article on the test puts the largest error of the limit at about 7 percent at n = 10, 2.6 percent at n = 100 and 0.9 percent at n = 1000, which is why the page shows both values and the gap between them.
Critical values tell the same story. At level 0.05 the one-term constant is the square root of minus ln(0.025) over 2, which is 1.3581. The asymptotic cut-off for n = 12 is 1.3581 divided by the square root of 12, or 0.3921. The exact cut-off, found by bisection on the exact distribution, is 0.3754. The constant itself lies within 3 millionths of the Kolmogorov quantile, so the weakness is the small n, not the constant. Our D of 0.2025 is below both cut-offs, so neither version rejects the standard normal at this level. A sample whose D landed between 0.3754 and 0.3921 would slip past the formula and be caught by the exact value.
Fitting first breaks the test
The most common misuse is quiet. A reader computes the sample mean and standard deviation, plugs them into a normal curve and runs the test. The tables assume that the reference was fixed before the data were seen. The NIST handbook says that if location, scale or shape parameters are estimated from the data, the critical region of the test is no longer valid. Wikipedia adds that the Lilliefors test is the special case worked out for a normal reference.
The effect is large and runs in one direction. Fitting pulls the curve toward the staircase, so D shrinks. For the twelve readings, using their own mean of 0.26 and standard deviation of 0.8274 gives D = 0.0853, and the standard exact p-value would be 0.9999, a near-perfect fit by construction. A simulation shows the scale of the problem. In 40,000 seeded samples of twelve standard normal values, with the mean and spread refitted from each sample, the 95th percentile of D was 0.2417, far below the table’s 0.3754, and none of them reached 0.3754. With the reference fixed in advance, the same cut-off was passed by 4.8 percent of samples, close to the 5 percent the level promises.
That is why the page has a switch for parameters taken from the sample. With it on, the page still reports D, but it claims no p-value and no critical value. It also warns when typed parameters agree with the sample’s own estimates to three digits. It does not offer the replacement: the Lilliefors, Anderson-Darling and other tests built for estimated parameters are outside its scope.
Two samples, and what ties do
Two samples need no reference curve at all. D is the largest gap between the two staircases, and its asymptotic p-value uses x equal to D times the square root of n m over n + m. When all pooled values differ, the exact p-value can be counted. Walk a path from (0, 0) to (n, m), stepping right for each value of the first sample and up for each value of the second, in sorted order. Every path is one equally likely split of the pooled values, and the gap at a point on a path is the absolute value of i m minus j n, divided by n m.
Try 1.2, 0.8, 2.1, 1.7, 0.9, 1.5 and 2.4 against 0.4, 0.6, 1.1, 0.2, 0.7, 1.0, 0.3 and 0.5. D is 0.75. Of the 6,435 possible splits, 84 give a gap that large, so the exact p-value is 84/6435, which reduces to 28/2145, or 0.013054. The asymptotic formula says 0.029991, more than twice as large. At the 0.02 level the exact value rejects and the formula does not.
Ties change the count. Real measurements are rounded, so values repeat, and a repeated value moves both staircases in a single jump. Wikipedia notes that the permutation distribution of the statistic, given the pooled sample, yields an exact p-value that stays valid whether or not there are ties. R’s documentation says its exact two-sample method runs with or without ties. SciPy’s documentation says the samples are assumed to be drawn from a continuous distribution.
Take 1, 2, 2, 3, 3, 3, 4 against 2, 3, 4, 4, 5, 5, 6, 6. D is 0.607143. The page counts all 6,435 splits of the pooled values, grouped by tie, and 261 of them reach that gap, so p is 261/6435, or 29/715, or 0.040559. SciPy 1.17.1, run on the same two lists with its exact method, returns 0.087024, more than twice as large, and the Kolmogorov formula gives 0.127519. Three methods give three answers, and only the first counts the splits of the data actually supplied.
Exact counting has a cost, so the page states its caps. Path counts reach hundreds of digits quickly, far beyond what an ordinary number holds, so the page uses BigInt, which MDN describes as holding integer values too large for the number type. The counting runs in a web worker, a background thread, so the page stays responsive. Lattice paths are used for n times m up to 40,000 when no values tie, and a count of every split is used with ties when there are at most 200,000. Beyond those limits only the asymptotic value is shown, and it is labelled.
What the test cannot say
A large p-value is not evidence of fit. It says the gap is within what chance produces, and at twelve observations that is a wide band: even the exact cut-off is 0.3754, so the sample must sit more than a third away from the curve before the test objects. Wikipedia notes that the statistic needs a relatively large number of points to reject, and that for testing normality it is less powerful than the Shapiro-Wilk or Anderson-Darling tests.
The test also expects a continuous reference with fixed parameters, and this page covers only normal, uniform and exponential references. It does not handle discrete distributions, censored data or estimated parameters. For a one-sample list with repeated values from rounding, R’s documentation warns that even modest rounding can have a significant effect on the calculated statistic, so treat those p-values as approximate.
To reproduce the figures, load the twelve readings with a standard normal reference. You should see D+ = 0.040930, D- = 0.202523, an exact p-value of 0.638159, an asymptotic one of 0.708636, and critical values of 0.3754 and 0.3921 at level 0.05. Then switch on the option for parameters taken from the sample and watch the p-values disappear.
Every figure above was computed again outside the page, in Python with exact fractions, a brute-force listing of all splits, 50-digit matrix arithmetic and SciPy’s routines. The normal curve uses the error function, evaluated by the series and the continued fraction in the NIST Digital Library of Mathematical Functions and compared with Python’s own math.erfc.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Kolmogorov-Smirnov test Wikipedia
- 1.3.5.16. Kolmogorov-Smirnov Goodness-of-Fit Test NIST e-Handbook of Statistical Methods
- Evaluating Kolmogorov's Distribution Journal of Statistical Software
- Computing the Two-Sided Kolmogorov-Smirnov Distribution Journal of Statistical Software
- R: Kolmogorov-Smirnov Tests The R Project, ks.test documentation
- kstest SciPy documentation
- ks_2samp SciPy documentation
- scipy.stats.kstwobign SciPy documentation
- Series Expansions (DLMF 7.6) NIST Digital Library of Mathematical Functions
- Continued Fractions (DLMF 7.9) NIST Digital Library of Mathematical Functions
- BigInt MDN Web Docs
- Web Workers API MDN Web Docs
- fractions - Rational numbers Python Software Foundation
- math - Mathematical functions Python Software Foundation