pyerrors/examples/example_combined_fit.ipynb
2022-12-16 18:55:43 +01:00

40 KiB

None <html> <head> </head>
In [1]:
import pyerrors as pe
import numpy as np
import matplotlib.pyplot as plt
In [2]:
x_test = {'a':[0,1,2,3,4,5],'b':[0,1,2,3,4,5]}
y_test = {'a':[pe.Obs([np.random.normal(i, i*1.5, 1000)],['ensemble1']) for i in range(1,7)],
          'b':[pe.Obs([np.random.normal(val, val*1.5, 1000)],['ensemble1']) for val in [1.0,2.5,4.0,5.5,7.0,8.5]]}
for key in y_test.keys():
    [item.gamma_method() for item in y_test[key]]
In [3]:
def func_a(a, x):
    return a[1] * x + a[0]

def func_b(a, x):
    return a[2] * x + a[0]

funcs_test = {"a": func_a,"b": func_b}
In [4]:
output_test = pe.fits.least_squares(x_test,y_test,funcs_test,expected_chisquare=True)
Fit with 3 parameters
Method: migrad
Optimization terminated successfully.
chisquare/d.o.f.: 0.3395164548834892
fit parameters [0.98791658 1.00784727 1.56875359]
chisquare/expected_chisquare: 0.339844373345418
In [5]:
output_test.gamma_method()
In [6]:
print(output_test)
Goodness of fit:
χ²/d.o.f. = 0.339516
χ²/χ²exp  = 0.339844
p-value   = 0.9620
Fit parameters:
0	 0.988(35)
1	 1.008(32)
2	 1.569(42)

In [7]:
colour= {'a':'red','b':'black'}
plt.figure()
for key in funcs_test.keys():
    plt.errorbar(x_test[key],[o.value for o in y_test[key]],ls='none',marker='*',color=colour[key],yerr=[o.dvalue for o in y_test[key]],capsize=3,label=key)
    plt.plot([x_val for x_val in x_test[key]],[funcs_test[key](output_test.fit_parameters,x_val) for x_val in x_test[key]],color=colour[key],label='func_'+key)
plt.legend()
plt.show()
In [8]:
x_const = {'c':[0,1,2,3,4,5,6,7,8,9],'d':list(np.arange(10,20))}
y_const = {'c':[pe.Obs([np.random.normal(1, val, 1000)],['ensemble1']) 
               for val in [0.25,0.3,0.01,0.2,0.5,1.3,0.26,0.4,0.1,1.0]],
          'd':[pe.Obs([np.random.normal(1, val, 1000)],['ensemble1'])
               for val in [0.5,1.12,0.26,0.25,0.3,0.01,0.2,1.0,0.38,0.1]]}
for key in y_const.keys():
    [item.gamma_method() for item in y_const[key]]
In [9]:
#needs to be vectorized for expected chi2 to  work (jacobian matrix incorrect dim. otherwise)
#@anp.vectorize
def func_const(a,x):
    return a[0]#*anp.ones(len(x))

funcs_const = {"c": func_const,"d": func_const}
In [10]:
output_const = pe.fits.least_squares(x_const,y_const,funcs_const,method='migrad')
Fit with 1 parameter
Method: migrad
Optimization terminated successfully.
chisquare/d.o.f.: 1.444161495357013
fit parameters [0.9997047]
In [11]:
output_const.gamma_method()
In [12]:
output_const
Out[12]:
        chisquare: 27.439068411783246
 chisquare_by_dof: 1.444161495357013
              dof: 19
     fit_function: {'c': <function func_const at 0x7fd602cf69d8>, 'd': <function func_const at 0x7fd602cf69d8>}
   fit_parameters: [Obs[0.99970(22)]]
       iterations: 15
          message: 'Optimization terminated successfully.'
           method: 'migrad'
          p_value: 0.09483431965197764
In [13]:
output_const = pe.fits.least_squares(x_const,y_const,funcs_const,method='migrad')
Fit with 1 parameter
Method: migrad
Optimization terminated successfully.
chisquare/d.o.f.: 1.444161495357013
fit parameters [0.9997047]
In [14]:
y_const_ls = []
for key in y_const:
    for item in y_const[key]:
        y_const_ls.append(item)
In [15]:
print(y_const_ls)
[Obs[1.0101(80)], Obs[0.9908(97)], Obs[0.99919(32)], Obs[0.9962(64)], Obs[0.965(17)], Obs[1.004(42)], Obs[1.0094(82)], Obs[1.004(13)], Obs[0.9974(31)], Obs[0.954(34)], Obs[1.004(16)], Obs[1.058(37)], Obs[0.9893(84)], Obs[0.9895(85)], Obs[0.9914(96)], Obs[1.00028(33)], Obs[1.0005(62)], Obs[0.957(32)], Obs[0.988(13)], Obs[1.0040(32)]]
In [16]:
output_const2 = pe.fits.least_squares(list(np.arange(0,20)),y_const_ls, func_const)
Fit with 1 parameter
Method: Levenberg-Marquardt
`ftol` termination condition is satisfied.
chisquare/d.o.f.: 1.4441614953561615
In [17]:
output_const2.gamma_method()
In [18]:
output_const2
Out[18]:
        chisquare: 27.439068411767067
 chisquare_by_dof: 1.4441614953561615
              dof: 19
     fit_function: <function func_const at 0x7fd602cf69d8>
   fit_parameters: [Obs[0.99970(22)]]
       iterations: 7
          message: '`ftol` termination condition is satisfied.'
           method: 'Levenberg-Marquardt'
          p_value: 0.0948343196523247
In [19]:
colour= {'c':'red','d':'black'}
plt.figure()
for key in funcs_const.keys():
    plt.errorbar(x_const[key],[o.value for o in y_const[key]],ls='none',marker='*',
                 color=colour[key],yerr=[o.dvalue for o in y_const[key]],capsize=3,label=key)
plt.plot(np.arange(0,20),[func_const(output_const.fit_parameters,x_val) for x_val in list(np.arange(0,20))],
         label='combined fit',color ='lightblue')
plt.plot(np.arange(0,20),[func_const(output_const2.fit_parameters,x_val) for x_val in list(np.arange(0,20))],
         label='one fit',color='black',ls='--')
plt.legend()
plt.show()
</html>