[英]Chi-squared minimization in python using lmfit
I'm trying to carry out a multi-parameter fit using python and the lmfit
module. 我正在尝试使用python和
lmfit
模块进行多参数拟合。 I've been following the example shown here as the basis for my code. 我一直在遵循此处显示的示例作为我的代码的基础。 As far as I understand the code, I should be able to carry out a least-squares fit, provided I properly define my objective function (gives the residuals) and supply it with the right arguments.
就我理解的代码而言,我应该能够进行最小二乘拟合,前提是我正确定义了目标函数(给出了残差)并为其提供了正确的参数。
This is my current objective function: 这是我当前的目标函数:
# 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
where the fit_model(args*)
method is defined by 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')
This gives me what I expect to get: a numpy.ndarray
the length of my data. 这给了我期望得到的结果:
numpy.ndarray
我的数据长度。 The problem I'm having is that when I try to minimize the chi-squared fit with 我遇到的问题是,当我尝试最小化卡方拟合时,
result = minimize(objfunc,params,args=(trans,sum_in,sum_out,data))
I get the error message: 我收到错误消息:
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
I've tried to figure out what this means from the lmfit
source code, but it's a bit beyond my understanding. 我试图从
lmfit
源代码中弄清楚这意味着什么,但这超出了我的理解。 Does anyone know how I can resolve this error? 有谁知道我该如何解决这个错误?
Thanks 谢谢
This problem seems to be caused by having more parameters than data points. 此问题似乎是由于参数多于数据点引起的。 Checked my inputs and resolved the problem!
检查了我的输入并解决了问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.