# Using Kolmogorov–Smirnov test

A newbie question (please provide detailed answers): I am trying to use the Kolmogorov–Smirnov test. I managed to calculate the difference between the empirical points and the theoretical distribution $D$ (following Wikipedia). But then I am a bit confused about the test:

1. Is the null hypothesis that the empirical data is or is not distributed according to the theoretical distribution?
2. How do I determine the critical level $\alpha$ when I have $D$? I can use this class to calculate the Kolmogorov Smirnov distribution.

I think I have all the ingredients, but I am not sure how to put them together.

## 2 Answers

1) The null hypothesis is that the data is distributed according to the theoretical distribution.

2) Let $$N$$ be your sample size, $$D$$ be the observed value of the Kolmogorov-Smirnov test statistic, and define $$\lambda = D(0.12 + \sqrt{N} + 0.11 / \sqrt{N})$$. Then the p-value for the test statistic is approximately:

$$Q = 2 \sum_{j=1}^{\infty}(-1)^{j-1}\exp\{-2j^2\lambda^2\}$$

Obviously you can't calculate the infinite sum, but if you sum over 100 values or so this will get you very, very, very close. This approximation is quite good even for small values of $$N$$, as low as 5 if I recall correctly, and gets better as $$N$$ increases. Note, however, that @whuber in comments proposes a better approach.

This is a perfectly reasonable alternative to the Shapiro-Wilk test I suggested in answer to your other question, by the way. Shapiro-Wilk is more powerful, but if your sample size is in the high hundreds, the Kolmogorov-Smirnov test will have quite a bit of power too.

• +1. But--over 100 values?! The terms will almost surely underflow well before then, at least in double precision. Because the series alternates, the error does not exceed the absolute value of the last omitted term. Thus, to compute within $\epsilon$, find the smallest $n$ for which $2\exp(-2n^2\lambda^2)\le\epsilon$; i.e., $n=\lceil\sqrt{\log(2/\epsilon)/(2\lambda^2)}\rceil$, and compute the sum for $j=n$ down to $j=1$ (to minimize rounding error). – whuber Jan 2 '12 at 16:38
• @whuber - You're quite right (of course) and I should have mentioned checking for underflow. Your solution is more elegant than mine would have been anyway! – jbowman Jan 2 '12 at 16:45
• +1 This gave me the information I was looking for after spending a few frustrating hours of searching the internet! – NK1406 Nov 30 '19 at 19:46
• @jbowman any chance that you can provide a reference for the derivation of this approximation? – Jsevillamol Dec 16 '19 at 11:35
1. No. Null hypothesis that the empirical data is distributed according to the theoretical distribution.

2. Not familiar with java function. But KS test critical values are available online. Also available in the appendix of statistics books which deals nonparametric tests. You can compare few values with the java function and the table. (Please let us know if it is different)

• What are the values for large n? In my case n is between about 300 and 800 I think. (the web page is not really readable for this case) – Grzenio Jan 2 '12 at 15:45
• It is provided in the webpage, but not clear. See the last row (Over 35 ) 1.07 ___ Ö N indicates $1.07/\sqrt(N)$. Similarly for other values. – vinux Jan 2 '12 at 16:17
• Early on in my exploration of distribution tests I was told that formal tests such as the KS are "notoriously unreliable." I have certainly found that to be the case, and experienced analysts tend to treat such tests warily as well. E.g., with N in the hundreds, KS results will point "over-eagerly" toward non-normality. Sometimes a distribution being tested for normality will be "normal enough" for one's purposes regardless of what the KS test shows. Typically one needs to use one's judgment in interpreting the KS in conjunction with plots such as the histogram, Q-Q, and/or P-P. – rolando2 Jan 2 '12 at 16:25
• Thanks! Just to double check my understanding, if I got D=0.0236 using 741 data points then I can't reject the hypothesis that my data has a given distribution even at 0.20? – Grzenio Jan 2 '12 at 16:26
• You are right. Fail to reject the null hypothesis – vinux Jan 2 '12 at 16:47