Beispiel #1
0
def MCMC(theta, x, y, yerr, P, name):

    # 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, P))
    bi, pr = 100, 300
    start = time.clock()
    print("Burn-in")
    p0, lp, state = sampler.run_mcmc(p0, bi)
    sampler.reset()
    print("Production run")
    sampler.run_mcmc(p0, pr)
    elapsed = time.clock() - start
    print 'time = ', elapsed/60., 'mins'

    print("Making triangle plots")
    fig_labels = ["$A$", "$l_2$", "$l_1$", "$s$"]
    fig = triangle.corner(sampler.flatchain, truths=theta, labels=fig_labels[:len(theta)])
    fig.savefig("%striangle.png"%name)

    print("Plotting traces")
    pl.figure()
    for i in range(ndim):
        pl.clf()
        pl.axhline(theta[i], color = "r", zorder=2)
        pl.plot(sampler.chain[:, :, i].T, 'k-', alpha=0.3, zorder=1)
        pl.savefig("{0}.png".format(i))

    # Flatten chain
    samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

    # Find values
    mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                      zip(*np.percentile(samples, [16, 50, 84], axis=0)))

    theta = np.array(mcmc_result)[:, 0]
    print 'mcmc result (exp) = ', np.exp(theta)
    print 'mcmc result (lin) = ', theta

    like = lnlike(theta, x, y, yerr, P)
    print "Final lnlike = ", like

    # plot mcmc result
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.')
    xs = np.arange(min(x), max(x), 0.01)
    pl.plot(xs, predict(xs, x, y, yerr, theta, P)[0], 'r-')
    pl.xlabel('time (days)')
    pl.savefig('%sresult'%name)

    savedata = np.empty(len(theta)+1)
    savedata[:len(theta)] = theta
    savedata[-1] = like
    np.savetxt('%sresult.txt'%name, savedata)

    return like
Beispiel #2
0
def lnprob(theta, x, y, yerr, bm, bp):
    lp = lnprior(theta, bm, bp)
    if not np.isfinite(lp):
        return -np.inf
    try:
        return lp + lnlike(theta, x, y, yerr)
    except:
        print theta
        raise
Beispiel #3
0
def MCMC(theta, x, y, yerr, bm, bp):

    # 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, bm, bp))
    bi, pr = 200, 2000
    start = time.clock()
    print("Burn-in")
    p0, lp, state = sampler.run_mcmc(p0, bi)
    sampler.reset()
    print("Production run")
    sampler.run_mcmc(p0, pr)
    elapsed = time.clock() - start
    print 'time = ', elapsed / 60., 'mins'

    print("Making triangle plots")
    fig_labels = ["$A$", "$l_2$", "$l_1$", "$s$", "$P$"]
    fig = triangle.corner(sampler.flatchain,
                          truths=theta,
                          labels=fig_labels[:len(theta)])
    fig.savefig("triangle.png")

    print("Plotting traces")
    pl.figure()
    for i in range(ndim):
        pl.clf()
        pl.axhline(theta[i], color="r", zorder=2)
        pl.plot(sampler.chain[:, :, i].T, 'k-', alpha=0.3, zorder=1)
        pl.savefig("{0}.png".format(i))

    # Flatten chain
    samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

    # Find values
    mcmc_result = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                      zip(*np.percentile(samples, [16, 50, 84], axis=0)))

    theta = np.array(mcmc_result)[:, 0]
    print 'mcmc result = ', theta

    like = lnlike(theta, x, y, yerr)
    print "Final lnlike = ", like

    # plot mcmc result
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.')
    xs = np.arange(min(x), max(x), 0.01)
    pl.plot(xs, predict(xs, x, y, yerr, theta, theta[4])[0], 'r-')
    pl.xlabel('time (days)')
    pl.savefig('result')
Beispiel #4
0
def MCMC(theta, x, y, yerr, bm, bp):

    # Compute initial likelihood
    print 'initial lnlike = ', lnlike(theta, x, y, yerr)

    # 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,
                                                                    bm, bp))
    bi, pr = 200, 2000
    print("Burn-in")
    p0, lp, state = sampler.run_mcmc(p0, bi)
    sampler.reset()

    nstep = 2000
    nruns = 100.

    print("Production run")
    for j in range(int(nstep/nruns)):

        print 'run', j
        p0, lp, state = sampler.run_mcmc(p0, nruns)

        print("Plotting traces")
        pl.figure()
        for i in range(ndim):
            pl.clf()
            pl.axhline(theta[i], color = "r")
            pl.plot(sampler.chain[:, :, i].T, 'k-', alpha=0.3)
            pl.savefig("%s.png" %i)

        flat = sampler.chain[:, 50:, :].reshape((-1, ndim))
        mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                          zip(*np.percentile(flat, [16, 50, 84], axis=0)))
        print mcmc_result
        mres = np.array(mcmc_result)[:, 0]
        print 'mcmc_result = ', mres

        print("Making triangle plot")
        fig_labels = ["$A$", "$l_2$", "$l_1$", "$s$", "$P$"]
        fig = triangle.corner(sampler.flatchain, truths=theta,
                              labels=fig_labels[:len(theta)])
        fig.savefig("triangle.png")

    # Flatten chain
    samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

    # Find values
    mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                      zip(*np.percentile(samples, [16, 50, 84], axis=0)))

    theta = np.array(mcmc_result)[:, 0]
    print 'mcmc result = ', theta

    like = lnlike(theta, x, y, yerr)
    print "Final lnlike = ", like

    # plot mcmc result
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.')
    xs = np.arange(min(x), max(x), 0.01)
    pl.plot(xs, predict(xs, x, y, yerr, theta, theta[4])[0], 'r-')
    pl.xlabel('time (days)')
    pl.savefig('result')
Beispiel #5
0
    y = np.random.multivariate_normal(np.zeros(len(x)), K)

    # plot data
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.', capsize=0, ecolor='0.5', zorder=2)
    xs = np.linspace(min(x), max(x), 1000)
    pl.plot(xs, predict(xs, x, y, yerr, theta, P)[0], color='#339999', linestyle = '-',\
            zorder=1, linewidth='2')
    pl.xlabel('$\mathrm{Time~(days)}$')
    pl.ylabel('$\mathrm{Normalised~Flux}$')
    pl.gca().yaxis.set_major_locator(MaxNLocator(prune='lower'))
    pl.savefig('data')

    print "Initial parameters = (exp)", theta
    start = time.clock()
    print "Initial lnlike = ", lnlike(theta, x, y, yerr, P), "\n"
    elapsed = (time.clock() - start)
    print 'time =', elapsed

    # Grid over periods
    Periods = np.arange(0.2, 5, 0.2)
    L = np.zeros_like(Periods)

    for i, P in enumerate(Periods):
        L[i] = MCMC(theta, x, y, yerr, P, i)
        pl.clf()
        pl.plot(Periods, L, 'k-')
        pl.xlabel('Period')
        pl.ylabel('Likelihood')
        pl.savefig('update')
Beispiel #6
0
# subsample and truncate
x_sub, y_sub, yerr_sub = subs(x, y, yerr, 1., 500, 100)

# A, l2, l1, s
P = np.log(.7)
theta = [-2., -2., -1.2, 1., P]

# plot data and prediction
pl.clf()
pl.errorbar(x_sub, y_sub, yerr=yerr_sub, fmt='k.', capsize=0)
xs = np.linspace(min(x_sub), max(x_sub), 100)
pl.plot(xs, predict(xs, x_sub, y_sub, yerr_sub, theta, P)[0], color='.7')
pl.savefig('single_binary_data')

# Compute initial likelihood
print 'initial lnlike = ', lnlike(theta, x, y, yerr)

# bm, bp = minimum and maximum periods
bm, bp = np.log(.2), np.log(2)

# 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, bm, bp))

# print("Burn-in")
# p0, lp, state = sampler.run_mcmc(p0, 200)
# sampler.reset()

nstep = 2000
nruns = 100.
Beispiel #7
0
    np.savetxt("mcmc_result%s.txt"%fname, mcmc_result)
    mres = np.array(mcmc_result)[:, 0]
    print 'mcmc_result = ', mres

    print("Making triangle plot")
    fig_labels = ["$A_1$", "$l2_1$", "$l1_1$", "$s$", "$P_1$", "$A_2$", "$l2_2$", \
            "$l1_2$", "$P_2$", "$a$"]
    fig = triangle.corner(sampler.flatchain, truths=theta, labels=fig_labels[:len(theta)])
    fig.savefig("triangle_%s.png" %fname)

# Flatten chain
samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

# Find values
mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                  zip(*np.percentile(samples, [16, 50, 84], axis=0)))

theta = np.array(mcmc_result)[:, 0]
print 'mcmc result = ', theta

like = lnlike(theta, x, y, yerr)
print "Final lnlike = ", like

# plot mcmc result
pl.clf()
pl.errorbar(x, y, yerr=yerr, fmt='k.')
xs = np.arange(min(x), max(x), 0.01)
pl.plot(xs, predict(xs, x, y, yerr, theta, theta[4])[0], 'r-')
pl.xlabel('time (days)')
pl.savefig('result%s'%fname)
Beispiel #8
0
    y = np.random.multivariate_normal(np.zeros(len(x)), K)

    # plot data
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.', capsize=0, ecolor='0.5', zorder=2)
    xs = np.linspace(min(x), max(x), 1000)
    pl.plot(xs, predict(xs, x, y, yerr, theta, P)[0], color='#339999', linestyle = '-',\
            zorder=1, linewidth='2')
    pl.xlabel('$\mathrm{Time~(days)}$')
    pl.ylabel('$\mathrm{Normalised~Flux}$')
    pl.gca().yaxis.set_major_locator(MaxNLocator(prune='lower'))
    pl.savefig('data')

    print "Initial parameters = (exp)", theta
    start = time.clock()
    print "Initial lnlike = ", lnlike(theta, x, y, yerr, P),"\n"
    elapsed = (time.clock() - start)
    print 'time =', elapsed

    # Grid over periods
    Periods = np.arange(0.2, 5, 0.2)
    L = np.zeros_like(Periods)

    for i, P in enumerate(Periods):
        L[i] = MCMC(theta, x, y, yerr, P, i)
        pl.clf()
        pl.plot(Periods, L, 'k-')
        pl.xlabel('Period')
        pl.ylabel('Likelihood')
        pl.savefig('update')