#NEWLINE# def gof_binning_discrete( rvs , distfn , arg , nsupp = 20 ) : #NEWLINE# #INDENT# 'get bins for chisquare type gof tests for a discrete distribution\n\n    Parameters\n    ----------\n    rvs : array\n        sample data\n    distname : string\n        name of distribution function\n    arg : sequence\n        parameters of distribution\n    nsupp : integer\n        number of bins. The algorithm tries to find bins with equal weights.\n        depending on the distribution, the actual number of bins can be smaller.\n\n    Returns\n    -------\n    freq : array\n        empirical frequencies for sample; not normalized, adds up to sample size\n    expfreq : array\n        theoretical frequencies according to distribution\n    histsupp : array\n        bin boundaries for histogram, (added 1e-8 for numerical robustness)\n\n    Notes\n    -----\n    The results can be used for a chisquare test ::\n\n        (chis,pval) = stats.chisquare(freq, expfreq)\n\n    originally written for scipy.stats test suite,\n    still needs to be checked for standalone usage, insufficient input checking\n    may not run yet (after copy/paste)\n\n    refactor: maybe a class, check returns, or separate binning from\n        test results\n    todo :\n      optimal number of bins ? (check easyfit),\n      recommendation in literature at least 5 expected observations in each bin\n\n    ' #NEWLINE# n = len ( rvs ) #NEWLINE# wsupp = ( 1.0 / nsupp ) #NEWLINE# distsupport = lrange ( max ( distfn . a , ( - 1000 ) ) , ( min ( distfn . b , 1000 ) + 1 ) ) #NEWLINE# last = 0 #NEWLINE# distsupp = [ max ( distfn . a , ( - 1000 ) ) ] #NEWLINE# distmass = [ ] #NEWLINE# for ii in distsupport : #NEWLINE# #INDENT# current = distfn . cdf ( ii , * arg ) #NEWLINE# if ( ( current - last ) >= ( wsupp - 1e-14 ) ) : #NEWLINE# #INDENT# distsupp . append ( ii ) #NEWLINE# distmass . append ( ( current - last ) ) #NEWLINE# last = current #NEWLINE# if ( current > ( 1 - wsupp ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( distsupp [ ( - 1 ) ] < distfn . b ) : #NEWLINE# #INDENT# distsupp . append ( distfn . b ) #NEWLINE# distmass . append ( ( 1 - last ) ) #NEWLINE# #UNINDENT# distsupp = np . array ( distsupp ) #NEWLINE# distmass = np . array ( distmass ) #NEWLINE# histsupp = ( distsupp + 1e-08 ) #NEWLINE# histsupp [ 0 ] = distfn . a #NEWLINE# if ( np . __version__ < '1.5' ) : #NEWLINE# #INDENT# ( freq , hsupp ) = np . histogram ( rvs , histsupp , new = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( freq , hsupp ) = np . histogram ( rvs , histsupp ) #NEWLINE# #UNINDENT# cdfs = distfn . cdf ( distsupp , * arg ) #NEWLINE# return ( np . array ( freq ) , ( n * distmass ) , histsupp )