A11 · Lesson 39 · Least-squares fitting
Least squares, residuals and the error in a slope
You measure the current through a resistor at eight voltages. The points almost line up, but not quite, because the meter is noisy. The resistance is one over the slope of the line through them. Which line, exactly? And how precisely do eight noisy points pin down the slope? Least squares picks the line, and the scatter of the points around it tells you how far to trust the answer. It's how sensors are calibrated, how device parameters are extracted, and, in matrix form, the projection of A7.
- The dots are eight measured currents. The solid line is your trial line, ; the dashed one is the least-squares line.
- Adjust and to make the sum of squared vertical misses as small as you can. Can you beat the dashed line?
- The readout also gives the resistance your slope implies, .
Predict
The best line through these points has slope 0.497 mA/V, and the points scatter about 0.1 mA from it. Roughly how well is the resistance determined?
To about 3%. The slope's standard error is the scatter divided by the spread of the voltages, mA/V, which is 3% of the slope. So kΩ. More points, or a wider voltage range, would shrink it.
Name it
The least-squares line
For data and a model , the residuals are . Least squares chooses and to minimize . Setting both partial derivatives to zero (A9.10) gives
with and . The line passes through the point of averages .
Matrix form. Stack the equations as , with a column of ones and a column of values in and . The least-squares solves the normal equations : it projects onto the columns of (A7.27). The same works for any model that's linear in its unknowns, such as polynomials. In NumPy, np.polyfit(x, y, 1) returns and np.linalg.lstsq(A, y) solves the general case.
Why squares? If the noise is Gaussian and independent, minimizing the squared residuals is exactly maximum likelihood (lesson 35).
How good is the fit
- Noise estimate: , dividing by because two parameters were fitted (Bessel's correction again).
- Slope uncertainty: . Spreading the values out shrinks it as much as adding points.
- : , the fraction of the variation the line explains.
- Look at the residuals. A curve or a trend in them means the model is wrong, whatever says.
Straightening curves
Many EE laws become lines after a transformation. An RC decay gives , a line in with slope . A diode's is a line in versus . Fit the line, then convert back. (Taking logs also reshapes the noise, so points near zero get too much weight; use only points well above the noise.)
Worked example
Find the least-squares line for the eight points in the sim, the resistance and its uncertainty.
Show the solution
- Center the data. V and V².
- Fit the line. The fit gives mA/V and mA, so
- Noise from the residuals. The residuals give SSE mA², so
- Slope uncertainty. a relative error of 3.1%.
Answer the line is (mA), and kΩ.
Worked example
An RC decay reads 3.0 V at 1 ms and 1.1 V at 3 ms. Estimate τ by straightening the curve.
Show the solution
- Slope of the straightened curve. On a plot of against ,
- Time constant.
- With more points. You'd fit a line to all the pairs.
Answer is about 1.99 ms.
Check
For some data, = 21 and = 42. What is the least-squares slope?
Check
A fit has slope 0.8, and the data average = 3, = 2. What is the intercept?
Check
A straight-line fit to 8 points leaves SSE = 0.06. What is the estimated noise per point?
Check
A fit has noise estimate = 0.1 and = 42. What is the standard error of the slope?
Check
A fit leaves SSE = 0.5, and the data's total sum of squares about their mean is 20. What is ?
Check
A line fitted to against (in ms) for an RC decay has slope −0.2 per ms. What is τ?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
Write fit_line(x, y) returning (a, b) for the least-squares line , using and (no polyfit). The test compares with NumPy on random data.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write slope_se(x, y) returning the standard error of the least-squares slope: fit the line, compute from the residuals, and return .
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 spreading the values out make the slope more precise?
The slope is set by how changes between points far apart in . Noise moves each point up or down by about the same amount wherever it is, so its effect on the slope is that error divided by how far apart the points are. Points bunched together let a small vertical error swing the line a lot; points spread wide pin it down. That's why , the spread of the values, sits in the denominator of the slope's standard error, and why you should measure across the widest range you trust the model over.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.