Data fitting
Fitting models to data by least squares, from straight lines to non-linear models, with honest uncertainties and a critical view of how well a model fits.
This tutorial starts from the line of best fit drawn by eye on a graph, and its first lessons need only school mathematics: graphs and gradients, sums, and the differentiation of a polynomial. Everything else, from partial derivatives and matrices to the covariance matrix, the condition number and gradient descent, is introduced where it is needed, in short lessons that build on one another. It is written for readers who know Python, NumPy and Matplotlib, at the level of the book Learning Scientific Programming with Python.
Every example comes from mathematics, engineering or the physical sciences, with real data where they exist. Each lesson highlights the results that surprise people who have fitted data before, especially those that come from breaking the assumptions behind least squares, and demonstrates each of them by simulation or from data rather than asserting it. Each lesson ends with exercises at three levels of difficulty (★ to ★★★), whose solutions are hidden until the reader asks for them. The tutorial ends with a project, fitting the semi-empirical mass formula to the 2020 atomic mass evaluation, whose solutions are for readers who are logged in, and a cheat sheet of NumPy's and SciPy's fitting functions.
Lessons
- Least squares
-
1.
Line of best fit
The line of best fit through points on a graph, drawn by eye and then chosen by a criterion: the residuals, the sum of their squares, and a search by brute force for the line that minimises it.