From Data to a Best-Fit Line
Given data points \((x_i,y_i)\), a line will not generally pass through every point. A best-fit line instead chooses its intercept and slope to make the total squared vertical error as small as possible. The calculation can be organized as a least-squares problem: the unknown coefficients form a vector, and the columns of a design matrix describe the constant and \(x\)-dependent parts of the line.
Here we work over \(\mathbb{R}\), with \(n\) data points and not all \(x_i\) equal. Write a candidate line as \(\widehat y=a+bx\), where \(a\) is the intercept and \(b\) is the slope. Its residual at data point \(i\) is \(r_i=y_i-(a+bx_i)\). The least-squares line minimizes the sum of squared residuals, \(\sum_{i=1}^n r_i^2\). In matrix form, the coefficient vector is \(\mathbf{c}=\begin{bmatrix}a\\b\end{bmatrix}\), and each row of the design matrix records the constant term and the corresponding \(x_i\).
The first column of \(\mathbf{X}\) represents the intercept; its second column represents the slope contribution. Thus \(\mathbf{X}\mathbf{c}\) contains the fitted values \(a+bx_i\). When the \(x_i\) are not all equal, these two columns are linearly independent: if \(\alpha+\beta x_i=0\) for every \(i\), subtracting the equations for two distinct \(x_i\) gives \(\beta=0\), and then \(\alpha=0\). This condition makes the least-squares coefficients unique.
Why the Normal Equations Identify the Best Fit
For a candidate coefficient vector \(\mathbf{c}\), the residual is \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{c}\). The normal equations are \(\mathbf{X}^{\top}\mathbf{X}\mathbf{c}=\mathbf{X}^{\top}\mathbf{y}\), equivalently \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\). For a line, this says that the residual is orthogonal to each of the two design-matrix columns: the constant column and the \(x\)-values column.
Proof. For any change \(\mathbf{h}\in\mathbb{R}^p\), the residual from the coefficients \(\mathbf{c}+\mathbf{h}\) is \(\mathbf{r}-\mathbf{X}\mathbf{h}\). Expanding its squared norm gives
If \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), the middle term vanishes, and the last term is nonnegative. Therefore no change \(\mathbf{h}\) can reduce the squared residual, so \(\mathbf{c}\) is a minimizer. Conversely, suppose \(\mathbf{c}\) is a minimizer. For any \(\mathbf{h}\), changing the coefficients to \(\mathbf{c}+t\mathbf{h}\), where \(t\in\mathbb{R}\), changes the squared residual by \(-2t\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}+t^2\|\mathbf{X}\mathbf{h}\|^2\). If \(\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}\ne0\), choosing \(t\) with the same sign as this quantity and sufficiently small magnitude makes this change negative: the linear term is negative and dominates the quadratic term. That contradicts minimality. Thus \(\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}=0\) for every \(\mathbf{h}\); taking \(\mathbf{h}\) to be each standard basis vector gives \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\). Finally, if the columns are independent, then \(\mathbf{X}\mathbf{h}=\mathbf{0}\) implies \(\mathbf{h}=\mathbf{0}\). For a minimizer, the displayed expansion reduces to \(\|\mathbf{r}\|^2+\|\mathbf{X}\mathbf{h}\|^2\), which is strictly larger whenever \(\mathbf{h}\ne\mathbf{0}\). Hence the minimizer is unique. \(\square\)
Worked Example: Solve the Normal Equations
Worked Example: Fit a Line to Four Data Points
Find the least-squares line for \((0,1),(1,2),(2,2),(3,4)\) using the design matrix and normal equations.
The design matrix and response vector are
The matrix products are \(\mathbf{X}^{\top}\mathbf{X}=\begin{bmatrix}4&6\\6&14\end{bmatrix}\) and \(\mathbf{X}^{\top}\mathbf{y}=\begin{bmatrix}9\\18\end{bmatrix}\): the entries come from \(4\) ones, \(\sum x_i=6\), \(\sum x_i^2=14\), \(\sum y_i=9\), and \(\sum x_i y_i=18\). Thus the normal equations are
Row-reduce the augmented matrix. First apply \(R_2\to R_2-\tfrac32R_1\), then \(R_2\to\tfrac15R_2\), \(R_1\to R_1-6R_2\), and \(R_1\to\tfrac14R_1\):
Therefore \(a=\tfrac9{10}\), \(b=\tfrac9{10}\), and the best-fit line is \(\widehat y=\tfrac9{10}+\tfrac9{10}x\). The design-matrix columns are independent because the \(x_i\) are not all equal, so the theorem guarantees this line is the unique least-squares fit.
Check: The fitted values are \(\tfrac9{10},\tfrac{18}{10},\tfrac{27}{10},\tfrac{36}{10}\), so the residual vector is \(\mathbf{r}=(\tfrac1{10},\tfrac15,-\tfrac7{10},\tfrac25)^\top\). Its entries sum to \(\tfrac1{10}+\tfrac2{10}-\tfrac7{10}+\tfrac4{10}=0\), and their \(x\)-weighted sum is \(0+\tfrac2{10}-\tfrac{14}{10}+\tfrac{12}{10}=0\). Hence \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), as required.
The Closed-Form Coefficients
The same calculation can be written using averages and centered data. Define \(\bar x=\frac1n\sum_{i=1}^n x_i\) and \(\bar y=\frac1n\sum_{i=1}^n y_i\). Provided the \(x_i\) are not all equal, the best-fit line has slope and intercept
These formulas follow by writing the two normal equations as \(na+b\sum x_i=\sum y_i\) and \(a\sum x_i+b\sum x_i^2=\sum x_i y_i\). The first gives \(a=\bar y-b\bar x\). Substituting this into the second gives \(b\bigl(\sum x_i^2-(\sum x_i)^2/n\bigr)=\sum x_i y_i-(\sum x_i)(\sum y_i)/n\). The parenthesized expression equals \(\sum(x_i-\bar x)^2\), and the right side equals \(\sum(x_i-\bar x)(y_i-\bar y)\), yielding the stated slope. The denominator is a sum of squares; it is zero only if every \(x_i=\bar x\).
Worked Example: Use the Centered-Sum Formulas
Recalculate the line for \((0,1),(1,2),(2,2),(3,4)\) using the closed-form formulas.
The means are \(\bar x=(0+1+2+3)/4=3/2\) and \(\bar y=(1+2+2+4)/4=9/4\). The centered \(x\)-values are \(-3/2,-1/2,1/2,3/2\), and the centered \(y\)-values are \(-5/4,-1/4,-1/4,7/4\). The numerator of the slope is
The denominator is \(\tfrac94+\tfrac14+\tfrac14+\tfrac94=5\). Therefore \(b=(9/2)/5=9/10\), and \(a=9/4-(9/10)(3/2)=45/20-27/20=9/10\). This agrees with the normal-equation calculation.
Check: The residuals are again \(\tfrac1{10},\tfrac15,-\tfrac7{10},\tfrac25\). Their sum is \(0\), and their weighted sum \(0(\tfrac1{10})+1(\tfrac15)+2(-\tfrac7{10})+3(\tfrac25)=0\). Thus both the intercept-column and slope-column orthogonality conditions hold.
Worked Example: A Second Dataset
Worked Example: Compare Both Methods on Three Points
Fit a line to \((1,3),(2,5),(4,6)\), first by normal equations and then by the closed-form coefficients.
Here \(\sum x_i=7\), \(\sum x_i^2=21\), \(\sum y_i=14\), and \(\sum x_i y_i=37\). Hence the normal equations are
Apply \(R_2\to R_2-\tfrac73R_1\), \(R_2\to\tfrac{3}{14}R_2\), \(R_1\to R_1-7R_2\), and \(R_1\to\tfrac13R_1\):
Thus \(a=5/2\) and \(b=13/14\). For the formula check, \(\bar x=7/3\), \(\bar y=14/3\). The centered sums are \(\sum(x_i-\bar x)^2=14/3\) and \(\sum(x_i-\bar x)(y_i-\bar y)=13/3\), so \(b=(13/3)/(14/3)=13/14\). Then \(a=14/3-(13/14)(7/3)=5/2\).
Check: The fitted values at \(x=1,2,4\) are \(24/7,61/14,87/14\). Subtracting them from the observations gives residuals \(-3/7,9/14,-3/14\). Their sum is \(-6/14+9/14-3/14=0\), and their \(x\)-weighted sum is \(-6/14+18/14-12/14=0\). The residual is orthogonal to both design columns, verifying the fit.
Common Pitfall: Omitting the Intercept
A frequent setup error is using the single-column design matrix \(\begin{bmatrix}0&1&2&3\end{bmatrix}^{\top}\) for the first dataset. That matrix fits lines of the form \(\widehat y=bx\), which are forced through the origin; it does not fit the general line \(a+bx\). For that restricted model the least-squares slope is \(\sum x_i y_i/\sum x_i^2=18/14=9/7\), not \(9/10\). Its residuals are \(1,5/7,-4/7,1/7\), whose sum is \(1+5/7-4/7+1/7=9/7\ne0\). This fails the constant-column orthogonality condition because the constant column was omitted. The correction is to include a column of ones whenever the model includes an intercept.
For a correctly formed line-fitting design matrix, the two orthogonality checks have a direct interpretation: residuals sum to zero, and their \(x\)-weighted sum is zero. They are not separate rules to memorize; together they say \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), which certifies the least-squares minimum. When the \(x\)-values vary, the fit is unique, and either the normal equations or the centered-sum formulas produces the same coefficients.
Check Your Understanding
For each question, keep the intercept in the model unless the question explicitly says otherwise.
- For \((0,2),(1,3),(2,5)\), write the design matrix and response vector.
- For those data, compute \(\bar x\), \(\bar y\), and the closed-form slope and intercept.
- State the two residual sums that must be zero for a line fit, and explain which design-matrix column each corresponds to.
- Why does a dataset with all \(x_i\) equal fail to determine a unique slope and intercept?
- For the first worked dataset, compute the slope obtained by forcing the line through the origin and check whether its residuals sum to zero.