def coreFunc(a): """runs the below for an angle, a Allows multiprocessing across a range of values for a """ i,d,a = a model = Model(x0, pot=pot, rng=rng, step_size = d, n_steps = n_steps, rand_steps=False) c = acorr.Autocorrelations_1d(model) c.runModel(n_samples=n_samples, n_burn_in=n_burn_in, mixing_angle = a, verbose=False, verb_pos=i) acs = c.getAcorr(separations, opFn, norm = False) # get parameters generated pacc = c.model.p_acc pacc_err = np.std(c.model.sampler.accept.accept_rates) ans = errors.uWerr(c.op_samples, acorr=acs) # get errors op_av, op_diff, _, itau, itau_diff, _, acns = ans # extract data w = errors.getW(itau, itau_diff, n=n_samples) # get window length if not np.isnan(w): acns_err = errors.acorrnErr(acns, w, n_samples) # get error estimates acs = acns else: acns_err = np.full(acs.shape, np.nan) itau = itau_diff = np.nan return op_av, op_diff, acs, acns_err, itau, itau_diff, pacc, pacc_err, w
def coreFunc(a): """runs the below for an angle, a""" i, a, n_samples = a model = Model( x0, pot=pot, spacing=spacing, # set up model rng=rng, step_size=step_size, n_steps=n_steps, rand_steps=rand_steps) c = acorr.Autocorrelations_1d(model) # set up autocorrs c.runModel( n_samples=n_samples, n_burn_in=n_burn_in, # run MCMC mixing_angle=a, verbose=explicit_prog, verb_pos=i) cfn = opFn(c.model.samples) # run func on HMC samples # get parameters generated p = c.model.p_acc # get acceptance rates at each M-H step # Calculating integrated autocorrelations ans = errors.uWerr(cfn) xx, f_diff, _, itau, itau_diff, itaus, _ = ans w = errors.getW(itau, itau_diff, n=cfn.shape[0]) out = itau, itau_diff, f_diff, w return xx, p, itau, itau_diff, f_diff, w
def preparePlot(op_samples, ans, n, itau_theory=None, acn_theory=None, mcore=False): """Prepares the plot according to the output of uWerr Required Inputs op_samples :: np.ndarray :: function acted upon the HMC samples ans :: tuple :: output form uWerr n :: int :: number of samples from MCMC Optional Input itau_theory :: float :: theoretical iTau value acn_theory :: float :: theoretical ac values plotted against ficticous time NOT window length mcore :: bool :: flag that ans is a nested list of l_ans = [ans, ans, ...] """ f_aav, f_diff, f_ddiff, itau, itau_diff, itaus, acn = ans if not np.isnan(itau): w = errors.getW(itau, itau_diff, n) # get window l = w * 2 itaus_diff = errors.itauErrors(itaus, n=n) # calcualte error in itau acn_diff = errors.acorrnErr(acn, w, n) # note acn not ac is used itau_label = r"$\tau_{\text{int}}(w_{\text{best}} = " + r"{}) = ".format(int(w)) \ + r"{:.2f} \pm {:.2f}$".format(itau, itau_diff) else: w = np.nan l = acn.size // 2 itaus_diff = np.zeros(acn.shape) acn_diff = np.zeros(acn.shape) itau_label = r"$\tau_{\text{int}}(w_{\text{best}}:\ \text{n/a}) = \text{n/a}$" # a quick hack l = min(acn.size, itaus.size, l) if acn_theory is not None: acn_theory = acn_theory[:l] g_int = np.cumsum(np.nan_to_num(acn[1:l] / acn[0])) # recreate the g_int function g = np.asarray([errors.gW(t, v, 1.5, n) for t, v in enumerate(g_int, 1) ]) # recreate the gW function x = np.arange(l) # same size for itaus and acn g = np.insert(g, 0, np.nan) # g starts at x[1] not x[0] # create items for plotting gta = (g[:l], None, ''), (itaus[:l], itaus_diff[:l], itau_label, itau_theory), (acn[:l], acn_diff[:l], '', acn_theory) return x, gta, w
def coreFunc(a): """runs the below for an angle, a Allows multiprocessing across a range of values for a """ i, a = a model = Model(x0, pot=pot, spacing=spacing, rng=rng, step_size=step_size, n_steps=n_steps, rand_steps=rand_steps) c = acorr.Autocorrelations_1d(model) c.runModel(n_samples=n_samples, n_burn_in=n_burn_in, mixing_angle=a, verbose=True, verb_pos=i) store.store(c.model.samples, file_name, '_samples') store.store(c.model.traj, file_name, '_trajs') acs = c.getAcorr(separations, opFn, norm=False, prll_map=prll_map, max_sep=50) # non norm for uWerr # get parameters generated traj = np.cumsum(c.trajs) p = c.model.p_acc xx = c.op_mean acorr_seps = c.acorr_ficticous_time acorr_counts = c.acorr_counts store.store(p, file_name, '_probs') store.store(acs, file_name, '_acs') ans = errors.uWerr(c.op_samples, acorr=acs) # get errors _, _, _, itau, itau_diff, _, acns = ans # extract data w = errors.getW(itau, itau_diff, n=n_samples) # get window length acns_err = errors.acorrnErr(acns, w, n_samples) # get error estimates if rand_steps: # correct for N being different for each correlation acns_err *= np.sqrt(n_samples) / acorr_counts return xx, acns, acns_err, p, w, traj, acorr_seps
separations = np.linspace(min_sep, max_sep, (max_sep - min_sep) / res + 1) # multicore result = prll_map(aFn, separations, verbose=True) a, counts = zip(*result) a = np.asarray(a) # the autocorrelation array counts = np.array(counts) # the counts at each separation # mask = 1-np.isnan(a) # a = a[mask] a = a * counts # grab errors print 'getting errors...' ans = uWerr(op_samples, a) _, _, _, itau, itau_diff, _, acns = ans # extract data my_w = getW(itau, itau_diff, n=n_samples) # get window length my_err = acorrnErr(acns, my_w, n_samples) # get autocorr errors my_err *= np.sqrt(n_samples) / np.sqrt(counts) # theory calclations th_x = np.linspace(min_sep, max_sep, 1000) th = np.array([hmc(p, phi, r, i) for i in th_x]) from scipy.optimize import curve_fit fn = lambda x, a, b, c: a * np.cos(b * x)**2 + c expFn = lambda x, a, b, c: a * np.exp(-b * x) + c cos_sq = curve_fit(fn, separations[:50], acns[:50]) exp_fit = curve_fit(expFn, separations[1:80], counts[1:80]) norm_cnt = expFn(0, *exp_fit[0]) / counts[1]
print 'Comparing autocorrelation calculations...' # assert that the autocorrelation routine is the same av_xx = comparison_xx.mean() norm = ((comparison_xx-av_xx)**2).mean() my_acorr = np.asarray(map(lambda s: myAcorr(comparison_xx, av_xx, s), np.asarray(separations))) christian_class = Christian_Autocorrelation(comparison_xx) christian_acorr = christian_class.acf()[:c_len] christian_acorr = np.asarray(christian_acorr) diffs = christian_acorr[:my_acorr.size] - my_acorr print " > av. difference: {}".format(diffs.mean()) print 'Checking integration window calculation:' christian_tint, christian_dtint, christian_w = christian_class.tauintW(False) _,_,my_w = windowing(comparison_xx, av_xx, 1.0, comparison_xx.size, fast=True) print "Christian Window:{}".format(christian_w) print "My Window:{}".format(my_w) ans = uWerr(comparison_xx, acorr=my_acorr) # get errors _, _, _, itau, itau_diff, _, acns = ans # extract data my_w = getW(itau, itau_diff, n=comparison_xx.size) # get window length my_err = acorrnErr(acns, my_w, comparison_xx.size) # get autocorr errors christian_acorr = np.asarray(christian_acorr) christian_err = acorrnErr(christian_acorr, christian_w, comparison_xx.size) all_plot = preparePlot(christian_acorr[:2*my_w], acns[:2*my_w], my_err[:2*my_w], christian_err[:2*my_w]) store(all_plot, file_name, '_allPlot') plot(save = saveOrDisplay(save, file_name+"_compareAc"), **all_plot)