from scipy.stats import chisquare as _chisquare, kstest as _kstest
from numpy import inf, zeros_like, histogram, log2, ceil

def kolsmi(dist, fit_result, data):
    """
    Perform a Kolmogorow–Smirnow-Test for goodness of fit.

    This tests the H0 hypothesis, if data is a sample of dist

    Args:
        dist:         A mle.Distribution instance
        fit_result:   The solution dict, returned by the Distribution.fit method
        data:         The data used in Distribution.fit
    Returns:
        teststat:     the test statistic, e.g. the max distance between the
                      cumulated distributions
        p-value:      the p-value, probability that dist describes the data
    """
    teststat, pvalue = _kstest(data, lambda x: dist.cdf(x, **fit_result["x"]))
    return teststat, pvalue

def chisquare(dist, fit_result, data, bins=None, range=None):
    """
    Perform a Chi^2 test for goodness of fit.

    Tests the H0 hypothesis if the distances between fit result and
    data are compatible  with random fluctuations.

    Args:
        dist:         A mle.Distribution instance
        fit_result:   The solution dict, returned by the Distribution.fit method
        data:         The data used in Distribution.fit
        bins:         Number of bins for the histogram (default: 1+log2(N))
        range:        Range for the histogram (default: min(data), max(data))
    Returns:
        chisquare:    the test statistic, chi^2/ndf
        p-value:      the p-value, probability that differences between dist
                      and data are compatible with random fluctuation
    """

    if bins is None:
        bins = ceil(2*len(data)**(1.0/3.0))

    entries, edges = histogram(data, bins=bins, range=range)
    cdf = dist.cdf(edges, **fit_result["x"])
    exp_entries = len(data) * (cdf[1:] - cdf[:-1])

    chisq, pvalue = _chisquare(entries,
                               exp_entries,
                               ddof=len(fit_result["x"])
                               )
    chisq = chisq/(bins - len(fit_result["x"]) - 1)
    return chisq, pvalue

@@ -64,64 +64,27 @@
 est%0A
-from numpy import inf, zeros_like, histogram, log2, ceil
+import numpy as _np
 %0A%0Ade
@@ -1581,16 +1581,71 @@
    %22%22%22%0A%0A
+    # rule of thumb for number if bins if not provided%0A
     if b
@@ -1672,16 +1672,20 @@
  bins = 
+_np.
 ceil(2*l
@@ -1727,16 +1727,20 @@
 edges = 
+_np.
 histogra
@@ -1770,16 +1770,61 @@
 e=range)
+%0A%0A    # get expected frequencies from the cdf
 %0A    cdf
@@ -1879,16 +1879,26 @@
 tries = 
+_np.round(
 len(data
@@ -1921,16 +1921,105 @@
 df%5B:-1%5D)
+)%0A%0A    # use only bins where more then 4 entries are expected%0A    mask = exp_entries %3E= 5
 %0A%0A    ch
@@ -2046,24 +2046,30 @@
 uare(entries
+%5Bmask%5D
 ,%0A          
@@ -2100,16 +2100,22 @@
 _entries
+%5Bmask%5D
 ,%0A      
@@ -2217,20 +2217,29 @@
  chisq/(
-bins
+_np.sum(mask)
  - len(f

fix chisquare again, now working as expected