Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python / Scipy - implementing optimize.curve_fit 's sigma into optimize.leastsq

I am fitting data points using a logistic model. As I sometimes have data with a ydata error, I first used curve_fit and its sigma argument to include my individual standard deviations in the fit.

Now I switched to leastsq, because I needed also some Goodness of Fit estimation that curve_fit could not provide. Everything works well, but now I miss the possibility to weigh the least sqares as "sigma" does with curve_fit.

Has someone some code example as to how I could weight the least squares also in leastsq?

Thanks, Woodpicker

like image 912
Woodpicker Avatar asked May 12 '13 17:05

Woodpicker


2 Answers

I just found that it is possible to combine the best of both worlds, and to have the full leastsq() output also from curve_fit(), using the option full_output:

popt, pcov, infodict, errmsg, ier = curve_fit(func, xdata, ydata, sigma = SD, full_output = True)

This gives me infodict that I can use to calculate all my Goodness of Fit stuff, and lets me use curve_fit's sigma option at the same time...

like image 191
Woodpicker Avatar answered Sep 28 '22 02:09

Woodpicker


Assuming your data are in arrays x, y with yerr, and the model is f(p, x), just define the error function to be minimized as (y-f(p,x))/yerr.

like image 33
ev-br Avatar answered Sep 28 '22 02:09

ev-br