如何在numpy.polyfit中包含测量错误 [英] How to include measurement errors in numpy.polyfit
问题描述
numpy.polyfit的文档 HERE 定义可选输入向量 w 作为应用于y坐标的权重".该定义尚不清楚,并且似乎不是最小二乘拟合的权重的标准定义(请参见例如
The documentation HERE for numpy.polyfit defines the optional input vector w as the "weights to apply to the y-coordinates". This definition is unclear and does not seem the standard definition for the weights in least-squares fits (see e.g. HERE).
在常见的情况下,人有1Σ的情况下,如何计算在numpy.polyfit中使用的权重?输入测量值的误差(标准偏差)?
How does one calculate the weights to use in numpy.polyfit, in the common situation where one has the 1σ error (standard deviation) of the input measurements?
推荐答案
简短回答
w 和1&sigma之间的关系. numpy.polyfit中的错误是
Short Answer
The relation between w and the 1σ errors in numpy.polyfit is
w = 1/sigma
这与每个人的期望都不同.
which is different from what everybody will expect.
following my comment on Github, the numpy.polyfit 1.16 documentation now explicitly states "use 1/sigma (not 1/sigma**2)" to avoid people thinking there is a typo in the formula.
通常使用最小二乘法拟合来定义权重向量,以使拟合最小化平方误差(请参见例如维基百科或 NIST )
In least-squares fitting one generally defines the weights vector in such a way that the fit minimizes the squared error (see e.g. Wikipedia or NIST)
chi2 = np.sum(weights*(p(x) - y)**2)
在常见情况下,1Σ错误(标准偏差)"sigma"是已知的,它具有熟悉的教科书关系,其中权重是方差的倒数
In the common situation where the 1σ errors (standard deviations) "sigma" are known one has the familiar textbook relation where the weights are the reciprocal of the variance
weights = 1/sigma**2
但是numpy.polyfit文档将权重定义为应用于y坐标的权重".这个定义不是很正确.权重不仅适用于y坐标,还适用于拟合残差.
However the numpy.polyfit documentation defines the weight as "weights to apply to the y-coordinates". This definition is not quite correct. The weights apply to the fit residuals, not only to the y-coordinates.
更重要的是,看一下Numpy(v1.9.1)代码中的数学运算,看来Numpy代码在最后的平方意义上解决了下面的线性问题,其中 w 矢量确实可以乘以y坐标
More importantly, looking at the math in the Numpy (v1.9.1) code it appears that the Numpy code solves the linear problem below in the last-squares sense, where the w vector does indeed multiply the y-coordinate
(vander*w[:, np.newaxis]).dot(x) == y*w
但是从最小二乘意义上求解上面的数组表达式等同于用括号内的 w 最小化下面的表达式
But solving the above array expression in the least-squares sense is equivalent to minimizing the expression below with w inside the parenthesis
chi2 = np.sum((w*(vander.dot(x) - y))**2)
或者,使用Numpy文档的符号
Or, using the notation of the Numpy documentation
chi2 = np.sum((w*(p(x) - y))**2)
以 w 与1&sigma之间的关系表示的方式错误是
in such a way that the relation between w and the 1σ errors is
w = 1/sigma
这与每个人的期望都不同.
which is different from what everybody will expect.
这篇关于如何在numpy.polyfit中包含测量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!