# constants in scipy implementation of kolmogorov-smirnov two sample test

I am hoping some more enlightened statistician can help me to understand two constants used in the source of the scipy implementation of k-s 2 sample test.

I already sent an e-mail to the dev group, but I'm not sure I'll get a reply.

A pointer to the source is here: https://github.com/scipy/scipy/blob/master/scipy/stats/stats.py#L4756

Why it's strange is that it's a call to a function returning the p-value, where two constants (0.12, 0.11) are used for no apparent reason.

Here's the entirety of the function:

    data1 = np.sort(data1)
data2 = np.sort(data2)
n1 = data1.shape
n2 = data2.shape
data_all = np.concatenate([data1, data2])
cdf1 = np.searchsorted(data1, data_all, side='right') / (1.0*n1)
cdf2 = np.searchsorted(data2, data_all, side='right') / (1.0*n2)
d = np.max(np.absolute(cdf1 - cdf2))
# Note: d absolute not signed distance
en = np.sqrt(n1 * n2 / float(n1 + n2))
try:
prob = distributions.kstwobign.sf((en + 0.12 + 0.11 / en) * d)
except:
prob = 1.0

return Ks_2sampResult(d, prob)


This is how the KS test statistic is computed for "Case 0" in EDF Statistics for Goodness of Fit and Some Comparisons (1974).

Quote from the paper:

Thus we have a Case 0 situation. The modified D then becomes D* = ... + 0.12 + 0.11 / ...

http://www.statsref.com/HTML/index.html?komogorov-smirnov.html also has some details.

Basically, the author argues that in the classical framework, the constants are better than square root of sample size.

I don't know enough whether the constants are really better than square root of sample size. Please take a look at the paper to decide yourself.

• Thank you. I can't find the argument that the constants are better. Actually I see the author argue that the method has not been so widely used due to the computational complexity compared to the chi-squared test. Also, given the year of the publication, I'd incline towards thinking that the reason was similar like the reason for most tables of constants in statistics - reducing computational complexity. – nln Feb 28 '17 at 5:51
• @nln You might be right. Back then, computing a sqrt root was an expensive operation. – SmallChess Feb 28 '17 at 5:54
• On that note, I can't find evidence to say that the test loses strength/precision because of this, so I guess this is one more "42" type of constants I'll have to live with. Marking as answered, thank you again. – nln Feb 28 '17 at 5:58