Deriving CDF of Kolmogorov-Smirnov Test Statistic: Jan Vrbik
Deriving CDF of Kolmogorov-Smirnov Test Statistic: Jan Vrbik
Deriving CDF of Kolmogorov-Smirnov Test Statistic: Jan Vrbik
https://www.scirp.org/journal/am
ISSN Online: 2152-7393
ISSN Print: 2152-7385
Jan Vrbik
Department of Mathematics and Statistics, Brock University, St. Catharines, Ontario, Canada
1. Introduction
The article’s goal is to present a comprehensive summary of deriving the distri-
bution of the usual Kolmogorov-Smirnov test statistic, both in its exact and ap-
proximate form. We concentrate on practical aspects of this exercise, meaning
that
• reaching a modest (three significant digit) accuracy is usually considered
quite adequate,
• computing critical and P-values of the test is the primary objective, implying
that it is the upper tail of the distribution which is most important,
• methods capable of producing practically instantaneous results are preferable
to those taking several seconds, minutes, or more,
• simple, easy to understand (and to code) techniques have a great conceptual
advantage over complex, black-box type algorithms.
This is the reason why our review excludes some existing results (however
deep and mathematically interesting they may be); we concentrate only on the
most relevant techniques (this is also the reason why our bibliography is delibe-
rately far from complete).
Ui = F ( X i ) (2)
def
1.3. Discretization
In this article, we aim to find the CDF of Dn , namely
Pr ( Dn ≤ d ) (3)
1 2 n
only for a discrete set of n values of d, namely for d = , ,, , even though
n n n
1
Dn is a continuous random variable whose support is the ,1 interval.
2n
This proves to be sufficient for any (but extremely small) n, since our discrete
results can be easily extended to all values of d by a sensible interpolation.
There are technique capable of yielding exact results for any value of d (see [1]
or [2]), but they have some of the disadvantages mentioned above and will not
be discussed here in any detail; nevertheless, for completeness, we present a Ma-
thematica code of Durbin’s algorithm in the Appendix.
2. Linear-Algebra Solution
This, and the next two section, are all based mainly on [3], later summarized by
[4].
n ⋅ ( Fe ( d i ) − d i )
def
Ti = (4)
i
where d i = , i = 0,1,2,, n ; note that n ⋅ Fe ( d i ) equals the number of the
n
U i observations which are smaller than d i , also note that T0 and Tn are
always identically equal to 0. We can then show that
Claim 1. Dn > d j if and only if at least one of the Ti values is equal to j or
–j.
Proof. When Ti = j , then there is a value of d to the left of d i such that
j
Fe ( d ) − d > j , implying that Dn > ; similarly, when Ti = − j then there is a
n
value of d to the right of d i such that Fe ( d ) − d < − j , implying the same.
To prove the reverse, we must first realize that no one-step decrease in the
T0 , T1 ,, Tn sequence can be bigger than 1 (this happens when there are no ob-
servations between the corresponding d i and d i +1 ); this implies that the T se-
quence must always pass through all integers between the smallest and the larg-
est value ever reached by T.
Since n ⋅ Dn > j implies that either n ⋅ ( Fe ( d ) − d ) has exceeded the value
of j at some d, or it has reached a value smaller than −j, it then follows that at
least one Ti has to be equal to either j or −j respectively. ■
which yields a single value of the desired CDF (or rather, of its complement) of
Dn . To get the full (at least in the discretized sense) picture of the distribution,
the procedure now needs to be repeated for each possible value of J.
The whole algorithm can be summarized by the following Mathematica code
(note that instead of superscripts, interpreted by Mathematica as powers, we
have to use “overscripts”).
3. Generating-Function Solution
We now present an alternate way of building the same (discretized, but other-
wise exact) solution. We start by defining the following function of two integer
arguments
def ii + j
pij = (11)
( i + j )!
Note that, when i + j is negative (i is always positive), pij is equal to 0.
k
Claim 2. The binomial probability in can be expressed in terms of
n
three such p functions, as follows
m−k
k p ⋅p
k
in = i − k mn − i − m + k (12)
m pn − m
Proof.
k i (m − k )
n −i
⋅
i! ( n − i )!
n −i
n! k m − k
i
n k
=i = (13)
m i!( n − i )! m m mn
n!
■
Note that has the value of 0 whenever the number of successes (the sub-
script) is either negative or bigger than n (the superscript). Similarly, 00 is al-
ways equal to 1.
and
pk− J ⋅ pnJ − k k −1 pk−2−Ji ⋅ pnJ − k k
p0k − i ⋅ pnJ − k
= ∑ Pr ( A ) ⋅ + ∑ Pr ( B ) ⋅ (15)
pn−−J i pnJ − i
i i
= p0n i 1= i J
respectively.
Cancelling pn−−J k in each term of (14) and multiplying by p0n yields
k −1
k
pn pn
=pkJ ∑ pn0−i Pr ( A i ) ⋅ p0k −i + ∑ pn0−i Pr ( Bi ) ⋅ pk2 −J i (16)
=i 1 =
−J i J J
and
p0n
Pr ( Bi )
def
bi = (19)
pnJ − i
Note that n has disappeared from (17), making ai and bi potentially infi-
nite sequences (consider letting n have any positive value; in that sense ai is
well defined for any i from 1 to ∞ and bi for any i from J to ∞ ). Once we
solve for these two sequences, converting them back to Pr ( A i ) and Pr ( Bi )
for any specific value of n is a simple task; this approach thus effectively deals
with all n at the same time!
Similarly modifying (15) results in
k −1 k
pk− J = ∑ ai ⋅ pk−2−Ji + ∑ bi ⋅ p0k −i (20)
=i 1 =i J
(for any k > J ), utilizing the previous definition of ai and bi . The equations,
together with (17), constitute an infinite set of linear equations for elements of
the two sequences. To find the corresponding solution, we reach for a different
mathematical tool.
a (t ) ∑ ak ⋅ t k
def
G= (21)
k =1
b (t ) ∑ bk ⋅ t k
def
G=
k =1
∞
G j ( t ) =δ j ,0 + ∑ pkj ⋅ t k
def
k =1
combining two sequences in this manner is called their convolution. Note the
importance (for correctness of the Ga ⋅ G0 result) of including δ j ,0 in the de-
finition of G0 ( t ) .
Similarly, it follows from (20) that
G− J ( t ) = Ga ( t ) ⋅ G−2 J ( t ) + Gb ( t ) ⋅ G0 ( t ) (22)
namely
∑ +∑
n −1 n −i n −1
−J a ⋅p bi ⋅ pnJ − i
=i 1 =i
n
i 1
(23)
0 p
which follows from solving (18) and (19) for Pr ( A i ) and Pr ( Bi ) respectively.
The numerator of the last expression is clearly (by the same convolution argu-
ment) the coefficient of t n in the expansion of
Ga ( t ) ⋅ G− J ( t ) + Gb ( t ) ⋅ GJ ( t ) (24)
GD ( t ) =
def
(25)
(G ( t )
0
2
)
− G2 J ( t ) G−2 J ( t ) ⋅ p0n
which is obtained by substituting the solution to (Gj) and (22) into (24), and
further dividing by p0n ; Pr ( Dn > d J ) is then provided by the resulting coeffi-
cient of t n .
Note that, based on the same expansion, we can get Pr ( Dn > d J ) for any
smaller n as well, just by correspondingly replacing the value of p0n . Neverthe-
less, the process still needs to be repeated with all relevant values of J.
The corresponding Mathematica code looks as follows:
4. Asymptotic Solution
As we have seen, neither of the previous two solutions is very practical (and ul-
timately not even feasible) as the sample size increases. In that case, we have to
switch to using an approximate (also referred to as asymptotic) solution.
Large-n Formulas
First, we must replace the old definition of pij , namely (11), by
def i i + j ⋅ e−i
pij = (26)
( i + j )!
Note that this does not affect (12), nor any of the subsequent formulas up to and
including (25), since the various e − i factors always cancel out.
Also note that the definition can be easily extended to real (not just integer)
arguments by using Γ ( i + j + 1) in place of ( i + j )! , where Γ denotes the
usual gamma function.
1) Laplace representation
Note that, from now on, the summations defining the G functions in (21) stay
infinite (no longer truncated to the first n terms only).
Consider a (rather general) generating function
∞
(t ) ∑ pk ⋅ t k
def
=
G (27)
k =0
(
G e− s n ) becomes
∞
∑ p x ⋅n exp ( − s ⋅ x ) (29)
x =0
1
in steps of
n
1
Making the assumption that expanding p x ⋅n in powers of results in
n
q( x) 1 q( x)
=
p x ⋅n + O 3 2 (30)
n n n
(and our results do have this property), then (29) is approximately equal to
∞
1
⋅ ∑ q ( x ) exp ( − s ⋅ x ) + (31)
n x =0
1
in steps of
n
(n ⋅ x ) exp ( −n ⋅ x ) n
n⋅ x + j
pkj
n⋅ = (33)
n ( n ⋅ x + j )!
To be able to reach a finite answer, j itself needs to be replaced by z n ; note
that doing that with our J changes Pr ( Dn > d J ) to Pr n ⋅ Dn > z . ( )
It happens to be easier to take the limit of the natural logarithm of (33),
namely
( x ⋅ n + z n ) ln ( x ⋅ n ) − x ⋅ n + 12 ln n − ln ( x ⋅ n + z n )! (34)
instead.
With the help of the following version of Stirling’s formula (ignore its last
term for the time being)
1 1
ln ( m!) m ln m − m − ln m + ln 2 π + + (35)
2 12m
and of (we do not need the last two terms as yet)
( )
ln x ⋅ n + z n ln ( x ⋅ n ) +
x n
z
−
z2
+
z3
−
z4
2 x 2 n 3 x 3n 3 2 4 x 4 n 2
+ (36)
j
where z = ; this follows from (32) and the following result:
n
Claim 3.
v
exp − − x ⋅ s
π
Iv =
def ∞
∫0
x
x
dx =
s
(
⋅ exp −2 v ⋅ s ) (39)
s⋅ y
v
after the x = substitution. Solving the resulting simple differential equa-
s⋅ y
tion for I v yields
Iv = (
c ⋅ exp 2 v ⋅ s ) (42)
where c is equal to
∞ exp ( − x ⋅ s ) ∞ (
exp −u 2 ⋅ s ) π
=I0 ∫0 =
x
dx ∫0 u
=⋅ 2udu
s
(43)
(
GJ e − s n
=
)
G− J e − s n
→
( )
exp − z 2 s ( )
n →∞
n n 2s
(
G2 J e − s n
=
)
G−2 J e − s n
→
( )
exp −2 z 2 s ( )
n →∞
n n 2s
J
where z = (always positive).
n
3) Approximating GD
The corresponding Laplace representation of (25) further divided by n, let us
denote it L D / n ( s ) , is then equal to
E E2
2⋅ − 2⋅
2s 2s 2 s 2 s =2 ⋅ E ⋅ 2 π =2⋅
2π ∞
⋅ ∑ ( −1) E k
k −1
(45)
1− E 1
2
1 + E 2s 2 s k =1
⋅
2 s 2π
where=
def
( )
E exp −2 z 2 s . This is based on substituting the right-hand sides of
(44) into (25), and on the following result:
p0n n ne− n n 1
=
lim ⋅ n lim= (46)
n →∞ n n →∞ n! 2π
(Stirling’s formula again); the last limit also makes it clear why we had to divide
(25) by n: to ensure getting a finite result again.
We now need to find the q D / n ( x ) function corresponding to (45), i.e. the
q D / n (1)
latter’s ILT, and convert it to p n = according to (30); this yields an ap-
n
proximation for the coefficient of t n in the expansion of (25), still divided by n.
q (1)
(
The ultimate answer to Pr nDn > z is thus D / n
n
) ⋅n = q D / n (1) .
or, equivalently,
Pr ( )
nDn ≤ z 1 − 20 ( z ) (51)
where
∞
0 ( z ) =
∑ ( −1)
k −1
( )
def
exp −2 z 2 k 2 (52)
k =1
1
Note that the error of this approximation is of the O type, which
n
1
means that it decreases, roughly (since there are also terms proportional to ,
n
1 1
32
, etc.), with . Also note that the right hand side of (51) can be easily
n n
evaluated by calling a special function readily available (under various names) with
most symbolic programming languages, for example “JacobiTheta4(0, exp(−2∙z2))”
of Maple or “EllipticTheta[4, 0, Exp[−2z2]]” of Mathematica.
The last formula has several advantages over the approach of the previous two
sections: firstly, it is easy and practically instantaneous to evaluate (the infinite
series converges rather quickly only between 2 and 10 terms are required to
reach a sufficient accuracy when 0.3 < z the CDF is practically zero otherwise),
secondly, it is automatically a continuous function of z (no need to interpolate),
and finally, it provides an approximate distribution of nDn for all values of n
5. High-Accuracy Solution
Results of this section were obtained (in a slightly different form, and building
on previously published results) by [5] and further expounded by a more accessible
1
[6]; their method is based on expanding (in powers of ) the matrix-algebra
n
solution. Here we present an alternate approach, similarly expanding the gene-
rating-function solution instead; this appears an easier way of deriving the indi-
1 1
vidual and -proportional corrections to (50). We should mention that
n n
1
the cited articles include the 3 2 -proportional correction as well; it would not
n
be difficult to extend our results in the same manner, if deemed beneficial.
To improve accuracy of our previous asymptotic solution, (34) and, conse-
1 1
quently, (38) have to be extended by extra and -proportional terms
n n
(note that (35) and (36) were already presented in this extended form), getting
(
G j e− s n )
n
z2 z 3 − 3z ⋅ x z 6 − 12 z 4 x + 27 z 2 x 2 − 6 x 3
exp − − s ⋅ x ⋅ 1 + 2
+ 4
+
∫
∞ 2x 6x n 72 x n dx
0
2π ⋅ x
=
(
exp − 2 z 2 s ) ⋅ 1 + s ⋅ z 2s
+
z 2 s 2 − 3 2 z 2 s 3 + 3s
+
2s 3 n 18n
(53)
j def
where the sign corresponds to a positive (negative) z = , respectively.
n
The corresponding tedius algebra is usually delegated to a computer (it is no
longer feasible to show all the details here), the necessary integrals are found by
differentiating each side of the equation in (38) with respect to z2, from one up
to four times.
The last expression represents an excellent approximation to the G functions
of (44), with the exception of
∞
ks
( ) ∑p
def
G0 e − s n = k
0 ⋅ exp − (54)
k =0 n
(
G0 e − s n ) 1
+
1
+
2s
+ (55)
n 2 s 3 n 12n
Proof. The following elegant proof has been suggested by [7].
It is well known that the value of Lambert W ( z ) function is defined as a so-
lution to we w = z , and that its Taylor expansion is given by
( −k )
k −1
∞
∑ k!
zk (56)
k =1
implying that
∞
k k − k (1+ λ )
∑ k! e
k =0
d
1 + W −e − λ −1
=
dλ
( ) (57)
■
Differentiating
we w = −e − λ −1 (58)
dw
with respect to λ , cancelling e w , and solving for yields
dλ
dw w
= − (59)
dλ 1+ w
implying that
∞
kk − k (1+ λ ) 1 def 1
∑ k! e = =
1+ w u
(60)
k =0
rather than (58). Solving the last equation for λ and expanding the answer in
powers of u results in
u2 u3 u4
λ= + + + (62)
2 3 4
Inverting the last power series (which can be easily done to any number of terms)
yields the following expansion:
2λ ( 2λ )
32
u= 2λ − + + (63)
3n 36
1 s
Similarly expanding , replacing λ by and further dividing by n
u n
proves our claim.
Having achieved more accurate approximation for all our G functions, and
with the following extension of (46)
n ne− n n 1 1
⋅ 1 − + (64)
n! 2 π 12n
(68)
(the first row has already been proven; the remaining three follow by differen-
tiating both of its sides with respect to zk (taken as a single variable), up to three
times).
This results in the following replacement
∞
E
⋅ 2 s → ∑ ( −1) e −2 z k 4k 2 z 2 − 1
k −1
( )
2 2
2π ⋅
1+ E k =1
∞
E
(
⋅ 2 s → ∑ e −2 z k 4k 2 z 2 − 1 )
2 2
2π ⋅ (69)
1− E k =1
∞
−2 −2 z 2 k 2
2π ⋅
zE
⋅ 2s → z ∑ e (8k 3 z 3 − 6kz )
(1 + E ) k =1 k − 1
2
where all three series are still fast-converging. Note that the binomial coefficient
( −1)
k −1
of the last sum equals to k.
We can then present our final answer for Pr ( nDn > z ) in the manner of
the following Mathematica code; the resulting KS function can then compute
(practically instantaneously) this probability for any n and z.
can never exceed 0.20%; such an accuracy would be normally considered quite
adequate (approximating Student’s t30 by Normal distribution can yield an er-
ror almost as large as 1%).
As mentioned already, the approximation of Pr ( nDn > z ) can be made
even more accurate by adding, to the current expansion, the following extra
n − 3 2 -proportional correction
z ∞
+
27n 3 2 k =1
k −1
(
∑ ( −1) exp −2 z 2k 2 )
(70)
107 k 4 78
× k k 2 +
2
+ 3 ( −1) ⋅ 1 − k 2 z 2 − + 16k 4 z 4
5 3 5
Final Simplification
1
We have already seen that the -proportional error is removed by the fol-
n
lowing trivial modification of (50)
Pr ( )
nDn ≤ z =1 − 20 z +
1
6 n
(71)
Note that this amounts only to a slight shift of the whole curve to the left, but
1
leaves us with a full O -type error.
n
When willing to compromise, [8] has taken this one step further: it is possible
to show that, by extending the argument of 0 to
1 z −1
z+ + (72)
6 n 4n
1
yields results which are very close to achieving the full -proportional correction
n
of (65) as well; this is a fortuitous empirical results which can be easily verified
computationally (when n = 10 , the maximum error of the last approximation
increases to 0.27%, for n = 300 it goes up to 0.0096% still practically negligi-
ble).
( )
∞
1
nDn ≤ z 1 + 2 ∑ ( −1) exp −2 z + + (73)
k
k
4n
Pr
6 n
k =1
making it accurate enough to be used as a practical substitute for exact results
even with relatively small samples. Furthermore, the right hand side of this
formula can be easily evaluated by computer software (see the comment fol-
lowing (52)).
Conflicts of Interest
The author declares no conflicts of interest regarding the publication of this
paper.
References
[1] Durbin, J. (1973) Distribution Theory for Tests Based on the Sample Distribution
Function. Society for Industrial and Applied Mathematics, Philadelphia.
https://doi.org/10.1137/1.9781611970586
[2] Marsaglia, G., Tsang, W.W. and Wang, J. (2003) Evaluating Kolmogorov’s Distri-
bution. Journal of Statistical Software, 8, 1-4. https://doi.org/10.18637/jss.v008.i18
[3] Feller, W. (1948) On the Kolmogorov-Smirnov Limit Theorems for Empirical Dis-
tributions. Annals of Mathematical Statistics, 19, 177-189.
https://doi.org/10.1214/aoms/1177730243
[4] Kendall, M.G. and Stuart, A. (1973) The Advanced Theory of Statistics. Vol. 2,
Appendix
The following Mathematica function computes the exact Pr ( Dn ≤ d ) for any
value of d; using it to produce a full graph of the corresponding CDF will work
only for a sample size not much bigger than 700, since the algorithm’s computa-
tional time increases exponentially with not only n, but also with increasing val-
ues of d.