Ejemplo n.º 1
0
    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 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
Ejemplo n.º 3
0
    def core(i):
        """multiprocessing"""
        av = c.getTwoPoint(separation=i)

        ans = errors.uWerr(c.op_samples)  # get errors
        f_aav, f_diff, _, itau, itau_diff, _, acns = ans  # extract data
        return f_aav, f_diff, itau
Ejemplo n.º 4
0
        def coreFunc(a):
            """runs the below for an angle, a"""
            i, a = 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=True,
                verb_pos=i)
            acs = c.getAcorr(separations,
                             opFn,
                             norm=False,
                             prll_map=None,
                             ac=acs,
                             max_itau=max_itau)
            cfn = c.op_samples
            # get parameters generated
            p = c.model.p_acc  # get acceptance rates at each M-H step
            xx = np.average(
                cfn)  # get average of the function run over the samples

            ans = errors.uWerr(cfn, acorr=acs)
            if itauFunc:
                t = itauFunc(tau=n_steps * step_size, m=1, pa=p, theta=a)
            else:
                t = None
            if acTheory is not None:
                th_x = np.linspace(0, c.acorr_ficticous_time, 10000)
                ac_th = np.asarray([th_x, [acTheory(t, p, a) for t in th_x]])
            else:
                ac_th = None

            x, gta, w = preparePlot(cfn,
                                    n=n_samples,
                                    itau_theory=t,
                                    mcore=True,
                                    acn_theory=ac_th)
            return xx, traj, p, x, gta, w
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
 def minimise_func(arg):
     """runs the below for an angle, a
     Allows multiprocessing across a range of values for a
     """
     mixing_angle, step_size = arg
     print 'angle:{:10.4f}; step size:{:10.4f}'.format(mixing_angle, step_size)
     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
aFn = lambda s: acorrMapped(
    op_samples, tsum, s, av_op, norm=1.0, tol=tolerance, counts=True)
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])
del acs
# calculate autocorrelations
aFn = lambda s: acorrMapped(
    op_samples, tsum, s, av_op, norm=1.0, tol=tolerance, counts=True)
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

# grab errors
print 'getting errors...'
ans = uWerr(op_samples, a * counts)
_, _, _, 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)

from scipy.optimize import curve_fit
fn = lambda x, a, b, c: a * np.exp(-b * x) + c
f = curve_fit(fn, separations[1:100], counts[1:100])
th_x = np.linspace(min_sep, max_sep, 1000)

pp = Pretty_Plotter()
pp._teXify()
pp._updateRC()

fig = plt.figure(figsize=(8, 8))  # make plot
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
def main(x0,
         pot,
         file_name,
         n_samples,
         n_burn_in,
         mixing_angle,
         angle_labels,
         opFn,
         op_name,
         rand_steps=False,
         step_size=.5,
         n_steps=1,
         spacing=1.,
         itauFunc=None,
         separations=range(5000),
         acTheory=None,
         max_sep=None,
         save=False):
    """Takes a function: opFn. Runs HMC-MCMC. Runs opFn on HMC samples.
        Calculates Autocorrelation + Errors on opFn.
    
    Required Inputs
        x0          :: np.array :: initial position input to the HMC algorithm
        pot         :: potential class :: defined in hmc.potentials
        file_name   :: string :: 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
        mixing_angle :: iterable :: mixing angles for the HMC algorithm
        angle_labels :: list :: list of angle label text for legend plotting
        opFn        :: func :: a function to run over samples
        op_name     :: str :: label for the operator for plotting
    
    Optional Inputs
        rand_steps :: bool :: probability of with prob
        step_size :: float :: MDMC step size
        n_steps :: int :: number of MDMC steps
        spacing ::float :: lattice spacing
        save :: bool :: True saves the plot, False prints to the screen
        acTheory :: func :: acTheory(t, pa, theta) takes in acceptance prob., time, angle 
        separations :: range / nparray :: the number of separations for A/C
        max_sep :: float :: the maximum separation tom consider for autocorrelations
    """
    rng = np.random.RandomState()
    multi_angle = len(mixing_angle) > 1  # see if multiprocessing is needed

    print 'Running Model: {}'.format(file_name)

    # output to print to screen
    out = lambda p,x,a:  '> measured at angle:{:3.1f}:'.format(a) \
        + ' <x^2>_L = {}; <P_acc>_HMC = {:4.2f}'.format(x, p)

    if not multi_angle:
        mixing_angle = mixing_angle[0]
        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=mixing_angle,
            verbose=True)
        acs = c.getAcorr(separations, opFn, norm=False, prll_map=prll_map)
        cfn = c.op_samples

        # get parameters generated
        traj = c.model.traj  # get trajectory lengths for each LF step
        p = c.model.p_acc  # get acceptance rates at each M-H step
        xx = np.average(
            c.op_samples)  # get average of the function run over the samples

        if itauFunc:
            t = itauFunc(tau=(n_steps * step_size),
                         m=1,
                         pa=p,
                         theta=mixing_angle)
        else:
            t = None
        if acTheory is not None:
            ac_th = np.asarray(
                [acTheory(t, p, mixing_angle) for t in c.acorr_ficticous_time])
        else:
            ac_th = None

        ans = errors.uWerr(cfn, acorr=acs)
        x, gta, w = preparePlot(cfn,
                                ans,
                                n=n_samples,
                                itau_theory=t,
                                mcore=False,
                                acn_theory=ac_th)
        window_fns, int_ac_fns, acorr_fns = [[item] for item in gta]
        ws = [w]  # makes compatible with multiproc
        x_lst = [x]  # again same as last two lines
        print out(p, xx, mixing_angle)

    else:  # use multicore support

        def coreFunc(a):
            """runs the below for an angle, a"""
            i, a = 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=True,
                verb_pos=i)
            acs = c.getAcorr(separations,
                             opFn,
                             norm=False,
                             prll_map=None,
                             ac=acs,
                             max_itau=max_itau)
            cfn = c.op_samples
            # get parameters generated
            p = c.model.p_acc  # get acceptance rates at each M-H step
            xx = np.average(
                cfn)  # get average of the function run over the samples

            ans = errors.uWerr(cfn, acorr=acs)
            if itauFunc:
                t = itauFunc(tau=n_steps * step_size, m=1, pa=p, theta=a)
            else:
                t = None
            if acTheory is not None:
                th_x = np.linspace(0, c.acorr_ficticous_time, 10000)
                ac_th = np.asarray([th_x, [acTheory(t, p, a) for t in th_x]])
            else:
                ac_th = None

            x, gta, w = preparePlot(cfn,
                                    n=n_samples,
                                    itau_theory=t,
                                    mcore=True,
                                    acn_theory=ac_th)
            return xx, traj, p, x, gta, w

        #
        # use multiprocessing
        l = len(mixing_angle)  # number of mixing angles
        ans = prll_map(coreFunc, zip(range(l), mixing_angle), verbose=False)

        # unpack from multiprocessing
        xx, traj, ps, x_lst, gtas, ws = zip(*ans)

        print '\n' * l  # hack to avoid text overlapping in terminal
        for p, x, a in zip(
                ps, xx, mixing_angle):  # print intermediate results to screen
            print out(p, x, a)
        window_fns, int_ac_fns, acorr_fns = zip(
            *gtas)  # separate out to respective lists

    lines = {
        0: window_fns,
        1: int_ac_fns,
        2: acorr_fns
    }  # create a dictionary for plotting
    #
    print 'Finished Running Model: {}'.format(file_name)

    subtitle = r"Potential: {}; Lattice Shape: ".format(pot.name) \
        + r"${}$; $a={:.1f}; \delta\tau={:.1f}; n={}$".format(
            x0.shape, spacing, step_size, n_steps)

    # all_plot contains all necessary keyword arguments
    all_plot = {
        'lines_d': lines,
        'x_lst': x_lst,
        'ws': ws,
        'subtitle': subtitle,
        'mcore': multi_angle,
        'angle_labels': angle_labels,
        'op_name': op_name
    }

    store.store(all_plot, file_name, '_allPlot')

    plot(lines_d=lines,
         x_lst=x_lst,
         ws=ws,
         subtitle=subtitle,
         mcore=multi_angle,
         angle_labels=angle_labels,
         op_name=op_name,
         save=saveOrDisplay(save, file_name))
    pass
Ejemplo n.º 11
0
    for i in ax:
        i.grid(True)
    pp.save_or_show(save, PLOT_LOC)
    pass


if __name__ == '__main__':
    d = './correlations/ref_code/'  # grab uWerr data from Matlab script
    with open(d + '/uWerr_out.dat') as f:
        actual = json.load(f)
    actual['itau_aav'] = np.loadtxt(d + 'uWerr_out_tauintFbb.dat')
    actual['acorr'] = np.loadtxt(d + 'uWerr_out_gammaFbb.dat')
    a = np.loadtxt(d + 'actime_tint20_samples.dat')
    # run the uWerr from here
    value, dvalue, ddvalue, tauint, dtauint, y, y2 = uWerr(a)
    res = {
        'value': value,
        'dvalue': dvalue,
        'ddvalue': ddvalue,
        'dtauint': dtauint,
        'tauint': tauint,
        # 'CFbbopt':c_aav,
        'itau_aav': y,
        'acorr': y2
    }

    w = np.around((ddvalue / dvalue)**2 * a.size - .5, 0)
    w2 = np.around((dtauint / tauint / 2.)**2 * a.size - .5 + tauint, 0)
    assert w == w2  # sanity check