Data fitting with fit uncertainties

This started out as a way to make sure I understood the numpy array slicing methods, and builds on my previous post about using scipy to fit data. I define a 3 parameter exponential decay

    \[\mathrm{func}(x, a, b, c) = a e^{-b x} + c\]

add some gaussian noise, and then use scipy to get the best fit as well as the covariance matrix. My understanding is that the square root of the diagonal elements gives me the 1 \sigma uncertainty on the corresponding fit parameter. So I then use the uncertainties on a, b, c to compute all 8 possible effective parameter values and their corresponding fit arrays.

I then use numpy to find the standard deviation of the 8 different fit values at each x, and use this as the 1\;\sigma uncertainty on the fit at a given x. Once I have this array of fit uncertainties, I plot the best fit curve, the fit+ 1\sigma curve, the fit - 1\sigma curve, and use the matplotlib plot.bar( ) function to plot the \pm 3\sigma bars.

In any case, here is the script: CurveFitting.py , and here is an iPythonNotebook with some exposition: CurveFitWith1SigmaBand.ipynb .

Here is the final plot: (click on plot to see .pdf version)

About PaulNakroshis

Professor of Physics at the University of Southern Maine.
This entry was posted in curve fitting, data analysis, error bars, iPython, Matplotlib, Plotting. Bookmark the permalink.

2 Responses to Data fitting with fit uncertainties

  1. Justin says:

    Hello, I would like to know if i can use this image in teaching material to discuss scatter graphs and best fitting?

Leave a Reply

Your email address will not be published. Required fields are marked *

*