def chi_square_fit(cdf, params, data, ndivs=20, minsamples=5, plot=False, start=-util.INF, end=util.INF): from rasmus import gnuplot 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 = gnuplot.plot(util.mget(x, ind), obs) p.plot(util.mget(x, ind), expected) return chi2, pval
def qqnorm(data, plot=None): """Quantile-quantile plot""" from rasmus import gnuplot data2 = sorted(data) norm = [random.normalvariate(0, 1) for x in range(len(data2))] norm.sort() if plot == None: return gnuplot.plot(data2, norm) else: plot.plot(data2, norm) return plot
def test_cdf_coal_bounded(self): n = 1000 k = 4 t = 500 step = .1 x = list(frange(0, 500, step)) y = [coal.prob_bounded_coal(i, k, n, t) * step for i in x] y2 = cumsum(y) y3 = [coal.cdf_bounded_coal(i, k, n, t) for i in x] p = plot(x, y2, style="lines") p.plot(x, y3, style="lines") p.plot([0, 500], [1, 1], style="lines") fequals(y2, y3, eabs=.01)
def chi_square_fit(cdf, params, data, ndivs=20, minsamples=5, plot=False, start=-util.INF, end=util.INF): from rasmus import gnuplot 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 = gnuplot.plot(util.mget(x, ind), obs) p.plot(util.mget(x, ind), expected) return chi2, pval
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)) gnuplot.plot(x, y) #============================================================================= # OLD CODE ''' def smooth_old(x, radius): """ return an averaging of vals using a radius Note: not implemented as fast as possible runtime: O(len(vals) * radius) """
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)) gnuplot.plot(x, y) #============================================================================= # OLD CODE ''' def smooth_old(x, radius): """ return an averaging of vals using a radius Note: not implemented as fast as possible runtime: O(len(vals) * radius) """ vlen = len(x) # simple case