# [SciPy-User] How to estimate error in polynomial coefficients from scipy.polyfit?

josef.pktd@gmai... josef.pktd@gmai...
Thu Mar 25 15:58:32 CDT 2010

```On Thu, Mar 25, 2010 at 4:52 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
>
>
> On Thu, Mar 25, 2010 at 2:32 PM, Jeremy Conlin <jlconlin@gmail.com> wrote:
>>
>> I am using scipy.polyfit to fit a curve to my data.  Members of this
>> list have been integral in my understanding of how to use this
>> function.  Now I would like to know how I can get the uncertainties
>> (standard deviations) of polynomial coefficients from the returned
>> values from scipy.polyfit.  If I understand correctly, the residuals
>> are sometimes called the R^2 error, right?  That gives an estimate of
>> how well we have fit the data.  I don't know how to use rank or any of
>> the other returned values to get the uncertainties.
>>
>
> You want the covariance of the coefficients, (A.T * A)^-1/var, where A is
> the design matrix. I'd have to see what the scipy fit returns to tell you
> more. In anycase, from that you can plot curves at +/- sigma to show the
> error bounds on the result. I can be more explicit if you want.
>
> Chuck

the easiest way to get the full statistical results for the fit is to
use statsmodels

something like
import scikits.statsmodels as sm
results = sm.OLS(y, np.vandermonde(x, order)).fit()
dir(results)
with standard deviations on parameter estimates, ...

results.params should be the same as np.polyfit
results.model.predict(xnew)
and there are somewhere the prediction errors

without statsmodels it is a few lines of code, but requires "thinking"

Josef

>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
```