def lnprob(theta, x, y, yerr): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf try: return lp + lnlike(theta, x, y, yerr) except: print theta raise
# theta = [0., 0, .5, .5, 0.] # inital try theta = [0., .2, .5, .5, 6.2] # better parameterisation theta = ml_theta # plot data pl.clf() pl.errorbar(x, y, yerr=yerr, fmt='k.') xs = np.linspace(min(x), max(x), 500) pl.plot(xs, predict(xs, x, y, yerr, theta)[0], 'r-') pl.xlabel('time (days)') pl.savefig('data') raw_input('enter') print "Initial parameters = (exp)", theta start = time.clock() print "Initial lnlike = ", lnlike(theta, x, y, yerr),"\n" elapsed = (time.clock() - start) print 'time =', elapsed # Sample the posterior probability for m. nwalkers, ndim = 64, len(theta) p0 = [theta+1e-4*np.random.rand(ndim) for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args = (x, y, yerr)) bi, pr = 500, 1000 # print 'predicted time = ', (elapsed*bi+elapsed*pr) start = time.clock() print("Burn-in") p0, lp, state = sampler.run_mcmc(p0, bi) sampler.reset() print("Production run") sampler.run_mcmc(p0, pr)
q = tbdata["SAP_QUALITY"] # remove nans n = np.isfinite(x)*np.isfinite(y)*np.isfinite(yerr)*(q==0) l = 500. x = x[n][:l] y = y[n][:l] yerr = yerr[n][:l] mu = np.median(y) y = y/mu - 1. yerr /= mu pl.clf() pl.errorbar(x, y, yerr=yerr, fmt='k.') pl.xlabel('time (days)') pl.savefig('data') theta = [1e-8, 1., 10., 2.] P = np.arange(0.1, 5, 0.1) L = np.empty_like(P) for i, per in enumerate(P): theta[1] = per L[i] = lnlike(theta, x, y, yerr) pl.clf() pl.plot(P, L, 'k-') pl.xlabel('Period (days)') pl.ylabel('likelihood') pl.savefig('likelihood2')
# theta = [0., 0, .5, .5, 0.] # inital try theta = [0., .2, .5, .5, 6.2] # better parameterisation theta = ml_theta # plot data pl.clf() pl.errorbar(x, y, yerr=yerr, fmt='k.') xs = np.linspace(min(x), max(x), 500) pl.plot(xs, predict(xs, x, y, yerr, theta)[0], 'r-') pl.xlabel('time (days)') pl.savefig('data') raw_input('enter') print "Initial parameters = (exp)", theta start = time.clock() print "Initial lnlike = ", lnlike(theta, x, y, yerr), "\n" elapsed = (time.clock() - start) print 'time =', elapsed # Sample the posterior probability for m. nwalkers, ndim = 64, len(theta) p0 = [theta + 1e-4 * np.random.rand(ndim) for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr)) bi, pr = 500, 1000 # print 'predicted time = ', (elapsed*bi+elapsed*pr) start = time.clock() print("Burn-in") p0, lp, state = sampler.run_mcmc(p0, bi) sampler.reset() print("Production run") sampler.run_mcmc(p0, pr)