def run_hmc_sampler(model, sps, lnprobf, initial_center, rp, pool=None): """Run a (single) HMC chain, performing initial steps to adjust the epsilon. """ import hmc sampler = hmc.BasicHMC() eps = 0. # need to fix this: length = niter = nsegmax = nchains = None # initial conditions and calulate initial epsilons pos, prob, thiseps = sampler.sample(initial_center, model, iterations=10, epsilon=None, length=length, store_trajectories=False, nadapt=0) eps = thiseps # Adaptation of stepsize for k in range(nsegmax): # Advance each sampler after adjusting step size afrac = sampler.accepted.sum() * 1.0 / sampler.chain.shape[0] if afrac >= 0.9: shrink = 2.0 elif afrac <= 0.6: shrink = 1 / 2.0 else: shrink = 1.00 eps *= shrink pos, prob, thiseps = sampler.sample(sampler.chain[-1, :], model, iterations=iterations, epsilon=eps, length=length, store_trajectories=False, nadapt=0) # this should not have actually changed during the sampling alleps[k] = thiseps # Main run afrac = sampler.accepted.sum() * 1.0 / sampler.chain.shape[0] if afrac < 0.6: eps = eps / 1.5 hpos, hprob, eps = sampler.sample(initial_center, model, iterations=niter, epsilon=eps, length=length, store_trajectories=False, nadapt=0) return sampler
def run_hmc(initial, model, length=100, nsegmax=20, iterations=5000, adapt_iterations=100, verbose=False): """Run an HMC sampler for the given number of steps after a certain amount of adaptation. returns the sampler object. """ hsampler = hmc.BasicHMC(verbose=verbose) pos, prob, teps = hsampler.sample(initial, model, iterations=10, epsilon=None, length=length, store_trajectories=False, nadapt=0) eps = teps # Adapt epsilon while burning-in for k in range(nsegmax): #advance each sampler after adjusting step size afrac = hsampler.accepted.sum() * 1.0 / hsampler.chain.shape[0] if afrac >= 0.9: shrink = 2.0 elif afrac <= 0.6: shrink = 1 / 2.0 else: shrink = 1.00 eps *= shrink pos, prob, teps = hsampler.sample(hsampler.chain[-1, :], model, epsilon=eps, length=length, iterations=adapt_iterations, store_trajectories=False, nadapt=0) # Production hsampler.sample(hsampler.chain[-1, :], model, iterations=iterations, length=length, epsilon=eps, store_trajectories=True) return hsampler
raxes[i, 0].imshow(stamp.pixel_values.T, origin='lower') raxes[i, 1].imshow(im.T, origin='lower') resid = raxes[i, 2].imshow((stamp.pixel_values - im).T, origin='lower') rfig.colorbar(resid, ax=raxes[i, :].tolist()) labels = ['Data', 'Model', 'Data-Model'] [ax.set_title(labels[i]) for i, ax in enumerate(raxes[0, :])] # --- Sampling ----- if False: p0 = ptrue.copy() #model.renormalize_theta(ptrue) import hmc #initialize sampler and sample sampler = hmc.BasicHMC(model, verbose=False) eps = sampler.find_reasonable_epsilon(p0) #eps = 0.01 #sys.exit() iterations = 50 length = 200 pos, prob, eps = sampler.sample(p0 * 1.1, iterations=iterations, epsilon=eps, length=length, store_trajectories=True) vals = pos # model.renormalize_theta(pos) rfig, raxes = pl.subplots(len(stamps), 3, sharex=True, sharey=True) raxes = np.atleast_2d(raxes) for i, stamp in enumerate(stamps):