Get critical value of Y in a two sample Kolmogorov–Smirnov test in python

641 views Asked by At

In a two sample Kolmogorov–Smirnov test for (Y1,Y2), I want to know the critical value of Y which maximize the KS value(sometimes called D).

However, ks_2samponly return D and p-value.See scipy.stats.ks_2samp¶ .

Is there any package can do two sample Kolmogorov–Smirnov test and return a critical value of Y for me?

1

There are 1 answers

0
Garvey On

I read scipy.stats.ks_2samp source code, and change it into

def ks_2samp_val(data1, data2):
    data1 = np.sort(data1)
    data2 = np.sort(data2)
    n1 = data1.shape[0]
    n2 = data2.shape[0]
    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)
    return data_all[np.argmax(np.absolute(cdf1 - cdf2))]

which works for me