【问题标题】:Using the absolute_sigma parameter in scipy.optimize.curve_fit在 scipy.optimize.curve_fit 中使用 absolute_sigma 参数
【发布时间】:2015-07-29 15:34:22
【问题描述】:

为了进行匹配,我目前正在使用来自scipy.optimizecurve_fit

我已经计算了与我的每个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

【问题讨论】:

    标签: python scipy curve-fitting


    【解决方案1】:

    如果您的数据中有绝对不确定性,即如果您的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]]
    

    【讨论】:

    • 我得到错误的方式是使用折刀重采样方法。所以我计算了相同的数量,但每次都从我的数据中省略一个子样本。所以我相信在我的情况下,我知道 yerrors 与我的数据具有相同的单位。对吗?
    • 我不能 100% 确定...如果没有对您的数据和算法的详细描述,这很难说...抱歉。
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多