def chiSquareFit(xbins, ybins, func, nsamples, nparams, minsamples=5): sizes = [xbins[i + 1] - xbins[i] for i in xrange(len(xbins) - 1)] sizes.append(sizes[-1]) # NOTE: assumes bins are of equal size # only focus on bins that are large enough counts = [ybins[i] * sizes[i] * nsamples for i in xrange(len(xbins) - 1)] expected = [] for i in xrange(len(xbins) - 1): expected.append( (func(xbins[i]) + func(xbins[i + 1])) / 2.0 * sizes[i] * nsamples) # ensure we have enough expected samples in each bin ind = util.find(util.gefunc(minsamples), expected) counts = util.mget(counts, ind) expected = util.mget(expected, ind) if len(counts) == 0: return [0, 1], counts, expected else: return chiSquare([counts], [expected], nparams), counts, expected
def chiSquareFit(xbins, ybins, func, nsamples, nparams, minsamples=5): sizes = [xbins[i+1] - xbins[i] for i in xrange(len(xbins)-1)] sizes.append(sizes[-1]) # NOTE: assumes bins are of equal size # only focus on bins that are large enough counts = [ybins[i] * sizes[i] * nsamples for i in xrange(len(xbins)-1)] expected = [] for i in xrange(len(xbins)-1): expected.append((func(xbins[i]) + func(xbins[i+1]))/2.0 * sizes[i] * nsamples) # ensure we have enough expected samples in each bin ind = util.find(util.gefunc(minsamples), expected) counts = util.mget(counts, ind) expected = util.mget(expected, ind) if len(counts) == 0: return [0, 1], counts, expected else: return chiSquare([counts], [expected], nparams), counts, expected