# Linear Algebra/Topic: Line of Best Fit

 Linear Algebra ← Projection Onto a Subspace Topic: Line of Best Fit Topic: Geometry of Linear Maps →

Scientists are often presented with a system that has no solution and they must find an answer anyway. That is, they must find a value that is as close as possible to being an answer.

For instance, suppose that we have a coin to use in flipping. This coin has some proportion $m$ of heads to total flips, determined by how it is physically constructed, and we want to know if $m$ is near $1/2$ . We can get experimental data by flipping it many times. This is the result a penny experiment, including some intermediate numbers.

 number of flips 30 60 90 number of heads 16 34 51

Because of randomness, we do not find the exact proportion with this sample — there is no solution to this system.

${\begin{array}{*{1}{rc}r}30m&=&16\\60m&=&34\\90m&=&51\end{array}}$ That is, the vector of experimental data is not in the subspace of solutions.

${\begin{pmatrix}16\\34\\51\end{pmatrix}}\not \in \{m{\begin{pmatrix}30\\60\\90\end{pmatrix}}\,{\big |}\,m\in \mathbb {R} \}$ However, as described above, we want to find the $m$ that most nearly works. An orthogonal projection of the data vector into the line subspace gives our best guess.

${\frac {{\begin{pmatrix}16\\34\\51\end{pmatrix}}\cdot {\begin{pmatrix}30\\60\\90\end{pmatrix}}}{{\begin{pmatrix}30\\60\\90\end{pmatrix}}\cdot {\begin{pmatrix}30\\60\\90\end{pmatrix}}}}\cdot {\begin{pmatrix}30\\60\\90\end{pmatrix}}={\frac {7110}{12600}}\cdot {\begin{pmatrix}30\\60\\90\end{pmatrix}}$ The estimate ($m=7110/12600\approx 0.56$ ) is a bit high but not much, so probably the penny is fair enough.

The line with the slope $m\approx 0.56$ is called the line of best fit for this data.

Minimizing the distance between the given vector and the vector used as the right-hand side minimizes the total of these vertical lengths, and consequently we say that the line has been obtained through fitting by least-squares

(the vertical scale here has been exaggerated ten times to make the lengths visible).

We arranged the equation above so that the line must pass through $(0,0)$ because we take take it to be (our best guess at) the line whose slope is this coin's true proportion of heads to flips. We can also handle cases where the line need not pass through the origin.

For example, the different denominations of U.S. money have different average times in circulation (the $2 bill is left off as a special case). How long should we expect a$25 bill to last?

 denomination 1 5 10 20 50 100 average life (years) 1.5 2 3 5 9 20

The plot (see below) looks roughly linear. It isn't a perfect line, i.e., the linear system with equations $b+1m=1.5$ , ..., $b+100m=20$ has no solution, but we can again use orthogonal projection to find a best approximation. Consider the matrix of coefficients of that linear system and also its vector of constants, the experimentally-determined values.

$A={\begin{pmatrix}1&1\\1&5\\1&10\\1&20\\1&50\\1&100\end{pmatrix}}\qquad {\vec {v}}={\begin{pmatrix}1.5\\2\\3\\5\\9\\20\end{pmatrix}}$ The ending result in the subsection on Projection into a Subspace says that coefficients $b$ and $m$ so that the linear combination of the columns of $A$ is as close as possible to the vector ${\vec {v}}$ are the entries of $({{A}^{\rm {trans}}}A)^{-1}{{A}^{\rm {trans}}}\cdot {\vec {v}}$ . Some calculation gives an intercept of $b=1.05$ and a slope of $m=0.18$ .

Plugging $x=25$ into the equation of the line shows that such a bill should last between five and six years.

We close by considering the times for the men's mile race (Oakley & Baker 1977). These are the world records that were in force on January first of the given years. We want to project when a 3:40 mile will be run.

 year 1870 1880 1890 1900 1910 1920 1930 seconds 268.8 264.5 258.4 255.6 255.6 252.6 250.4 year 1940 1950 1960 1970 1980 1990 2000 seconds 246.4 241.4 234.5 231.1 229.0 226.3 223.1

We can see below that the data is surprisingly linear. With this input

$A={\begin{pmatrix}1&1860\\1&1870\\\vdots &\vdots \\1&1990\\1&2000\end{pmatrix}}\qquad {\vec {v}}={\begin{pmatrix}280.0\\268.8\\\vdots \\226.3\\223.1\end{pmatrix}}$ the Python program at this Topic's end gives

${\text{slope}}=-0.35$ and ${\text{intercept}}=925.53$ (rounded to two places; the original data is good to only about a quarter of a second since much of it was hand-timed).

When will a $220$ second mile be run? Solving the equation of the line of best fit gives an estimate of the year $2008$ .

This example is amusing, but serves as a caution — obviously the linearity of the data will break down someday (as indeed it does prior to 1860).

## Exercises

The calculations here are best done on a computer. In addition, some of the problems require more data, available in your library, on the net, in the answers to the exercises, or in the section following the exercises.

Problem 1

Use least-squares to judge if the coin in this experiment is fair.

 flips 8 16 24 32 40 heads 4 9 13 17 20
Problem 2

For the men's mile record, rather than give each of the many records and its exact date, we've "smoothed" the data somewhat by taking a periodic sample. Do the longer calculation and compare the conclusions.

Problem 3

Find the line of best fit for the men's $1500$  meter run. How does the slope compare with that for the men's mile? (The distances are close; a mile is about $1609$  meters.)

Problem 4
Find the line of best fit for the records for women's mile.
Problem 5

Do the lines of best fit for the men's and women's miles cross?

Problem 6

When the space shuttle Challenger exploded in 1986, one of the criticisms made of NASA's decision to launch was in the way the analysis of number of O-ring failures versus temperature was made (of course, O-ring failure caused the explosion). Four O-ring failures will cause the rocket to explode. NASA had data from 24 previous flights.

 temp °F 53 75 57 58 63 70 70 66 67 67 67 failures 3 2 1 1 1 1 1 0 0 0 0 temp °F 68 69 70 70 72 73 75 76 76 78 79 80 81 failures 0 0 0 0 0 0 0 0 0 0 0 0 0

The temperature that day was forecast to be $31^{\circ }{\text{F}}$ .

1. NASA based the decision to launch partially on a chart showing only the flights that had at least one O-ring failure. Find the line that best fits these seven flights. On the basis of this data, predict the number of O-ring failures when the temperature is $31$ , and when the number of failures will exceed four.
2. Find the line that best fits all 24 flights. On the basis of this extra data, predict the number of O-ring failures when the temperature is $31$ , and when the number of failures will exceed four.

Which do you think is the more accurate method of predicting? (An excellent discussion appears in (Dalal, Folkes & Hoadley 1989).)

Problem 7

This table lists the average distance from the sun to each of the first seven planets, using earth's average as a unit.

 Mercury Venus Earth Mars Jupiter Saturn Uranus 0.39 0.72 1.00 1.52 5.20 9.54 19.2
1. Plot the number of the planet (Mercury is $1$ , etc.) versus the distance. Note that it does not look like a line, and so finding the line of best fit is not fruitful.
2. It does, however look like an exponential curve. Therefore, plot the number of the planet versus the logarithm of the distance. Does this look like a line?
3. The asteroid belt between Mars and Jupiter is thought to be what is left of a planet that broke apart. Renumber so that Jupiter is $6$ , Saturn is $7$ , and Uranus is $8$ , and plot against the log again. Does this look better?
4. Use least squares on that data to predict the location of Neptune.
5. Repeat to predict where Pluto is.
6. Is the formula accurate for Neptune and Pluto?

This method was used to help discover Neptune (although the second item is misleading about the history; actually, the discovery of Neptune in position $9$  prompted people to look for the "missing planet" in position $5$ ). See (Gardner 1970)

Problem 8

William Bennett has proposed an Index of Leading Cultural Indicators for the US (Bennett 1993). Among the statistics cited are the average daily hours spent watching TV, and the average combined SAT scores.

 1960 1965 1970 1975 1980 1985 1990 1992 TV 5:06 5:29 5:56 6:07 6:36 7:07 6:55 7:04 SAT 975 969 948 910 890 906 900 899

Suppose that a cause and effect relationship is proposed between the time spent watching TV and the decline in SAT scores (in this article, Mr. Bennett does not argue that there is a direct connection).

1. Find the line of best fit relating the independent variable of average daily TV hours to the dependent variable of SAT scores.
2. Find the most recent estimate of the average daily TV hours (Bennett's cites Neilsen Media Research as the source of these estimates). Estimate the associated SAT score. How close is your estimate to the actual average? (Warning: a change has been made recently in the SAT, so you should investigate whether some adjustment needs to be made to the reported average to make a valid comparison.)

Computer Code

#!/usr/bin/python
# least_squares.py   calculate the line of best fit for a data set
# data file format: each line is two numbers, x and y
n = 0
sum_x = 0
sum_y = 0
sum_x_squared = 0
sum_xy = 0

fn = raw_input("Name of the data file? ")
datafile = open(fn,"r")
while 1:
if ln:
data = ln.split()
x = float(data)
y = float(data)
n += 1
sum_x += x
sum_y += y
sum_x_squared += x*x
sum_xy += x*y
else:
break
datafile.close()

slope = (n*sum_xy - sum_x*sum_y) / (n*sum_x_squared - sum_x**2)
intercept = (sum_y - slope*sum_x)/n
print "line of best fit: slope= %f  intercept= %f" % (slope, intercept)