def chi_square_fit(cdf, params, data, ndivs=20, minsamples=5, plot=False, start=-util.INF, end=util.INF): import scipy import scipy.stats # determine ndiv and binsize binsize = len(data) / ndivs if binsize < minsamples: ndivs = len(data) / minsamples binsize = len(data) / ndivs data = sorted(data) bins = [data[i:i+binsize] for i in xrange(0, len(data), binsize)] obs = scipy.array(map(len, bins)) ind = util.find(lambda x: x[-1] >= start and x[0] <= end, bins) obs = util.mget(obs, ind) x = [bin[0] for bin in bins] expected = [len(data) * cdf(x[1], params)] expected.extend([len(data) * (cdf(x[i+1], params) - cdf(x[i], params)) for i in range(1, len(x)-1)]) expected.append(len(data) * (1.0 - cdf(x[-1], params))) expected = scipy.array(util.mget(expected, ind)) chi2, pval = scipy.stats.chisquare(obs, expected) if plot: p = util.plot(util.mget(x, ind), obs) p.plot(util.mget(x, ind), expected) return chi2, pval
def qqnorm(data, plot=None): """Quantile-quantile plot""" data2 = util.sort(data) norm = [random.normalvariate(0, 1) for x in range(len(data2))] norm.sort() if plot == None: return util.plot(data2, norm) else: plot.plot(data2, norm) return plot
def chi_square_fit(cdf, params, data, ndivs=20, minsamples=5, plot=False, start=-util.INF, end=util.INF): import scipy import scipy.stats # determine ndiv and binsize binsize = len(data) / ndivs if binsize < minsamples: ndivs = len(data) / minsamples binsize = len(data) / ndivs data = sorted(data) bins = [data[i:i + binsize] for i in xrange(0, len(data), binsize)] obs = scipy.array(map(len, bins)) ind = util.find(lambda x: x[-1] >= start and x[0] <= end, bins) obs = util.mget(obs, ind) x = [bin[0] for bin in bins] expected = [len(data) * cdf(x[1], params)] expected.extend([ len(data) * (cdf(x[i + 1], params) - cdf(x[i], params)) for i in range(1, len(x) - 1) ]) expected.append(len(data) * (1.0 - cdf(x[-1], params))) expected = scipy.array(util.mget(expected, ind)) chi2, pval = scipy.stats.chisquare(obs, expected) if plot: p = util.plot(util.mget(x, ind), obs) p.plot(util.mget(x, ind), expected) return chi2, pval
if __name__ == "__main__": # iter_window from rasmus import util vals = sorted([random.random() * 20 for x in range(600)]) vals += sorted([40 + random.random() * 20 for x in range(600)]) ''' win = filter(lambda x: len(x) > 0, list(iter_window_index(vals, 5))) p = util.plot(util.cget(win, 2))#, style="lines") p.enableOutput(False) p.plot(util.cget(win, 3)) #, style="lines") for i, y in enumerate(vals): p.plot([i, len(vals)], [y, y], style="lines") p.enableOutput(True) p.replot() ''' def mean2(v): if len(v) == 0: return 0.0 else: return mean(v) x, y = zip(*iter_window_step(vals, 5, 1, len)) util.plot(x, y)
# iter_window from rasmus import util vals = sorted([random.random() * 20 for x in range(600)]) vals += sorted([40 + random.random() * 20 for x in range(600)]) ''' win = filter(lambda x: len(x) > 0, list(iter_window_index(vals, 5))) p = util.plot(util.cget(win, 2))#, style="lines") p.enableOutput(False) p.plot(util.cget(win, 3)) #, style="lines") for i, y in enumerate(vals): p.plot([i, len(vals)], [y, y], style="lines") p.enableOutput(True) p.replot() ''' def mean2(v): if len(v) == 0: return 0.0 else: return mean(v) x, y = zip(* iter_window_step(vals, 5, 1, len)) util.plot(x, y)