# Kolmogorov-Smirnov test strange output

I am trying to fit my data to the one of the continuous PDF (I suggest it to be gamma- or lognormal-distributed). The data consists of about 6000 positive floats. But the results of the Kolmogorov-Smirnov test completely refute my expectations providing the very low p-values.

Data empirical distribution

Distribution fitting

Python code:

import numpy
import sys
import json
import matplotlib.pyplot as plt
import scipy
from scipy.stats import *

dist_names = ['gamma', 'lognorm']
limit = 30

def distro():
#input file
with open(sys.argv[1]) as f:

#output
results = {}
size = y.__len__()
x = scipy.arange(size)
h = plt.hist(y, bins=limit, color='w')
for dist_name in dist_names:
dist = getattr(scipy.stats, dist_name)
param = dist.fit(y)
goodness_of_fit = kstest(y, dist_name, param)
results[dist_name] = goodness_of_fit
pdf_fitted = dist.pdf(x, *param) * size
plt.plot(pdf_fitted, label=dist_name)
plt.xlim(0, limit-1)
plt.legend(loc='upper right')
for k, v in results.iteritems():
print(k, v)
plt.show()


This is the output:

• p-value is almost 0 'lognorm', (0.1111486360863001, 1.1233698406822002e-66)
• p-value is 0 'gamma', (0.30531260123096859, 0.0)

Does it mean that my data does not fit gamma distribution?.. But they seem so similar...

• With so many data points, the standard error of the KS statistic is very small, and so the fact that it's visually a reasonable fit is irrelevant - the test can still tell it doesn't fit. But note that you're misapplying the Kolmogorov Smirnov test, since it's a test for a completely specified distribution and you're estimating parameters from the data. In any case it's not clear to me why you'd do a hypothesis test here. Do you really believe the true population distribution is exactly gamma or lognormal? Why? What would convince you of that rather than something else that looks like that?... – Glen_b Nov 3 '13 at 2:44
• (ctd)... and if you think it's only an approximation, why wouldn't you anticipate rejection in a large sample? If you're interested in 'is this a good approximation?' try looking at QQ plots, which will tell you where the devaitions occur and that may help you decide if it's 'near enough' for whatever purpose you'd want to specify an approximate distributional form for. – Glen_b Nov 3 '13 at 2:47
• Thank you for responce. The aim of my work is the comparing of the several empirical distributions (kind of the one being discussed here). I wanted to use parametric methods in order to estimate the significance of the distribution parameters difference. But they seem to be restricted because of such a good results of fitting – Vitaly Isaev Nov 3 '13 at 20:26
• Why use such simple parametric models when you have so much data? – Glen_b Nov 3 '13 at 21:56
• Just a lack of knowledge... :) I would be appreciated if you give me advice about the modern methods of empirical distributions comparison. – Vitaly Isaev Nov 3 '13 at 22:50