# [SciPy-user] Re: optimize.leastsq -> fjac and the covariance redux

Brendan Simons brendansimons at yahoo.ca
Tue Mar 15 11:55:56 CST 2005

```OK, solved both problems.

The r matrix obtained from fjac must be post
multiplied by the permutation matrix, not its
transpose.  Line 254 in minpack.py should be changed
from:

R = dot(r, transpose(perm))

to:

R = dot(r, perm)

That gets the ordering of the Covariance to match my
test.

As for the second issue, (the fact that giving Dfun=J
in leastsq leads to the wrong minimum) I did some
googling and found this post from John Hunter a year
ago:

>John Hunter  scipy-user at scipy.net
>Mon, 21 Apr 2003 13:55:09 -0500
>
>I am using leastsq to do a best fit to a simple
>exponential function.
>In my test script, I find that if I use col_deriv=0,
I >get a different
>answer than I get if I do a col_deriv=1 with the err
>func returning
>the transposed jacobian.  When I compare the true
>parameters with the
>best fit parameters, the correct answer is with
>col_deriv=1 but not
>with col_deriv=0.  Am I misusing this parameter or is
>something amiss?

--example snipped--

Sure enough, providing the transpose of J to Dfun, and
setting col_deriv = True gives the right answer!  This
is obviously a bug in how leastsq handles col_deriv,
and needs to be looked at.

The good news is I now have a covariance matrix!
Thanks Travis!
-Brendan

On March 15 I wrote:
>
>Here C has all the right outputs, but in the wrong
>order.  Evidently
>we're applying the
>ipvt permutation matrix wrong somehow.
>
>Here's something worse.  If I include the jacobian in

>the leastsq call
>as follows
>pMin, C, infodict, ier, mesg = leastsq(resid, p0,
>args=(y_meas, x),
>Dfun = J, full_output=True)

>the resulting pMin is the same as p0, and C is way
off
>of C_true.  What
>gives?

-Brendan

______________________________________________________________________