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
예제 #2
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
예제 #3
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
예제 #4
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
예제 #5
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