def _runDeltaH(self, pot, n_samples = 10000, n_burn_in = 20, n_steps=20, step_size=.1): """Obtains the average exponentiated change in H for a given potential Optional Inputs n_samples :: int :: number of samples n_burn_in :: int :: number of burnin steps n_steps :: int :: LF trajectory lengths step_size :: int :: Leap Frog step size """ x0 = np.random.random(self.lattice_shape) model = Model(x0, pot, spacing=self.spacing, n_steps=n_steps, step_size=step_size, accept_kwargs={'get_delta_hs':True}) model.run(n_samples=n_samples, n_burn_in=n_burn_in, verbose = True) delta_hs = np.asarray(model.sampler.accept.delta_hs[n_burn_in:]).flatten() accept_rates = np.asarray(model.sampler.accept.accept_rates[n_burn_in:]).flatten() av_acc = np.asscalar(accept_rates.mean()) return (delta_hs, av_acc)
def coreFunc(n_steps): """function for multiprocessing support Required Inputs n_steps :: int :: the number of LF steps """ model = Model(x0.copy(), pot, step_size=step_size, n_steps=n_steps, accept_kwargs={'get_delta_hs': True}) model.sampler.accept.store_acceptance = True prob = 1. delta_hs = 1. av_dh = -1 accept_rates = [] delta_hs = [] samples = [] while av_dh < 0: model.run(n_samples=n_samples, n_burn_in=n_burn_in) accept_rates += model.sampler.accept.accept_rates[n_burn_in:] delta_hs += model.sampler.accept.delta_hs[n_burn_in:] samples.append(model.samples.copy()) av_dh = np.mean(delta_hs) if av_dh < 0: tqdm.write('running again -ve av_dh') accept_rates = np.asarray(accept_rates) samples = np.concatenate(tuple(samples), axis=0) prob = accept_rates.mean() meas_av_exp_dh = np.asscalar((1. / np.exp(delta_hs)).mean()) # ans = errors.uWerr(accept_rates) # get errors # f_aav, f_diff, _, itau, itau_diff, _, acns = ans # extract data mean = accept_rates.mean() err = np.std(accept_rates) / np.sqrt(n_samples) th_err = np.std(accept_rates) / np.sqrt(n_samples) theory = acceptance(dtau=step_size, delta_h=av_dh) return mean, theory, err, th_err
def minimise_func(args): """runs the below for an angle, a Allows multiprocessing across a range of values for a """ step_size, n_steps = args print 'step size:{}; n steps {}'.format(step_size, n_steps) model = Model(x0, pot=pot, rng=rng, step_size=step_size) model.run(n_samples=n_samples, n_burn_in=n_burn_in, mixing_angle=mixing_angle, verbose=True) op_samples = opFn(model.samples) # get parameters generated # pacc = c.model.p_acc # pacc_err = np.std(c.model.sampler.accept.accept_rates) ans = errors.uWerr(op_samples) # get errors op_av, op_diff, _, itau, itau_diff, _, _ = ans # extract data return itau #, itau_diff, pacc, pacc_err
def main(x0, pot, file_name, n_samples, n_burn_in, y_label, extra_data = [], save = False): """A wrapper function Required Inputs x0 :: np.array :: initial position input to the HMC algorithm pot :: potential class :: defined in hmc.potentials file_name :: string :: the final plot will be saved with a similar name if save=True n_samples :: int :: number of HMC samples n_burn_in :: int :: number of burn in samples y_label :: str :: the y axis label Optional Inputs extra_data :: list of dicts :: see below save :: bool :: True saves the plot, False prints to the screen Expectations extra_data = [{'f':actual,'x':x, 'label':theory}, {'f':fitted,'x':x, 'label':None}] 'f' contains a function with respect to a linear x range (x-axis) 'label' is a string or None """ model = Model(x0, pot=pot) print 'Running Model: {}'.format(file_name) model.run(n_samples=n_samples, n_burn_in=n_burn_in, verbose = True) print 'Finished Running Model: {}'.format(file_name) # first contains the last burn in sample samples = np.asarray(model.samples)[1:] plot(samples = samples, y_label = y_label, subtitle = r'Potential: {}, Lattice shape: {}'.format(pot.name, x0.shape), extra_data = extra_data, save = saveOrDisplay(save, file_name) )
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np from models import Basic_HMC as Model from hmc.potentials import Klein_Gordon as KG pot = KG(m=0.) n, dim = 10, 1 x0 = np.random.random((n,)*dim) n_samples = 1000000 n_burn_in = 50 step_size = .2 n_steps = 20 model = Model(x0.copy(), pot, step_size=step_size, n_steps=n_steps) model.run(n_samples=n_samples, n_burn_in=n_burn_in, verbose=True)
for i,j in zip(np.ravel(x), np.ravel(y))] )) z = np.asarray(z).reshape(n_points, n_points) return x, y, z # if __name__ == '__main__': n_burn_in = 15 n_samples = 100 pot = MVG() x0 = np.asarray([[-4.], [4.]]) model = Model(x0, pot=pot) # adjust for nice plotting print "Running Model: {}".format(__file__) model.run(n_samples=n_samples, n_burn_in=n_burn_in) print "Finished Running Model: {}".format(__file__) # change shape from (n, 2) -> (2, n) samples = model.samples burn_in = model.burn_in xyz = getPotential(pot.uE) f_name = os.path.basename(__file__) save_name = os.path.splitext(f_name)[0] + '.png' plot(burn_in=burn_in,
# if __name__ == '__main__': n_burn_in = 10 n_samples = 100 pot = Ring_Potential(scale=5, bias=-1) x0 = np.asarray([[0.], [0.]]) rng = np.random.RandomState() model = Model(x0, rng=rng, pot=pot, n_steps=50, step_size=0.1, verbose=True, rand_steps=True) # adjust for nice plotting print "Running Model: {}".format(__file__) model.run(n_samples=n_samples, n_burn_in=n_burn_in, mixing_angle=np.pi / 6.) print "Finished Running Model: {}".format(__file__) # change shape from (n, 2) -> (2, n) samples = model.samples burn_in = model.burn_in