I have been using scipy.optimize.leastsq quite a bit lately, but whenever I call it I only use the return "x" (the solution) from this long list of return values. I can't see myself needing any of the other values it returns. I'm curious, has anyone used them? Did it work well for what you used it for?
2 Answers
They are really useful, if you want to look into how well the fit worked. For instance, cov_x
is the covariance matrix. Its diagonal entries are the estimation errors squared, so if you have parameters x[i]
then sqrt(cov_x[i,i])
will be the estimated uncertainties of these parameters. Its off-diagonal entries on the other hand tell you something about the correlations between fit parameters. The wikipedia article about the covariance matrix is very informative on the mathematical details.
The further values are intended more for debugging as far as I can see, so one could probably design the API somewhat differently, to handle this kind of thing via exceptions instead, but the information there still can be very useful if required.

- 866
- 10
- 20
-
That article is far more interesting than the documentation, thanks. Do you have any insight on why "ier", the flag of whether a solution was found or not, is an int and not a boolean? The convention of 1, 2, 3, and 4 meaning success and any other number meaning failure seems very unnecessary when you also have a string telling you about the failure mode. – Sam M. Jul 24 '14 at 14:45
-
No, I am also only a user of scipy, so I cannot tell you what the rationale behind that is. – Emilia Bopp Jul 25 '14 at 13:39
Many of these return values reflect (in variable names and values) the outputs of the underlying Fortran code from MINPACK (lmdif and/or lmder). Why both 'ier' and 'mesg' are returned, while other things are stuffed in infodict, and why the spelling follows Fortran77 conventions is beyond me.
It's unfortunate the return is not more Pythonic (say returning a OptimizeResult instance, as the new-ish minimize() does, perhaps adding a 'covariance' member and maybe more from infodict). I think that would require a wrapper level around leastsq().