## Re: [Matplotlib-users] Fitting a curve

 Re: [Matplotlib-users] Fitting a curve From: Steve Schmerler - 2007-08-30 13:48:41 ```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. ```

 [Matplotlib-users] Fitting a curve From: Wolfgang Kerzendorf - 2007-08-30 08:35:13 ```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 ```
 Re: [Matplotlib-users] Fitting a curve From: Peter I. Hansen - 2007-08-30 11:21:42 ```On 8/30/07, 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. gnuplot can do that in a relatively painless way. ```
 Re: [Matplotlib-users] Fitting a curve From: Steve Schmerler - 2007-08-30 13:48:41 ```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. ```
 Re: [Matplotlib-users] Fitting a curve From: Christian Meesters - 2007-08-31 07:29:12 ```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 ```