A1139 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, I=a+bV; the dashed one is the least-squares line.
  • Adjust a and b 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, 1/b.

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?

Name it

The least-squares line

For data (xi,yi) and a model y=a+bx, the residuals are ri=yi−a−bxi. Least squares chooses a and b to minimize SSE=∑ri2. Setting both partial derivatives to zero (A9.10) gives

b=SxySxxa=y¯−bx¯

with Sxx=∑(xi−x¯)2 and Sxy=∑(xi−x¯)(yi−y¯). The line passes through the point of averages (x¯,y¯).

Matrix form. Stack the equations as Aθ≈y, with a column of ones and a column of x values in A and θ=(a,b). The least-squares θ solves the normal equations ATAθ=ATy: it projects y onto the columns of A (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 (b,a) 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: se=SSEn−2, dividing by n−2 because two parameters were fitted (Bessel's correction again).
  • Slope uncertainty: sb=seSxx. Spreading the x values out shrinks it as much as adding points.
  • R2: 1−SSE∑(yi−y¯)2, 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 R2 says.

Straightening curves

Many EE laws become lines after a transformation. An RC decay v=V0e−t/τ gives ln⁡v=ln⁡V0−tτ, a line in t with slope −1τ. A diode's I=ISeV/nVT is a line in ln⁡I versus V. 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
  1. Center the data. V¯=3.5 V and Sxx=42 V².
  2. Fit the line. The fit gives b=0.4968 mA/V and a=0.015 mA, so R=1b=2.013kΩ
  3. Noise from the residuals. The residuals give SSE =0.0582 mA², so se=0.0582/6=0.098mA
  4. Slope uncertainty. sb=0.09842=0.0152mA/V a relative error of 3.1%.

Answer the line is I=0.015+0.4968V (mA), and R=2.01±0.06 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
  1. Slope of the straightened curve. On a plot of ln⁡v against t, slope=ln⁡1.1−ln⁡3.03−1=−1.0032per ms
  2. Time constant. τ=21.003=1.99ms
  3. With more points. You'd fit a line to all the (t,ln⁡v) pairs.

Answer τ is about 1.99 ms.

Check

For some data, Sxy = 21 and Sxx = 42. What is the least-squares slope?

Check

A fit has slope 0.8, and the data average x¯ = 3, y¯ = 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 se = 0.1 and Sxx = 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 R2?

Check

A line fitted to ln⁡v against t (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 y=a+bx, using b=Sxy/Sxx and a=y¯−bx¯ (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 se=SSE/(n−2) from the residuals, and return se/Sxx.

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 x values out make the slope more precise?

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.