简体   繁体   中英

curve fitting in scipy is of poor quality. How can I improve it?

I'm doing a fit of a set results to a predicted function. The function might be interpreted as linear but I might have to change it a little so I am doing curve fitting instead of linear regression. I use the curve_fit function in scipy . Here is how I use it

 kappa = 1
        alpha=2
        popt,pcov = curve_fit(fitFunc1,self.X[0:3],self.Y[0:3],sigma=self.Err[0:3],p0=[kappa,alpha])

and here is fitFunc1

def fitFunc1(X,kappa,alpha):

    out = []
    for x in X:
        y = log(kappa)
        y += 4*log(pi)
        y += alpha*x 
        y -= 2*log(2)
        out.append(-y)
    return np.array(out)

Here is an example of the fit 在此处输入图片说明 . The green line is a matlab fit. The red one is a scipy fit. I carry the fist over the first three dots.

You are using non-linear fitting routines to fit the data, not linear least-squares as invoked by A\\b . The result is that the matlab and/or scipy minimization routines are getting stuck in local minima during the optimizations, leading to different results.

You should get the same results (to within numerical precision) if you apply logs to the raw data prior to linear fitting with A\\b (in matlab).

edit

Inspecting function fitFunc1 it looks like the x/y data have already been transformed prior to the fit within scipy.

I performed a linear fit with the data shown, using matlab. The results using linear least squares with the operation polyfit(x,y,1) (essentially a linear fit) is very similar to the scipy result:

在此处输入图片说明

In any case, the data looks piecewise linear so a better solution may be to attempt a piecewise linear fit. On the other the log transformation can do all sorts of unwanted stuff, so performing nonlinear fits on the original data without performing a log tranform may be the best solution.

If you don't mind having a little bit of extra work I suggest using PyMinuit or iMinuit , both are minimisation packages based on Seal Minuit.

Then you can minimise a Chi Sq function or maximise the likelihood of your data in relation to your fit function. They also provide all the errors and everything you would like to know about the fit.


Hope this helps! xD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM