简体   繁体   English

使用三维数据的非线性最小二乘最小化

[英]Non-linear least square minimization using three dimensional data

I have a 3D data and I would like to fit a non-linear model to the data using lmfit . 我有一个3D数据,我想使用lmfit将非线性模型拟合到数据中。 This is the code I have written but it doesn't work. 这是我编写的代码,但它不起作用。

from lmfit import minimize, Parameters, Parameter, report_fit
import numpy as np

m=np.array([13.8, 14.38, 14.316, 13.799, 14.135, 13.791, 14.531, 14.262, 14.08, 14.267, 14.259, 14.849, 14.396, 13.609])
lower_error_m=np.array([0.221,0.159,0.202,0.276,0.267,0.217,0.109,0.174,0.183,0.317,0.099,0.124,0.300,0.258])
upper_error_m=np.array([0.466,0.988,1.141,0.819,0.944,0.864,1.126,0.837,0.619,0.421,0.692,0.530,0.997,0.655])
asymmetric_error_m = [lower_error_m, upper_error_m]
r=np.array([7.168, 5.99, 6.296, 6.756, 6.297, 6.639, 5.857, 5.94, 7.312, 6.201, 7.191, 5.624, 5.584, 6.701])
lower_error_r=np.array([1.709,1.882,1.038,1.656,1.676,1.593,1.937,1.474,1.511,1.247,1.504,1.386,1.492,1.294])
upper_error_r=np.array([0.223,0.168,0.300,0.399,0.405,0.444,0.201,0.306,0.362,0.058,0.401,0.267,0.471,0.529])
asymmetric_error_r = [lower_error_r, upper_error_r]
q=np.array([0.13, 0.312,0.327,0.322,0.327,0.381,0.357,0.386,0.133,0.354,0.128,0.339,0.479,0.325])
# define objective function: returns the array to be minimized
def fcn2min(params, m, q, data):
    """ model decaying sine wave, subtract data"""
    A0 = params['A0'].value
    B  = params['B'].value
    alpha = params['alpha'].value
    model = 10**( np.log10(A0/(1+q)**B)+alpha*(m-14))
    return model - data

# create a set of Parameters
params = Parameters()
params.add('A0',   value= 6)
params.add('B', value= 0.1)
params.add('alpha', value= -0.1)
# do fit, here with leastsq model
result = minimize(fcn2min, params, args=(m, q, r))

I am getting the following error: 我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/vol/anaconda/lib/python2.7/site-packages/lmfit-0.9.2_4_gfc81f36-py2.7.egg/lmfit/minimizer.py", line 774, in minimize
    return fitter.minimize(method=method)
  File "/vol/anaconda/lib/python2.7/site-packages/lmfit-0.9.2_4_gfc81f36-py2.7.egg/lmfit/minimizer.py", line 706, in minimize
    return function(**kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/lmfit-0.9.2_4_gfc81f36-py2.7.egg/lmfit/minimizer.py", line 547, in leastsq
    lsout = scipy_leastsq(self.__residual, vars, **lskws)
  File "/vol/anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 371, in leastsq
    shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
  File "/vol/anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 20, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File "/vol/anaconda/lib/python2.7/site-packages/lmfit-0.9.2_4_gfc81f36-py2.7.egg/lmfit/minimizer.py", line 258, in __residual
    out = self.userfcn(params, *self.userargs, **self.userkws)
  File "<stdin>", line 6, in fcn2min
TypeError: unsupported operand type(s) for +: 'int' and 'list'

This is the model I am trying to fit: 这是我想要的模型:

在此输入图像描述

where A0 , B and alpha are free parameters of the model. 其中A0Balpha是模型的自由参数。 I also have errors on m and r and I am wondering how I could include them in the fitting process? 我在mr上也有错误,我想知道如何将它们包含在拟合过程中? Thanks. 谢谢。

A. Your example runs to completion without such an error for me, with lmfit version 0.9.3. 答:对于我来说,你的例子运行完成没有这样的错误,使用lmfit版本0.9.3。

B. You can include uncertainties in the data by having your objective function return B.通过让目标函数返回,可以在数据中包含不确定性

   return (model - data)/ uncertainty

where uncertainty is the uncertainty in the data. uncertainty是数据中的不确定性。

C. Sorry, but least-squares minimization as with MINPACK does not easily support asymmetric errors or errors in the independent variable. C.抱歉,与MINPACK一样,最小二乘最小化不容易支持自变量中的非对称错误或错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM