AstroSpeedrunSearch
D6

Least squares by hand

Builds on
Sections D1, D3 and D5
Reading time
45 min
Exercises
6
Olympiad problems
1

Space-weather services warn satellite operators a day or two before a coronal mass ejection reaches Earth, and some of their simplest forecasts rest on straight lines fitted to past storms, like the line through the ten clouds of Section D1. Those points scatter widely, and two people drawing the best line by eye would not draw quite the same one. The method of least squares gives a single line that anyone can reproduce with a table of sums and a calculator, and it gives the uncertainties of the slope and intercept from the scatter of the points. In this section we see what the method does, set out its working so that it can be done by hand in the exam, and use it to forecast the arrival of two more storms.

D6.1The line that fits best

Before you readMake a guess first

The method of least squares chooses, from all possible straight lines, the one that makes a certain quantity as small as possible. Which quantity?

For a trial line y=mx+c, each point lies a distance r=y−(mx+c) above or below it, its from Section D3. The method of least squares chooses the line that makes the sum of the squared residuals, ∑r2, as small as possible. Squaring makes every residual count whether the point is above or below the line, and it makes a point twice as far away count four times as much, so the line cannot ignore a point that lies far from it. The method measures residuals up and down only, so it treats the values of x as exact and puts all the uncertainty into y. The quantity with the larger uncertainty therefore belongs on the vertical axis.

Figure D.6Squares on the residualsinteractive
Trial line
Sum of the squared residuals
The figure shows the average accelerations a of the ten coronal mass ejections of Section D1 against their starting speeds u, in units of u0=1000 km/s. The sliders set the slope and intercept of a trial line, drawn heavy. Each point's residual is the thin vertical stroke between the point and the line, and the square drawn on it has an area proportional to the residual squared, so the total grey area stands for the sum that least squares makes as small as possible. The table gives that sum and compares it with the least it can be. The words below set and , whose sum is only 1.01 times the least.
ResultThe least-squares line(D.6)
m=SxySxx,c=y¯−mx¯Sxx=∑(x−x¯)2Sxy=∑(x−x¯)(y−y¯)
m,cthe slope and intercept of the line
x¯,y¯the means of the values of x and of y
∑a sum over all the points
The line passes through the centre of the points C, at (x¯,y¯), like every line of Section D3.
Show the derivation

The sum ∑(y−mx−c)2 is smallest where its derivatives with respect to c and to m are both zero. The derivative with respect to c is −2∑(y−mx−c), which is zero when c=y¯−mx¯. Putting this value of c into the sum gives ∑(y−y¯−m(x−x¯))2, whose derivative with respect to m is −2∑(x−x¯)(y−y¯−m(x−x¯)). This is zero when Sxy−mSxx=0, which gives m=Sxy/Sxx.

D6.2Working it out by hand

Before you readMake a guess first

Five points have x¯=2.2, and one of them has x=2.2. How much does that point change the slope of the least-squares line?

In the exam the working earns the marks, so we set it out as a table with one row for each point and columns for x, y, x−x¯, y−y¯, (x−x¯)2 and (x−x¯)(y−y¯). The sums of the last two columns are Sxx and Sxy. The means should carry one more figure than the data, because the differences x−x¯ are small differences between larger numbers.

Example D.12The Tully–Fisher relation by least squares

The faster a spiral galaxy rotates, the brighter it is. In IOAA 2021 the five galaxies of the table, with maximum rotation speeds V and absolute magnitudes K in the infrared, were to be fitted with K=mlog⁡(V/km s−1)+c by least squares, taking log⁡V as x. Find m and c.

V (km/s)K (mag)
79.4−16.8
100.1−19.2
158.5−21.3
251.2−21.4
316.2−24.0
Solution

To three places the logarithms are 1.900, 2.000, 2.200, 2.400 and 2.500, so x¯=2.200, and y¯=−20.54.

xyx−x¯y−y¯(x−x¯)2(x−x¯)(y−y¯)
1.900−16.8−0.3003.740.0900−1.122
2.000−19.2−0.2001.340.0400−0.268
2.200−21.30.000−0.760.00000.000
2.400−21.40.200−0.860.0400−0.172
2.500−24.00.300−3.460.0900−1.038

The sums are Sxx=0.2600 and Sxy=−2.600, so

m=−2.6000.2600=−10.00,c=−20.54−(−10.00)(2.200)=1.46.
AnswerThe relation is K=−10.00log⁡V+1.46. With the logarithms kept to four places, as a calculator keeps them, the intercept comes out as 1.47, the value of the official solution. The difference is far smaller than the uncertainty of the intercept, which we find below.

A calculator with a statistics mode, such as the Casio fx-82CW handed out at IOAA 2026, fits the same line from a list of values of x and y, and it calls the intercept a and the slope b. It is a quick check on the table, but the table and the sums are the working that a marker looks for.

D6.3The uncertainties of the slope and intercept

Before you readMake a guess first

Ten points scatter about a line. If the same scatter were spread over twice as wide a range of x, how would the uncertainty of the slope change?

The scatter of the points about the line tells us how uncertain the line is. The typical size of a residual is estimated by ∑r2/(n−2), where dividing by n−2 instead of n allows for the two constants that the line has already taken from the same n points, just as a standard deviation divides by N−1, as Section D7 explains. The slope is more certain when the points spread over a wide range of x, which is what Sxx measures. The intercept is the value of the line at x=0, and it is less certain the farther the data lie from x=0.

ResultUncertainties of a least-squares line(D.7)
σm=∑r2(n−2)Sxxσc=σm∑x2n
σm,σcthe uncertainties of the slope and of the intercept
rthe residuals of the points from the least-squares line
nthe number of points
These hold when the points scatter at random by about the same amount, and with only a few points they are rough. When the data come with error bars, the steepest and shallowest lines of Section D3 are the other accepted way, and an answer should say which one it used.

For the five galaxies the residuals from the line are 0.74, −0.66, −0.76, 1.14 and −0.46 magnitudes, and their squares add up to ∑r2=3.07. The sum of the squared logarithms is ∑x2=24.46, so ∑x2/n=4.892, and

σm=3.073×0.2600=1.98,σc=1.98×4.892=1.98×2.212=4.4.

With the rule of Section D3 for rounding, the relation is K=(−10.0±2.0)log⁡V+(1±4). The intercept looks hopelessly uncertain, because it is the magnitude of a galaxy rotating at 1 km/s, far outside the data. At the centre of the points the line is known to about 0.45 magnitudes, which is ∑r2/(n−2) divided by n.

Calculators also give the correlation coefficient r=Sxy/SxxSyy, where Syy=∑(y−y¯)2. It lies between −1 and +1, and it is close to one of them when the points lie close to a straight line. For the five galaxies r=−0.946. A value near ±1 does not show that a straight line is the right model, though. The periods of Jupiter's moons against the sizes of their orbits, the curve of Figure D.4, give r=0.997.

D6.4Lines through the origin

Before you readMake a guess first

A model says that y=mx exactly, with no intercept. Should the fitted line be made to pass through the origin?

When the model has no intercept, least squares gives m=∑xy/∑x2, and its uncertainty is σm=∑r2/((n−1)∑x2), with n−1 because the line now takes only one constant from the data. A line through the origin is right for Hubble's law, where a galaxy at zero distance has no velocity of recession. It is wrong whenever the model has an offset, such as an unknown zero point of a magnitude scale.

*D6.5Weighted least squaresOptional extension

When the points have different uncertainties σi, the precise ones should count for more. Each point is given a weight wi=1/σi2, and every sum of the method becomes a weighted sum. The centre of the points is then x¯w=∑wx/∑w and y¯w=∑wy/∑w, with Sxx=∑w(x−x¯w)2 and Sxy=∑w(x−x¯w)(y−y¯w). The slope is still Sxy/Sxx and the line still passes through the centre, while the uncertainties now come from the error bars themselves, as σm=1/Sxx and σc=1/∑w+x¯w2σm2.

Common errorsSection D6
  1. When the quantity on the horizontal axis is the more uncertain one, least squares gives a slope that is too shallow. The more uncertain quantity belongs on the vertical axis.
  2. The column of x−x¯ is spoilt when x¯ is rounded too early, because its values are small differences between larger numbers. The means need one more figure than the data.
  3. The uncertainties of the slope and intercept come out too small when ∑r2 is divided by n instead of n−2, by a noticeable amount when there are only a few points.
  4. The intercept is the value of the line at x=0, which may lie far outside the data, where even a well-determined line is very uncertain. A value read from the line near the centre of the points is much more certain.
  5. A correlation coefficient close to ±1 does not show that a straight line is the right model, because points on a smooth curve can give r=0.997.
ExercisesSection D66 questions
  1. D6.1

    For a set of points x¯=5, y¯=3, Sxx=40 and Sxy=−12. What is the intercept of the least-squares line?

  2. D6.2

    A least-squares line through six points has ∑r2=0.80 and Sxx=2.5. How uncertain is its slope?

  3. D6.3

    A model says y=mx with no intercept. For the data ∑xy=1250 and ∑x2=500. What is the slope of the least-squares line through the origin?

  4. D6.4

    For a set of points Sxx=4, Syy=25 and Sxy=9.6. What is the correlation coefficient?

  5. D6.5

    Why does the estimate of the scatter about a least-squares line divide ∑r2 by n−2?

  6. D6.6

    A fit gives an intercept of 1±4, but at the centre of the points the line is known to within 0.45. What is the main reason?

Olympiad problemsPast IOAA problems, restated1 problem
2025Forecasting the arrival of solar storms28 points

Adapted from IOAA 2025 (Mumbai), data-analysis problem 2, parts 1 and 2. We have restated it in our own words and kept the original data. The solution was last checked on 20 October 2026.

The table gives, for ten coronal mass ejections, the speed u at which each left the surface of the Sun, its speed v on reaching Earth and the time τ it took to arrive. Take the distance from the surface of the Sun to Earth as 214 solar radii, with a solar radius of 6.957×105 km, and treat the space in between as empty.

Cloudu (km/s)v (km/s)τ (h)
CME-A80447074.5
CME-B247360127.5
CME-C523396103.5
CME-D83041571.0
CME-E665400104.5
CME-F347350101.5
CME-G44637599.5
CME-H15536097.0
CME-I101651567.0
CME-J68341054.0

(a) Find the average acceleration a of each cloud in m s⁻². This was worth 3 marks. (b) A model says that a=m(u/u0)+α, with u0=1000 km/s. Find m and α and their uncertainties from a suitable graph. This was worth 15 marks. (c) Tabulate the model's acceleration amodel for each cloud, and find the root-mean-square deviation δarms=∑(a−amodel)2/10. This was worth 4 marks. (d) Two more clouds, CME-1 and CME-2, leave the Sun at 1044 km/s and 273 km/s. Taking each one's acceleration from the model as constant, predict how long each takes to reach Earth. This was worth 4 marks. (e) They actually arrived after 46.0 h and 74.5 h. The model counts as valid for a cloud if its prediction is within 20% of the observed time. Say whether it is valid for each. This was worth 2 marks.

AnswerThe model is a=(−3.11±0.24)u/u0+(1.10±0.15) m s⁻², with a root-mean-square deviation of 0.176 m s⁻². It predicts 48.2 h for CME-1, within 20% of the observed time, so it is valid there, and 125 h for CME-2, far outside 20%, so it is not valid there. The official solution, with a line drawn by hand, finds m=−3.05±0.09 and α=1.08±0.06 m s⁻², within our uncertainties, and the same deviation and conclusions. Its uncertainties come from steepest and shallowest lines judged by eye, which are narrower than the scatter of the points suggests.