在scipy.optimize.curve中使用绝对西格玛参数

2024-04-29 20:11:22 发布

您现在位置:Python中文网/ 问答频道 /正文

为了执行拟合,我当前正在使用scipy.optimize中的^{}

我已经计算出了与我的每个ydata相关的误差,我想把数据中计算出的sigma = y_errors添加到拟合中

即最小化sum( ((f(xdata, *popt) - ydata) / sigma)**2 ),而不仅仅是sum( (f(xdata, *popt) - ydata))

我可以看到可以在文档中指定参数sigma。我不清楚的是absolute_sigma参数。文件中的解释让我很困惑。

我应该设置absolute_sigma= = True?或者,如果需要考虑与我的每个ydata相关联的y_errors,应该将其设置为False


Tags: 文件数据文档true参数scipysigma误差
1条回答
网友
1楼 · 发布于 2024-04-29 20:11:22

如果数据中存在绝对不确定性,即如果y_errors的单位与ydata的单位相同,则应设置absolute_sigma= = True。然而,通常情况下,y_errors的单位并不精确,只知道相对大小。后一种情况的一个例子可能是,某些y值来自相同x值的重复测量。那么,对重复的y值进行加权是对不重复的y值进行加权的两倍是有意义的,但是这个加权的单位(2)与y的任何单位都不相同。

下面是一些代码来说明区别:

import numpy as np
from scipy.optimize import curve_fit
from scipy.stats import norm

# defining a model
def model(x, a, b):
    return a * np.exp(-b * x)

# defining the x vector and the real value of some parameters
x_vector = np.arange(100)
a_real, b_real = 1, 0.05

# some toy data with multiplicative uncertainty
y_vector = model(x_vector, a_real, b_real) * (1 + norm.rvs(scale=0.08, size=100))

# fit the parameters, equal weighting on all data points
params, cov = curve_fit(model, x_vector, y_vector )
print params
print cov


# fit the parameters, weighting each data point by its inverse value
params, cov = curve_fit(model, x_vector, y_vector, 
                        sigma=1/y_vector, absolute_sigma=False)
print params
print cov

# with absolute_sigma=False:
## multiplicative transformations of y_data don't matter
params, cov = curve_fit(model, x_vector, y_vector, 
                        sigma=100/y_vector, absolute_sigma=False)
print params
print cov

# but absolute_sigma=True:
## multiplicative transformations of sigma carry through to pcov
params, cov = curve_fit(model, x_vector, y_vector,
                        sigma=100/y_vector, absolute_sigma=True)
print params
print cov

[ 1.03190409  0.05093425]
[[  1.15344847e-03   5.70001955e-05]
 [  5.70001955e-05   5.92595318e-06]]

[ 1.0134898   0.04872328]
[[  1.57940876e-04   1.56490218e-05]
 [  1.56490218e-05   3.56159680e-06]]

[ 1.0134898   0.04872328]
[[  1.57940878e-04   1.56490220e-05]
 [  1.56490220e-05   3.56159682e-06]]

[ 1.0134898   0.04872328]
[[ 2978.10865352   295.07552766]
 [  295.07552766    67.15691613]]

相关问题 更多 >