A line and curve fitter
The problem
Fit straight lines and parabolas to noisy data using only plain Python lists: build the normal equations, solve them by elimination, and judge the fit from the errors.
Requirements
- Read a list of points and build the sums needed for the normal equations
- Solve the equations with Gaussian elimination written by you
- Report the coefficients, the sum of squared errors and the residuals
- Compare a line with a parabola on the same data and say which fits better
Technologies
- Python 3
- Matrices as lists of lists
- Gaussian elimination
- Least squares and normal equations
Step by step
- Create a data set of at least ten points from a known curve plus small errors.
- Write a function that builds AᵀA and Aᵀy for a model with k columns.
- Solve the system and print the coefficients, then check that the residuals add up to about zero.
- Fit both a line and a parabola and compare their squared errors.
Expected result
A program that recovers the hidden curve from noisy points and explains how good the fit is.
How it is evaluated
- Coefficients are close to the true ones
- The residuals add up to about zero
- The elimination handles a row swap correctly
- The comparison states which model fits better and why
Ways to improve it
- Add an outlier and show how it changes the fit
- Fit a cubic with the same function
- Use fractions to get exact coefficients on small data