Linear regression slope error in numpy
As @ebarr mentioned in the comments, you can use np.polyfit to return the leftovers using a keyword argument full=True
.
Example:
x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0]) z, residuals, rank, singular_values, rcond = np.polyfit(x, y, 3, full=True)
residuals
, then it is the sum of least squares.
Alternatively, you can use the keyword argument cov=True
to get the covariance matrix.
Example:
x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0]) z, cov = np.polyfit(x, y, 3, cov=True)
Then the diagonal elements cov
are the variances of the coefficients with respect to z, i.e. np.sqrt(np.diag(cov))
gives the standard deviations of the coefficients. You can use standard deviations to estimate the likelihood that an absolute error will exceed a certain value, for example. by inserting standard deviations into the uncertainty spread calculation . If you use, for example, 3 * standard deviations in the spread of uncertainty, you calculate an error that will not be exceeded 99.7% of the time.
One final hint: you have to choose whether you choose full=True
or cov=True
. cov=True
only works when full=False
(default) or vice versa.
source to share