scipy.optimize.curvefit:拟合中的不对称错误 [英] scipy.optimize.curvefit: Asymmetric error in fit

查看:81
本文介绍了scipy.optimize.curvefit:拟合中的不对称错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 scipy.optimize.curvefit 使函数适合我的数据。

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

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

效果很好。

但是,现在我正在尝试使用非对称错误,而且我不知道该怎么做-甚至可能。

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.

通过非对称错误,我的意思不是说错误: 3 + -0.5 而是 3 +0.6 -0.2
因此ERR是一个具有两列的数组。

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.

如果有人对如何执行此操作有一个想法,那将是很棒的选择-或者我可以指出一个可能执行此操作的其他Python例程。

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](两列)

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

推荐答案

在当前版本中,恐怕不是可行的。 curve_fit 是流行的Fortran库 minipack 的包装。检查 \scipy_install_path\optimize\minipack.py 的源代码,您将看到:(第498-509行):

In the current version, I am afraid it is not doable. curve_fit is a wrap around the popular Fortran library minipack. Check the source code of \scipy_install_path\optimize\minipack.py, you will see: (line 498-509):

if sigma is None:
    func = _general_function
else:
    func = _weighted_general_function
    args += (1.0/asarray(sigma),)

基本上,这意味着 sigma 未提供,则将调用 minipack 中未加权的Levenberg-Marquardt方法。如果提供了 sigma ,则将调用加权LM。这意味着,如果要提供 sigma ,则必须将其提供为长度为 X 的数组。 Y

Basically what it means is that of sigma is not provided, then the unweighted Levenberg-Marquardt method in minipack will be called. If sigma is provided, then the weighted LM will be called. That implies, if sigma is to be provided, it must be provided as a array of the same length of X or Y.

这意味着,如果要在 Y 上保留不对称错误残差,则必须对您的目标函数,如@Jaime建议的那样。

That means if you want to have asymmetric error residue on Y, you have to come up with some modification to your target function, as @Jaime suggested.

这篇关于scipy.optimize.curvefit:拟合中的不对称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆