def make_bootstrap(fitter,dset,n_bs):
 bs_datalist = (gvd.avg_data(d) for d in gvd.bootstrap_iter(dset,n_bs));
 bs = gvd.Dataset();
 for bs_fit in fitter.bootstrap_iter(bs_datalist):
  p = bs_fit.pmean;
  for key in p:
   bkey = utf.get_basekey(key)
   if bkey[0] == 'log':
    bs.append(key[3:],gv.exp(p[key]));
   else:
    bs.append(key,p[key]);
 bs_avg = gvd.avg_data(bs,bstrap=True);
 return bs_avg;
Example #2
0
 def test_bootstrap(self):
     """ corr3 -- bootstrap """
     if PRINT_FITS:
         print("======== " + self.getdoc())
     tp = 2 * self.T
     models = [
         self.mkcorr2(a=("a", "ao"), b=("a", "ao"), dE=("dEa", "dEao"), tp=tp),
         self.mkcorr2(a=("b", "bo"), b=("b", "bo"), dE=("dEb", "dEbo"), tp=tp),
         self.mkcorr3(
             a=("a", "ao"),
             b=("b", "bo"),
             dEa=("dEa", "dEao"),
             dEb=("dEb", "dEbo"),
             Vnn="Vnn",
             Von="Von",
             Vno="Vno",
             Voo="Voo",
             tpa=tp,
             tpb=tp,
         ),
     ]
     self.dofit(models)
     fitter = self.dofit(models)
     bsdata = ds.Dataset()
     for fit in fitter.bootstrap_iter(n=50):
         bsdata.append(fit.pmean)
     bsfit = ds.avg_data(bsdata, bstrap=True)
     self.assert_fitclose(bsfit, self.p)
     self.assert_fitsagree(fitter.fit.p, bsfit)
Example #3
0
 def test_bootstrap(self):
     """ corr2 -- bootstrap """
     if PRINT_FITS:
         print("======== " + self.getdoc())
     models = [self.mkcorr(a="a", b="a", dE="dE", tp=None)]
     fitter = self.dofit(models)
     bsdata = ds.Dataset()
     for fit in fitter.bootstrap_iter(n=40):
         bsdata.append(fit.pmean)
     bsfit = ds.avg_data(bsdata, bstrap=True)
     self.assert_fitclose(bsfit, self.p)
     self.assert_fitsagree(fitter.fit.p, bsfit)
Example #4
0
 def __init__(self, pfile):
     params = yaml.load(pfile)
     self.datafile = params['datafile']
     self.NT = params['NT']
     self.NX = params['NX']
     self.m_l = params['m_l']
     self.m_s = params['m_s']
     self.m_c = params['m_c']
     self.Text = params['Text']
     self.ncon = params['ncon']
     self.pi2twist = params['pi2twist']
     self.k2twist = params['k2twist']
     self.pi0 = params['pi0']
     self.pi0_err = params['pi0_err']
     self.bin = 1
     self.data_full = Dataset(self.datafile.rstrip())
     self.data = avg_data(bin_data(self.data_full,self.bin))
Example #5
0
def dirtyfit_3pt(cdict,
                 nexp,
                 key,
                 current,
                 tcut_3pt=None,
                 tcut_2pt=None,
                 loosener=default_loosener,
                 verbose=False):
    """
    Takes in a dictionary of correlators, the key for a 3-point correlator and the name of the current,
    produces a dirty estimate of the 3-point transition amplitude by taking the ratio

    R = C_{3pt}(t)/ C_{2pt,1}(t) * C_{2pt,2}(T-t),

    where the C_{2pt,1/2} are the correlators for the two states on either side of the current.
    t is the timeslice of the current and T is the source/sink temporal separation.

    This ratio is equal to J/a1*a2, where J is the transition amplitude and a1/2 are the amplitudes
    of the 2-point correlators. Then we find J = R * a1 * a2.

    Requires 2-point data keys of the format "meson.ss", where "meson" labels the meson,
    and the "s"'s labels the source/sink combination,
    e.g. etac.ll is a local-local eta_c correlator.

    Requires 3-point data keys of the format "meson1.J.meson2_T{T}.ss".
    """

    if verbose: print('Performing dirty fit on correlator', key)

    if not loosener: loosener = default_loosener

    # fetching labels
    try:
        tag = re.findall('^(.*)_T\d\d?', key)[0]  # M1.J.M2_T{T}.ll -> M1.J.M2
        meson1 = re.findall('^(.*)\.' + current,
                            key)[0]  # M1.J.M2_T{T}.ll -> M1
        meson2 = re.findall(current + '\.(.*)_T\d\d?',
                            key)[0]  # M1.J.M2_T{T}.ll -> M2

        source_ = re.findall('_T\d\d?\.[a-zA-Z]([a-zA-Z])$',
                             key)  # M1.J.M2_T{T}.ll -> l
        if len(source_) > 0: source = source_[0]
        else: source = ''

    except IndexError:
        print('The key', key,
              'is in the wrong format, it must look like M1.J.M2_T{T}.ll')
        sys.exit(1)

    T = int(re.findall('_T(\d\d?)', key)[0])  # M1.J.M2_T{T}.ll -> {T}

    if verbose:
        print('found meson labels =', meson1, ',', meson2, ', sources =',
              source)
        print('found T =', T)

    c3 = cdict[key]

    if not tcut_3pt:
        tcut_3pt = int(T / 3.)
        if verbose: print('tcut_3pt set to', tcut_3pt)

    c2 = []
    amp = []
    for meson in [meson1, meson2]:
        if verbose: print('finding amplitude for', meson, '...')

        # finding corresponding 2point correlators
        try:
            if source == '': c2.append(cdict[meson])
            else: c2.append(cdict[meson + '.' + source + source])
        except KeyError:
            print('cannot find correlator for', meson, 'to go with', key)
            sys.exit(1)

        Tlat = len(c2[-1])

        if not tcut_2pt:
            tcut_2pt = int(Tlat / 10.)
            if verbose: print('tcut_2pt set to', tcut_2pt)

        # finding ground state amplitude of 2pt correlators
        amp.append(
            avg_data(amp_superav2(c2[-1])[tcut_2pt:int(Tlat / 2) - tcut_2pt]) *
            gv.gvar(1, loosener))

        if verbose: print('amp =', amp[-1])

    # J = approximation of 3-point transition amplitude
    J = avg_data(superav2(ratio(
        c3, c2[0], c2[1]))[tcut_3pt:T - tcut_3pt]) * np.product(amp) * gv.gvar(
            1, loosener)

    if verbose: print('J =', J)

    result = gv.BufferDict()
    for V in ['Vnn_', 'Von_', 'Vno_', 'Voo_']:
        result.add(V + tag, [[gv.gvar(0.01, 1.0) for i in range(nexp)]
                             for j in range(nexp)])

    result['Vnn_' + tag][0][0] = J

    if verbose: print('result =', result)

    return gv.add_parameter_parentheses(result)
Example #6
0
def dirtyfit(correlator,
             nexp,
             key,
             tcut=None,
             loosener=default_loosener,
             zero_buffer=default_zerobuffer,
             verbose=False):
    """
    Takes in single correlator and associated key,
    returns dictionary of best guesses for correlator parameters.

    Requires data keys of the format "meson.ss", where "meson" labels the meson,
    and the "s"'s labels the source/sink combination,
    e.g. etac.ll is a local-local eta_c correlator.
    """

    if verbose: print('Performing dirty fit on correlator', key)

    Tlat = len(correlator)
    if not tcut:
        tcut = int(Tlat / 10.)
        if verbose: print('tcut set to ', tcut)

    if not loosener: loosener = default_loosener
    if not zero_buffer: zero_buffer = defalt_zerobuffer

    result = gv.BufferDict()

    # finding ground state energy
    mass = avg_data(
        effective_mass(superav2(correlator))[tcut:int(Tlat / 2) -
                                             tcut]) * gv.gvar(1, loosener)
    if verbose: print('mass = ', mass)

    # finding ground state amplitude
    amp = avg_data(amp_superav2(correlator)[tcut:int(Tlat / 2) -
                                            tcut]) * gv.gvar(1, loosener)
    if verbose: print('amplitude = ', amp)

    # 'excited_correlator' = correlators with ground state term removed
    excited_correlator = [
        correlator[t] - amp * exp(-mass * t) for t in range(Tlat)
    ]

    # finding first excited energy
    spectrum = np.mean(
        effective_mass(superav2(excited_correlator))[tcut:int(Tlat / 2) -
                                                     tcut]) * gv.gvar(
                                                         1, loosener)

    spectrum = gv.gvar(zero_buffer / (1 - spectrum.sdev / spectrum.mean),
                       zero_buffer / (spectrum.mean / spectrum.sdev - 1))
    # this transforms spectrum to a gvar that is 1 sigma
    # away from zero_buffer with the same fractional sdev

    # finding first excited state amplitude
    spectrum_amps = np.mean(
        amp_superav2(excited_correlator)[tcut:int(Tlat / 2) - tcut]) * gv.gvar(
            1, loosener)
    spectrum_amps = gv.gvar(
        zero_buffer / (1 - spectrum_amps.sdev / spectrum_amps.mean),
        zero_buffer / (spectrum_amps.mean / spectrum_amps.sdev - 1))
    # this transforms spectrum_amps to a gvar that is 1 sigma
    # away from zero_buffer with the same fractional sdev

    # building key
    try:
        meson = re.findall('^(.*)\.[a-zA-Z][a-zA-Z]$', key)[0]  # M.ll -> M
        source = key.split('.')[-1][0]  # M.ll -> l
    except IndexError:
        meson = key
        source = ''
    if verbose:
        print('found meson label = ', meson, ', source label = ', source)

    # building dictionary of results
    result.add('log' + meson + ':a' + source,
               [safelog(spectrum_amps) for i in range(nexp)])
    result.add('logdE:' + meson, [safelog(spectrum) for i in range(nexp)])

    result['log' + meson + ':a' + source][0] = safelog(amp)
    result['logdE:' + meson][0] = safelog(mass)

    # making guesses for oscillating states -
    # ground state amplitudes smaller and ground state energies larger than non-oscillating
    result.add('logo' + meson + ':a' + source,
               [safelog(gv.gvar(amp.mean / 2, amp.mean)) for i in range(nexp)])
    result.add('logdE:o' + meson, [safelog(spectrum) for i in range(nexp)])

    result['logdE:o' + meson][0] = safelog(gv.gvar(mass.mean * 1.5, mass.mean))

    if verbose: print('result = ', result)

    return gv.add_parameter_parentheses(result)
Example #7
0
Usage:

    avg.py file1 file2 ... (for text files)

    avg.py file.h5 group1 group2 ... (for hdf5 file)

Returns a table showing averages and standard deviations for all
quantities in the dataset files. (See gvar.dataset.Dataset for
information on file formats.)
"""
from __future__ import print_function

import fileinput
import warnings
import sys

import gvar
from gvar.dataset import Dataset, avg_data

warnings.simplefilter("ignore")

if sys.argv and sys.argv[1][-3:] == '.h5':
    dset = Dataset(sys.argv[1], h5group=sys.argv[2:])
else:
    dset = Dataset(fileinput.FileInput(openhook=fileinput.hook_compressed))
ndset = Dataset()
# put number of items in key
for k in dset:
    ndset[str(k) + (' [{}]'.format(len(dset[k])))] = dset[k]
data = avg_data(ndset)
print(gvar.tabulate(data, headers=False))