A7 · Lesson 29 · Lab: least squares and QR in code
Fitting data, and doing it accurately
Least squares is the workhorse of measurement: calibrating sensors, fitting time constants, estimating parameters from noisy data. The normal equations give the answer in one line, and for many problems they're fine. For badly conditioned ones they throw away half the available digits, because forming squares the condition number. In this lab you'll fit an RTD's calibration curve, watch classical Gram-Schmidt lose orthogonality where the modified version keeps it, compare the normal equations with QR on a fit that punishes the shortcut, and weight measurements by how much you trust them.
- The three points are , and , the data from lesson 27. The solid line has the slope you choose, with the intercept that suits it best.
- The readout is the sum of squared residuals. Find the slope that makes it smallest.
- The dashed line is the least-squares fit, . At its slope the readout bottoms out at 0.167.
Predict
A fitting matrix has condition number . About how many significant digits can the normal equations deliver in double precision?
About 4. has condition number , and double precision carries about 16 digits, so only about 4 survive. QR works with itself and keeps about 10. For a well-conditioned fit, say condition number 10, both are fine; the difference matters as the columns become nearly dependent, as powers of do on a short interval.
Name it
Three ways to solve least squares
- Normal equations:
np.linalg.solve(A.T @ A, A.T @ b). Fast and simple; condition number squared. - QR:
Q, R = np.linalg.qr(A), then solve by back substitution. About twice the work; condition number unchanged. - SVD, through
np.linalg.lstsq: the most robust, and it handles rank-deficient with the pseudoinverse.
Classical and modified Gram-Schmidt
Both subtract projections onto the 's found so far. Classical computes every coefficient from the original column; modified updates the column after each subtraction and computes the next coefficient from what's left. In exact arithmetic they agree. In floating point, the modified version's 's stay orthogonal to about the rounding level times the condition number, while the classical version's can lose orthogonality entirely.
Weighted least squares
When measurement has standard deviation , minimize . Dividing row of and entry of by turns it into ordinary least squares. The normal equations become with . Precise points pull the fit harder.
Worked example
Why does the trial line's error bottom out at slope 1.5?
Show the solution
- The error with the best intercept. With the best intercept for each slope, the sum of squared residuals is
- The sums. They are sums of products of deviations from the means:
- Minimize the parabola. That's a parabola in , smallest at where it equals
- Intercept.
Answer the error is a parabola in the slope, lowest at (with intercept 1.167), where the sum of squares is 0.167.
Worked example
A degree-9 polynomial is fitted to 20 equally spaced points on . How do the normal equations and QR compare?
Show the solution
- Condition number. The Vandermonde matrix's condition number is .
- Normal equations. With data generated from coefficients that are all 1, the normal equations recover them with a largest error of .
- QR. It recovers them with a largest error of , about 400 000 times better.
- Why. That's roughly the factor of the normal equations give away by squaring it.
Answer QR is about 400 000 times more accurate, a largest error of against .
Check
A fit's matrix has condition number 1000. About how many digits can the normal equations keep, out of 16?
Check
Two measurements have standard deviations 0.1 and 0.5. How many times more weight does the first get in weighted least squares?
Check
An RTD's resistance follows . Fit the data in the starter by the normal equations, set c to the three coefficients, and set alpha = c[1] / c[0], the temperature coefficient.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write cgs(A) (classical Gram-Schmidt) and mgs(A) (modified). On the matrix in the starter, set loss_c and loss_m to from each.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Fit a degree-9 polynomial to 20 points on whose true coefficients are all 1. Solve by the normal equations and by QR, and set err_normal and err_qr to each one's largest coefficient error.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Four measurements in the starter have standard deviations sg. Fit by weighted least squares, scaling each row by , and set b0 and b1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does QR give a more accurate least-squares answer than the normal equations when the columns are nearly dependent?
Nearly dependent columns make ill conditioned, with condition number , so any method loses about digits to the problem itself. The normal equations form first, whose condition number is , and solving it loses digits: the information that separates the nearly dependent columns is partly rounded away the moment the products are summed. QR never forms . It rotates into triangular form with orthogonal transformations, which don't change lengths or amplify errors, so it loses only what the problem's own conditioning demands. For , that's about 10 digits kept instead of 4.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.