-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEXAMPLE_chi2_test.py
32 lines (23 loc) · 969 Bytes
/
EXAMPLE_chi2_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# minimal needed modules and suppose curve_fit_utils to be in this directory
import numpy as np
from scipy.optimize import curve_fit
from curve_fit_utils import chi2_gof_test
# generate some fake data according to a model
pinit = np.ones(2)
def model(x, *p):
return p[0]+p[1]*x**2
xdata = np.arange(10)
ydata = model(xdata, *pinit)
yerrs = np.random.normal(loc=5, size=len(ydata)) # add some random errors
ydata += np.random.normal(scale=yerrs) # smear data with random noise
# fit the model using curve_fit
popt, pcov = curve_fit(model, xdata, ydata,
p0=pinit, sigma=yerrs, absolute_sigma=True)
# test the model
MSE, SSE, ndof, pvalue = chi2_gof_test(model, xdata, ydata, popt
sigma=yerrs, full_output=True)
# print results
print 'Reduced Chi-Square: ', MSE
print 'Chi-Square: ', SSE
print 'Degrees of freedom: ', ndof
print 'P-Value: ', pvalue #look at this to decide the results of the test