# [SciPy-User] leastsq

josef.pktd@gmai... josef.pktd@gmai...
Thu Jun 27 08:21:27 CDT 2013

```On Thu, Jun 27, 2013 at 9:04 AM, Frédéric Parrenin
<parrenin.ujf@gmail.com> wrote:
> Dear Josef,
>
> OK to use the curve_fit function with a change of variables to have a
> diagonal covariance matrix.
>
> However, here are two questions/remarks:
> - Curve_fit takes as input both the parameters to fit and a variable x where
> the data are 'located'. This approach seems sub-optimal since in many
> inverse problems, the function is evaluated for all x at a time. Running the
> function independently N times will significantly decrease the computation
> time.

I don't understand this part. We are fitting a curve to N
observations. We need all of them to calculate the residual sum of
squares.

> Maybe in this case the best thing to do is to declare that x is empty, but
> how to do that in practice?

You don't need to use x, you can just write f as a method in a class
and attach whatever attributes you want to reuse in the f method. (I'm
not completely remember how this was implemented, and no time to look
it up right now.)

> - It is not very clear from the scipy doc
> http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
> what the function f is supposed to return. Is it just a scalar function or
> can it be a ndarray or even something else?

the function should return an array of predicted values, one element
for each observation

>
> Some more complex examples in the doc would really help to better understand
> how it works.

There are several examples on stackoverflow, including the case when f
is a method in a class

Josef

>
> Best regards,
>
> Frédéric Parrenin
>
>
>
>
>
> 2013/6/26 <josef.pktd@gmail.com>
>
>> On Wed, Jun 26, 2013 at 12:26 PM, Frédéric Parrenin
>> <parrenin.ujf@gmail.com> wrote:
>> > Dear all,
>> >
>> > I am experimenting the optimize module of scipy.
>> > My optimization problem is a leastsq problem.
>> > However, the leastsq function seems to be not appropriate for two
>> > reasons:
>> > - there is no possibility to specify a covariance matrix between the
>> > leastsq
>> > terms. They are supposed to be independent, which is a too strong
>> > assumption
>> > in my case.
>> > - the analyzed covariance matrix (i.e. the inverse of the jacobian of
>> > the
>> > cost function) cannot be simply outputed.
>> >
>> > Of course I could use a more generic optimization function, like the
>> > minimize one.
>> > However this seems sub-optimal because the minimisation of a least
>> > squares
>> > problem can dealt more efficiently (the jacobian of the cost function
>> > can be
>> > approximated using the jacobian of the terms to minimize).
>> >
>> > Can anybody help me?
>> > Are there plans to improve the leastsq function?
>>
>> leastsq is a low level function and I think we should not load it up
>> with any options.
>>
>> for weighted least-squares the more highlevel interface with
>> However it doesn't allow for a full covariance matrix for the errors.
>>
>> If you want to use leastsq with a full covariance matrix, then you
>> could transform both sides yourself, similar to what is done in
>> curve_fit, but with the cholesky of the inverse covariance matrix.
>> We use that in statsmodels.GLS, but only for linear models.
>> But, if there a large number of observations, then using the full
>> covariance matrix is inefficient, and in many cases a more direct
>> transformation can be used.
>>
>> nonlinear least squares is still largely missing in statsmodels.
>>
>> I don't know if any of the other packages that are based on leastsq
>> have the option.
>>
>> Josef
>>
>>
>>
>> >
>> > Best regards,
>> >
>> > Frédéric Parrenin
>> >
>> >
>> > _______________________________________________
>> > SciPy-User mailing list
>> > SciPy-User@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/scipy-user
>> >
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```