Find Polyfit Uncertainty

I use a simple polyfit order 2 to put a string in the sample data:

 np.polyfit(x, y, 2) 

which returns the odds.

Now I want to find the ambiguity of the inline string and tried to use the cov argument, which returns the 3x3 covariance matrix:

 np.polyfit(x, y, 2, cov=True) 

But I'm not sure how to calculate the uncertainty, which, according to my Google search, should be calculated by squaring the diagonal of the covariance matrix.

+5
source share
2 answers

This problem is solved with the help of the “Estimation of Errors in Least Square Fittings” P.Kh. Richter, 1995, Progress Report TDA 42-122.

From the report, this item may already be enough for you

The first case discussed above, namely, error determination is one or more suitable parameters, a simple answer is given in terms of the diagonal elements of the covariance fitting matrix, and this is well known.

Diagonal elements of interest to you, for example:

 x = linspace(0,1,1000) # comment and uncomment the last term to see how the fit appears in the figure, # and how the covariances of the single polynomial coefficients vary in turn. y = cos(x)*x**2+x+sin(x-1.) #+(x*1.3)**6 p,cov = polyfit(x,y,2,cov=True) plot(x,y,'b') plot(x,polyval(p,x),'r') print sqrt(diag(cov)) 

In general, the handbook examines how this error in polynomial coefficients is also an error of the dependent variable y as a function of the independent variable x . From the report:

The purpose of this article is to discuss the above errors and, in particular, to present results that will allow us to determine the standard error of selection as a function of an independent variable, as well as establish confidence limits for these errors.

+6
source

For your convenience, I made a fully working Python 3 example based on gg349 answer.

 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,1000) # comment and uncomment the last term to see how the fit appears in the figure, # and how the covariances of the single polynomial coefficients vary in turn. y = np.cos(x) * x**2 + x + np.sin(x - 1.) \ # + (x * 1.3)**6 p, cov = np.polyfit(x, y, 2, cov=True) plt.plot(x, y) plt.plot(x, np.polyval(p,x)) plt.show() print(np.sqrt(np.diag(cov))) 
0
source

Source: https://habr.com/ru/post/1210317/


All Articles