Example #1
0
def main():
    gv.ranseed(4)
    x = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
    y_samples = [
        [2.8409,  4.8393,  6.8403,  8.8377, 10.8356, 12.8389, 14.8356, 16.8362, 18.8351, 20.8341],
        [2.8639,  4.8612,  6.8597,  8.8559, 10.8537, 12.8525, 14.8498, 16.8487, 18.8460, 20.8447],
        [3.1048,  5.1072,  7.1071,  9.1076, 11.1090, 13.1107, 15.1113, 17.1134, 19.1145, 21.1163],
        [3.0710,  5.0696,  7.0708,  9.0705, 11.0694, 13.0681, 15.0693, 17.0695, 19.0667, 21.0678],
        [3.0241,  5.0223,  7.0198,  9.0204, 11.0191, 13.0193, 15.0198, 17.0163, 19.0154, 21.0155],
        [2.9719,  4.9700,  6.9709,  8.9706, 10.9707, 12.9705, 14.9699, 16.9686, 18.9676, 20.9686],
        [3.0688,  5.0709,  7.0724,  9.0730, 11.0749, 13.0776, 15.0790, 17.0800, 19.0794, 21.0795],
        [3.1471,  5.1468,  7.1452,  9.1451, 11.1429, 13.1445, 15.1450, 17.1435, 19.1425, 21.1432],
        [3.0233,  5.0233,  7.0225,  9.0224, 11.0225, 13.0216, 15.0224, 17.0217, 19.0208, 21.0222],
        [2.8797,  4.8792,  6.8803,  8.8794, 10.8800, 12.8797, 14.8801, 16.8797, 18.8803, 20.8812],
        [3.0388,  5.0407,  7.0409,  9.0439, 11.0443, 13.0459, 15.0455, 17.0479, 19.0493, 21.0505],
        [3.1353,  5.1368,  7.1376,  9.1367, 11.1360, 13.1377, 15.1369, 17.1400, 19.1384, 21.1396],
        [3.0051,  5.0063,  7.0022,  9.0052, 11.0040, 13.0033, 15.0007, 16.9989, 18.9994, 20.9995],
        ]
    y = gv.dataset.avg_data(y_samples)
    svd = gv.dataset.svd_diagnosis(y_samples)
    y = gv.svd(y, svdcut=svd.svdcut)
    if SHOW_PLOTS:
        svd.plot_ratio(show=True)

    def fcn(p):
        return p['y0'] + p['s'] * x

    prior = gv.gvar(dict(y0='0(5)', s='0(5)'))
    fit = lsqfit.nonlinear_fit(data=y, fcn=fcn, prior=prior)
    print(fit)
Example #2
0
def main():
    x, y = make_data()  # collect fit data
    p0 = None  # make larger fits go faster (opt.)
    for nexp in range(1, 7):
        print('************************************* nexp =', nexp)
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x, y), fcn=fcn, prior=prior, p0=p0)
        print(fit)  # print the fit results
        if nexp > 2:
            E = fit.p['E']  # best-fit parameters
            a = fit.p['a']
            print('E1/E0 =', E[1] / E[0], '  E2/E0 =', E[2] / E[0])
            print('a1/a0 =', a[1] / a[0], '  a2/a0 =', a[2] / a[0])
        if fit.chi2 / fit.dof < 1.:
            p0 = fit.pmean  # starting point for next fit (opt.)
        print()

    # error budget analysis
    # outputs = {
    #     'E1/E0':E[1]/E[0], 'E2/E0':E[2]/E[0],
    #     'a1/a0':a[1]/a[0], 'a2/a0':a[2]/a[0]
    #     }
    # inputs = {'E':fit.prior['E'], 'a':fit.prior['a'], 'y':y}
    outputs = gv.BufferDict()
    outputs['E2/E0'] = E[2] / E[0]
    outputs['E1/E0'] = E[1] / E[0]
    outputs['a2/a0'] = a[2] / a[0]
    outputs['a1/a0'] = a[1] / a[0]
    inputs = gv.BufferDict()
    inputs['a'] = fit.prior['a']
    inputs['y'] = y
    inputs['E'] = fit.prior['E']
    print('================= Error Budget Analysis')
    print(gv.fmt_values(outputs))
    print(gv.fmt_errorbudget(outputs, inputs))
Example #3
0
def prep_plot(plt, data):
    if not SHOW_PLOT or not ONE_W:
        return plt
    fit = lsqfit.nonlinear_fit(
        data=gv.gvar(data.y, data.sig), prior=make_prior(), fcn=data.fitfcn
        )
    print(fit)
    if False:
        plt.rc('text',usetex=True)
        plt.rc('font',family='serif', serif=['Times'])
        ebargs = dict(
            fmt='o', mfc='w', alpha=1.0, ms=1.5 * 2.5,
            capsize=1.5 * 1.25, elinewidth=.5 * 1.5, mew=.5 * 1.5
            )
        fw = 3.3   # make bigger
        fh = fw /1.61803399 
        # plt.figure(figsize=(fw,fh))
        plt.rcParams["figure.figsize"] = (fw, fh)
    else:
        plt.rc('font',family='serif')
        ebargs = dict(fmt='o', mfc='w', alpha=1.0)

    plt.errorbar(x=data.x, y=data.y, yerr=data.sig, c='b', **ebargs)
    
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')
    plot_fit(plt, fit.p, data, 'k:') # , color='0.8')
    return plt
Example #4
0
def main():
    x, y = make_data()
    prior = make_prior()
    fit = lsqfit.nonlinear_fit(prior=prior, data=(x, y), fcn=fcn)
    print(fit)
    print('p1/p0 =', fit.p[1] / fit.p[0], 'p3/p2 =', fit.p[3] / fit.p[2])
    print('corr(p0,p1) = {:.4f}'.format(gv.evalcorr(fit.p[:2])[1, 0]))
Example #5
0
def do_fit_BsEtas(Fits,f,Nijk,Npow,addrho,svdnoise,priornoise,prior,fpf0same):
    # have to define function in here so it only takes p as an argument (I think)
    ###############################
    def fcn(p):
        models = gv.BufferDict()
        if 'f0_qsq{0}'.format(qsqmaxphys) in f:
            models['f0_qsq{0}'.format(qsqmaxphys)] = make_f0_BsEtas(Nijk,Npow,addrho,p,Fits[0],0,p['qsq_qsq{0}'.format(qsqmaxphys)],p['z_qsq{0}'.format(qsqmaxphys)],Fits[0]['masses'][0],fpf0same,0,newdata=True)
        if 'fp_qsq{0}'.format(qsqmaxphys) in f:
            models['fp_qsq{0}'.format(qsqmaxphys)] = make_fp_BsEtas(Nijk,Npow,addrho,p,Fits[0],0,p['qsq_qsq{0}'.format(qsqmaxphys)],p['z_qsq{0}'.format(qsqmaxphys)],Fits[0]['masses'][0],fpf0same,0,newdata=True)
        if 'f0_qsq{0}'.format(0) in f:
            models['f0_qsq{0}'.format(0)] = make_f0_BsEtas(Nijk,Npow,addrho,p,Fits[0],0,0,p['z_qsq{0}'.format(0)],Fits[0]['masses'][0],fpf0same,0,newdata=True)
        for Fit in Fits:
            for mass in Fit['masses']:
                for twist in Fit['twists']:
                    tag = '{0}_m{1}_tw{2}'.format(Fit['conf'],mass,twist)
                    #tag2 = '{0}_m{1}_tw{2}'.format(Fit['conf'],Fit['masses'][0],Fit['twists'][0])
                    #print(gv.evalcorr([f['f0_{0}'.format(tag)],f['f0_{0}'.format(tag2)]])[0][1])
                    if 'f0_{0}'.format(tag) in f:
                        models['f0_{0}'.format(tag)] = make_f0_BsEtas(Nijk,Npow,addrho,p,Fit,Fit['a'].mean,p['qsq_{0}'.format(tag)],p['z_{0}'.format(tag)],mass,fpf0same,float(mass)) #second mass is amh
                    if 'fp_{0}'.format(tag) in f:
                        models['fp_{0}'.format(tag)] = make_fp_BsEtas(Nijk,Npow,addrho,p,Fit,Fit['a'].mean,p['qsq_{0}'.format(tag)],p['z_{0}'.format(tag)],mass,fpf0same,float(mass)) #second mass is amh
                    
                        
        return(models)
    #################################
    
    p0 = None
    #if os.path.isfile('Fits/pmean{0}{1}{2}.pickle'.format(addrho,Npow,Nijk)):
    #    p0 = gv.load('Fits/pmean{0}{1}{2}.pickle'.format(addrho,Npow,Nijk))
    fit = lsqfit.nonlinear_fit(data=f, prior=prior, p0=p0, fcn=fcn, svdcut=1e-5 ,add_svdnoise=svdnoise, add_priornoise=priornoise, maxit=500, tol=(1e-8,0.0,0.0),fitter='gsl_multifit', alg='subspace2D', solver='cholesky' ,debug=False)
    gv.dump(fit.pmean,'Fits/pmean{0}{1}{2}.pickle'.format(addrho,Npow,Nijk))
    print(fit.format(maxline=True))
    return(fit.p)
Example #6
0
def do_2st_3t_fit_combine2_jack(ratio1_,
                                ratio2_,
                                t2_,
                                ncut1_=1,
                                ncut2_=0,
                                dm_=gv.gvar(0.4, np.inf),
                                show=True):
    nconf_ = ratio1_.coords['jackknife'].size
    t_ = ratio1_.coords['t'].values
    data1_ = ratio1_.sel(t2=t2_).squeeze().transpose(
        'jackknife', 't2', 't').values.reshape(nconf_, t2_.size * t_.size)
    data2_ = ratio2_.sel(t2=t2_).squeeze().transpose(
        'jackknife', 't2', 't').values.reshape(nconf_, t2_.size * t_.size)
    data1_fit = prep_data_ratio_new_jack(data1_, ncut1_, ncut2_, t2_, t_)
    data2_fit = prep_data_ratio_new_jack(data2_, ncut1_, ncut2_, t2_, t_)
    x_temp = {}
    x_temp['1'] = data1_fit[0]
    x_temp['2'] = data2_fit[0]
    data_temp = {}
    data_temp['1'] = data1_fit[1]
    data_temp['2'] = data2_fit[1]
    data_fit = (x_temp, data_temp)

    prior0_2st_3t_combine2['dm'] = dm_
    fit_ = lsqfit.nonlinear_fit(data=data_fit,
                                prior=prior0_2st_3t_combine2,
                                fcn=fcn_2st_3t_combine2,
                                debug=True)
    if show:
        print_fit(fit_)
        print(fit_.p['c0_2'] - fit_.p['c0_1'])
    return fit_
Example #7
0
def main():
    gv.ranseed([2009,2010,2011,2012,2013]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    sys_stdout = sys.stdout
    for nexp in range(3,6):
        prior = make_prior(nexp,x)
        fit = lsqfit.nonlinear_fit(data=y,fcn=f,prior=prior,p0=p0) # ,svdcut=SVDCUT)
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
        fit.check_roundoff()
        if nexp == 4:
            sys.stdout = tee.tee(sys.stdout,open("eg2.out","w"))
        print '************************************* nexp =',nexp
        print fit                   # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        sys.stdout = sys_stdout
        print

    #
    if DO_BOOTSTRAP:
        Nbs = 10                                     # number of bootstrap copies
        outputs = {'E1/E0':[], 'E2/E0':[], 'a1/a0':[],'a2/a0':[],'E1':[],'a1':[]}   # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean['E']                     # best-fit parameters
            a = bsfit.pmean['a']
            outputs['E1/E0'].append(E[1]/E[0])       # accumulate results
            outputs['E2/E0'].append(E[2]/E[0])
            outputs['a1/a0'].append(a[1]/a[0])
            outputs['a2/a0'].append(a[2]/a[0])
            outputs['E1'].append(E[1])
            outputs['a1'].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit.chi2/bsfit.dof

        # extract means and standard deviations from the bootstrap output
        for k in outputs:
            outputs[k] = gv.gvar(np.mean(outputs[k]),np.std(outputs[k]))
        print 'Bootstrap results:'
        print 'E1/E0 =',outputs['E1/E0'],'  E2/E1 =',outputs['E2/E0']
        print 'a1/a0 =',outputs['a1/a0'],'  a2/a0 =',outputs['a2/a0']
        print 'E1 =',outputs['E1'],'  a1 =',outputs['a1']

    if DO_PLOT:
        print fit.format(100)                   # print the fit results
        import pylab as pp
        from gvar import mean,sdev
        fity = f(x,fit.pmean)
        ratio = y/fity
        pp.xlim(0,21)
        pp.xlabel('x')
        pp.ylabel('y/f(x,p)')
        pp.errorbar(x=gv.mean(x),y=gv.mean(ratio),yerr=gv.sdev(ratio),fmt='ob')
        pp.plot([0.0,21.0],[1.0,1.0])
        pp.show()
Example #8
0
def bad_analysis():
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1a.out', 'w'))
	x, y = make_data()
	p0 = np.ones(5.)              # starting value for chi**2 minimization
	fit = lsqfit.nonlinear_fit(data=(x, y), p0=p0, fcn=f)
	print fit.format(maxline=True)
	make_plot(x, y, fit)
	return fit
Example #9
0
def bad_analysis():
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1a.out', 'w'))
    x, y = make_data()
    p0 = np.ones(5.)  # starting value for chi**2 minimization
    fit = lsqfit.nonlinear_fit(data=(x, y), p0=p0, fcn=f)
    print fit.format(maxline=True)
    make_plot(x, y, fit, name='eg-appendix1a')
    return fit
Example #10
0
def fitscript_v2(trange,T,data,priors,fcn,init=None,basak=None):
    sets = len(data)/T
    #print "sets:", sets
    pmean = []
    psdev = []
    post = []
    p0 = []
    prior = []
    tmintbl = []
    tmaxtbl = []
    chi2 = []
    dof = []
    Q = []
    lgbftbl = []
    rawoutput = []
    for tmin in range(trange['tmin'][0], trange['tmin'][1]+1):
        for tmax in range(trange['tmax'][0], trange['tmax'][1]+1):
            x = x_indep(tmin, tmax)
            xlist, y = y_dep(x, data, sets)
            if basak is not None:
                x = {'indep': x, 'basak': basak}
            else: pass
            fit = lsqfit.nonlinear_fit(data=(x,y),prior=priors,fcn=fcn,p0=init,maxit=1000000) #,svdcut=1E-3)
            pmean.append(fit.pmean)
            psdev.append(fit.psdev)
            post.append(fit.p)
            p0.append(fit.p0)
            prior.append(fit.prior)
            tmintbl.append(tmin)
            tmaxtbl.append(tmax)
            chi2.append(fit.chi2)
            dof.append(fit.dof)
            lgbftbl.append(fit.logGBF)
            Q.append(fit.Q)
            rawoutput.append(fit)
    #fcnname = str(fcn.__name__)
    #fitline = fcn(x,fit.p)
    #print "%s_%s_t, %s_%s_y, +-, %s_%s_fit, +-" %(fcnname, basak[0], fcnname, basak[0], fcnname, basak[0])
    #for i in range(len(xlist)):
    #    print xlist[i], ',', y[i].mean, ',', y[i].sdev, ',', fitline[i].mean, ',', fitline[i].sdev
    #print '======'
    #print fcn.__name__, basak
    #print fit
    #print '======'
    fittbl = dict()
    fittbl['tmin'] = tmintbl
    fittbl['tmax'] = tmaxtbl
    fittbl['pmean'] = pmean
    fittbl['psdev'] = psdev
    fittbl['post'] = post
    fittbl['p0'] = p0
    fittbl['prior'] = prior
    fittbl['chi2'] = chi2
    fittbl['dof'] = dof
    fittbl['logGBF'] = lgbftbl
    fittbl['Q'] = Q
    fittbl['rawoutput'] = rawoutput
    return fittbl
Example #11
0
def main():
    gv.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    for nexp in range(3,8):
        print('************************************* nexp =',nexp)
        prior = make_prior(nexp)
        # eps = gv.gvar(1,1e-300)   # use svdcut to make it independent
        # prior['a'] *= eps
        # y *= eps
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior, 
                                   p0=p0,svdcut=SVDCUT)
        print(fit)                  # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print('E1/E0 =',(E[1]/E[0]).fmt(),'  E2/E0 =',(E[2]/E[0]).fmt())
        print('a1/a0 =',(a[1]/a[0]).fmt(),'  a2/a0 =',(a[2]/a[0]).fmt())
        print()
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
    
    if DO_BOOTSTRAP:
        Nbs = 10                                     # number of bootstrap copies
            
        outputs = {'E1/E0':[], 'E2/E0':[], 'a1/a0':[],'a2/a0':[],'E1':[],'a1':[]}   # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean['E']                     # best-fit parameters
            a = bsfit.pmean['a']
            outputs['E1/E0'].append(E[1]/E[0])       # accumulate results
            outputs['E2/E0'].append(E[2]/E[0])
            outputs['a1/a0'].append(a[1]/a[0])
            outputs['a2/a0'].append(a[2]/a[0])
            outputs['E1'].append(E[1])
            outputs['a1'].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit.chi2/bsfit.dof

        # extract means and standard deviations from the bootstrap output
        for k in outputs:
            outputs[k] = gv.dataset.avg_data(outputs[k],bstrap=True).fmt(3)
                                 # gv.gvar(np.mean(outputs[k]),
                                 # np.std(outputs[k])).fmt(3)
        print('Bootstrap results:')
        print('E1/E0 =',outputs['E1/E0'],'  E2/E0 =',outputs['E2/E0'])
        print('a1/a0 =',outputs['a1/a0'],'  a2/a0 =',outputs['a2/a0'])
        print('E1 =',outputs['E1'],'  a1 =',outputs['a1'])
        
    if DO_PLOT:
        print(fit.format(100))                   # print the fit results
        import pylab as plt   
        ratio = y/f(x,fit.pmean)
        plt.xlim(0,21)
        plt.xlabel('x')
        plt.ylabel('y/f(x,p)')
        plt.errorbar(x=x,y=gv.mean(ratio),yerr=gv.sdev(ratio),fmt='ob')
        plt.plot([0.0,21.0],[1.0,1.0])
        plt.show()
Example #12
0
def fit_data(data):
    nstates = 2
    x = np.arange(5, 10)
    y = data[x]
    p = priors(nstates)
    fitc = fit_functions(T=len(data) + 1, nstates=nstates)
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=p, fcn=fitc.twopt)
    print fit
    return fit
Example #13
0
 def __call__(self, nstates, times, **fitter_kwargs):
     """ Run the fit. """
     x, y = self.build_xy(times.tmin_src, times.tmin_snk, times.t_step)
     prior = self.build_prior(nstates.n, nstates.m)
     if self.correlated:
         fit = lsqfit.nonlinear_fit(data=(x, y),
                                    fcn=ratio_model,
                                    prior=prior,
                                    **fitter_kwargs)
     else:
         fit = lsqfit.nonlinear_fit(udata=(x, y),
                                    fcn=ratio_model,
                                    prior=prior,
                                    **fitter_kwargs)
     fit = serialize.SerializableRatioAnalysis(fit, nstates, times,
                                               self.ds.m_src, self.ds.m_snk)
     self._fit = fit
     return fit
Example #14
0
def prior_analysis():
	x, y = make_data()
	# loose prior
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1d.out', 'w'))
	prior = gv.gvar(91 * ['0(3)'])   # prior for the fit
	fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
	print fit.format(maxline=True)
	# really loose prior
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1h.out', 'w'))
	prior = gv.gvar(91 * ['0(20)'])   # prior for the fit
	fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
	print fit.format(maxline=True)
	make_plot(x, y, fit, xmax=0.96)
	# tight prior
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1e.out', 'w'))
	prior = gv.gvar(91 * ['0.0(3)'])   # prior for the fit
	fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
	print fit.format(maxline=True)
Example #15
0
def prior_analysis():
    x, y = make_data()
    # loose prior
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1d.out', 'w'))
    prior = gv.gvar(91 * ['0(3)'])  # prior for the fit
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
    print fit.format(maxline=True)
    # really loose prior
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1h.out', 'w'))
    prior = gv.gvar(91 * ['0(20)'])  # prior for the fit
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
    print fit.format(maxline=True)
    make_plot(x, y, fit, xmax=0.96, name='eg-appendix1d')
    # tight prior
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1e.out', 'w'))
    prior = gv.gvar(91 * ['0.0(3)'])  # prior for the fit
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
    print fit.format(maxline=True)
Example #16
0
def main():
    gv.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    for nexp in range(3,8):
        print('************************************* nexp =',nexp)
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior,p0=p0,svdcut=SVDCUT)
        print(fit)                  # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print('E1/E0 =',(E[1]/E[0]).fmt(),'  E2/E0 =',(E[2]/E[0]).fmt())
        print('a1/a0 =',(a[1]/a[0]).fmt(),'  a2/a0 =',(a[2]/a[0]).fmt())
        print()
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
    
    if DO_ERRORBUDGET:
        outputs = OrderedDict([
            ('E1/E0', E[1]/E[0]), ('E2/E0', E[2]/E[0]),         
            ('a1/a0', a[1]/a[0]), ('a2/a0', a[2]/a[0])
            ])
        inputs = OrderedDict([
            ('E', fit.prior['E']), ('a', fit.prior['a']),
            ('y', y), ('svd', fit.svdcorrection)
            ])
        print(fit.fmt_values(outputs))
        print(fit.fmt_errorbudget(outputs,inputs))
        
    if DO_EMPBAYES:
        def fitargs(z,nexp=nexp,prior=prior,f=f,data=(x,y),p0=p0):
            z = gv.exp(z)
            prior['a'] = [gv.gvar(0.5,0.5*z[0]) for i in range(nexp)]
            return dict(prior=prior,data=data,fcn=f,p0=p0)
        ##
        z0 = [0.0]
        fit,z = lsqfit.empbayes_fit(z0,fitargs,tol=1e-3)
        print(fit)                  # print the optimized fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print('E1/E0 =',(E[1]/E[0]).fmt(),'  E2/E0 =',(E[2]/E[0]).fmt())
        print('a1/a0 =',(a[1]/a[0]).fmt(),'  a2/a0 =',(a[2]/a[0]).fmt())
        print("prior['a'] =",fit.prior['a'][0].fmt())
        print()
    
    if DO_PLOT:
        import pylab as pp   
        from gvar import mean,sdev     
        fity = f(x,fit.pmean)
        ratio = y/fity
        pp.xlim(0,21)
        pp.xlabel('x')
        pp.ylabel('y/f(x,p)')
        pp.errorbar(x=x,y=mean(ratio),yerr=sdev(ratio),fmt='ob')
        pp.plot([0.0,21.0],[1.0,1.0])
        pp.show()
def Analyse_NM():
    def f_pot_NM(x, p):
        xt = (x - p['n_sat']) / (3 * p['n_sat'])

        v0 = p['E_sat+E_sym'] - T_SM(
            p['n_sat']) - T_SM(p['n_sat']) * (2**(2 / 3) - 1)
        v1 = p['L_sym'] - 2 * T_SM(p['n_sat']) - 2 * T_SM(
            p['n_sat']) * (2**(2 / 3) - 1)
        v2 = p['K_sat+K_sym'] + 2 * T_SM(p['n_sat']) - 2 * T_SM(
            p['n_sat']) * (-1 * 2**(2 / 3) + 1)
        v3 = p['Q_sat+Q_sym'] - 8 * T_SM(p['n_sat']) - 2 * T_SM(
            p['n_sat']) * (4 * 2**(2 / 3) - 4)
        v4 = p['Z_sat+Z_sym'] + 56 * T_SM(p['n_sat']) - 8 * T_SM(
            p['n_sat']) * (-7 * 2**(2 / 3) + 7)

        ans = v0 + v1 * xt + (v2 / 2.) * xt**2 + (v3 / 6.) * xt**3 + (
            v4 / 24.) * (xt)**4
        return ans

    def f_pot_NM_c(x, p):
        xt = (x - p['n_sat']) / (3 * p['n_sat'])
        lam = f_pot_NM(0, p) * 3.**5
        beta = 3
        return f_pot_NM(x, p) + lam * xt**5 * np.exp(-42 *
                                                     (x / 0.16)**(beta / 3))

    def f_NM(x, p):
        return T_NM(x) + f_pot_NM_c(x, p)

    # prior_eNM = {}    # Drischler prior
    # prior_eNM['n_sat'] = SM3_par['n_sat']
    # prior_eNM['E_sat+E_sym'] = gv.gvar(16.85, 3.33)
    # prior_eNM['L_sym'] = gv.gvar(48.1, 3.6)
    # prior_eNM['K_sat+K_sym'] = gv.gvar(42,62)
    # prior_eNM['Q_sat+Q_sym'] = gv.gvar(-303, 338)
    # prior_eNM['Z_sat+Z_sym'] = gv.gvar(-1011, 593)

    prior_eNM = {}  # Jerome priors
    prior_eNM['n_sat'] = SM3_par['n_sat']
    prior_eNM['E_sat+E_sym'] = gv.gvar(16.0, 3.0)
    prior_eNM['L_sym'] = gv.gvar(50, 10)
    prior_eNM['K_sat+K_sym'] = gv.gvar(100, 100)
    prior_eNM['Q_sat+Q_sym'] = gv.gvar(0, 400)
    prior_eNM['Z_sat+Z_sym'] = gv.gvar(-500, 500)

    x = td
    y = te_NM_av

    fit = lsqfit.nonlinear_fit(data=(x, y),
                               prior=prior_eNM,
                               fcn=f_NM,
                               debug=True,
                               svdcut=ts_NM.svdcut)
    NM3_par = fit.p

    return f_NM, NM3_par
Example #18
0
def fit_data(data,
             fit_range,
             particles,
             Pcm,
             nstates,
             nsinks,
             cov=None,
             print_fit=True):
    if nsinks == 1:
        if print_fit:
            print "fitting with " + str(nstates) + " states and smeared data"
        else:
            pass
    else:
        if print_fit:
            print "fitting with " + str(
                nstates) + " states and point and smeared data"
        else:
            pass
    x = fit_range
    y = data
    if cov is None:
        cov_matrix = gv.evalcov(y)
        #cov_matrix = gv.evalcov(y)/len(y)
        #cov_matrix = np.identity(len(x))
        if print_fit:
            print np.shape(cov_matrix)
        else:
            pass
    else:
        cov_matrix = cov
    y = gv.mean(y)
    #you'll need to update the pipi function to deal with number of sinks as well as nstates
    if particles == "pipi":
        p = pipi_priors(nstates, nsinks, Pcm)
        #fitc = pipi_fit_function(nstates=nstates,T=len(data))
        if nsinks == 1:
            fitc = pipi_fit_function(nstates=nstates, T=48, sinks=["s"])
        else:
            fitc = pipi_fit_function(nstates=nstates, T=48, sinks=["s", "p"])
    elif particles == "pion":
        p = pion_priors(nstates, nsinks, Pcm)
        p0 = pion_priors(nstates, nsinks, Pcm)
        if nsinks == 1:
            fitc = pion_fit_function(nstates=nstates, T=48, sinks=["s"])
        else:
            fitc = pion_fit_function(nstates=nstates, T=48, sinks=["s", "p"])
    #fit = lsqfit.nonlinear_fit(data=(x,y,cov_matrix),prior=p0,fcn=fitc.full_func)
    fit = lsqfit.nonlinear_fit(data=(x, y, cov_matrix),
                               prior=p,
                               fcn=fitc.full_func)
    if print_fit:
        print fit
    else:
        pass
    return fit
Example #19
0
def main():
    p0 = [0.5, 0.4, 0.7]

    N = 50000  # takes 2min to do 2000000; scales linearly
    x = np.linspace(0.2, 2., N)
    y = make_fake_data(x, p0, f)
    print('y = [{} {} ... {}]\n'.format(y[0], y[1], y[-1]))
    prior = gv.gvar(['0(1)', '0(1)', '0(1)'])
    fit = lsqfit.nonlinear_fit(udata=(x, y), prior=prior, fcn=f)
    print(fit)
Example #20
0
def xval_wrapper(pen, win, parameter_file_path, splines, spline_power,
                 data_file_path, data_error_file_path, k):
    try:
        parameters = read_parameters(parameter_file_path)
        data = read_data(data_file_path)
        tr = data[:win]
        val = data[win:(win + 7)]

        mi = SEIRModel(fit_columns=["hospital_census", "vent_census"],
                       update_parameters=flexible_beta)
        xx, pp = prepare_model_parameters(parameters=parameters,
                                          data=tr,
                                          beta_fun='flexible_beta',
                                          splines=splines,
                                          spline_power=spline_power)
        pp['beta_splines'] = gvar([0 for i in range(k)],
                                  [pen for i in range(k)])
        mi.fit_start_date = xx["day0"]
        xx["error_infos"] = (read_csv(data_error_file_path).set_index("param")
                             ["value"].to_dict())
        fit = nonlinear_fit(
            data=(xx, get_yy(tr, **xx["error_infos"])),
            prior=pp,
            fcn=mi.fit_fcn,
            debug=False,
        )
        # detect and handle degenerate fits
        # THIS IS A TEMPORARY HACK
        splinecoefvec = np.array([
            fit.p['beta_splines'][i].mean
            for i in range(len(fit.p['beta_splines']))
        ])
        cv = np.std(splinecoefvec) / np.mean(splinecoefvec)
        if cv < .1:
            MSE = -9999
            error = "degenerate fit"
        else:
            xx = fit.x.copy()
            xx["dates"] = xx["dates"].union(
                date_range(xx["dates"].max(), freq="D", periods=8))
            prediction_df = mi.propagate_uncertainties(xx, fit.p)
            prediction_df.index = prediction_df.index.round("H")
            mg = val.merge(prediction_df, left_index=True, right_index=True)
            # scaling
            hosp = (mg.hosp - np.mean(mg.hosp)) / np.std(mg.hosp)
            hosp_hat = (mg.hospital_census.apply(lambda x: x.mean) -
                        np.mean(mg.hosp)) / np.std(mg.hosp)
            vent = (mg.vent - np.mean(mg.vent)) / np.std(mg.vent)
            vent_hat = (mg.vent_census.apply(lambda x: x.mean) -
                        np.mean(mg.vent)) / np.std(mg.vent)
            MSE = mse(hosp, hosp_hat) + mse(vent, vent_hat)
            error = ""
        return dict(mse=MSE, pen=pen, win=win, error=error)
    except Exception as e:
        return dict(mse=-9999, pen=pen, win=win, error=e)
Example #21
0
def jackknife_fitting_prior(data_, func_, prior0_, ncut1, ncut2, t1, t2):
    jk_res = []
    for ij in range(data_.shape[0]):
        data_new_ = np.delete(data_, ij, 0)
        data_fit = prep_data_ratio(data_new_, ncut1, ncut2, t1, t2)
        fit = lsqfit.nonlinear_fit(data=data_fit,
                                   prior=prior0_,
                                   fcn=func_,
                                   debug=True)
        jk_res.append(fit.p)
    return jk_res
Example #22
0
 def minfcn(z, save=save):
     args = fitargs(z)
     if save['lastp0'] is not None:
         args['p0'] = save['lastp0']
     fit = lsqfit.nonlinear_fit(**args)
     if numpy.isnan(fit.logGBF):
         raise ValueError
     else:
         save['lastz'] = z 
         save['lastp0'] = fit.pmean
     return -fit.logGBF
Example #23
0
def bootstrap_fitting(nbs_, data_, func_, p0_, ncut1, ncut2, t1, t2):
    bs_res = []
    for ib in range(nbs_):
        xb_ = get_boot_sample(np.arange(0, data_.shape[0], 1))
        data_new_ = data_[xb_, ...]
        data_fit = prep_data_ratio(data_new_, ncut1, ncut2, t1, t2)
        fit = lsqfit.nonlinear_fit(data=data_fit,
                                   p0=p0_,
                                   fcn=func_,
                                   debug=True)
        bs_res.append(fit.p)
    return bs_res
Example #24
0
def f_perform_fit(all_dta,
                  x,
                  y,
                  par,
                  f_func,
                  verbose=True,
                  concise=False,
                  plot=True,
                  full_data=False,
                  error_band=False,
                  semilog=False):
    '''
    Function wrapper to perform correlator fit for mesons.
    Reads dictionary dta with x and y data.
    Performs both cosh and sinh fits.
    '''

    # Performing meson fit

    p0 = f_make_p0(par)
    fit = lsqfit.nonlinear_fit(data=(x, y['y']),
                               fcn=f_func,
                               p0=p0,
                               extend=True,
                               svdcut=1e-8)

    # Print the fit results

    if concise:
        pass
    else:
        print "*****************************", '\n'
        if verbose:
            print f_func.__doc__.strip('\n').strip(
                '  ')  # Prints the functional form of fit function.
            print(fit.format(maxline=True)), "\n\n"

        else:
            print fit, "\n\n"

    # Plot fit
    if plot:
        f_fit_plot(x,
                   y,
                   all_dta,
                   fit,
                   full_data=full_data,
                   error_band=error_band,
                   semilog=semilog)

    return fit
Example #25
0
def main():
    param, data = collect_data('spline.p')
    F, prior = make_fcn_prior(param)
    fit = lsqfit.nonlinear_fit(data=data, prior=prior, fcn=F)
    print(fit)
    # create f(m)
    f = gv.cspline.CSpline(fit.p['mknot'], fit.p['fknot'])
    # create error budget
    outputs = {'f(1)':f(1), 'f(5)':f(5), 'f(9)':f(9)}
    inputs = {'data':data}
    inputs.update(prior)
    print(gv.fmt_values(outputs))
    print(gv.fmt_errorbudget(outputs=outputs, inputs=inputs))
    make_plot(param, data, fit)
Example #26
0
def main():
    x, y = make_data()
    prior = make_prior(100)  # 100 exponential terms in all
    p0 = None
    for nexp in range(1, 6):
        # marginalize the last 100 - nexp terms (in ymod_prior)
        fit_prior = gv.BufferDict()  # part of prior used in fit
        ymod_prior = gv.BufferDict()  # part of prior absorbed in ymod
        for k in prior:
            fit_prior[k] = prior[k][:nexp]
            ymod_prior[k] = prior[k][nexp:]
        ymod = y - fcn(x, ymod_prior)  # remove temrs in ymod_prior

        # fit modified data with just nexp terms (in fit_prior)
        fit = lsqfit.nonlinear_fit(
            data=(x, ymod),
            prior=fit_prior,
            fcn=fcn,
            p0=p0,
            tol=1e-15,
            svdcut=1e-4,
        )

        # print fit information
        print('************************************* nexp =', nexp)
        print(fit.format(True))
        p0 = fit.pmean

    # print summary information and error budget
    E = fit.p['E']  # best-fit parameters
    a = fit.p['a']
    # outputs = {
    #     'E1/E0':E[1] / E[0], 'E2/E0':E[2] / E[0],
    #     'a1/a0':a[1] / a[0], 'a2/a0':a[2] / a[0]
    #     }
    # inputs = {
    #     'E prior':prior['E'], 'a prior':prior['a'],
    #     'svd cut':fit.svdcorrection,
    #     }
    outputs = gv.BufferDict()
    outputs['E2/E0'] = E[2] / E[0]
    outputs['E1/E0'] = E[1] / E[0]
    outputs['a2/a0'] = a[2] / a[0]
    outputs['a1/a0'] = a[1] / a[0]
    inputs = gv.BufferDict()
    inputs['E prior'] = prior['E']
    inputs['svd cut'] = fit.svdcorrection
    inputs['a prior'] = prior['a']
    print(fit.fmt_values(outputs))
    print(fit.fmt_errorbudget(outputs, inputs))
def Analyse_SM():
    def f_pot_SM(x, p):
        xt = (x - p['n_sat']) / (3 * p['n_sat'])

        v0 = p['E_sat'] - T_SM(p['n_sat'])
        v1 = -2 * T_SM(p['n_sat'])
        v2 = p['K_sat'] + 2 * T_SM(p['n_sat'])
        v3 = p['Q_sat'] - 8 * T_SM(p['n_sat'])
        v4 = p['Z_sat'] + 56 * T_SM(p['n_sat'])

        ans = v0 + v1 * xt + (v2 / 2.) * xt**2 + (v3 / 6.) * xt**3 + (
            v4 / 24.) * (xt)**4
        return ans

    def f_pot_SM_c(x, p):
        xt = (x - p['n_sat']) / (3 * p['n_sat'])
        lam = f_pot_SM(0, p) * 3.**5
        beta = 3
        return f_pot_SM(x, p) + lam * xt**5 * np.exp(-17 *
                                                     (x / 0.16)**(beta / 3))

    def f_SM(x, p):
        return T_SM(x) + f_pot_SM_c(x, p)

    # prior_e_SM = {}          # Drischler prior
    # prior_e_SM['n_sat'] = gv.gvar(0.171, 0.016)
    # prior_e_SM['E_sat'] = gv.gvar(-15.16, 1.24)
    # prior_e_SM['K_sat'] = gv.gvar(214, 22)
    # prior_e_SM['Q_sat'] = gv.gvar(-139, 104)
    # prior_e_SM['Z_sat'] = gv.gvar(1306, 214)

    prior_e_SM = {}  # Jerome priors
    prior_e_SM['n_sat'] = gv.gvar(0.16, 0.01)
    prior_e_SM['E_sat'] = gv.gvar(-15.5, 1.0)
    prior_e_SM['K_sat'] = gv.gvar(230, 20)
    prior_e_SM['Q_sat'] = gv.gvar(-300, 400)
    prior_e_SM['Z_sat'] = gv.gvar(1300, 500)

    x = td
    y = te_SM_av

    fit = lsqfit.nonlinear_fit(data=(x, y),
                               prior=prior_e_SM,
                               fcn=f_SM,
                               debug=True,
                               svdcut=ts_SM.svdcut)
    SM3_par = fit.p

    return f_SM, SM3_par
Example #28
0
def marginalized_analysis():
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1c.out', 'w'))
	x, y = make_data()
	prior = gv.gvar(91 * ['0(1)'])   # prior for the fit
	ymod = y - (f(x, prior) - f(x, prior[:1]))
	priormod = prior[:1]
	fit = lsqfit.nonlinear_fit(data=(x, ymod), prior=priormod, fcn=f)
	print fit.format(maxline=True)
	sys.stdout = STDOUT
	print lsqfit.wavg(list(ymod) + list(priormod))
	make_plot(x, ymod, fit, 'ymod(x)')
	inputs = dict(prior=prior, y0=y[0], y1=y[1], y2=y[2], y3=y[3], y4=y[4])
	outputs = dict(p0=fit.p[0])
	print gv.fmt_errorbudget(inputs=inputs, outputs=outputs)
	return fit
Example #29
0
def marginalized_analysis():
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1c.out', 'w'))
    x, y = make_data()
    prior = gv.gvar(91 * ['0(1)'])  # prior for the fit
    ymod = y - (f(x, prior) - f(x, prior[:1]))
    priormod = prior[:1]
    fit = lsqfit.nonlinear_fit(data=(x, ymod), prior=priormod, fcn=f)
    print fit.format(maxline=True)
    sys.stdout = STDOUT
    print lsqfit.wavg(list(ymod) + list(priormod))
    make_plot(x, ymod, fit, 'ymod(x)', name='eg-appendix1c')
    inputs = dict(prior=prior, y0=y[0], y1=y[1], y2=y[2], y3=y[3], y4=y[4])
    outputs = dict(p0=fit.p[0])
    print gv.fmt_errorbudget(inputs=inputs, outputs=outputs)
    return fit
Example #30
0
def good_analysis():
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1b.out', 'w'))
	x, y = make_data()
	prior = gv.gvar(N * ['0(1)'])   # prior for the fit
	fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
	print fit.format(maxline=True)
	make_plot(x, y, fit)
	inputs = gv.BufferDict(prior=prior)
	for xi, yi in zip(x, y):
		inputs['y(%.2f)' % xi] = yi
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1g.out', 'w'))
	inputs = dict(prior=prior, y=y)
	outputs = dict(p0=fit.p[0])
	print gv.fmt_errorbudget(inputs=inputs, outputs=outputs)
	return fit
Example #31
0
def good_analysis(plot=MAKE_PLOTS):
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1b.out', 'w'))
    x, y = make_data()
    prior = gv.gvar(N * ['0(1)'])  # prior for the fit
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f)
    print fit.format(maxline=True)
    if plot:
        make_plot(x, y, fit, name='eg-appendix1b')
    inputs = gv.BufferDict(prior=prior)
    for xi, yi in zip(x, y):
        inputs['y(%.2f)' % xi] = yi
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1g.out', 'w'))
    inputs = dict(prior=prior, y=y)
    outputs = dict(p0=fit.p[0])
    print gv.fmt_errorbudget(inputs=inputs, outputs=outputs)
    return fit
Example #32
0
def main():
    # pendulum data exhibits experimental error in ability to measure theta
    t = gv.gvar([ 
        '0.10(1)', '0.20(1)', '0.30(1)', '0.40(1)',  '0.50(1)', 
        '0.60(1)',  '0.70(1)',  '0.80(1)',  '0.90(1)', '1.00(1)'
        ])
    theta = gv.gvar([
        '1.477(79)', '0.791(79)', '-0.046(79)', '-0.852(79)', 
        '-1.523(79)', '-1.647(79)', '-1.216(79)', '-0.810(79)', 
        '0.185(79)', '0.832(79)'
        ])

    for t_n, theta_n in zip(t, theta):
        print("{}  {:>10}".format(t_n.fmt(2), theta_n.fmt(3)))
    # prior: assume experimental error in ability to specify theta(0)
    prior = gv.BufferDict()
    prior['g/l'] = gv.gvar('40(20)')
    prior['theta(0)'] = gv.gvar('1.571(50)')
    prior['t'] = t

    # fit function: use class Pendulum object to integrate pendulum motion
    def fitfcn(p, t=None):
        if t is None:
            t = p['t']
        pendulum = Pendulum(p['g/l'])
        return pendulum(p['theta(0)'], t)

    # do the fit and print results
    fit = lsqfit.nonlinear_fit(data=theta, prior=prior, fcn=fitfcn)
    sys.stdout = tee.tee(STDOUT, open('case-pendulum.out', 'w'))
    print(fit.format(maxline=True))
    sys.stdout = STDOUT
    print('fit/exact for (g/l) =', fit.p['g/l'] / (2*np.pi) ** 2)
    print('fit/exact for theta(0) =', fit.p['theta(0)'] / (np.pi / 2.))
    
    if MAKE_PLOT:
        # make figure (saved to file pendulum.pdf)
        plt.figure(figsize=(4,3))
        # start plot with data
        plt.errorbar(
            x=gv.mean(t), xerr=gv.sdev(t), y=gv.mean(theta), yerr=gv.sdev(theta),
            fmt='k.',
            )
        # use best-fit function to add smooth curve for 100 points
        t = np.linspace(0., 1.1, 100)
        th = fitfcn(fit.p, t)
        show_plot(t, th)
Example #33
0
def main():
    # pendulum data exhibits experimental error in ability to measure theta
    t = gv.gvar([
        '0.10(1)', '0.20(1)', '0.30(1)', '0.40(1)',  '0.50(1)',
        '0.60(1)',  '0.70(1)',  '0.80(1)',  '0.90(1)', '1.00(1)'
        ])
    theta = gv.gvar([
        '1.477(79)', '0.791(79)', '-0.046(79)', '-0.852(79)',
        '-1.523(79)', '-1.647(79)', '-1.216(79)', '-0.810(79)',
        '0.185(79)', '0.832(79)'
        ])

    for t_n, theta_n in zip(t, theta):
        print("{}  {:>10}".format(t_n.fmt(2), theta_n.fmt(3)))
    # prior: assume experimental error in ability to specify theta(0)
    prior = gv.BufferDict()
    prior['g/l'] = gv.gvar('40(20)')
    prior['theta(0)'] = gv.gvar('1.571(50)')
    prior['t'] = t

    # fit function: use class Pendulum object to integrate pendulum motion
    def fitfcn(p, t=None):
        if t is None:
            t = p['t']
        pendulum = Pendulum(p['g/l'])
        return pendulum(p['theta(0)'], t)

    # do the fit and print results
    fit = lsqfit.nonlinear_fit(data=theta, prior=prior, fcn=fitfcn)
    sys.stdout = tee.tee(STDOUT, open('case-pendulum.out', 'w'))
    print(fit.format(maxline=True))
    sys.stdout = STDOUT
    print('fit/exact for (g/l) =', fit.p['g/l'] / (2*np.pi) ** 2)
    print('fit/exact for theta(0) =', fit.p['theta(0)'] / (np.pi / 2.))

    if MAKE_PLOT:
        # make figure (saved to file pendulum.pdf)
        plt.figure(figsize=(4,3))
        # start plot with data
        plt.errorbar(
            x=gv.mean(t), xerr=gv.sdev(t), y=gv.mean(theta), yerr=gv.sdev(theta),
            fmt='b.',
            )
        # use best-fit function to add smooth curve for 100 points
        t = np.linspace(0., 1.1, 100)
        th = fitfcn(fit.p, t)
        show_plot(t, th)
Example #34
0
def fit_data(x, y, p):
    prior = make_priors(y, p)
    corr = gv.evalcorr(prior)
    #for k1 in prior:
    #    for k2 in prior:
    #        c = np.squeeze(corr[(k1,k2)])
    #        if c not in [1.0,0.0]:
    #            print k1, k2, c
    #        else: pass
    p['fv']['mpiL'] = y['mpiL']
    p['ma']['mpiL'] = y['mmaL']
    fitc = fit_functions(fv=p['fv'], ma=p['ma'])
    fit = lsqfit.nonlinear_fit(data=(x, y['y']),
                               prior=prior,
                               fcn=fitc.fit_switch,
                               maxit=1000000)
    print fit.format('v')
    return {'fit': fit, 'prior': prior, 'fitc': fitc}
Example #35
0
def Fit_e_sym4_eta():

    e_sym4_eta_av = []

    for h in range(6):
        e_sym4_eta_av.append(esym4_eta[:, h])

    s4_eta = gv.dataset.svd_diagnosis(e_sym4_eta_av)
    e_sym4_eta_av = gv.dataset.avg_data(e_sym4_eta_av, spread=True)

    prior_esym4 = {
    }  # comes from posterior of (e_sym - e_sym2) fit subtraction
    prior_esym4['n_sat'] = gv.gvar('0.1606(74)')
    prior_esym4['E_sym4'] = gv.gvar('1.3(1.5)')
    prior_esym4['L_sym4'] = gv.gvar('0.7(5.7)')
    prior_esym4['K_sym4'] = gv.gvar('-20(57)')
    prior_esym4['Q_sym4'] = gv.gvar('107(432)')
    prior_esym4['Z_sym4'] = gv.gvar('101(1058)')

    def f_esym4(x, p):
        xt = (x - p['n_sat']) / (3 * p['n_sat'])

        ans = p['E_sym4'] + (p['K_sym4']/2)*xt**2 \
            + (p['Q_sym4']/6)*xt**3 + (p['Z_sym4']/24)*(xt)**4 \
            + p['L_sym4']*xt
        return ans

    # def f_esym4_c(x,p):
    #     xt = (x-p['n_sat'])/(3*p['n_sat'])
    #     b = 6.93
    #     lam = f_esym4(0,p) * 3.**5
    #     return f_esym4(x,p)  + lam * xt**5  * np.exp(-b*x/0.16)

    x = td
    y = e_sym4_eta_av
    fit = lsqfit.nonlinear_fit(data=(x, y),
                               prior=prior_esym4,
                               fcn=f_esym4,
                               debug=True,
                               svdcut=0.1)

    e_sym4_eta_par = fit.p

    return e_sym4_eta_par
Example #36
0
def fitscript_v2(trange, T, data, priors, fcn, result_flag='off'):
    if trange['tmax'][1] > T/2:
        sets = len(data)/T
    else:
        sets = len(data)/(T/2)
    posterior = []
    tmintbl = []
    tmaxtbl = []
    chi2tbl = []
    lgbftbl = []
    lgpostd = []
    for tmin in range(trange['tmin'][0], trange['tmin'][1]+1):
        for tmax in range(trange['tmax'][0], trange['tmax'][1]+1):
            x = x_indep(tmin, tmax)
            y = y_dep(x, data, sets)
            fit = lsqfit.nonlinear_fit(data=(x,y), prior=priors, fcn=fcn)
            if result_flag=='on':
                print tmin, tmax,
                print fit
            posterior.append(fit.p)
            tmintbl.append(tmin)
            tmaxtbl.append(tmax)
            chi2tbl.append(fit.chi2/fit.dof)
            lgbftbl.append(fit.logGBF)
            # log posterior probability
            # factor of log 2pi**k/2
            pifactor = ((tmax-tmin+1)/2.0) * np.log(2*np.pi)
            # log det data covariance
            datacov = gv.evalcov(y) # data covariance
            L = np.linalg.cholesky(datacov) # cholesky decomposition
            #logdetA = 2.*np.trace(np.log(L))
            logsqrtdetA = np.trace(np.log(L))
            chi2factor = -0.5*fit.chi2
            lgpostd.append(-pifactor-0.5*fit.chi2)
    fittbl = dict()
    fittbl['tmin'] = tmintbl
    fittbl['tmax'] = tmaxtbl
    fittbl['post'] = posterior
    fittbl['chi2dof'] = chi2tbl
    fittbl['logGBF'] = lgbftbl
    fittbl['logposterior'] = lgpostd
    fittbl['rawoutput'] = fit
    return fittbl
Example #37
0
def empbayes_fit(z0, fitargs, **minargs): 
    """ Call ``lsqfit.nonlinear_fit(**fitargs(z))`` varying ``z``,
    starting at ``z0``, to maximize ``logGBF`` (empirical Bayes procedure).
        
    The fit is redone for each value of ``z`` that is tried, in order
    to determine ``logGBF``.
        
    :param z0: Starting point for search.
    :type z0: array
    :param fitargs: Function of array ``z`` that determines which fit 
        parameters to use. The function returns these as an argument
        dictionary for :func:`lsqfit.nonlinear_fit`.
    :type fitargs: function
    :param minargs: Optional argument dictionary, passed on to 
        :class:`lsqfit.multiminex`, which finds the minimum.
    :type minargs: dictionary
    :returns: A tuple containing the best fit (object of type 
        :class:`lsqfit.nonlinear_fit`) and the optimal value for parameter ``z``.
    """
    if minargs == {}: # default
        minargs = dict(tol=1e-3, step=math.log(1.1), maxit=30, analyzer=None)
    save = dict(lastz=None, lastp0=None)
    def minfcn(z, save=save):
        args = fitargs(z)
        if save['lastp0'] is not None:
            args['p0'] = save['lastp0']
        fit = lsqfit.nonlinear_fit(**args)
        if numpy.isnan(fit.logGBF):
            raise ValueError
        else:
            save['lastz'] = z 
            save['lastp0'] = fit.pmean
        return -fit.logGBF
    try:
        z = multiminex(numpy.array(z0), minfcn, **minargs).x
    except ValueError:
        print('*** empbayes_fit warning: null logGBF')
        z = save['lastz']
    args = fitargs(z)
    if save['lastp0'] is not None:
        args['p0'] = save['lastp0']
    return lsqfit.nonlinear_fit(**args), z
Example #38
0
def fit_data(params,data):
    result = dict()
    result['tmin'] = []
    result['tmax'] = []
    result['Ns'] = []
    result['fit'] = []
    T = len(data)/len(params['corrs'])
    Ncorr = len(params['corrs'])
    for tmin in range(params['tmin'][0],params['tmin'][1]+1):
        for tmax in range(params['tmax'][0],params['tmax'][1]+1):
            for n in range(params['Ns'][0],params['Ns'][1]+1):
                print "trange: [%s,%s] Ns: %s" %(tmin,tmax,n)
                x = dict()
                x['t'] = np.array(range(tmin, tmax+1))
                x['corrs'] = params['corrs']
                tmask = [i+j*T for j in range(Ncorr) for i in x['t']]
                y = data[tmask]
                try:
                    #f = open('./nucleon_p0/%s_%s_%s.yml' %(tmin,tmax,n),'r')
                    f = open('./nucleon_p0/boot0.yml','r')
                    p0 = yml.load(f)
                    f.close()
                except:
                    p0 = None
                fcn = fit_function(T,n)
                priors = set_priors(params,n)
                fit = lsqfit.nonlinear_fit(data=(x,y),prior=priors,p0=p0,fcn=fcn.twopt_fit,maxit=100000)
                result['tmin'].append(tmin)
                result['tmax'].append(tmax)
                result['Ns'].append(n)
                result['fit'].append(fit)
                if params['update_boot0']:
                    #f = open('./nucleon_p0/%s_%s_%s.yml' %(tmin,tmax,n),'w+')
                    f = open('./nucleon_p0/boot0.yml', 'w+')
                    boot0 = dict()
                    for k in fit.p.keys():
                        boot0[k] = fit.p[k].mean
                    yml.dump(boot0,f)
                    f.flush()
                    f.close()
    return result
    def main(self, pt2_t, ra_tseq, ra_t, fh_t1, fh_t2, pt2, ra_re, ra_im,
             fh_re, fh_im):
        '''
        pt2_t:2pt 的自变量
        ra_tseq:3pt 的 tseq
        ra_t:3pt 的插入流时间
        fh_t1 和 fh_t2:因为 FH 需要 ( sum(tseq=t2) - sum(tseq=t1) ) / (t2 - t1),所以需要两组自变量,不过现在这个 fit function 用不到 fh_t2

        pt2:2pt 的因变量
        ra_re, ra_im:ratio 因变量的实虚部
        fh_re, fh_im:fh 因变量的实虚部
        '''

        priors = self.pri()

        x = {}
        y = gv.BufferDict()
        if self.fit_2pt == True:
            x['2pt'] = pt2_t
            y['2pt'] = pt2

        if self.fit_ratio == True:
            x['ra_re'] = [ra_tseq, ra_t]
            y['ra_re'] = ra_re
            x['ra_im'] = [ra_tseq, ra_t]
            y['ra_im'] = ra_im

        if self.fit_fh == True:
            x['fh_re'] = fh_t1
            y['fh_re'] = fh_re
            x['fh_im'] = fh_t1
            y['fh_im'] = fh_im

        fit_result = lsf.nonlinear_fit(data=(x, y),
                                       prior=priors,
                                       fcn=self.fcn,
                                       maxit=10000,
                                       svdcut=1e-100,
                                       fitter='scipy_least_squares')

        return fit_result
Example #40
0
def do_2st_3t_fit(ratio_,
                  t2_,
                  ncut1_=1,
                  ncut2_=0,
                  dm_=gv.gvar(0.4, np.inf),
                  show=True):
    nconf_ = ratio_.coords['conf'].size
    t_ = ratio_.coords['t'].values
    data_ = ratio_.sel(t2=t2_).squeeze().transpose(
        'conf', 't2', 't').values.reshape(nconf_, t2_.size * t_.size)
    data_fit = prep_data_ratio_new(data_, ncut1_, ncut2_, t2_, t_)
    # print(data_fit[0].shape)
    # print(data_fit[1].shape)
    prior0_2st_3t['dm'] = dm_
    fit_ = lsqfit.nonlinear_fit(data=data_fit,
                                prior=prior0_2st_3t,
                                fcn=fcn_2st_3t,
                                debug=True)
    if show:
        print_fit(fit_)
    return fit_
Example #41
0
def do_fit(options, paramlabel, icorre, data):
    xmin = options.xrange[0]
    xmax = options.xrange[1]
    choose_t = options.choose
    rmin = options.rmin
    rmax = options.rmax

    paramout = {}
    chi2dof = []
    Q = []
    for i in paramlabel:
        paramout[i] = []

    newtmin = choose_t.copy()
    for tmin in range(rmin[icorre], rmin[icorre] + 8):
        print("*" * 25 + "tmin = " + str(tmin) + ", tmax = " +
              str(rmax[icorre]) + "*" * 25)
        newtmin[icorre] = tmin
        x, y = make_data_all(data, newtmin, rmax)

        prior = make_prior_all_twostate()

        fit = lsqfit.nonlinear_fit(data=(x, y),
                                   fcn=fcn_all_twostate,
                                   prior=prior,
                                   p0=None)
        print(fit)

        if tmin == choose_t[icorre]:
            print("-" * 25 + "choosed tmin = " + str(tmin) + "-" * 25)
            print("\n")
            choose_fit = fit

        for i in paramlabel:
            paramout[i].append(fit.p[i])

        chi2dof.append(fit.chi2 / fit.dof)
        Q.append(fit.Q)

    return (chi2dof, Q, paramout), choose_fit
Example #42
0
def main():
    # pendulum data exhibits experimental error in ability to measure theta
    t = [ 0.1, 0.2, 0.3, 0.4,  0.5, 0.6,  0.7,  0.8,  0.9, 1.]
    theta = gv.gvar([
        '1.477(79)', '0.791(79)', '-0.046(79)', '-0.852(79)',
        '-1.523(79)', '-1.647(79)', '-1.216(79)', '-0.810(79)',
        '0.185(79)', '0.832(79)'
        ])

    # prior: assume experimental error in ability to specify theta(0)
    prior = gv.BufferDict()
    prior['g/l'] = (2 * math.pi) ** 2 * gv.gvar(1, 0.1)
    prior['theta(0)'] = gv.gvar(math.pi / 2., 0.05)

    # fit function: use class Pendulum object to integrate pendulum motion
    def fitfcn(p, t=t):
        pendulum = Pendulum(p['g/l'])
        return pendulum(p['theta(0)'], t)

    # do the fit and print results
    fit = lsqfit.nonlinear_fit(data=theta, prior=prior, fcn=fitfcn)
    print(fit.format(maxline=True))
    print('fit/exact for (g/l) =', fit.p['g/l'] / (2*math.pi) ** 2)
    print('fit/exact for theta(0) =', fit.p['theta(0)'] / (math.pi / 2.))

    if MAKE_PLOT:
        # make figure (saved to file pendulum.pdf)
        plt.figure(figsize=(4,3))
        # start plot with data
        plt.errorbar(
            x=t, y=gv.mean(theta), yerr=gv.sdev(theta),
            fmt='k.',
            )
        # use best-fit function to add smooth curve for 100 points
        t = np.linspace(0., 1.1, 100)
        th = fitfcn(fit.p, t)
        show_plot(t, th)
Example #43
0
def fitscript(trange, data, prior, fcn, sets=1, result_flag='off'):
    print "depricated to v2 I think?"
    posterior = []
    tmintbl = []
    tmaxtbl = []
    for tmin in range(trange['tmin'][0], trange['tmin'][1]+1):
        for tmax in range(trange['tmax'][0], trange['tmax'][1]+1):
            x = x_indep(tmin, tmax)
            y = y_dep(x, data, sets)
            fit = lsqfit.nonlinear_fit(data=(x,y), prior=prior, fcn=fcn)
            if result_flag=='on':
                print tmin, tmax,
                #for n in prior.keys():
                #    print fit.p[n].mean,'(',fit.p[n].sdev,')',
                #print '\n'
                print fit
            posterior.append(fit.p)
            tmintbl.append(tmin)
            tmaxtbl.append(tmax)
    fittbl = dict()
    fittbl['tmin'] = tmintbl
    fittbl['tmax'] = tmaxtbl
    fittbl['post'] = posterior
    return fittbl
Example #44
0
def main():
    gv.ranseed([2009, 2010, 2011, 2012])  # initialize random numbers (opt.)
    x, y = make_data()  # make fit data
    p0 = None  # make larger fits go faster (opt.)
    sys_stdout = sys.stdout
    for nexp in range(3, 8):
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x, y), fcn=f, prior=prior, p0=p0, svdcut=1e-15)  # ,svdcut=SVDCUT)
        if fit.chi2 / fit.dof < 1.0:
            p0 = fit.pmean  # starting point for next fit (opt.)
        if nexp == 5:
            sys.stdout = tee.tee(sys_stdout, open("eg3.out", "w"))
        print "************************************* nexp =", nexp
        print fit  # print the fit results
        E = fit.p["E"]  # best-fit parameters
        a = fit.p["a"]
        print "E1/E0 =", E[1] / E[0], "  E2/E0 =", E[2] / E[0]
        print "a1/a0 =", a[1] / a[0], "  a2/a0 =", a[2] / a[0]
        # print E[1]-E[0], E[-1]-E[-2]
        # print (E[1]/E[0]).partialsdev(fit.prior['E'])
        # print (E[1]/E[0]).partialsdev(fit.prior['a'])
        # print (E[1]/E[0]).partialsdev(fit.y)
        sys.stdout = sys_stdout
        print
    # sys.stdout = tee.tee(sys_stdout, open("eg3a.out", "w"))
    # for i in range(1):
    #     print '--------------------- fit with %d extra data sets' % (i+1)
    #     x, y = make_data(1)
    #     prior = fit.p
    #     fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f1,prior=prior, svdcut=SVDCUT)
    #     print fit
    sys.stdout = sys_stdout

    if DO_BOOTSTRAP:
        Nbs = 10  # number of bootstrap copies
        outputs = {"E1/E0": [], "E2/E0": [], "a1/a0": [], "a2/a0": [], "E1": [], "a1": []}  # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean["E"]  # best-fit parameters
            a = bsfit.pmean["a"]
            outputs["E1/E0"].append(E[1] / E[0])  # accumulate results
            outputs["E2/E0"].append(E[2] / E[0])
            outputs["a1/a0"].append(a[1] / a[0])
            outputs["a2/a0"].append(a[2] / a[0])
            outputs["E1"].append(E[1])
            outputs["a1"].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit.chi2/bsfit.dof

        # extract means and standard deviations from the bootstrap output
        for k in outputs:
            outputs[k] = gv.gvar(np.mean(outputs[k]), np.std(outputs[k]))
        print "Bootstrap results:"
        print "E1/E0 =", outputs["E1/E0"], "  E2/E1 =", outputs["E2/E0"]
        print "a1/a0 =", outputs["a1/a0"], "  a2/a0 =", outputs["a2/a0"]
        print "E1 =", outputs["E1"], "  a1 =", outputs["a1"]

    if DO_PLOT:
        print fit.format(100)  # print the fit results
        import pylab as pp
        from gvar import mean, sdev

        fity = f(x, fit.pmean)
        ratio = y / fity
        pp.xlim(0, 21)
        pp.xlabel("x")
        pp.ylabel("y/f(x,p)")
        pp.errorbar(x=x, y=mean(ratio), yerr=sdev(ratio), fmt="ob")
        pp.plot([0.0, 21.0], [1.0, 1.0])
        pp.show()
Example #45
0
def main():
    gv.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    sys_stdout = sys.stdout
    sys.stdout = tee.tee(sys.stdout, open("eg1.out","w"))
    for nexp in range(1, 11):
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior,p0=p0) #, svdcut=SVDCUT)
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
        if nexp > 5 and nexp < 10:
            print(".".center(73))
            continue
        elif nexp not in [1]:
            print("")
        print '************************************* nexp =',nexp
        print fit.format()                   # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        if nexp > 2:
            print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
            print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]

    # redo fit with 4 parameters since that is enough
    prior = make_prior(4)
    fit = lsqfit.nonlinear_fit(data=(x,y), fcn=f, prior=prior, p0=fit.pmean)
    sys.stdout = sys_stdout
    print fit
    # extra data 1
    print '\n--------------------- fit with extra information'
    sys.stdout = tee.tee(sys_stdout, open("eg1a.out", "w"))
    def ratio(p):
        return p['a'][1] / p['a'][0]
    newfit = lsqfit.nonlinear_fit(data=gv.gvar(1,1e-5), fcn=ratio, prior=fit.p)
    print (newfit)
    E = newfit.p['E']
    a = newfit.p['a']
    print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
    print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]

    # alternate method for extra data
    sys.stdout = tee.tee(sys_stdout, open("eg1b.out", "w"))
    fit.p['a1/a0'] = fit.p['a'][1] / fit.p['a'][0]
    new_data = {'a1/a0' : gv.gvar(1,1e-5)}
    new_p = lsqfit.wavg([fit.p, new_data])
    print 'chi2/dof = %.2f\n' % (new_p.chi2 / new_p.dof)
    print 'E:', new_p['E'][:4]
    print 'a:', new_p['a'][:4]
    print 'a1/a0:', new_p['a1/a0']

    if DO_BAYES:
        # Bayesian Fit
        gv.ranseed([123])
        prior = make_prior(4)
        fit = lsqfit.nonlinear_fit(data=(x,y), fcn=f, prior=prior, p0=fit.pmean)
        sys.stdout = tee.tee(sys_stdout, open("eg1c.out", "w"))
        # print fit

        expval = lsqfit.BayesIntegrator(fit, limit=10.)
        # adapt integrator to PDF
        expval(neval=10000, nitn=10)

        # calculate expectation value of function g(p)
        fit_hist = gv.PDFHistogram(fit.p['E'][0])
        def g(p):
            parameters = [p['a'][0], p['E'][0]]
            return dict(
                mean=parameters,
                outer=np.outer(parameters, parameters),
                hist=fit_hist.count(p['E'][0]),
                )
        r = expval(g, neval=10000, nitn=10, adapt=False)

        # print results
        print r.summary()
        means = r['mean']
        cov = r['outer'] - np.outer(r['mean'], r['mean'])
        print 'Results from Bayesian Integration:'
        print 'a0: mean =', means[0], '  sdev =', cov[0,0]**0.5
        print 'E0: mean =', means[1], '  sdev =', cov[1,1]**0.5
        print 'covariance from Bayesian integral =', np.array2string(cov, prefix=36 * ' ')
        print

        print 'Results from Least-Squares Fit:'
        print 'a0: mean =', fit.p['a'][0].mean, '  sdev =', fit.p['a'][0].sdev
        print 'E0: mean =', fit.p['E'][0].mean, '  sdev =', fit.p['E'][0].sdev
        print 'covariance from least-squares fit =', np.array2string(gv.evalcov([fit.p['a'][0], fit.p['E'][0]]), prefix=36*' ',precision=3)
        sys.stdout = sys_stdout

        # make histogram of E[0] probabilty
        plt = fit_hist.make_plot(r['hist'])
        plt.xlabel('$E_0$')
        plt.ylabel('probability')
        plt.savefig('eg1c.png', bbox_inches='tight')
        # plt.show()


    # # extra data 2
    # sys.stdout = tee.tee(sys_stdout, open("eg1b.out", "w"))
    # newfit = fit
    # for i in range(1):
    #     print '\n--------------------- fit with %d extra data sets' % (i+1)
    #     x, ynew = make_data()
    #     prior = newfit.p
    #     newfit = lsqfit.nonlinear_fit(data=(x,ynew), fcn=f, prior=prior) # , svdcut=SVDCUT)
    #     print newfit
    sys.stdout = sys_stdout
    # def fcn(x, p):
    #     return f(x, p), f(x, p)
    # prior = make_prior(nexp)
    # fit = lsqfit.nonlinear_fit(data=(x, [y, ynew]), fcn=fcn, prior=prior, p0=newfit.pmean) # , svdcut=SVDCUT)
    # print(fit)


    if DO_BOOTSTRAP:
        Nbs = 40                                     # number of bootstrap copies

        outputs = {'E1/E0':[], 'E2/E0':[], 'a1/a0':[],'a2/a0':[],'E1':[],'a1':[]}   # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean['E']                     # best-fit parameters
            a = bsfit.pmean['a']
            outputs['E1/E0'].append(E[1]/E[0])       # accumulate results
            outputs['E2/E0'].append(E[2]/E[0])
            outputs['a1/a0'].append(a[1]/a[0])
            outputs['a2/a0'].append(a[2]/a[0])
            outputs['E1'].append(E[1])
            outputs['a1'].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit.chi2/bsfit.dof

        # extract means and standard deviations from the bootstrap output
        for k in outputs:
            outputs[k] = gv.gvar(np.mean(outputs[k]),np.std(outputs[k]))
        print 'Bootstrap results:'
        print 'E1/E0 =',outputs['E1/E0'],'  E2/E1 =',outputs['E2/E0']
        print 'a1/a0 =',outputs['a1/a0'],'  a2/a0 =',outputs['a2/a0']
        print 'E1 =',outputs['E1'],'  a1 =',outputs['a1']

    if DO_PLOT:
        import matplotlib.pyplot as plt
        ratio = y / fit.fcn(x,fit.pmean)
        plt.xlim(4, 21)
        plt.xlabel('x')
        plt.ylabel('y / f(x,p)')
        plt.errorbar(x=x,y=gv.mean(ratio),yerr=gv.sdev(ratio),fmt='ob')
        plt.plot([4.0, 21.0], [1.0, 1.0], 'b:')
        plt.savefig('eg1.png', bbox_inches='tight')
        plt.show()
Example #46
0
def main():
    gd.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    max_prior = make_prior(20)      # maximum sized prior
    p0 = None                       # make larger fits go faster (opt.)
    sys_stdout = sys.stdout
    if USE_SVD:
        sys.stdout = tee.tee(sys_stdout,open("eg5a.out","w"))
    for nexp in range(1,5):
        print '************************************* nexp =',nexp
        fit_prior = lsqfit.GPrior()     # prior used in fit
        ymod_prior = lsqfit.GPrior()    # part of max_prior absorbed in ymod
        for k in max_prior:
            fit_prior[k] = max_prior[k][:nexp]
            ymod_prior[k] = max_prior[k][nexp:]
        x,y = make_data(ymod_prior)     # make fit data
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=fit_prior,p0=p0,svdcut=SVDCUT,maxit=10000)
        if nexp==4 and not USE_SVD:
            sys.stdout = tee.tee(sys_stdout, open("eg5b.out", "w"))
        print fit.format(100)                   # print the fit results
        # if nexp>3:
        #     E = fit.p['E']              # best-fit parameters
        #     a = fit.p['a']
        #     print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        #     print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
            # E = fit.palt['E']              # best-fit parameters
            # a = fit.palt['a']
            # print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
            # print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        print
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
    E = fit.p['E']              # best-fit parameters
    a = fit.p['a']
    print 'E1/E0 =',(E[1]/E[0]).fmt(),'  E2/E0 =',(E[2]/E[0]).fmt()
    print 'a1/a0 =',(a[1]/a[0]).fmt(),'  a2/a0 =',(a[2]/a[0]).fmt()
    sys.stdout = sys_stdout
    
    if DO_ERRORBUDGET:
        if USE_SVD:
            sys.stdout = tee.tee(sys_stdout,open("eg5d.out","w"))
        outputs = {'E1/E0':E[1]/E[0], 'E2/E0':E[2]/E[0],         
                 'a1/a0':a[1]/a[0], 'a2/a0':a[2]/a[0]}
        inputs = {'E':max_prior['E'],'a':max_prior['a'],'svd':fit.svdcorrection}
        print fit.fmt_values(outputs)
        print fit.fmt_errorbudget(outputs,inputs)
        sys.stdout = sys_stdout
        outputs = {''}

    if DO_BOOTSTRAP:
        Nbs = 40                                     # number of bootstrap copies
        outputs = {'E1/E0':[], 'E2/E0':[], 'a1/a0':[],'a2/a0':[],'E1':[],'a1':[]}   # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean['E']                     # best-fit parameters
            a = bsfit.pmean['a']
            outputs['E1/E0'].append(E[1]/E[0])       # accumulate results
            outputs['E2/E0'].append(E[2]/E[0])
            outputs['a1/a0'].append(a[1]/a[0])
            outputs['a2/a0'].append(a[2]/a[0])
            outputs['E1'].append(E[1])
            outputs['a1'].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit
        # extract means and "standard deviations" from the bootstrap output
        outputs = gd.dataset.avg_data(outputs,bstrap=True)
        # for k in outputs:
        #     outputs[k] = gd.gvar(np.mean(outputs[k]),np.std(outputs[k]))
        if USE_SVD:
            sys.stdout = tee.tee(sys_stdout,open("eg5e.out","w"))
        print 'Bootstrap results:'
        print 'E1/E0 =',outputs['E1/E0'].fmt(),'  E2/E1 =',outputs['E2/E0'].fmt()
        print 'a1/a0 =',outputs['a1/a0'].fmt(),'  a2/a0 =',outputs['a2/a0'].fmt()
        print 'E1 =',outputs['E1'].fmt(),'  a1 =',outputs['a1'].fmt()
Example #47
0
def wavg(dataseq, prior=None, fast=False, **kargs):
    """ Weighted average of |GVar|\s or arrays/dicts of |GVar|\s.
        
    The weighted average of several |GVar|\s is what one obtains from
    a  least-squares fit of the collection of |GVar|\s to the
    one-parameter fit function ::

        def f(p): 
            return N * [p[0]]

    where ``N`` is the number of |GVar|\s. The average is the best-fit 
    value for ``p[0]``.  |GVar|\s with smaller standard deviations carry 
    more weight than those with larger standard deviations. The averages
    computed by ``wavg`` take account of correlations between the |GVar|\s.

    If ``prior`` is not ``None``, it is added to the list of data 
    used in the average. Thus ``wavg([x2, x3], prior=x1)`` is the 
    same as ``wavg([x1, x2, x3])``. 
        
    Typical usage is ::
        
        x1 = gvar.gvar(...)
        x2 = gvar.gvar(...)
        x3 = gvar.gvar(...)
        xavg = wavg([x1, x2, x3])   # weighted average of x1, x2 and x3
    
    where the result ``xavg`` is a |GVar| containing the weighted average.

    The individual |GVar|\s in the last example can be  replaced by
    multidimensional distributions, represented by arrays of |GVar|\s
    or dictionaries of |GVar|\s (or arrays of |GVar|\s). For example, ::

        x1 = [gvar.gvar(...), gvar.gvar(...)]
        x2 = [gvar.gvar(...), gvar.gvar(...)]
        x3 = [gvar.gvar(...), gvar.gvar(...)]
        xavg = wavg([x1, x2, x3])   
            # xavg[i] is wgtd avg of x1[i], x2[i], x3[i]

    where each array ``x1``, ``x2`` ... must have the same shape. 
    The result ``xavg`` in this case is an array of |GVar|\s, where 
    the shape of the array is the same as that of ``x1``, etc.

    Another example is ::

        x1 = dict(a=[gvar.gvar(...), gvar.gvar(...)], b=gvar.gvar(...))
        x2 = dict(a=[gvar.gvar(...), gvar.gvar(...)], b=gvar.gvar(...))
        x3 = dict(a=[gvar.gvar(...), gvar.gvar(...)])
        xavg = wavg([x1, x2, x3])   
            # xavg['a'][i] is wgtd avg of x1['a'][i], x2['a'][i], x3['a'][i]
            # xavg['b'] is gtd avg of x1['b'], x2['b']  

    where different dictionaries can have (some) different keys. Here the 
    result ``xavg`` is a :class:`gvar.BufferDict`` having the same keys as
    ``x1``, etc.
     
    Weighted averages can become costly when the number of random samples being 
    averaged is large (100s or more). In such cases it might be useful to set
    parameter ``fast=True``. This causes ``wavg`` to estimate the weighted 
    average by incorporating the random samples one at a time into a 
    running average::

        result = prior
        for dataseq_i in dataseq:
            result = wavg([result, dataseq_i], ...)

    This method is much faster when ``len(dataseq)`` is large, and gives the
    exact result when there are no correlations between different elements
    of list ``dataseq``. The results are approximately correct when 
    ``dataseq[i]`` and ``dataseq[j]`` are correlated for ``i!=j``.

    :param dataseq: The |GVar|\s to be averaged. ``dataseq`` is a one-dimensional
        sequence of |GVar|\s, or of arrays of |GVar|\s, or of dictionaries 
        containing |GVar|\s or arrays of |GVar|\s. All ``dataseq[i]`` must
        have the same shape.
    :param prior: Prior values for the averages, to be included in the weighted
        average. Default value is ``None``, in which case ``prior`` is ignored.
    :type prior: |GVar| or array/dictionary of |GVar|\s
    :param fast: Setting ``fast=True`` causes ``wavg`` to compute an 
        approximation to the weighted average that is much faster to calculate 
        when averaging a large number of samples (100s or more). The default is 
        ``fast=False``.
    :type fast: bool 
    :param kargs: Additional arguments (e.g., ``svdcut``) to the fitter 
        used to do the averaging.
    :type kargs: dict
        
    Results returned by :func:`gvar.wavg` have the following extra 
    attributes describing the average:
        
    .. attribute:: chi2
        
        ``chi**2`` for weighted average.
        
    .. attribute:: dof
        
        Effective number of degrees of freedom.
        
    .. attribute:: Q
        
        The probability that the ``chi**2`` could have been larger, 
        by chance, assuming that the data are all Gaussain and consistent
        with each other. Values smaller than 0.1 or suggest that the 
        data are not Gaussian or are inconsistent with each other. Also 
        called the *p-value*.

        Quality factor `Q` (or *p-value*) for fit.

    .. attribute:: time

        Time required to do average.

    .. attribute:: svdcorrection

        The *svd* corrections made to the data when ``svdcut`` is not ``None``.

    .. attribute:: fit

        Fit output from average.
    """
    if len(dataseq) <= 0:
        if prior is None:
            return None 
        wavg.Q = 1
        wavg.chi2 = 0
        wavg.dof = 0
        wavg.time = 0
        wavg.fit = None
        wavg.svdcorrection = None
        if hasattr(prior, 'keys'):
            return BufferDictWAvg(dataseq[0], wavg)
        if numpy.shape(prior) == ():
            return GVarWAvg(prior, wavg)
        else:
            return ArrayWAvg(numpy.asarray(prior), wavg)        
    elif len(dataseq) == 1 and prior is None:
        wavg.Q = 1
        wavg.chi2 = 0
        wavg.dof = 0
        wavg.time = 0
        wavg.fit = None
        wavg.svdcorrection = None
        if hasattr(dataseq[0], 'keys'):
            return BufferDictWAvg(dataseq[0], wavg)
        if numpy.shape(dataseq[0]) == ():
            return GVarWAvg(dataseq[0], wavg)
        else:
            return ArrayWAvg(numpy.asarray(dataseq[0]), wavg)
    if fast:
        chi2 = 0
        dof = 0
        time = 0
        ans = prior
        svdcorrection = gvar.BufferDict()
        for i, dataseq_i in enumerate(dataseq):
            if ans is None:
                ans = dataseq_i
            else:
                ans = wavg([ans, dataseq_i], fast=False, **kargs)
                chi2 += wavg.chi2
                dof += wavg.dof
                time += wavg.time
                if wavg.svdcorrection is not None:
                    for k in wavg.svdcorrection:
                        svdcorrection[str(i) + ':' + k] = wavg.svdcorrection[k]
        wavg.chi2 = chi2
        wavg.dof = dof
        wavg.time = time
        wavg.Q = gammaQ(dof / 2., chi2 / 2.)
        wavg.svdcorrection = svdcorrection
        wavg.fit = None
        ans.dof = wavg.dof
        ans.Q = wavg.Q
        ans.chi2 = wavg.chi2
        ans.time = wavg.time
        ans.svdcorrection = wavg.svdcorrection
        ans.fit = wavg.fit
        return ans
    if hasattr(dataseq[0], 'keys'):
        data = {}
        keys = []
        if prior is not None:
            dataseq = [prior] + list(dataseq)
        for dataseq_i in dataseq:
            for k in dataseq_i:
                if k in data:
                    data[k].append(dataseq_i[k])
                else:
                    data[k] = [dataseq_i[k]]
                    keys.append(k)
        data = gvar.BufferDict(data, keys=keys)
        p0 = gvar.BufferDict()
        for k in data:
            p0[k] = gvar.mean(data[k][0]) + gvar.sdev(data[k][0]) / 10.
        def fcn(p):
            ans = gvar.BufferDict()
            for k in data:
                ans[k] = len(data[k]) * [p[k]]
            return ans
    else:
        p = numpy.asarray(dataseq[0])
        data = [] if prior is None else [prior]
        data += [dataseqi for dataseqi in dataseq]
        p0 = numpy.asarray(gvar.mean(data[0]) + gvar.sdev(data[0]) / 10.)
        data = numpy.array(data)
        def fcn(p):
            return len(data) * [p]
    fit = lsqfit.nonlinear_fit(data=data, fcn=fcn, p0=p0, **kargs)
    # wavg.Q = fit.Q
    # wavg.chi2 = fit.chi2
    # wavg.dof = fit.dof
    # wavg.time = fit.time
    # wavg.svdcorrection = fit.svdcorrection
    # wavg.fit = fit
    if p0.shape is None:
        return BufferDictWAvg(gvar.BufferDict(p0, buf=fit.p.flat), fit)
    elif p0.shape == ():
        return GVarWAvg(fit.p.flat[0], fit)
    else:
        return ArrayWAvg(fit.p.reshape(p0.shape), fit)
Example #48
0
def main():
    gv.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    for nexp in range(3,5):
        print '************************************* nexp =',nexp
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior,p0=p0)
        print fit                   # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        print
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
    sys_stdout = sys.stdout
    if DO_ERRORBUDGET:

        lines = [
            "E = fit.p['E']",
            "a = fit.p['a']",
            "print(E[1] / E[0])",
            "print((E[1] / E[0]).partialsdev(fit.prior['E']))",
            "print((E[1] / E[0]).partialsdev(fit.prior['a']))",
            "print((E[1] / E[0]).partialsdev(y))"
            ]
        sys.stdout = tee.tee(sys_stdout, open("eg4c.out","w"))
        for line in lines:
            print ">>>", line
            if line[:5] == "print":
                print(eval(line[5:]))
        # print E[1]/E[0]
        # print (E[1]/E[0]).partialsdev(fit.prior['E'])
        # print (E[1]/E[0]).partialsdev(fit.prior['a'])
        # print (E[1]/E[0]).partialsdev(y)
        outputs = {'E1/E0':E[1]/E[0], 'E2/E0':E[2]/E[0],
                 'a1/a0':a[1]/a[0], 'a2/a0':a[2]/a[0]}
        inputs = {'E':fit.prior['E'],'a':fit.prior['a'],'y':y}

        sys.stdout = tee.tee(sys_stdout, open("eg4b.out","w"))
        print fit.fmt_values(outputs)
        print fit.fmt_errorbudget(outputs,inputs)
        sys.stdout = sys_stdout

    if DO_SIMULATIONS:
        # fit simulations
        sys.stdout = tee.tee(sys_stdout, open("eg4d.out","w"))

        for sfit in fit.simulated_fit_iter(3):
            print '************************************* simulation'
            print(sfit)
            sE = sfit.p['E']             # best-fit parameters
            sa = sfit.p['a']
            E = sfit.pexact['E']
            a = sfit.pexact['a']
            print 'E1/E0 =', sE[1] / sE[0], '  E2/E0 =', sE[2] / sE[0]
            print 'a1/a0 =', sa[1] / sa[0], '  a2/a0 =', sa[2] / sa[0]
            print '\nSimulated Fit Values - Exact Values:'
            print 'E1/E0:', (sE[1] / sE[0]) - (E[1] / E[0]),\
               '  E2/E0:', (sE[2] / sE[0]) - (E[2] / E[0])
            print 'a1/a0:', (sa[1] / sa[0]) - (a[1] / a[0]),\
               '  a2/a0:', (sa[2] / sa[0]) - (a[2] / a[0])

            # compute chi**2 comparing fit results to exact results
            sim_results = [sE[0], sE[1], sa[0], sa[1]]
            exact_results = [E[0], E[1], a[0], a[1]]
            chi2 = gv.chi2(sim_results, exact_results, svdcut=1e-8)
            print '\nParameter chi2/dof [dof] = %.2f' % (chi2/chi2.dof), '[%d]' % chi2.dof, '  Q = %.1f' % chi2.Q
            print
        sys.stdout = sys_stdout

    if DO_EMPBAYES:
        def fitargs(z,nexp=nexp,prior=prior,f=f,data=(x,y),p0=p0):
            z = gv.exp(z)
            prior['a'] = [gv.gvar(0.5,0.5*z[0]) for i in range(nexp)]
            return dict(prior=prior,data=data,fcn=f,p0=p0)
        ##
        z0 = [0.0]
        fit,z = lsqfit.empbayes_fit(z0,fitargs,tol=1e-3)
        sys.stdout = tee.tee(sys_stdout, open("eg4a.out","w"))
        print fit                   # print the optimized fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        # print "prior['a'] =",fit.prior['a'][0]
        sys.stdout = sys_stdout
        print

    if DO_PLOT:
        import pylab as pp
        from gvar import mean,sdev
        fity = f(x,fit.pmean)
        ratio = y/fity
        pp.xlim(0,21)
        pp.xlabel('x')
        pp.ylabel('y/f(x,p)')
        pp.errorbar(x=x,y=mean(ratio),yerr=sdev(ratio),fmt='ob')
        pp.plot([0.0,21.0],[1.0,1.0])
        pp.show()
Example #49
0
from gvar import *
from lsqfit import nonlinear_fit
import functools
import inspect
import sys

ranseed([123])
ygen = gvar(0.02, 0.2) - 0.005
print (ygen)

y = [gvar(ygen(), ygen.sdev) for i in range(20)]
ystr = [yi.fmt(2) for yi in y]
y = gvar(ystr)
print (ystr[:])

print
log_prior = BufferDict(loga = log(gvar(0.02, 0.02)))  
sqrt_prior = BufferDict(sqrta = sqrt(gvar(0.02, 0.02)))
prior = BufferDict(a = gvar(0.02, 0.02))

stdout = sys.stdout
for p in [prior, log_prior, sqrt_prior]:
	key = list(p.keys())[0]
	sys.stdout = open("eg6-{}.out".format(key), "w")
	def fcn(p, N=len(y)):
		return N*[p['a']]
	f = nonlinear_fit(prior=p, fcn=fcn, data=(y), extend=True)
	print (f)
	print ("a =", f.p['a'].fmt())
sys.stdout = stdout
Example #50
0
    PLOT = False
    RESULTS = False

    # least-squares fit
    x = np.array([0.1, 1.2, 1.9, 3.5])
    y = gv.gvar(["1.2(1.0)", "2.4(1)", "2.0(1.2)", "5.2(3.2)"])
    prior = gv.BufferDict()
    prior["a"] = "0(5)"
    prior["s"] = "0(2)"
    prior["g"] = "2(2)"
    prior = gv.gvar(prior)

    def f(x, p):
        return p["a"] + p["s"] * x ** p["g"]

    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=f, debug=True)
    print(fit)

    hist = gv.PDFHistogram(fit.p["s"] * fit.p["g"])

    # Bayesian integral to evaluate expectation value of s*g
    def g(p):
        sg = p["s"] * p["g"]
        return dict(moments=[sg, sg ** 2, sg ** 3, sg ** 4], histogram=hist.count(sg))

    expval = lsqfit.BayesIntegrator(fit, limit=20.0)
    warmup = expval(neval=2000, nitn=10)
    results = expval(g, neval=2000, nitn=10, adapt=False)
    if RESULTS:
        print(results.summary())
    stats = hist.analyze(results["histogram"]).stats
Example #51
0
def fitscript_v3(trange,fhtrange,T,data,priors,fcn,init=None,basak=None):
    sets = len(data)/T
    #print "sets:", sets
    pmean = []
    psdev = []
    post = []
    p0 = []
    prior = []
    tmintbl = []
    tmaxtbl = []
    fhtmintbl = []
    fhtmaxtbl = []
    chi2 = []
    chi2f = []
    dof = []
    lgbftbl = []
    Q = []
    rawoutput = []
    for tmin in range(trange['tmin'][0], trange['tmin'][1]+1):
        for tmax in range(trange['tmax'][0], trange['tmax'][1]+1):
            for fhtmin in range(fhtrange['tmin'][0], fhtrange['tmin'][1]+1):
                for fhtmax in range(fhtrange['tmax'][0], fhtrange['tmax'][1]+1):
                    x2 = x_indep(tmin, tmax)
                    fhx = x_indep(fhtmin, fhtmax)
                    x = [x2,fhx]
                    xlist, y = y_dep_v2(x, data, sets)
                    if basak is not None:
                        x = {'indep': x, 'basak': basak}
                    else: pass
                    fit = lsqfit.nonlinear_fit(data=(x,y),prior=priors,fcn=fcn,p0=init,maxit=1000000) #,svdcut=1E-5)
                    print fhtmin, fhtmax, fit.p['gA00'].mean, fit.p['gA00'].sdev, fit.p['E0'].mean, fit.p['E0'].sdev, fit.chi2/fit.dof, fit.Q, fit.logGBF
                    pmean.append(fit.pmean)
                    psdev.append(fit.psdev)
                    post.append(fit.p)
                    p0.append(fit.p0)
                    prior.append(fit.prior)
                    tmintbl.append(tmin)
                    tmaxtbl.append(tmax)
                    fhtmintbl.append(fhtmin)
                    fhtmaxtbl.append(fhtmax)
                    chi2.append(fit.chi2)
                    dof.append(fit.dof)
                    lgbftbl.append(fit.logGBF)
                    Q.append(fit.Q)
                    rawoutput.append(fit)
                    chi2f.append(chi2freq(fit.chi2,fit.prior,fit.p))
    ## print correlation matrix of data
    #corr = gv.evalcorr(fit.y)
    #string = ''
    #for i in range(len(corr)):
    #    for j in range(len(corr)):
    #        string +='%s,%s,%s\n' %(str(i),str(j),str(corr[i,j]))
    #f = open('./temp.csv','w')
    #f.write(string)
    #f.flush()
    #f.close()
    ## end corr
    #fitline = fcn(x,fit.p)
    #print "t, y, +-, fit, +-"
    #for i in range(len(xlist)):
    #    print xlist[i], ',', y[i].mean, ',', y[i].sdev, ',', fitline[i].mean, ',', fitline[i].sdev
    #print fit
    #print '======'
    #print fcn.__name__, basak
    #print fit
    #print '======'
    fittbl = dict()
    fittbl['tmin'] = tmintbl
    fittbl['tmax'] = tmaxtbl
    fittbl['fhtmin'] = fhtmintbl
    fittbl['fhtmax'] = fhtmaxtbl
    fittbl['pmean'] = pmean
    fittbl['psdev'] = psdev
    fittbl['post'] = post
    fittbl['p0'] = p0
    fittbl['prior'] = prior
    fittbl['chi2'] = chi2
    fittbl['chi2f'] = chi2f
    fittbl['dof'] = dof
    fittbl['logGBF'] = lgbftbl
    fittbl['Q'] = Q
    fittbl['rawoutput'] = rawoutput
    return fittbl
Example #52
0
   "data1" : np.array([0.1,1.0]),
   "data2" : np.array([0.1,0.5])
}
                           # a priori values for fit parameters
prior = dict(a=gv.gvar(0.5,0.5),b=gv.gvar(0.5,0.5))

# print(y["data1"][0].mean,"+-",y["data1"][0].sdev)
# print(gv.evalcov(y["data1"]))
def fcn(x,p):              # fit function of x and parameters p
   ans = {}
   for k in ["data1","data2"]:
      ans[k] = gv.exp(p['a'] + x[k]*p['b'])
   ans['b/a'] = p['b']/p['a']
   return ans

# do the fit
fit = lsqfit.nonlinear_fit(data=(x,y),prior=prior,fcn=fcn)
sys.stdout = open("eg0.out","w")
print(fit.format(maxline=True))     # print standard summary of fit

p = fit.p                  # best-fit values for parameters
outputs = dict(a=p['a'],b=p['b'])
outputs['b/a'] = p['b']/p['a']
inputs = dict(y=y,prior=prior)
print(fit.fmt_values(outputs))             # tabulate outputs
print(fit.fmt_errorbudget(outputs,inputs)) # print error budget for outputs

# save best-fit values in file "outputfile.p" for later use
import pickle
pickle.dump(fit.p,open("outputfile.p","wb"))
Example #53
0
def main():
    ### 1) least-squares fit to the data
    x = np.array([
        0.2, 0.4, 0.6, 0.8, 1.,
        1.2, 1.4, 1.6, 1.8, 2.,
        2.2, 2.4, 2.6, 2.8, 3.,
        3.2, 3.4, 3.6, 3.8
        ])
    y = gv.gvar([
        '0.38(20)', '2.89(20)', '0.85(20)', '0.59(20)', '2.88(20)',
        '1.44(20)', '0.73(20)', '1.23(20)', '1.68(20)', '1.36(20)',
        '1.51(20)', '1.73(20)', '2.16(20)', '1.85(20)', '2.00(20)',
        '2.11(20)', '2.75(20)', '0.86(20)', '2.73(20)'
        ])
    prior = make_prior()
    fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=fitfcn, extend=True)
    if LSQFIT_ONLY:
        sys.stdout = tee.tee(STDOUT, open('case-outliers-lsq.out', 'w'))
    elif not MULTI_W:
        sys.stdout = tee.tee(STDOUT, open('case-outliers.out', 'w'))
    print(fit)

    # plot data
    plt.errorbar(x, gv.mean(y), gv.sdev(y), fmt='o', c='b')

    # plot fit function
    xline = np.linspace(x[0], x[-1], 100)
    yline = fitfcn(xline, fit.p)
    plt.plot(xline, gv.mean(yline), 'k:')
    yp = gv.mean(yline) + gv.sdev(yline)
    ym = gv.mean(yline) - gv.sdev(yline)
    plt.fill_between(xline, yp, ym, color='0.8')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.savefig('case-outliers1.png', bbox_inches='tight')
    if LSQFIT_ONLY:
        return

    ### 2) Bayesian integral with modified PDF
    pdf = ModifiedPDF(data=(x, y), fcn=fitfcn, prior=prior)

    # integrator for expectation values with modified PDF
    expval = lsqfit.BayesIntegrator(fit, pdf=pdf)

    # adapt integrator to pdf
    expval(neval=1000, nitn=15)

    # evaluate expectation value of g(p)
    def g(p):
        w = 0.5 + 0.5 * p['2w-1']
        c = p['c']
        return dict(w=[w, w**2], mean=c, outer=np.outer(c,c))

    results = expval(g, neval=1000, nitn=15, adapt=False)
    print(results.summary())
    # expval.map.show_grid(15)

    if MULTI_W:
        sys.stdout = tee.tee(STDOUT, open('case-outliers-multi.out', 'w'))

    # parameters c[i]
    mean = results['mean']
    cov = results['outer'] - np.outer(mean, mean)
    c = mean + gv.gvar(np.zeros(mean.shape), gv.mean(cov))
    print('c =', c)
    print(
        'corr(c) =',
        np.array2string(gv.evalcorr(c), prefix=10 * ' '),
        '\n',
        )

    # parameter w
    wmean, w2mean = results['w']
    wsdev = gv.mean(w2mean - wmean ** 2) ** 0.5
    w = wmean + gv.gvar(np.zeros(np.shape(wmean)), wsdev)
    print('w =', w, '\n')

    # Bayes Factor
    print('logBF =', np.log(expval.norm))
    sys.stdout = STDOUT

    if MULTI_W:
        return

    # add new fit to plot
    yline = fitfcn(xline, dict(c=c))
    plt.plot(xline, gv.mean(yline), 'r--')
    yp = gv.mean(yline) + gv.sdev(yline)
    ym = gv.mean(yline) - gv.sdev(yline)
    plt.fill_between(xline, yp, ym, color='r', alpha=0.2)
    plt.savefig('case-outliers2.png', bbox_inches='tight')
Example #54
0
def main():
    gd.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    for nexp in range(2,8):
        if nexp == 2:
            sys_stdout = sys.stdout
            sys.stdout = tee.tee(sys_stdout, open("eg4GBF.out","w"))
        print '************************************* nexp =',nexp
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior,p0=p0)
        print fit                   # print the fit results
        # E = fit.p['E']              # best-fit parameters
        # a = fit.p['a']
        # print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        # print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        print
        if nexp == 3:
            sys.stdout = sys_stdout
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
    if DO_ERRORBUDGET:
        print E[1]/E[0]
        print (E[1]/E[0]).partialsdev(fit.prior['E'])
        print (E[1]/E[0]).partialsdev(fit.prior['a'])
        print (E[1]/E[0]).partialsdev(y)
        outputs = {'E1/E0':E[1]/E[0], 'E2/E0':E[2]/E[0],         
                 'a1/a0':a[1]/a[0], 'a2/a0':a[2]/a[0]}
        inputs = {'E':fit.prior['E'],'a':fit.prior['a'],'y':y}
        
        sys.stdout = tee.tee(sys_stdout, open("eg4GBFb.out","w"))
        print fit.fmt_values(outputs)
        print fit.fmt_errorbudget(outputs,inputs)
        sys.stdout = sys_stdout
        
    if DO_EMPBAYES:
        def fitargs(z,nexp=nexp,prior=prior,f=f,data=(x,y),p0=p0):
            z = gd.exp(z)
            prior['a'] = [gd.gvar(0.5,0.5*z[0]) for i in range(nexp)]
            return dict(prior=prior,data=data,fcn=f,p0=p0)
        ##
        z0 = [0.0]
        fit,z = lsqfit.empbayes_fit(z0,fitargs,tol=1e-3)
        sys.stdout = tee.tee(sys_stdout, open("eg4GBFa.out","w"))
        print fit                   # print the optimized fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]
        print "prior['a'] =",fit.prior['a'][0]
        sys.stdout = sys_stdout
        print
    
    if DO_PLOT:
        import pylab as pp   
        from gvar import mean,sdev     
        fity = f(x,fit.pmean)
        ratio = y/fity
        pp.xlim(0,21)
        pp.xlabel('x')
        pp.ylabel('y/f(x,p)')
        pp.errorbar(x=x,y=mean(ratio),yerr=sdev(ratio),fmt='ob')
        pp.plot([0.0,21.0],[1.0,1.0])
        pp.show()
Example #55
0
def make_prior():
    prior = {}
    prior['a'] = gv.gvar(1.0, 1.0)
    prior['E'] = gv.gvar(4.5, 100.0)
    prior['b'] = gv.gvar(-10.0, 50.0)
    
    return prior


if __name__ == '__main__':
    gv.ranseed([2009, 2010, 2011, 2012])
    p0 = None
    prior = make_prior()
    #p0 = {'a':0.1,'E':0.5}   # ***** #
    #print f(X,p0)
    fit = lsqfit.nonlinear_fit(data = (X, Y), fcn=f, prior=prior, p0=p0)
    print fit
    print(fit.format(maxline=True))











            
            
Example #56
0
def main():
    gv.ranseed([2009,2010,2011,2012]) # initialize random numbers (opt.)
    x,y = make_data()               # make fit data
    p0 = None                       # make larger fits go faster (opt.)
    sys_stdout = sys.stdout
    sys.stdout = tee.tee(sys.stdout, open("eg1.out","w"))
    for nexp in range(3,20):
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x,y),fcn=f,prior=prior,p0=p0) #, svdcut=SVDCUT)
        if fit.chi2/fit.dof<1.:
            p0 = fit.pmean          # starting point for next fit (opt.)
        if nexp in [8, 9, 10]:
            print(".".center(73))
        if nexp > 7 and nexp < 19:
            continue
        elif nexp not in [3]:
            print("")
        print '************************************* nexp =',nexp
        print fit.format()                   # print the fit results
        E = fit.p['E']              # best-fit parameters
        a = fit.p['a']
        print 'E1/E0 =',E[1]/E[0],'  E2/E0 =',E[2]/E[0]
        print 'a1/a0 =',a[1]/a[0],'  a2/a0 =',a[2]/a[0]

    # extra data 1
    print '\n--------------------- fit with extra information'
    sys.stdout = tee.tee(sys_stdout, open("eg1a.out", "w"))
    def ratio(p):
        return p['a'][1] / p['a'][0]
    newfit = lsqfit.nonlinear_fit(data=gv.gvar(1,1e-5), fcn=ratio, prior=fit.p)
    print (newfit)
    # print(newfit.p['a'][1] / newfit.p['a'][0])
    # print(fit.p['a'][1] / fit.p['a'][0])

    # alternate method for extra data
    sys.stdout = tee.tee(sys_stdout, open("eg1b.out", "w"))
    fit.p['a1/a0'] = fit.p['a'][1] / fit.p['a'][0]
    new_data = {'a1/a0' : gv.gvar(1,1e-5)}
    new_p = lsqfit.wavg([fit.p, new_data])
    print 'chi2/dof = %.2f\n' % (new_p.chi2 / new_p.dof)
    print 'E:', new_p['E'][:4]
    print 'a:', new_p['a'][:4]
    print 'a1/a0:', new_p['a1/a0']

    # # extra data 2
    # sys.stdout = tee.tee(sys_stdout, open("eg1b.out", "w"))
    # newfit = fit
    # for i in range(1):
    #     print '\n--------------------- fit with %d extra data sets' % (i+1)
    #     x, ynew = make_data()
    #     prior = newfit.p
    #     newfit = lsqfit.nonlinear_fit(data=(x,ynew), fcn=f, prior=prior) # , svdcut=SVDCUT)
    #     print newfit
    sys.stdout = sys_stdout
    # def fcn(x, p):
    #     return f(x, p), f(x, p)
    # prior = make_prior(nexp)
    # fit = lsqfit.nonlinear_fit(data=(x, [y, ynew]), fcn=fcn, prior=prior, p0=newfit.pmean) # , svdcut=SVDCUT)
    # print(fit)


    if DO_BOOTSTRAP:
        Nbs = 40                                     # number of bootstrap copies

        outputs = {'E1/E0':[], 'E2/E0':[], 'a1/a0':[],'a2/a0':[],'E1':[],'a1':[]}   # results
        for bsfit in fit.bootstrap_iter(n=Nbs):
            E = bsfit.pmean['E']                     # best-fit parameters
            a = bsfit.pmean['a']
            outputs['E1/E0'].append(E[1]/E[0])       # accumulate results
            outputs['E2/E0'].append(E[2]/E[0])
            outputs['a1/a0'].append(a[1]/a[0])
            outputs['a2/a0'].append(a[2]/a[0])
            outputs['E1'].append(E[1])
            outputs['a1'].append(a[1])
            # print E[:2]
            # print a[:2]
            # print bsfit.chi2/bsfit.dof

        # extract means and standard deviations from the bootstrap output
        for k in outputs:
            outputs[k] = gv.gvar(np.mean(outputs[k]),np.std(outputs[k]))
        print 'Bootstrap results:'
        print 'E1/E0 =',outputs['E1/E0'],'  E2/E1 =',outputs['E2/E0']
        print 'a1/a0 =',outputs['a1/a0'],'  a2/a0 =',outputs['a2/a0']
        print 'E1 =',outputs['E1'],'  a1 =',outputs['a1']
        
    if DO_PLOT:
        import pylab as plt   
        ratio = y/fit.fcn(x,fit.pmean)
        plt.xlim(0,21)
        plt.xlabel('x')
        plt.ylabel('y/f(x,p)')
        plt.errorbar(x=x,y=gv.mean(ratio),yerr=gv.sdev(ratio),fmt='ob')
        plt.plot([0.0,21.0],[1.0,1.0])
        plt.show()
Example #57
0
def fitscript_v2(trange,nstates,T,data,p,init=None,basak=None):  # +++CHANGE+++
    sets = len(data)/T
    #print "sets:", sets
    pmean = []
    psdev = []
    post = []
    p0 = []
    prior = []
    tmintbl = []
    tmaxtbl = []
    nstatestbl = [] #+++CHANGE+++
    chi2 = []
    dof = []
    lgbftbl = []
    rawoutput = []
    for n in nstates: #+++CHANGE+++
        priors = dict_of_tuple_to_gvar(meson_priors(p,n)) #+++CHANGE+++
        fitfcn = c51.fit_function(T) #+++CHANGE+++
        fcn = fitfcn.twopt_baryon_ss_ps #+++CHANGE+++
        for tmin in range(trange['tmin'][0], trange['tmin'][1]+1):
            for tmax in range(trange['tmax'][0], trange['tmax'][1]+1):
                x = x_indep(tmin, tmax)
                xlist, y = y_dep(x, data, sets)
                if basak is not None:
                    x = {'indep': x, 'basak': basak}
                else: pass
                fit = lsqfit.nonlinear_fit(data=(x,y),prior=priors,fcn=fcn,p0=init)
                pmean.append(fit.pmean)
                psdev.append(fit.psdev)
                post.append(fit.p)
                p0.append(fit.p0)
                prior.append(fit.prior)
                tmintbl.append(tmin)
                tmaxtbl.append(tmax)
                nstatestbl.append(n) #+++CHANGE+++
                chi2.append(fit.chi2)
                dof.append(fit.dof)
                lgbftbl.append(fit.logGBF)
                rawoutput.append(fit)
    #fcnname = str(fcn.__name__)
    #fitline = fcn(x,fit.p)
    #print "%s_%s_t, %s_%s_y, +-, %s_%s_fit, +-" %(fcnname, basak[0], fcnname, basak[0], fcnname, basak[0])
    #for i in range(len(xlist)):
    #    print xlist[i], ',', y[i].mean, ',', y[i].sdev, ',', fitline[i].mean, ',', fitline[i].sdev
    print '======'
    print fcn.__name__, basak
    print fit
    print '======'
    fittbl = dict()
    fittbl['nstates'] = nstatestbl #+++CHANGE+++
    fittbl['tmin'] = tmintbl
    fittbl['tmax'] = tmaxtbl
    fittbl['pmean'] = pmean
    fittbl['psdev'] = psdev
    fittbl['post'] = post
    fittbl['p0'] = p0
    fittbl['prior'] = prior
    fittbl['chi2'] = chi2
    fittbl['dof'] = dof
    fittbl['logGBF'] = lgbftbl
    fittbl['rawoutput'] = rawoutput
    return fittbl