Fitting a curve

I know this is not completely matplotlib related but perhaps you can help me none the less:
I want to fit a curve to a set of data. It's a very easy curve: y=ax+b.
But I want errors for a and b and not only the rms. Is that possible. What tasks do you recommend for doing that.
Thanks in advance
    Wolfgang

gnuplot can do that in a relatively painless way.

···

On 8/30/07, Wolfgang Kerzendorf <wkerzendorf@...982...> wrote:

I know this is not completely matplotlib related but perhaps you can
help me none the less:
I want to fit a curve to a set of data. It's a very easy curve: y=ax+b.
But I want errors for a and b and not only the rms. Is that possible.
What tasks do you recommend for doing that.

Wolfgang Kerzendorf wrote:

I know this is not completely matplotlib related but perhaps you can help me none the less:
I want to fit a curve to a set of data. It's a very easy curve: y=ax+b.
But I want errors for a and b and not only the rms. Is that possible. What tasks do you recommend for doing that.
Thanks in advance
    Wolfgang

from http://mathworld.wolfram.com/LeastSquaresFitting.html:

(but here: y = a*x+b, so a <-> b)!

For the standard errors on a and b:

n = float(len(x))
xm = mean(x)
ym = mean(y)
SSxx = dot(x,x) - n*xm**2.0
SSyy = dot(y,y) - n*ym**2.0
SSxy = dot(x,y) - n*xm*ym
r = sqrt(SSxy**2.0 / (SSxx*SSyy))
s = sqrt((SSyy - (SSxy**2.0 / SSxx)) / (n-2.0))

sea = s / sqrt(SSxx)
seb = s * sqrt(1.0/n + (xm**2.0 / SSxx))

The values of sea, seb agree with gnuplot's "Asymptotic Standard Error".

···

--
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as possible.

Hoi,

There is still MPL's polyfit function and I have to admit that Steve
Schmerler's solution looks better that mine, but I've pasted a quick &
dirty solution here:
http://www.python-forum.de/topic-8363.html
It shows the use of polyfit as well as (almost) Steve's approach.

Further examples on linear regression and polynomal regression can be
found in http://matplotlib.sourceforge.net/users_guide_0.90.0.pdf

Also, you might want to have a closer look on the scipy web page:
http://www.scipy.org/ .

Cheers
Christian