title: Kolmogorov Smirnov test for the fitting goodness in python
body: <p>i am trying to fit distributions. The fitting is finished, but i need a measurement, to choose the best model. Many papers are using the Kolomogorov-Smirnov (KS) test. I tried to implement that, and i am getting very low p-value results.</p>
  <h1>The implementation:</h1>
  <pre><code>#Histigram plot
  
  binwidth = np.arange(0,int(out_threshold1),1)    
  n1, bins1, patches = plt.hist(h1, bins=binwidth, normed=1, facecolor='#023d6b', alpha=0.5, histtype='bar')
  
  #Fitting
  
  gevfit4 = gev.fit(h1)  
  pdf_gev4 = gev.pdf(lnspc, *gevfit4)   
  plt.plot(lnspc, pdf_gev4, label=&quot;GEV&quot;)
  
  logfit4 = stats.lognorm.fit(h)  
  pdf_lognorm4 = stats.lognorm.pdf(lnspc, *logfit4)  
  plt.plot(lnspc, pdf_lognorm4, label=&quot;LogNormal&quot;)
  
  weibfit4 = stats.weibull_min.fit(h1)  
  pdf_weib4 = stats.weibull_min.pdf(lnspc, *weibfit4)  
  plt.plot(lnspc, pdf_weib4, label=&quot;Weibull&quot;)
  
  burr12fit4 = stats.burr12.fit(h1)  
  pdf_burr124 = stats.burr12.pdf(lnspc, *burr12fit4)  
  plt.plot(lnspc, pdf_burr124, label=&quot;Burr&quot;)
  
  genparetofit4 = stats.genpareto.fit(h1)
  pdf_genpareto4 = stats.genpareto.pdf(lnspc, *genparetofit4)
  plt.plot(lnspc, pdf_genpareto4, label =&quot;Gen-Pareto&quot;)
  
  #KS-Test
  print(stats.kstest(h1, lambda k : stats.genpareto.cdf(k, *genparetofit),args=(),N=len(h1),alternative ='two-sided', mode ='approx'))
  print(stats.kstest(h1, lambda k : stats.lognorm.cdf(k, *logfit),args=(),N=len(h1),alternative ='two-sided', mode ='approx'))
  print(stats.kstest(h1, lambda k : gev.cdf(k, *gevfit),args=(),N=len(h1),alternative ='two-sided', mode ='approx'))
  print(stats.kstest(h1, lambda k : stats.weibull_min.cdf(k, *weibfit),args=(),N=len(h1),alternative ='two-sided', mode ='approx'))
  print(stats.kstest(h1, lambda k : stats.burr12.cdf(k, *burr12fit),args=(),N=len(h1),alternative ='two-sided', mode ='approx'))
  </code></pre>
  <p>After this runs, I get values like:</p>
  <pre><code>KstestResult(statistic=0.065689774346523788, pvalue=2.3778862070128568e-20)
  KstestResult(statistic=0.063434691987405312, pvalue=5.2567851875784095e-19)
  KstestResult(statistic=0.065047355887551062, pvalue=5.8076254324909468e-20)
  KstestResult(statistic=0.25249534411299968, pvalue=8.3670183092211739e-295)
  KstestResult(statistic=0.068528435880779559, pvalue=4.1395594967775799e-22)
  </code></pre>
  <p>Are these values reasonable? Is it still possible to chose the best model? Is the best fitted model, the model with the smallest statistic value?</p>
  <p><a href="https://i.stack.imgur.com/LDGBe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LDGBe.png" alt="Image of the fitted plots" /></a></p>
  <h1>EDIT:</h1>
  <p>I plotted the CDFs for two fitted distribution.
  <a href="https://i.stack.imgur.com/UtRN8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UtRN8.png" alt="CDF" /></a></p>
  <p>They seem very well fitted. But I still get those small p-values.</p>