A7 · Lesson 27 · Least squares and projection
The best answer when there's no exact one
Six measurements of current at six voltages give six equations for one unknown, the resistance. With measurement noise they can't all be true at once: the system has no exact solution. Yet there's clearly a best answer. Least squares finds it, and its geometry, dropping a perpendicular, is one of the most useful pictures in all of engineering.
Play
- The dots are six noisy measurements. The line goes through the origin with slope , the conductance.
- Move to make the line fit the dots as well as you can. The strip shows the sum of the squared vertical misses.
- Find the smallest total you can. Compare your slope with the least-squares slope in the strip.
Predict
The best slope minimizes the sum of squared misses. At that slope, what is true of the misses?
The voltage-weighted misses add to zero. That's what setting the derivative of the total to zero gives, and it says the vector of misses is perpendicular to the vector of voltages. Least squares is a projection: the best fit is the point in the reachable set closest to the data, and the leftover miss sticks out at right angles.
Name it
Least squares is a projection
Write the measurements as an overdetermined system : more equations than unknowns. For the line through the origin, is the column of voltages, is the single unknown , and is the column of currents.
No makes exactly, so choose the one that makes the residual as short as possible. The closest point to is where is perpendicular to every column of :
These are the normal equations. For a single column they reduce to
the formula in the strip. For a line with an intercept, has a second column of ones, and the normal equations are 2 × 2. This is exactly what np.polyfit solved in A4.
Projection. is the projection of onto the space the columns of can reach. In two dimensions, projecting onto the direction of gives : the same formula as .
In practice. Solve least-squares problems with np.linalg.lstsq(A, b), which avoids forming (that step squares the condition number and loses accuracy). The same mathematics fits curves, estimates channels in a radio receiver and locates you from GPS satellites.
Worked example
Fit to , , .
Show the solution
- The formula.
- Plug in.
Answer the best slope is .
Worked example
Fit to , , , and check the residuals.
Show the solution
- Normal equations.
- Solve. They give and .
- Residuals.
- Check them. They add to zero and are perpendicular to too, as the projection requires.
Answer , with residuals , 0.333 and .
Worked example
A capacitor's voltage is measured as 10, 6.1 and 3.6 V at = 0, 1 and 2 s. Fit .
Show the solution
- Take logarithms. is a straight line in , so fit = 2.3026, 1.8083, 1.2809 by least squares.
- Slope. With :
- Intercept. The intercept is 2.3081.
- Back to the exponential.
- A caution. Fitting the logarithm weights the small late readings more heavily than a direct fit would.
Answer = 10.06 V and = 1.958 s.
Check
Two measurements: 6 mA at 2 V and 14 mA at 4 V. What is the least-squares slope (in mA per volt) of a line through the origin?
Write units the way you normally would: 1.5 mA/V, 1500 µA/V and 0.0015 A/V are the same answer.
Check
Project onto the direction of . What is the first component of the projection?
Check
Fit a line with an intercept, , to the six measurements in the sim using np.linalg.lstsq. Set s and c.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Fit lines to , , .
(a)
What slope does , through the origin, get?
(b)
What slope does get?
Check
Fit to and .
Step 1
With , what is ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
. What is ?
Check
Fit by least squares to , and . What is the sum of the squared residuals at the best ?
Explain why
Why is the least-squares residual perpendicular to the columns of A?
The fitted values can only lie in the space the columns of A reach, and least squares picks the point there closest to the data . The closest point to in a line or plane is the foot of the perpendicular from , like dropping a plumb line. If the residual leaned along any column, you could move a little in that column's direction and get closer, so at the best fit the residual has no component along any column: it's perpendicular to all of them.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.