I'm trying to compare two lists of floating point data and see if they might have come from different underlying distributions. The two samples are a before and after, so by comparing the two I thought I could detect whether any change occurred between the two timeframes.
To do this I'm using the two-sample Kolmogorov-Smirnov test. I have the following function which calculates the core statistic used in the test:
def kolmogorov_smirnov(data1, data2):
"""
Given two lists of data, finds the two-sample Kolmogorov–Smirnov statistic
"""
data1 = sorted(data1)
data2 = sorted(data2)
index1 = 0
index2 = 0
ks_stat = 0
while index1 < len(data1) and index2 < len(data2):
if data1[index1] == data2[index2]:
index1 += 1
index2 += 1
elif data1[index1] < data2[index2]:
index1 += 1
elif data1[index1] > data2[index2]:
index2 += 1
ks_stat = max(ks_stat, abs(index1/len(data1) - index2/len(data2)))
return ks_stat
I realise that I can also shorten the while
loop like so:
while index1 < len(data1) and index2 < len(data2):
if data1[index1] <= data2[index2]:
index1 += 1
if data1[index1] >= data2[index2]:
index2 += 1
ks_stat = max(ks_stat, abs(index1/len(data1) - index2/len(data2)))
Which version should I use? Also, is there anything worth pointing out about the main code?