0
$\begingroup$

Okay, so I have simultaneously fitted 6 sets of data, with the same slope and different offsets:

def poly(x_, a, b, c1, c2, c3, c4, c5, c6):
    #all this is just to split x_data into the original parts of x
    l= len(x[0])
    l1= len(x[1])
    l2= len(x[2])
    l3= len(x[3])
    l4= len(x[4])
    l5= len(x[5])
    s=l+l1
    s1=l2+s
    s2=l3+s1
    s3=l4+s2
    s4=l5+s3


    a= np.hstack([
a*x_[:l]**2 + b*x_[:l] +c1,
a*x_[l:(s)]**2 + b*x_[l:(s)] +c2,
a*x_[(s):(s1)]**2 + b*x_[(s):(s1)] +c3,
a*x_[(s1):(s2)]**2 + b*x_[(s1):(s2)] +c4,
a*x_[(s2):(s3)]**2 + b*x_[(s2):(s3)] +c5,
a*x_[(s3):(s4)]**2 + b*x_[(s3):(s4)] +c6
])       
    #print a
    return a 
x_data = np.hstack([x[0],x[1],x[2],x[3],x[4],x[5]])
y_data = np.hstack([y[0],y[1],y[2],y[3],y[4],y[5]])
yerr_data = np.hstack([yerr[0],yerr[1],yerr[2],yerr[3],yerr[4],yerr[5]])

(a, b, c1, c2, c3, c4, c5, c6), pcov= curve_fit(poly, x_data, y_data,sigma=yerr_data,absolute_sigma=True)
ny_data = poly(x_data,a, b, c1, c2, c3, c4, c5, c6)

The code is crude, but it does the job... In the end I have 6 fitted curves. Now if I want to characterise the goodness of fit I wish to find the chi-squared and p-value of the fit. I calculate this using:

r_chi= (np.sum(((ny_data - y_data) ** 2)/yerr_data))/(len(y_data)-8-1) 
print r_chi

So I calculate the chi-squared and divide by (data length - number of parameters - 1).

The end result is r_chi = 0.0513125529638

I have a feeling I'm doing something wrong, any suggestions?

$\endgroup$

1 Answer 1

0
$\begingroup$

One approach would be to calculate the six individual values, one for each data set, and then sum the relative contribution to the total based on the number of data points in each data set.

As an example, if one of the data sets contained 15 percent of the total number of data points, then its share of the overall r_chi would be 15 percent.

$\endgroup$
1
  • $\begingroup$ This seems sensible, and makes sense :-) like a weighted chi-squared.. Thanks! $\endgroup$
    – Akerfeldt
    Commented Nov 2, 2016 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.