[SciPy-user] optimize.leastsq confusing when it comes to errors

josef.pktd@gmai... josef.pktd@gmai...
Thu Mar 26 17:36:23 CDT 2009


On Thu, Mar 26, 2009 at 5:40 PM, Pim Schellart
<P.Schellart@student.science.ru.nl> wrote:
> Dear Scipy users,
>
> This is my first post to the list so please let me know if I am posting the
> question in the wrong place.
> I need to fit several functions to a dataset and would love to use scipy for
> this, this would allow me to use Python for all my scientific work as this
> is the only task for which I still use gnuplot.
> But I find the documentation on optimize.leastsq very confusing.
> I can get the fit paramaters (using the example as a guide) but I also need
> the errors on the resulting parameters and basically all the information
> given by the default gnuplot fit command output, which is the following.
>
> ###################################################################
> final sum of squares of residuals : 1588.68
> rel. change during last iteration : -2.92059e-06
>
> degrees of freedom    (FIT_NDF)                        : 27
> rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 7.67072
> variance of residuals (reduced chisquare) = WSSR/ndf   : 58.84
>
> Final set of parameters            Asymptotic Standard Error
> =======================            ==========================
>
> a               = 1.77246          +/- 1.984        (111.9%)
> b               = 0.713972         +/- 0.06909      (9.677%)
> c               = -0.207495        +/- 1.413        (681.2%)
>
>
> correlation matrix of the fit parameters:
>
>               a      b      c
> a               1.000
> b               0.004  1.000
> c              -0.103 -0.088  1.000
> ###################################################################
>
> Furthermore my datapoints have associated errors and these need to be taken
> into account in the fit.
> How can I make sure this is done?
> Is optimize.leastsq build to do this or should I use a different function.
> It would be even better if the example in the tutorial is extended to
> include this information as I can imagine most real world applications need
> errors on both the input and the output.
> Thank you very much in advance.
>
> Kind regards,
>
> Pim Schellart
>
> P.S. I am aware of the gnuplot python module but I prefer scipy :)


look at curve_fit
http://projects.scipy.org/scipy/browser/trunk/scipy/optimize/minpack.py#L331

it's a wrapper around optimize.leastsq  that provides the correct
variance-covariance matrix of the parameter estimates and allows
(inverse) weights for the regression. the weights are the standard
deviation for each observation.

the rest you should be able to calculate in a few lines

A search of the mailing lists will provide some more
information/discussion on implementation details.

Josef


More information about the SciPy-user mailing list