statsmodels.stats.gof.gof_binning_discrete#

statsmodels.stats.gof.gof_binning_discrete(rvs, distfn, arg, nsupp=20)[source]#

Get bins for chisquare type gof tests for a discrete distribution

Parameters:
rvsarray_like

One-dimensional sample data.

distfndistribution instance

Discrete distribution function to be tested; needs support, ppf and cdf methods.

argsequence

parameters of distribution

nsuppint, optional

number of bins, at least 1. The default is 20. The algorithm tries to find bins with equal weights. depending on the distribution, the actual number of bins can be smaller.

Returns:
freqndarray

empirical frequencies for sample; not normalized, adds up to sample size if all observations are in the support

expfreqndarray

theoretical frequencies according to distribution

histsuppndarray

bin boundaries for histogram. The first is the lower end of the support. The others are the upper bin edges moved to the next representable float, so that the bins are right-half-open.

Raises:
ValueError

If nsupp is not an integer greater than or equal to 1.

Notes

The results can be used for a chisquare test

(chis,pval) = stats.chisquare(freq, expfreq)

The bins are quantile based and the expected count of each bin is at least len(rvs) / nsupp, except for the last (tail) bin, which can have a very small expected count. Observations outside the support of the distribution are dropped, so that freq does not add up to the sample size in that case.

If the ppf method of the distribution raises a RuntimeError, the quantiles are found by a search using only the cdf method. Both methods assume that the support is integer valued and that loc is an integer.

Changed in version 0.15.1: The support of the distribution is taken from distfn.support(*arg) instead of a fixed window of integers from -1000 to 1000. The bins now follow loc shifts and large means. The upper boundaries in histsupp are the next representable float above the bin edges instead of the edges plus 1e-8.

originally written for scipy.stats test suite, still needs to be checked for standalone usage, insufficient input checking may not run yet (after copy/paste)

refactor: maybe a class, check returns, or separate binning from

test results

todo :

optimal number of bins ? (check easyfit), recommendation in literature at least 5 expected observations in each bin