[英]Python - Using lmfit to fit a function
I am trying to use lmfit, but getting sporadic results for the parameters: 我正在尝试使用lmfit,但是参数偶尔会出现结果:
import numpy as np
import scipy.stats as sp
from scipy.optimize import curve_fit
from lmfit import minimize, Parameters, Parameter, report_fit
x = [0.01,
0.02,
0.03,
0.04,
0.05,
0.06,
0.07,
0.08,
0.09,
0.1,
0.2,
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
0.99999999,]
#some paramters to try to estimate
sigma1 = 6
scale1 = np.exp(5)
#generate values to use to fit
y = sp.lognorm(sigma1,scale=scale1).ppf(x)
#function set-up to use in lmfit
def invlognorm(params,x,data):
sigma = params['sigma']
mu = params['mu']
model = sp.lognorm(sigma,scale=mu).ppf(x)
return model - data
params = Parameters()
params.add('sigma', value= 1,)
params.add('mu', value= 1, )
# do fit, here with leastsq model
result = minimize(invlognorm,params, method = 'least-squares',args=(x, y))
and finally checking the results 最后检查结果
result.params.pretty_print()
Name Value Min Max Stderr Vary Expr Brute_Step
mu 2.161 -inf inf None True None None
sigma 6.754 -inf inf None True None None
but these are nowhere near the original values. 但是这些与原始值相去甚远。
Any help on what's going on here and how I can fix this so it gives reasonable results? 关于这里发生的事情以及如何解决此问题的任何帮助,这样可以得出合理的结果?
You will almost certainly need better starting values for the sigma
and mu
parameters. 几乎可以肯定的是,
sigma
和mu
参数需要更好的起始值。
The lognorm().ppf()
function diverges at x=1
, giving huge values which will completely dominate any measure of misfit such as chi-square. lognorm().ppf()
函数在x=1
处发散,给出了巨大的值,该值将完全支配任何不lognorm().ppf()
度量,例如卡方。 In addition, small changes in parameter values will have essentially no effect on the total misfit, and all fitting algorithms will quickly give up. 此外,参数值的微小变化基本上不会对总失配产生任何影响,并且所有拟合算法都会迅速放弃。 The huge value at
x=1
will also make any fit insensitive to the other data. x=1
处的巨大值也会使拟合对其他数据不敏感。 Perhaps you actually meant some other method of lognorm
such pdf
or cdf
? 也许您实际上是在说
lognorm
其他方法,例如pdf
或cdf
?
If not, you may want to fit "in log space" -- fit the log of your data to the log of the model. 如果不是,则可能需要在“日志空间”中拟合-将数据的日志拟合到模型的日志中。 That will reduce the importance of the
x=1
datum. 这将降低
x=1
数据的重要性。
Also, though it is not the cause of the problem, your fit did not actually use the leastsq
method as your comment says. 同样,尽管这不是问题的根源,但是您的配合实际上并没有使用您的注释中所述的
leastsq
方法。 To use the leastsq
(Levenberg-Marquardt method) use: 要使用
leastsq
(Levenberg-Marquardt方法),请使用:
# do fit, here with leastsq model
result = minimize(invlognorm, params, args=(x, y))
To use scipy.optimize.least_squares
(which actually use trust region reflective use 要使用
scipy.optimize.least_squares
(实际上使用信任区域反射式使用
# do fit, here with least_squares model
result = minimize(invlognorm,params, method='least_squares', args=(x, y))
(note spelling. Your example used least-squares
which is not recognized, and so causes the Nelder-Mead method to be used). (请注意拼写。您的示例使用了无法识别
least-squares
,因此导致使用Nelder-Mead方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.