 # absolute error of curve fit parameters

Hi,

I'm a little bit desperated. I googled for days to find an appropriate way
to calculate the absolute errors of parameters of non-linear fits. I found
different sites which suggest different, sometimes more or less similar
ways. But the results don't make sense. Here is a simple example:

from pylab import *
from scipy.optimize import curve_fit
def f(x,a,b):
return a*x+b
def fit(n=1000):
x,y=array([0,1,1]),array([1,2,4])
xf=[(min(x)+i*(max(x)-min(x))/float(n)) for i in range(0,int(n)+1)]
a,acov=curve_fit(f,x,y)
yf=[f(*tuple([i]+list(a))) for i in xf]
meansq=sum([pow(y[i]-f(x[i],a,a),2)/(len(x)) for i in
range(0,len(x))])
redchi2=sum([pow(y[i]-f(x[i],a,a),2)/(len(x)-len(a)) for i in
range(0,len(x))])
print 'standard dev:',sqrt(diag(acov))
print 'abs. meansq err.:',[acov[i][i]*sqrt(meansq) for i in
range(0,len(a))]
print 'abs. reduced meansq err.:',[acov[i][i]*sqrt(redchi2) for i in
range(0,len(a))]
fit()

I included 3 ways I found. The used arrays have 3 points, where for x=1 I
give 2 different y values. I would say,
a=2+/-1, b=1+/-0,
but no way yields to a similar result! How can I trust this function on more
complicated, really non-linear functions? Or is simply this example wrong?
Or are the methods not right?
I'm very thankful for any help!

Best regards,
André

···

--
View this message in context: http://old.nabble.com/absolute-error-of-curve-fit-parameters-tp32269685p32269685.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Andre,

Typically, I do something like the following:

from scipy.optimize import polyfit, polyval
import numpy as np.

# x and y are arrays of floats

a, b = polyfit(x, y, 1) # first-order polynomial

y_fit = polyval([a, b], x)
mean_abs_err = np.mean(np.abs(y_fit - y))

I hope that helps!
Ben Root

···

On Tue, Aug 16, 2011 at 2:15 AM, 4ndre <andre.dankert@…1003…7…> wrote:

Hi,

I’m a little bit desperated. I googled for days to find an appropriate way

to calculate the absolute errors of parameters of non-linear fits. I found

different sites which suggest different, sometimes more or less similar

ways. But the results don’t make sense. Here is a simple example:

from pylab import *

from scipy.optimize import curve_fit

def f(x,a,b):

``````    return a*x+b
``````

def fit(n=1000):

``````    x,y=array([0,1,1]),array([1,2,4])

xf=[(min(x)+i*(max(x)-min(x))/float(n)) for i in range(0,int(n)+1)]

a,acov=curve_fit(f,x,y)

yf=[f(*tuple([i]+list(a))) for i in xf]

meansq=sum([pow(y[i]-f(x[i],a,a),2)/(len(x)) for i in
``````

range(0,len(x))])

``````    redchi2=sum([pow(y[i]-f(x[i],a,a),2)/(len(x)-len(a)) for i in
``````

range(0,len(x))])

``````    print 'standard dev:',sqrt(diag(acov))

print 'abs. meansq err.:',[acov[i][i]*sqrt(meansq) for i in
``````

range(0,len(a))]

``````    print 'abs. reduced meansq err.:',[acov[i][i]*sqrt(redchi2) for i in
``````

range(0,len(a))]

fit()

I included 3 ways I found. The used arrays have 3 points, where for x=1 I

give 2 different y values. I would say,

a=2+/-1, b=1+/-0,

but no way yields to a similar result! How can I trust this function on more

complicated, really non-linear functions? Or is simply this example wrong?

Or are the methods not right?

I’m very thankful for any help!

Best regards,

André