Esempio n. 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
Esempio n. 2
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')
Esempio n. 3
0
def global_max(x, y, yerr, theta, Periods, P, r, s, b, save):

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

    L = np.zeros_like(Periods)
    results = np.zeros((len(L), 4))

    for i, p in enumerate(Periods):
        L[i], results[i, :] = maxlike(theta, x, y, yerr, p, i)

    iL = L == max(L)
    mlp = Periods[iL]
    mlresult = results[iL]
    print 'max likelihood period = ', mlp

    print 'mlresult = ', mlresult[0]
    # 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, mlresult[0], mlp)[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('%sml_data%s' % (int(KID), save))

    # plot GPeriodogram
    pl.clf()
    area = sp.integrate.trapz(np.exp(L))
    pl.plot(Periods, (np.exp(L)) / area, 'k-')
    pl.xlabel('Period')
    pl.ylabel('Likelihood')
    pl.title('Period = %s' % mlp)
    pl.savefig('%sml_likelihood%s' % (int(KID), save))

    # set period prior boundaries
    bm = mlp - b * mlp
    bp = mlp + b * mlp

    return L, mlp, bm, bp, mlresult[0]
Esempio n. 4
0
def global_max(x, y, yerr, theta, Periods, P, r, s, b, save):

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

        L = np.zeros_like(Periods)
        results = np.zeros((len(L), 4))

        for i, p in enumerate(Periods):
            L[i], results[i,:] = maxlike(theta, x, y, yerr, p, i)

        iL = L==max(L)
        mlp = Periods[iL]
        mlresult = results[iL]
        print 'max likelihood period = ', mlp

        print 'mlresult = ', mlresult[0]
        # 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, mlresult[0], mlp)[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('%sml_data%s' %(int(KID), save))

        # plot GPeriodogram
        pl.clf()
        area = sp.integrate.trapz(np.exp(L))
        pl.plot(Periods, (np.exp(L))/area, 'k-')
        pl.xlabel('Period')
        pl.ylabel('Likelihood')
        pl.title('Period = %s' %mlp)
        pl.savefig('%sml_likelihood%s' %(int(KID), save))

        # set period prior boundaries
        bm = mlp - b*mlp
        bp = mlp + b*mlp

        return L, mlp, bm, bp, mlresult[0]
Esempio n. 5
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')
Esempio n. 6
0
    m = [m0, m1, m2, m3, mlp]
    print 'm = ', m
    print 'r0, r1 = ', r0, r1

    # load lightcurve
    strKIDs = []
    [strKIDs.append(str(KID).zfill(4)) for i in range(nstars)]
    data = np.genfromtxt("/Users/angusr/angusr/Suz_simulations/final/lightcurve_%s.txt" \
            %strKIDs[KID]).T
    x = data[0]
    y = data[1]
    yerr = y*2e-5 # one part per million #FIXME: this is made up!

    # normalise so range is 2 - no idea if this is the right thing to do...
    yerr = 2*yerr/(max(y)-min(y))
    y = 2*y/(max(y)-min(y))
    y = y-np.median(y)

    print 'subsample and truncate'
    x_sub, y_sub, yerr_sub = subs(x, y, yerr, mlp, 500.)
    pl.clf()
    pl.plot(x_sub, y_sub, 'k.')
    xs = np.linspace(min(x_sub), max(x_sub), 100)
    pl.plot(xs, predict(xs, x_sub, y_sub, yerr_sub, m, m[4])[0], 'r-')
    pl.savefig('test')

    # better initialisation
    m = [-5, m[1], 1.5, 9., np.log(m[-1])]

    MCMC(m, x_sub, y_sub, yerr_sub, np.log(r0), np.log(r1), "4")
Esempio n. 7
0
    y = 2 * y / (max(y) - min(y))
    y = y - np.median(y)

    #     theta, P = [0., .2, .2, 1.], 1.7 # initial
    #     theta, P = [-2., -2., -1.2, 6.], 1.7 # better initialisation
    theta, P = [-2., -2., -1.2, 1.], 1.7  # generating fake data

    # generate fake data
    K = QP(theta, x, yerr, P)
    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)
Esempio n. 8
0
yerr = 2*yerr/(max(y)-min(y))
y = 2*y/(max(y)-min(y))
y = y-np.median(y)

# 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)
Esempio n. 9
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)
Esempio n. 10
0
    y = 2*y/(max(y)-min(y))
    y = y-np.median(y)

#     theta, P = [0., .2, .2, 1.], 1.7 # initial
#     theta, P = [-2., -2., -1.2, 6.], 1.7 # better initialisation
    theta, P = [-2., -2., -1.2, 1.], 1.7 # generating fake data

    # generate fake data
    K = QP(theta, x, yerr, P)
    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)