Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

python - scipy.optimize.curvefit: Asymmetric error in fit

I try to fit a function to my data using scipy.optimize.curvefit.

Q=optimization.curve_fit(func,X,Y, x0,ERR)

and it works well.

However, now I am trying to use an asymmetric error and I have no idea how to do that - or even if it is possible.

By asymmetric error I mean that the error is not for example: 3+-0.5 but 3 +0.6 -0.2. So that ERR is an array with two columns.

It would be great if somebody had an idea how to do that - or could me point to a different Python routine which might be able to do it.

That a snippet of the code I am using - but I am not sure it makes it clearer:

A=numpy.genfromtxt('WF.dat')
cc=A[:,4]
def func(A,a1,b1,c1):
    N=numpy.zeros(len(x))
    for i in range(len(x)):
        N[i]=1.0*erf(a1*(A[i,1]-c1*A[i,0]**b1))

return N


x0   = numpy.array([2.5  , -0.07 ,-5.0])
Q=optimization.curve_fit(func,A,cc, x0, Error)

And Error=[ErP,ErM] (2 columns)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Least squares algorithm like curve_fit or scipy.optimize.leastsq will not be able to do this because the loss function is quadratic, and so symmetric for positive and negative error.

I haven't seen any models for this and maybe PAIDA can handle it, as DanHickstein mentioned.

Otherwise, you could use the nonlinear optimizers like optimize.fmin and construct your own asymmetric loss function.

def loss_function(params, ...):
    error = (y - func(x, params))
    error_neg = (error < 0)
    error_squared = error**2 / (error_neg * sigma_low + (1 - error_neg) * sigma_upp))
    return error_squared.sum()

and minimize this with fmin or fmin_bfgs.

(I never tried this.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...