Julia Data Fitting
Data fitting is the process of finding a simple curve that stays close to a set of points. The method used here is called least squares.
A perfect line fits exactly
The points lie on the line 1 plus 2 times x. This is least squares in action.
A = [1.0 0.0; 1.0 1.0; 1.0 2.0]
y = [1.0, 3.0, 5.0]
coefs = A \ y
println(round.(coefs, digits=6))A noisy line fit gives rounded coefficients
The points sit near a straight line but not on it. The backslash operator still finds the closest line.
A = [1.0 0.0; 1.0 1.0; 1.0 2.0; 1.0 3.0]
y = [1.1, 2.9, 5.2, 6.8]
coefs = A \ y
println(round.(coefs, digits=6))The fitted line predicts a new point
The perfect line has intercept 1.0 and slope 2.0. At x equal to 3.0, the line gives 7.0.
A = [1.0 0.0; 1.0 1.0; 1.0 2.0]
y = [1.0, 3.0, 5.0]
coefs = A \ y
println(round(coefs[1] + coefs[2] * 3.0, digits=6))Example
A = [1.0 0.0; 1.0 1.0; 1.0 2.0; 1.0 3.0]
y = [1.1, 2.9, 5.2, 6.8]
coefs = A \ y
println(round.(coefs, digits=6))
println(round(coefs[1] + coefs[2] * 4.0, digits=6))Key points
- Line fit: a line fit is a straight line chosen to stay close to the points.
- Least squares: least squares picks the line with the smallest total squared error.
- Vandermonde matrix: a Vandermonde matrix holds powers of the input values in its columns.
- Backslash operator: the backslash operator solves for the line coefficients from the matrix and the outputs.
- Coefficient: a coefficient is one number in the fitted line, such as its intercept or slope.
In this example, a matrix and a list of outputs are fitted with the backslash operator. The first line prints the fitted coefficients, rounded to six digits. The second line uses those coefficients to predict the output at 4.0.
Note
the fitted line need not pass through every point.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does the backslash operator find for a line fit?