Data fitting with fit uncertainties

(Updated 11-Jan-2022; fixed broken links and updated notebook)

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 a Jupyter Notebook with some exposition: CurveFitWith1SigmaBand.ipynb

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

3sigmaPlot

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.

4 Responses to Data fitting with fit uncertainties

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.