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.
- distfn
distributioninstance Discrete distribution function to be tested; needs
support,ppfandcdfmethods.- argsequence
parameters of distribution
- nsupp
int,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:
- freq
ndarray empirical frequencies for sample; not normalized, adds up to sample size if all observations are in the support
- expfreq
ndarray theoretical frequencies according to distribution
- histsupp
ndarray 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.
- freq
- Raises:
ValueErrorIf
nsuppis 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 thatfreqdoes not add up to the sample size in that case.If the
ppfmethod of the distribution raises aRuntimeError, the quantiles are found by a search using only thecdfmethod. 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 inhistsuppare 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