Linear Algebra/Topic: Line of Best Fit

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. Because of randomness, we do not find the exact proportion with this sample &mdash; 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? 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. 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. 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 &mdash; 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.''

/Solutions/ Computer Code

Additional Data
Data on the progression of the world's records (taken from the Runner's World web site) is below.