【问题标题】:Chi-squared minimization in python using lmfit使用lmfit在python中进行卡方最小化
【发布时间】:2013-01-16 14:52:50
【问题描述】:

我正在尝试使用 python 和lmfit 模块进行多参数拟合。我一直按照here 显示的示例作为我的代码的基础。就我对代码的理解而言,只要我正确定义目标函数(给出残差)并为其提供正确的参数,我应该能够进行最小二乘拟合。

这是我当前的目标函数:

# Define objective function: each data point has a different
# objective function which is defined by the model method
# the objective function returns the array to be minimized
def objfunc(params,trans,sum_in,sum_out,data):
    """ model fit using branching ratios and resonance strength
        then subtract data """
    model = fit_model(params,trans,sum_in,sum_out)

    return model - data

fit_model(args*) 方法的定义是

def fit_model(params,trans,sum_in,sum_out):
    """ model the transition based upon the input string trans
        using parameter convention for the branching """
    model = []

    # The amplitude: technically the resonance strength term
    # here it gives the number of resonant decays
    amp = params['amp'].value

    # For each transition we want to retrieve the parameter values
    # for the branching ratios and evaluate the new value for
    # the fit (of that transition). The easiest way to do this is
    # to store the braching ratios with the same notation used
    # previously, and to explicity call those values using the
    # 'params.['']value' method
    for i in range(len(trans)):

        # Employs the termvalue() method to evalueate the branching
        # and efficiency values
        model.append( str(amp * termValue(trans[i]) + amp * termValue(sum_in[i]) - amp * termValue(sum_out[i])))

    return np.array(model,dtype='float64')

这给了我期望得到的结果:numpy.ndarray 我的数据长度。我遇到的问题是,当我尝试最小化卡方拟合时

result = minimize(objfunc,params,args=(trans,sum_in,sum_out,data)) 

我收到错误消息:

File "path/chisquare.py", line 94, in <module>
    result = minimize(objfunc,params,args=(trans,sum_in,sum_out,data))
  File "/usr/local/lib/python2.7/dist-packages/lmfit-0.7-py2.7.egg/lmfit/minimizer.py", line 498, in minimize
    fitter.leastsq()
  File "/usr/local/lib/python2.7/dist-packages/lmfit-0.7-py2.7.egg/lmfit/minimizer.py", line 369, in leastsq
    lsout = scipy_leastsq(self.__residual, self.vars, **lskws)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 278, in leastsq
    raise TypeError('Improper input: N=%s must not exceed M=%s' % (n,m))
TypeError: Improper input: N=26 must not exceed M=25

我试图从lmfit 源代码中弄清楚这意味着什么,但这有点超出我的理解。有谁知道我该如何解决这个错误?

谢谢

【问题讨论】:

    标签: python chi-squared


    【解决方案1】:

    这个问题似乎是由于参数多于数据点造成的。检查了我的输入并解决了问题!

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多