简体   繁体   中英

How to include measurement errors in numpy.polyfit

The documentation HERE for numpy.polyfit defines the optional input vector as the "weights to apply to the y-coordinates". 定义为“应用于y坐标的权重”。 This definition is unclear and does not seem the standard definition for the weights in least-squares fits (see eg HERE ).

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?

Short Answer

The relation between and the 1σ errors in numpy.polyfit is 和numpy.polyfit中的1σ错误之间的关系是

w = 1/sigma

which is different from what everybody will expect.

Edit: 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.

Explanation

In least-squares fitting one generally defines the weights vector in such a way that the fit minimizes the squared error (see eg Wikipedia or NIST )

chi2 = np.sum(weights*(p(x) - y)**2)

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

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.

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 vector does indeed multiply the y-coordinate 向量的确乘以y坐标

(vander*w[:, np.newaxis]).dot(x) == y*w

But solving the above array expression in the least-squares sense is equivalent to minimizing the expression below with inside the parenthesis 最小化下面的表达式

chi2 = np.sum((w*(vander.dot(x) - y))**2)

Or, using the notation of the Numpy documentation

chi2 = np.sum((w*(p(x) - y))**2)

in such a way that the relation between and the 1σ errors is 和1σ误差之间的关系是

w = 1/sigma

which is different from what everybody will expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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