简体   繁体   English

Python / Scipy - 将optimize.curve_fit的sigma实现到optimize.leastsq中

[英]Python / Scipy - implementing optimize.curve_fit 's sigma into optimize.leastsq

I am fitting data points using a logistic model. 我使用逻辑模型拟合数据点。 As I sometimes have data with a ydata error, I first used curve_fit and its sigma argument to include my individual standard deviations in the fit. 由于我有时会有ydata错误的数据,我首先使用curve_fit及其sigma参数来包含我在拟合中的各个标准偏差。

Now I switched to leastsq, because I needed also some Goodness of Fit estimation that curve_fit could not provide. 现在我切换到了最小化,因为我还需要一些曲率拟合无法提供的拟合优度估计。 Everything works well, but now I miss the possibility to weigh the least sqares as "sigma" does with curve_fit. 一切都运作良好,但现在我错过了权衡最小平方的可能性,因为“sigma”与curve_fit有关。

Has someone some code example as to how I could weight the least squares also in leastsq? 有人一些代码示例关于我如何在最小方格中加权最小二乘?

Thanks, Woodpicker 谢谢,Woodpicker

I just found that it is possible to combine the best of both worlds, and to have the full leastsq() output also from curve_fit(), using the option full_output: 我刚刚发现可以结合两个世界的最佳组合,并使用选项full_output从curve_fit()获得完整的leastsq()输出:

popt, pcov, infodict, errmsg, ier = curve_fit(func, xdata, ydata, sigma = SD, full_output = True)

This gives me infodict that I can use to calculate all my Goodness of Fit stuff, and lets me use curve_fit's sigma option at the same time... 这给了我infodict,我可以用来计算我所有的Fitness of Fit,并让我同时使用curve_fit的sigma选项......

假设您的数据在数组xyyerr ,并且模型是f(p, x) ,只需将误差函数定义为最小化为(yf(p,x))/yerr

The scipy.optimize.curve_fit docs say: scipy.optimize.curve_fit文档说:

pcov : 2d array pcov:2d数组

The estimated covariance of popt. 估计的popt协方差。 The diagonals provide the variance of the parameter estimate. 对角线提供参数估计的方差。 To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)). 要计算参数上的一个标准偏差,请使用perr = np.sqrt(np.diag(pcov))。 How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above. sigma参数如何影响估计的协方差取决于absolute_sigma参数,如上所述。

And the section on 和节

absolute_sigma : bool, optional absolute_sigma:bool,可选

If True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values. 如果为True,则以绝对意义使用sigma,并且估计的参数协方差pcov反映这些绝对值。

If False, only the relative magnitudes of the sigma values matter. 如果为假,则只有西格玛值的相对大小很重要。 The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor. 返回的参数协方差矩阵pcov基于通过常数因子缩放西格玛。 This constant is set by demanding that the reduced chisq for the optimal parameters popt when using the scaled sigma equals unity. 通过要求在使用缩放的西格玛等于1时弹出最佳参数的减少的chisq来设置该常数。 In other words, sigma is scaled to match the sample variance of the residuals after the fit. 换句话说,缩放西格玛以匹配拟合后残差的样本方差。 Mathematically, pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(MN) 数学上,pcov(absolute_sigma = False)= pcov(absolute_sigma = True)* chisq(popt)/(MN)

So, you could just leave absolute_sigma to the default value (False) and then use 因此,您可以将absolute_sigma保留为默认值(False),然后使用

 perr = np.sqrt(np.diag(pcov))
 fitStdErr0 = perr[0]
 fitStdErr1 = perr[1]
 ...

to get the standard deviation error of each fit parameter (as a 1D numpy array). 获得每个拟合参数的标准偏差误差(作为1D numpy数组)。 Now you can just pick the useful members (and combine them in a way that is most representative of your data ). 现在您可以选择有用的成员(并以最能代表您数据的方式组合它们)。

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

相关问题 使用python的optimize.leastsq()和optimize.curve_fit将数据拟合到Faddeeva函数 - Fitting data to Faddeeva function using python's optimize.leastsq() and optimize.curve_fit Scipy:使用optimize.leastsq时拟合参数的界限 - Scipy: bounds for fitting parameter(s) when using optimize.leastsq Scipy:optimize.fmin 和 optimize.leastsq 之间的区别 - Scipy: difference between optimize.fmin and optimize.leastsq 在 python - scipy optimize.curve_fit 中拟合二次高原 function 返回值取决于条件参数 - Fitting a Quadratic-Plateau in python - scipy optimize.curve_fit a function returns value depends on a conditional parameter scipy.optimize中python中curve_fit和leastsq之间的区别 - Difference between curve_fit and leastsq in python from scipy.optimize Pythonoptimize.curve_fit(关于现有答案) - Python optimize.curve_fit (regarding existing answer) Scipy ValueError:对象不足,无法使用optimize.leastsq进行所需的数组 - Scipy ValueError: object too deep for desired array with optimize.leastsq 在scipy.optimize.curve_fit中使用absolute_sigma参数 - Using the absolute_sigma parameter in scipy.optimize.curve_fit scipy optimize.curve_fit不能适合其返回值取决于条件的函数 - scipy optimize.curve_fit cannot fit a function whose return value depends on a conditional Python scipy.optimize.leastsq - Python scipy.optimize.leastsq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM