Data Analysis

Chapter 7 of 11 · 13 min read

Least squares fitting

The formulas for a best-fit line and its uncertainties, weighted fits, lines through the origin, and what r and χ² tell you.

The ordinary least squares line

For NN points (xi,yi)(x_i, y_i), the line y=mx+cy = mx + c that minimises ∑(yi−mxi−c)2\sum(y_i - mx_i - c)^2 has

m=N∑xiyi−∑xi∑yiΔ,c=∑xi2∑yi−∑xi∑xiyiΔ,Δ=N∑xi2−(∑xi)2.m = \frac{N\sum x_iy_i - \sum x_i\sum y_i}{\Delta}, \qquad c = \frac{\sum x_i^2\sum y_i - \sum x_i\sum x_iy_i}{\Delta}, \qquad \Delta = N\sum x_i^2 - \left(\sum x_i\right)^2.

Equivalently, m=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2m = \dfrac{\sum(x_i-\bar x)(y_i-\bar y)}{\sum(x_i-\bar x)^2} and c=yˉ−mxˉc = \bar y - m\bar x, which is less prone to rounding errors.

To get uncertainties, first estimate the scatter about the line, using N−2N-2 because two parameters were fitted:

sy=∑(yi−mxi−c)2N−2,σm=syNΔ,σc=sy∑xi2Δ.s_y = \sqrt{\frac{\sum(y_i - mx_i - c)^2}{N-2}}, \qquad \sigma_m = s_y\sqrt{\frac{N}{\Delta}}, \qquad \sigma_c = s_y\sqrt{\frac{\sum x_i^2}{\Delta}}.

Weighted fits

If each yiy_i has its own uncertainty σi\sigma_i, use weights wi=1/σi2w_i = 1/\sigma_i^2 and replace N→∑wiN\to\sum w_i, ∑xi→∑wixi\sum x_i\to\sum w_ix_i, and so on:

Δw=∑wi∑wixi2−(∑wixi)2,σm=∑wiΔw,σc=∑wixi2Δw.\Delta_w = \sum w_i\sum w_ix_i^2 - \left(\sum w_ix_i\right)^2, \qquad \sigma_m = \sqrt{\frac{\sum w_i}{\Delta_w}}, \qquad \sigma_c = \sqrt{\frac{\sum w_ix_i^2}{\Delta_w}}.

These errors come from the error bars, not from the scatter, so they are only honest if the error bars are.

A line through the origin

When theory says y=mxy = mx (Hubble's law, Δθ=θEX\Delta\theta = \theta_EX, MDM=kXM_{\rm DM} = kX), fit only the slope:

m=∑wixiyi∑wixi2,σm=1∑wixi2.m = \frac{\sum w_ix_iy_i}{\sum w_ix_i^2}, \qquad \sigma_m = \frac{1}{\sqrt{\sum w_ix_i^2}}.

This is exactly how the 2023 solution gets θE=4.5±0.4\theta_E = 4.5\pm0.4 mas from eight epochs.

Judging the fit

  • Correlation coefficient rr: close to ±1\pm1 means a tight linear relation. It does not tell you the line is the right model: a gentle curve can have r=0.99r = 0.99.
  • Residuals yi−(mxi+c)y_i - (mx_i + c): plot them against xx. Random scatter is good; a systematic arch means curvature you have not modelled.
  • Chi-square χ2=∑(yi−mxi−cσi)2\chi^2 = \sum\left(\dfrac{y_i - mx_i - c}{\sigma_i}\right)^2: for a good model and honest errors, χ2/(N−2)≈1\chi^2/(N-2)\approx1. Much larger means the errors are underestimated or the model is wrong; much smaller means the errors are overestimated.

Doing it with a calculator

Most scientific calculators have a linear regression ("LR", "STAT", "A+BX") mode that gives mm, cc and rr directly. They usually do not give errors, but they do give the sums ∑x\sum x, ∑y\sum y, ∑x2\sum x^2, ∑xy\sum xy, from which you get Δ\Delta and the formulas above. Practise this before the exam on the 2018 IRX–β data. You should recover IRX=0.98β+1.96\mathrm{IRX} = 0.98\beta + 1.96 in about five minutes.

Write the fitted equation on the graph, and draw the line on it as well. Several schemes give a separate mark for "best-fit equation noted on the same page".

Key formulas

Slope and intercept
m=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2,c=yˉ−mxˉm = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2}, \qquad c = \bar y - m\bar x
Their uncertainties
σm=syNΔ,σc=sy∑xi2Δ,sy2=∑ri2N−2\sigma_m = s_y\sqrt{\frac{N}{\Delta}}, \quad \sigma_c = s_y\sqrt{\frac{\sum x_i^2}{\Delta}}, \quad s_y^2 = \frac{\sum r_i^2}{N-2}
Weighted line through the origin
m=∑xiyi/σi2∑xi2/σi2,σm=(∑xi2/σi2)−1/2m = \frac{\sum x_iy_i/\sigma_i^2}{\sum x_i^2/\sigma_i^2}, \qquad \sigma_m = \Big(\sum x_i^2/\sigma_i^2\Big)^{-1/2}
Goodness of fit
χν2=1N−2∑i(yi−mxi−cσi)2≈1\chi^2_\nu = \frac{1}{N-2}\sum_i \left(\frac{y_i - mx_i - c}{\sigma_i}\right)^2 \approx 1

Common mistakes

  • Using N instead of N − 2 in the scatter about a fitted line.
  • Taking r ≈ 1 as proof that the relation is linear.
  • Fitting with an intercept when the physics requires the line to pass through the origin, or the reverse.
  • Fitting all points including ones the method says must be excluded (2023: a straight line through all eight epochs absorbs the deflection).

Practise this chapter

Past IOAA problems that use these skills.

Saved in this browser only.