Ejemplo n.º 1
0
def run_MCMC_onebin(binnum, nwalkers, burnlinks, links, hist_bincnt):
    ndim = 3
    p0 = np.random.rand(ndim * nwalkers).reshape((nwalkers, ndim))
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lambda param: log_likelihood_onebin(binnum-1,param))
    progbar.set_totalsteps(float(burnlinks)*nwalkers)
    print 'Begin burn in'
    pos, prob, state = sampler.run_mcmc(p0,burnlinks)
    progbar.update_progress(1)
    print 'Burn in completed'
    sampler.reset()
    progbar.set_totalsteps(float(links)*nwalkers)
    sampler.run_mcmc(pos, links)
    progbar.update_progress(1)
    print('Main chain completed')
    print("Mean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction)))
    # Determine the 95% confidence level by sorting the marginalized chain and finding the 
    # eta element that is 95% of the way down the sorted chain
    sorted_eta = np.sort(sampler.flatchain[:,0])
    conf95_index = np.round(0.95*len(sorted_eta) - 1).astype(np.int)
    conf998_index = np.round(0.998*len(sorted_eta) - 1).astype(np.int)
    conf95 = sorted_eta[conf95_index]
    conf998 = sorted_eta[conf998_index]
    print '95% upper limit: ' + str(conf95)
    plt.figure()
    y, x, o = plt.hist(sampler.flatchain[:,0], hist_bincnt, range=[0,conf998], histtype="step", normed = 1)
    yciel = 1.1*y.max()
    plt.ylim([0,yciel])
    plt.vlines(conf95, 0, yciel ,colors='r')
    plt.title('Dark Matter Signal Posterior PDF (bin %d)' % binnum,fontsize = 16)
    plt.xlabel(r'Signal Strength [$\eta$]',fontsize = 14)
    plt.ylabel(r'Probability Density', fontsize = 14)
    plt.text(1.05*conf95,0.75*yciel, '95% conf: {0:.3f}'.format(conf95), bbox=dict(facecolor='red',alpha=0.5))
    plt.show()
Ejemplo n.º 2
0
def test_MCMC():
    read_data("DMm400AVu_cf.txt")
    set_prior(uniform)
    ndim = 3
    nwalkers = 90
    burnlinks = 300
    links = 100
    p0 = np.random.rand(ndim * nwalkers).reshape((nwalkers, ndim))
    sampler = emcee.EnsembleSampler(nwalkers, ndim, log_likelihood_all)
    progbar.set_totalsteps(float(burnlinks)*nwalkers)
    print 'Begin burn in'
    pos, prob, state = sampler.run_mcmc(p0,burnlinks)
    progbar.update_progress(1)
    print 'Burn in completed'
    sampler.reset()
    progbar.set_totalsteps(float(links)*nwalkers)
    sampler.run_mcmc(pos, links)
    progbar.update_progress(1)
    print('Main chain completed')
    print("Mean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction)))
    sorted_eta = np.sort(sampler.flatchain[:,0])
    conf95_index = np.round(0.95*len(sorted_eta) - 1).astype(np.int)
    conf998_index = np.round(0.998*len(sorted_eta) - 1).astype(np.int)
    conf95 = sorted_eta[conf95_index]
    conf998 = sorted_eta[conf998_index]
    print '95% upper limit: ' + str(conf95)
    plt.figure()
    y, x, o = plt.hist(sampler.flatchain[:,0], 300, range=[0,conf998], histtype="step", normed = 1)
    yciel = 1.1*y.max()
    plt.ylim([0,yciel])
    plt.vlines(conf95, 0, yciel ,colors='r')
    plt.title('Dark Matter Signal Posterior PDF (all bins)',fontsize = 16)
    plt.xlabel(r'Signal Strength [$\eta$]',fontsize = 14)
    plt.ylabel(r'Probability Density', fontsize = 14)
    plt.text(1.05*conf95,0.75*yciel, '95% conf: {0:.3f}'.format(conf95), bbox=dict(facecolor='red',alpha=0.5))
    plt.show()