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

查看:30
本文介绍了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](2 列)

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

推荐答案

在目前的版本中,恐怕不行.curve_fit 是对流行的 Fortran 库 minipack 的封装.查看scipy_install_pathoptimizeminipack.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_pathoptimizeminipack.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,它必须以与 XY 相同长度的数组形式提供.

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天全站免登陆