def run_once(num_elements):
    import tee
    import time
    tic = time.clock()
    # 0 means don't use PyTorch
    with_pytorch = 0
    num_scatterers = int(1e6)
    tee.tee(f'{exe} {with_pytorch} {num_scatterers} {num_elements}')
    return time.clock() - tic
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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()
Exemplo n.º 5
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
Exemplo n.º 6
0
def main():
    sys_stdout = sys.stdout

    # version 1 - relative errors
    sys.stdout = tee.tee(sys_stdout, open("eg7a.out", "w"))

    # fit data and prior
    x = np.array([1., 2., 3., 4.])
    y = np.array([3.4422, 1.2929, 0.4798, 0.1725])
    prior = gv.gvar(['10(1)', '1.0(1)'])

    # fit function
    def fcn(x, p):
        return p[0] * gv.exp(-p[1] * x)

    # find optimal dy
    def fitargs(z):
        dy = y * z
        newy = gv.gvar(y, dy)
        return dict(data=(x, newy), fcn=fcn, prior=prior)

    fit, z = lsqfit.empbayes_fit(0.001, fitargs)
    print fit.format(True)
    if MAKE_PLOT:
        ratio = fit.y / fcn(x, fit.pmean)
        plt.errorbar(x=fit.x, y=gv.mean(ratio), yerr=gv.sdev(ratio), c='b')
        plt.plot([0.5, 4.5], [1.0, 1.0], c='r')

    # version 2 - additive errors
    sys.stdout = tee.tee(sys_stdout, open("eg7b.out", "w"))

    def fitargs(z):
        dy = np.ones_like(y) * z
        newy = gv.gvar(y, dy)
        return dict(data=(x, newy), fcn=fcn, prior=prior)

    fit, z = lsqfit.empbayes_fit(0.001, fitargs)
    print fit.format(True)

    if MAKE_PLOT:
        ratio = fit.y / fcn(x, fit.pmean)
        plt.errorbar(x=fit.x + 0.1,
                     y=gv.mean(ratio),
                     yerr=gv.sdev(ratio),
                     c='g')
        plt.show()
Exemplo n.º 7
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
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
0
def test_fit():
	sys.stdout = STDOUT
	fit = good_analysis()
	sp0 = []
	q = []
	sys.stdout = tee.tee(STDOUT, open(OUTDIR+'eg-appendix1f.out', 'w'))
	for sfit in fit.simulated_fit_iter(20):
		# print sfit.format(maxline=-1), sfit.p[0] - sfit.pexact[0]
		sp0.append(sfit.pmean[0])
		q.append(sfit.Q)
	print(np.average(sp0), np.std(sp0), np.average(q))
Exemplo n.º 11
0
def test_fit():
    sys.stdout = STDOUT
    fit = good_analysis(plot=False)
    sp0 = []
    q = []
    sys.stdout = tee.tee(STDOUT, open(OUTDIR + 'eg-appendix1f.out', 'w'))
    for sfit in fit.simulated_fit_iter(20):
        # print sfit.format(maxline=-1), sfit.p[0] - sfit.pexact[0]
        sp0.append(sfit.pmean[0])
        q.append(sfit.Q)
    print(np.average(sp0), np.std(sp0), np.average(q))
Exemplo n.º 12
0
    def run_test(self, testobj, fixtures=None):
        '''
        Run the given test.

        This performs the bulkwork of the testing framework.

        1. Handle setup for all fixtures required for the specific test.

        2. Run the test.

        3. Teardown the fixtures for the test which are tied locally to the\
            test.
        '''
        outdir = test_results_output_path(testobj)
        mkdir_p(outdir)
        fstdout_name = joinpath(outdir, config.constants.system_err_name)
        fstderr_name = joinpath(outdir, config.constants.system_out_name)

        # Capture the output into a file.
        with tee(fstderr_name, stderr=True, stdout=False),\
                tee(fstdout_name, stderr=False, stdout=True):
            return self._run_test(testobj, fstdout_name, fstderr_name,
                                  fixtures)
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo n.º 15
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)
Exemplo n.º 16
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)
Exemplo n.º 17
0
def component(df, bill_components):

    #df = component_type(df, bill_components)
    df = connection_type(df, bill_components)
    df = end_forms(df, bill_components)

    df = sleeve.sleeve(df, bill_components, comp_sleeve)
    df = adaptor.adaptor(df, bill_components, comp_adaptor)
    df = boss.boss(df, bill_components, comp_boss)
    df = elbow.elbow(df, bill_components, comp_elbow)
    df = float_.float_(df, bill_components, comp_float)
    df = hfl.hfl(df, bill_components, comp_hfl)
    df = nut.nut(df, bill_components, comp_nut)
    #df = other.other(df, bill_components, comp_other)
    df = straight.straight(df, bill_components, comp_straight)
    df = tee.tee(df, bill_components, comp_tee)
    df = threaded.threaded(df, bill_components, comp_threaded)

    return df
Exemplo n.º 18
0
def main():
    sys_stdout = sys.stdout
    sys.stdout = tee.tee(sys.stdout, open("eg2.out", "w"))
    x, y = make_data()
    prior = make_prior(x)
    fit = lsqfit.nonlinear_fit(prior=prior, data=y, fcn=fcn)
    print(fit.format())

    if MAKE_PLOT:
        import matplotlib.pyplot as plt
        plt.errorbar(x=gv.mean(x),
                     xerr=gv.sdev(x),
                     y=gv.mean(y),
                     yerr=gv.sdev(y),
                     fmt='ob')
        # plot fit line
        x = np.linspace(0.99 * min(x).mean, 1.01 * max(x).mean, 100)
        p = dict(b=fit.pmean['b'], x=x)
        y = fcn(p)
        plt.xlabel('x')
        plt.ylabel('y')
        plt.plot(x, y, ':r')
        plt.savefig('eg2.png', bbox_inches='tight')
        plt.show()
Exemplo n.º 19
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)
    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 = p['w']
        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(results.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')
Exemplo n.º 20
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()
Exemplo n.º 21
0
def main():
    sys_stdout = sys.stdout
    sys.stdout = tee.tee(sys.stdout, open("eg3a.out","w"))
    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) =', gv.evalcorr(fit.p[:2])[1,0]

    if DO_PLOT:
        plt.semilogx()
        plt.errorbar(
            x=gv.mean(x), xerr=gv.sdev(x), y=gv.mean(y), yerr=gv.sdev(y),
            fmt='ob'
            )
        # plot fit line
        xx = np.linspace(0.99 * gv.mean(min(x)), 1.01 * gv.mean(max(x)), 100)
        yy = fcn(xx, fit.pmean)
        plt.xlabel('x')
        plt.ylabel('y')
        plt.plot(xx, yy, ':r')
        plt.savefig('eg3.png', bbox_inches='tight')
        plt.show()

    sys.stdout = sys_stdout
    if DO_BOOTSTRAP:
        gv.ranseed(123)
        sys.stdout = tee.tee(sys_stdout, open('eg3c.out', 'w'))
        print fit
        print 'p1/p0 =', fit.p[1] / fit.p[0], '    p3/p2 =', fit.p[3] / fit.p[2]
        print 'corr(p0,p1) =', gv.evalcorr(fit.p[:2])[1,0]
        Nbs = 40
        outputs = {'p':[], 'p1/p0':[], 'p3/p2':[]}
        for bsfit in fit.bootstrap_iter(n=Nbs):
            p = bsfit.pmean
            outputs['p'].append(p)
            outputs['p1/p0'].append(p[1] / p[0])
            outputs['p3/p2'].append(p[3] / p[2])
        print '\nBootstrap Averages:'
        outputs = gv.dataset.avg_data(outputs, bstrap=True)
        print gv.tabulate(outputs)
        print 'corr(p0,p1) =', gv.evalcorr(outputs['p'][:2])[1,0]

        # make histograms of p1/p0 and p3/p2
        sys.stdout = sys_stdout
        print
        sys.stdout = tee.tee(sys_stdout, open('eg3d.out', 'w'))
        print 'Histogram Analysis:'
        count = {'p1/p0':[], 'p3/p2':[]}
        hist = {
            'p1/p0':gv.PDFHistogram(fit.p[1] / fit.p[0]),
            'p3/p2':gv.PDFHistogram(fit.p[3] / fit.p[2]),
            }
        for bsfit in fit.bootstrap_iter(n=1000):
            p = bsfit.pmean
            count['p1/p0'].append(hist['p1/p0'].count(p[1] / p[0]))
            count['p3/p2'].append(hist['p3/p2'].count(p[3] / p[2]))
        count = gv.dataset.avg_data(count)
        plt.rcParams['figure.figsize'] = [6.4, 2.4]
        pltnum = 1
        for k in count:
            print k + ':'
            print hist[k].analyze(count[k]).stats
            plt.subplot(1, 2, pltnum)
            plt.xlabel(k)
            hist[k].make_plot(count[k], plot=plt)
            if pltnum == 2:
                plt.ylabel('')
            pltnum += 1
        plt.rcParams['figure.figsize'] = [6.4, 4.8]
        plt.savefig('eg3d.png', bbox_inches='tight')
        plt.show()

    if DO_BAYESIAN:
        gv.ranseed(123)
        sys.stdout = tee.tee(sys_stdout, open('eg3e.out', 'w'))
        print fit
        expval = lsqfit.BayesIntegrator(fit)

        # adapt integrator to PDF from fit
        neval = 1000
        nitn = 10
        expval(neval=neval, nitn=nitn)

        # <g(p)> gives mean and covariance matrix, and histograms
        hist = [
            gv.PDFHistogram(fit.p[0]), gv.PDFHistogram(fit.p[1]),
            gv.PDFHistogram(fit.p[2]), gv.PDFHistogram(fit.p[3]),
            ]
        def g(p):
            return dict(
                mean=p,
                outer=np.outer(p, p),
                count=[
                    hist[0].count(p[0]), hist[1].count(p[1]),
                    hist[2].count(p[2]), hist[3].count(p[3]),
                    ],
                )

        # evaluate expectation value of g(p)
        results = expval(g, neval=neval, nitn=nitn, adapt=False)

        # analyze results
        print('\nIterations:')
        print(results.summary())
        print('Integration Results:')
        pmean = results['mean']
        pcov =  results['outer'] - np.outer(pmean, pmean)
        print '    mean(p) =', pmean
        print '    cov(p) =\n', pcov

        # create GVars from results
        p = gv.gvar(gv.mean(pmean), gv.mean(pcov))
        print('\nBayesian Parameters:')
        print(gv.tabulate(p))

        # show histograms
        print('\nHistogram Statistics:')
        count = results['count']
        for i in range(4):
            print('p[{}] -'.format(i))
            print(hist[i].analyze(count[i]).stats)
            plt.subplot(2, 2, i + 1)
            plt.xlabel('p[{}]'.format(i))
            hist[i].make_plot(count[i], plot=plt)
            if i % 2 != 0:
                plt.ylabel('')
        plt.savefig('eg3e.png', bbox_inches='tight')
        plt.show()

    if DO_SIMULATION:
        gv.ranseed(1234)
        sys.stdout = tee.tee(sys_stdout, open('eg3f.out', 'w'))
        print(40 * '*' + ' real fit')
        print(fit.format(True))

        Q = []
        p = []
        for sfit in fit.simulated_fit_iter(n=3, add_priornoise=False):
            print(40 * '=' + ' simulation')
            print(sfit.format(True))
            diff = sfit.p - sfit.pexact
            print '\nsfit.p - pexact =', diff
            print(gv.fmt_chi2(gv.chi2(diff)))
            print

    # omit constraint
    sys.stdout = tee.tee(sys_stdout, open("eg3b.out", "w"))
    prior = gv.gvar(4 * ['0(1)'])
    prior[1] = gv.gvar('0(20)')
    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) =', gv.evalcorr(fit.p[:2])[1,0]
Exemplo n.º 22
0
 def addnewtee(self, x, y):
     self.sim.unitops.append(tee(len(self.sim.unitops), x, y, globe.TeeDefaultNOut))
Exemplo n.º 23
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()
Exemplo n.º 24
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()
Exemplo n.º 25
0
# print (y)

print()
log_prior = BufferDict()
log_prior['log(a)'] = log(gvar(0.02, 0.02))
sqrt_prior = BufferDict()
sqrt_prior['sqrt(a)'] = sqrt(gvar(0.02, 0.02))
prior = BufferDict(a=gvar(0.02, 0.02))
unif_prior = BufferDict()
unif_prior['f(a)'] = BufferDict.uniform('f', 0, 0.04)

stdout = sys.stdout
for p in [prior, log_prior, sqrt_prior, unif_prior]:
    key = list(p.keys())[0].replace('(a)', '_a')
    sys.stdout = tee.tee(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))
    print(f)
    print("a =", f.p['a'])

sys.stdout = tee.tee(sys_stdout, open("eg6-erfinv.out", "w"))
prior = BufferDict()
prior['erfinv(50a-1)'] = gvar('0(1)') / sqrt(2)


def fcn(p, N=len(y)):
    a = 0.02 + 0.02 * p['50a-1']
Exemplo n.º 26
0
                    "--timesteps",
                    help="Number of timesteps.",
                    action="append",
                    dest="nlvar",
                    type=lambda x: "cntrl.nstlim=" + str(x))

parser.set_defaults(nlvar=[])
args = parser.parse_args(sys.argv[1:])
if args.runname is None:
    args.runname = args.dataset

rundir = os.path.join(case.CASEROOT, 'run', args.runname, RUNID)
runlog = os.path.join(rundir, 'submission.log')
if not os.path.exists(rundir):
    os.makedirs(rundir)
tee.tee(runlog, sys.stdout, sys.stderr)

lastrundir = os.path.join(case.CASEROOT, 'run', args.runname, 'latest')
dataset_matched = []
for dataset in datasets:
    if dataset.startswith(args.dataset):
        dataset_matched.append(dataset)
if len(dataset_matched) < 1:
    print("No dataset matched for %s" % args.dataset)
    sys.exit(1)
if len(dataset_matched) > 1:
    print("Multiple dataset matched for %s, available datasets matched: %s" %
          (args.dataset, str(dataset_matched)))
    sys.exit(1)
indir = os.path.join(case.INPUTROOT, dataset_matched[0])
Exemplo n.º 27
0

gv.ranseed(123456)


def f(x, p):
    return p[0] + p[1] * np.exp(-p[2] * x)


p0 = [0.5, 0.4, 0.7]

N = 10000
x = np.linspace(0.2, 1.0, N)
y = make_fake_data(x, p0, f)

sys.stdout = tee.tee(sys_stdout, open('eg9a.out', 'w'))
print('x = [{}  {} ... {}]'.format(x[0], x[1], x[-1]))
print('y = [{}  {} ... {}]'.format(y[0], y[1], y[-1]))
print('corr(y[0],y[9999]) =', gv.evalcorr([y[0], y[-1]])[1, 0])
print()


# fit function and prior
def fcn(x, p):
    return p[0] + p[1] * np.exp(-p[2] * x)


prior = gv.gvar(['0(1)', '0(1)', '0(1)'])

# Nstride fits, each to nfit data points
nfit = 100
Exemplo n.º 28
0
	help='exclude results with less than %(metavar)s duplicate files (implicit value: %(const)s)')
parser_duplicates.set_defaults(func=duplicates)

args = parser.parse_args()

# find.wide option
if args.func == find and args.wide:
	args.pattern = '*' + args.pattern + '*'
# duplicates.filter option
if args.func == duplicates and args.filter is None:
	args.filter = []

# log option
if args.log_file:
	if args.no_stdout:
		sys.stdout = args.log_file
	else:
		sys.stdout = tee.tee(
				codecs.getwriter(sys.stdout.encoding)(sys.stdout, 'replace'),
				args.log_file
				)
elif args.no_stdout: sys.stdout = fake_fd()

db = files_db.FilesDb(exclude_drives=args.exclude_drives, db_filepath=args.db_filepath)
if args.drives and args.exclude_drives: args.drives = [drive for drive in args.drives if drive not in args.exclude_drives]
_loaddb(db, args.quiet < 2)
if args.updatedb and args.func != updatedb: updatedb(db, args)
if args.quiet < 2: print

args.func(db, args)
Exemplo n.º 29
0
    def copyfrom(self, simcopyfrom):
        #//MemoryStream myStream = new MemoryStream();
        #//simulation tempsim = new simulation();
        #//BinaryFormatter bf = new BinaryFormatter();
        #//bf.Serialize(myStream, simcopyfrom);
        #//myStream.Seek(0, SeekOrigin.Begin);
        #//tempsim = (simulation)bf.Deserialize(myStream);
        #//myStream.Close();
        #//unitops = tempsim.unitops;
        #//streams = tempsim.streams;
        #//pidcontrollers = tempsim.pidcontrollers;
        if (len(self.unitops) == 0):
            self.unitops = []
            for i in range(len(simcopyfrom.unitops)):
                #if simcopyfrom.unitops[i].objecttype == globe.objecttypes.CoolingTowerSimple:
                #    #obj = coolingtowersimple()
                #    obj.coolingtowersimplecopyconstructor(simcopyfrom.unitops[i])
                #    self.unitops.append(obj)
                #elif simcopyfrom.unitops[i].objecttype == globe.objecttypes.CoolingTowerHeatExchangerSimple:
                #    obj = coolingtowerheatexchangersimple()
                #    obj.coolingtowerheatexchangersimplecopyconstructor(simcopyfrom.unitops[i])
                #    self.unitops.append(obj)
                if simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.CoolingTower:
                    obj = coolingtower(0, 0, 0)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                #elif simcopyfrom.unitops[i].objecttype == globe.objecttypes.DistillationColumn:
                #    obj = distillationcolumn()
                #    obj.copyfrom(simcopyfrom.unitops[i])
                #   self.unitops.append(obj)
                #elif simcopyfrom.unitops[i].objecttype == globe.objecttypes.Flange:
                #    obj = flange()
                #    obj.flangecopyconstructor(simcopyfrom.unitops[i])
                #    self.unitops.append(obj)
                #elif simcopyfrom.unitops[i].objecttype == globe.objecttypes.GasPipe:
                #   obj = gaspipe()
                #   obj.gaspipecopyconstructor(simcopyfrom.unitops[i])
                #    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.HeatExchangerSimple:
                    obj = heatexchangersimple(0, 0, 0)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.Mixer:
                    obj = mixer(0, 0, 0, simcopyfrom.unitops[i].nin)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.Pump:
                    obj = pump(0.0, 0.0, 0.0, 0.0, 0.0, \
                            0.0, 0.0, 1)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.Tee:
                    obj = tee(0.0, 0.0, 0.0, simcopyfrom.unitops[i].nout)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.Valve:
                    obj = valve(0, 0.0, 0.0)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                elif simcopyfrom.unitops[
                        i].objecttype == globe.objecttypes.Tank:
                    obj = tank(0, 0.0, 0.0)
                    obj.copyfrom(simcopyfrom.unitops[i])
                    self.unitops.append(obj)
                else:
                    break
        else:
            for i in range(len(simcopyfrom.unitops)):
                self.unitops[i].copyfrom(simcopyfrom.unitops[i])

        if (len(self.streams) == 0):
            self.streams = []
            for i in range(len(simcopyfrom.streams)):
                obj = stream(0, 0.0, 0.0, 0.0, 0.0)
                obj.copyfrom(simcopyfrom.streams[i])
                self.streams.append(obj)
        else:
            for i in range(len(simcopyfrom.streams)):
                self.streams[i].copyfrom(simcopyfrom.streams[i])

        #if (len(self.signals) == 0):
        #    self.signals = []
        #    for i in range(len(simcopyfrom.signals)):
        #        obj = signal()
        #       obj.copyfrom(simcopyfrom.signals[i])
        #        self.signals.append(obj)
        #else:
        #    for i in range(len(simcopyfrom.signals)):
        #        self.signals[i].copyfrom(simcopyfrom.signals[i])

        if (len(self.pidcontrollers) == 0):
            self.pidcontrollers = []
            for i in range(len(simcopyfrom.pidcontrollers)):
                obj = pidcontroller()
                obj.copyfrom(simcopyfrom.pidcontrollers[i])
                self.pidcontrollers.append(obj)
        else:
            for i in range(len(simcopyfrom.pidcontrollers)):
                self.pidcontrollers[i].copyfrom(simcopyfrom.pidcontrollers[i])

        #if (len(self.blocks) == 0):
        #    self.blocks = []
        #    for i in range(len(simcopyfrom.blocks)):
        #       if simcopyfrom.blocks[i].objecttype == globe.objecttypes.ControlMVSignalSplitter:
        #           obj = controlmvsignalsplitter()
        #           obj.copyfrom(simcopyfrom.blocks[i])
        #           blocks.append(obj)
        #       else:
        #           break
        #else:
        #   for i in range(len(simcopyfrom.blocks)):
        #        self.blocks[i].copyfrom(simcopyfrom.blocks[i])

        #//public List<nmpc> nmpccontrollers; The nmpc controller(s) are not going to be copied at this point in time.
        self.simi = simcopyfrom.simi  #//Counting index for this class for simulation indexing for historisation and simulation.
Exemplo n.º 30
0
import sys
import io
import subprocess
import case
sys.path.append(case.CASETOOL)
import filescan
import tee
#sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)

BLDID = datetime.datetime.strftime(datetime.datetime.now(), "%y%m%d-%H%M%S")
bldlog = os.path.join(case.CASEROOT, "bld", "bldlog.%s" % BLDID)
objdir = os.path.join(case.CASEROOT, "bld", "obj")
if not os.path.exists(objdir):
    os.makedirs(objdir)

tee.tee(bldlog, os.path.sys.stdout, sys.stderr)

os.environ["CASEROOT"] = case.CASEROOT
os.environ["PMEMDSRC"] = case.PMEMDSRC
os.environ["CASETOOL"] = case.CASETOOL
os.environ['BLDID'] = BLDID
os.environ["BINDIR"] = os.path.join(case.CASEROOT, "bld")

os.chdir(case.CASEROOT)
print("Commiting SourceMods before build...")
os.chdir("SourceMods")
os.system("git add -A .")
deleted_files = filescan.get_deleted('.')

os.system("git commit --allow-empty -m 'auto commit for build: %s'" % BLDID)
Exemplo n.º 31
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')
Exemplo n.º 32
0
def main():
    x, y = make_data()  # make fit data
    # y = gv.gvar(gv.mean(y), 0.75**2 * gv.evalcov(y))
    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, 7):
        prior = make_prior(nexp)
        fit = lsqfit.nonlinear_fit(data=(x, y), fcn=fcn, prior=prior, p0=p0)
        if fit.chi2 / fit.dof < 1.:
            p0 = fit.pmean  # starting point for next fit (opt.)
        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]
            print

    # error budget
    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}
    inputs = collections.OrderedDict()
    inputs['a'] = fit.prior['a']
    inputs['E'] = fit.prior['E']
    inputs['y'] = fit.data[1]
    print '================= Error Budget Analysis'
    print fit.fmt_values(outputs)
    print fit.fmt_errorbudget(outputs, inputs)

    sys.stdout = sys_stdout
    # print(gv.gvar(str(a[1])) / gv.gvar(str(a[0])) )
    # print(gv.evalcorr([fit.p['a'][1], fit.p['E'][1]]))
    # print(fit.format(True))

    # redo fit with 4 parameters since that is enough
    prior = make_prior(4)
    fit = lsqfit.nonlinear_fit(data=(x, y), fcn=fcn, prior=prior, p0=fit.pmean)
    sys.stdout = tee.tee(sys_stdout, open("eg1a.out", "w"))
    print '--------------------- original fit'
    print fit.format()
    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
    # extra data 1
    print '\n--------------------- new fit to extra information'

    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.format())
    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]

    if DO_PLOT:
        import matplotlib.pyplot as plt
        ratio = y / fit.fcn(x, fit.pmean)
        plt.xlim(4, 15)
        plt.ylim(0.95, 1.05)
        plt.xlabel('x')
        plt.ylabel('y / f(x,p)')
        plt.yticks([0.96, 0.98, 1.00, 1.02, 1.04],
                   ['0.96', '0.98', '1.00', '1.02', '1.04'])
        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()

    # 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=40000, 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=40000, 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()

    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']
Exemplo n.º 33
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()
Exemplo n.º 34
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()
Exemplo n.º 35
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()
Exemplo n.º 36
0
def do_fit(svdcut=None, do_plot=False):
    if svdcut is None:
        svdcut = lsqfit.nonlinear_fit.set()['svdcut']
        sys.stdout = tee.tee(sys_stdout, open('eg5a.out', 'w'))
        default_svd = True
    else:
        default_svd = False
    x, y = make_data()
    prior = make_prior(
        100)  # 20 exponential terms in all (10 gives same result)
    p0 = None
    for nexp in range(1, 6):
        # marginalize the last 100 - nexp terms
        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)
        # fit modified data with just nexp terms
        fit = lsqfit.nonlinear_fit(data=(x, ymod),
                                   prior=fit_prior,
                                   fcn=fcn,
                                   p0=p0,
                                   tol=1e-10,
                                   svdcut=svdcut)
        if not default_svd and nexp == 5:
            sys.stdout = tee.tee(sys_stdout, open('eg5b.out', 'w'))
        print '************************************* nexp =', nexp
        print fit.format(True)
        p0 = fit.pmean
        if do_plot:
            import matplotlib.pyplot as plt
            if nexp > 4:
                continue
            plt.subplot(2, 2, nexp)
            if nexp not in [1, 3]:
                plt.yticks([0.05, 0.10, 0.15, 0.20, 0.25], [])
            else:
                plt.ylabel('y')
            if nexp not in [3, 4]:
                plt.xticks([1.0, 1.5, 2.0, 2.5], [])
            else:
                plt.xlabel('x')
            plt.errorbar(x=x, y=gv.mean(ymod), yerr=gv.sdev(ymod), fmt='bo')
            plt.plot(x, y, '-r')
            plt.plot(x, fcn(x, fit.pmean), ':k')
            plt.text(1.75, 0.22, 'nexp = {}'.format(nexp))
            if nexp == 4:
                plt.savefig('eg5.png', bbox_inches='tight')
                plt.show()
    # 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,
    }
    print(gv.fmt_values(outputs))
    print(gv.fmt_errorbudget(outputs, inputs))
    sys.stdout = sys_stdout
Exemplo n.º 37
0
def main():
    gv.ranseed(SEED)
    y = exact(NSAMPLE)
    ysamples = [yi for yi in gv.raniter(y, n=NSAMPLE)]
    # above code (don't comment it out) generates the following
    ysamples = [
        [0.0092441016, 0.0068974057, 0.0051480509, 0.0038431422, 0.0028690492],
        [0.0092477405, 0.0069030565, 0.0051531383, 0.0038455855, 0.0028700587],
        [0.0092558569, 0.0069102437, 0.0051596569, 0.0038514537, 0.0028749153],
        [0.0092294581, 0.0068865156, 0.0051395262, 0.003835656, 0.0028630454],
        [0.009240534, 0.0068961523, 0.0051480046, 0.0038424661, 0.0028675632],
    ]
    dstr = '['
    for yi in ysamples:
        dstr += ('[' + len(yi) * '{:10.8g},' + '],').format(*yi)
    dstr += ']'
    ysamples = eval(dstr)
    print(np.array(ysamples).tolist())
    s = gv.dataset.svd_diagnosis(ysamples)
    # s.plot_ratio(show=True)
    y = s.avgdata
    x = np.array([15., 16., 17., 18., 19.])

    def f(p):
        return p['a'] * gv.exp(-p['b'] * x)

    prior = gv.gvar(dict(a='0.75(5)', b='0.30(3)'))
    sys_stdout = sys.stdout

    sys.stdout = tee.tee(sys_stdout, open('eg10a.out', 'w'))
    fit = lsqfit.nonlinear_fit(data=y, fcn=f, prior=prior, svdcut=0.0)
    print(fit)

    sys.stdout = tee.tee(sys_stdout, open('eg10b.out', 'w'))
    fit = lsqfit.nonlinear_fit(data=y, fcn=f, prior=prior, svdcut=s.svdcut)
    print(fit)

    sys.stdout = tee.tee(sys_stdout, open('eg10c.out', 'w'))
    fit = lsqfit.nonlinear_fit(data=y,
                               fcn=f,
                               prior=prior,
                               svdcut=s.svdcut,
                               add_svdnoise=True)
    print(fit)

    sys.stdout = tee.tee(sys_stdout, open('eg10d.out', 'w'))
    yex = gv.gvar(gv.mean(y), gv.evalcov(exact(1.)))
    fit = lsqfit.nonlinear_fit(data=yex, fcn=f, prior=prior, svdcut=0)
    print(fit)
    # fit.plot_residuals().show()

    sys.stdout = tee.tee(sys_stdout, open('eg10e.out', 'w'))
    fit = lsqfit.nonlinear_fit(data=y, fcn=f, prior=prior, svdcut=s.svdcut)
    print(fit)
    print('\n================ Add noise to prior, SVD')
    noisyfit = lsqfit.nonlinear_fit(data=y,
                                    prior=prior,
                                    fcn=f,
                                    svdcut=s.svdcut,
                                    add_svdnoise=True,
                                    add_priornoise=True)
    print(noisyfit.format(True))
    # save figures
    fit.qqplot_residuals(plot=plt).savefig('eg10e1.png', bbox_inches='tight')
    plt.cla()
    noisyfit.qqplot_residuals(plot=plt).savefig('eg10e2.png',
                                                bbox_inches='tight')
Exemplo n.º 38
0
    # true function
    fm = 1. - .3 / m - .3 / m**2
    plt.plot(m, fm, 'k--')
    for s in data:
        plt.plot()
        ainv, am = param[s]
        ms = ainv * am 
        d = gv.mean(data[s])
        derr = gv.sdev(data[s])
        col = next(coliter)
        plt.errorbar(x=ms, y=d, yerr=derr, fmt=col + 'o')
        plt.text(ms[-1] - 0.6, d[-1], s, color=col, fontsize='x-large')
        fs = gv.mean(fm)
        ams = m / ainv
        idx = ams < am[-1]
        ams = ams[idx]
        fs = gv.mean(fm[idx])
        for i, ci in enumerate(fit.p['c']):
            fs += ci.mean * ams ** (2 * (i + 1))
        plt.plot(m[idx], fs, col + ':')
    plt.xlabel('m')
    plt.ylabel('f')
    plt.text(8, 0.65, 'f(m)', fontsize='x-large')
    plt.savefig('eg-spline.png', bbox_inches='tight')
    plt.show()

if __name__ == "__main__":
    import tee 
    import sys
    sys.stdout = tee.tee(sys.stdout, open('eg-spline.out', 'w'))
    main()