# Linear Least Squares and Linear Regression

The method of least squares is a standard approach in regression analysis to approximate the solution of overdetermined systems (sets of equations in which there are more equations than unknowns) by minimizing the sum of the squares of the residuals (a residual being the difference between an observed value and the fitted value provided by a model) made in the results of each individual equation.

The most important application is in data fitting. When the problem has substantial uncertainties in the independent variable (the $X$ variable), then simple regression and least-squares methods have problems; in such cases, the methodology required for fitting errors-in-variables models may be considered instead of that for least squares.

Least-squares problems fall into two categories: linear or ordinary least squares and nonlinear least squares, depending on whether or not the residuals are linear in all unknowns. The linear least-squares problem occurs in statistical regression analysis; it has a closed-form solution. The nonlinear problem is usually solved by iterative refinement; at each iteration the system is approximated by a linear one, and thus the core calculation is similar in both cases.

Polynomial least squares describes the variance in a prediction of the dependent variable as a function of the independent variable and the deviations from the fitted curve.

When the observations come from an exponential family with identity as its natural sufficient statistics and mild-conditions are satisfied (e.g. for normal, exponential, Poisson and binomial distributions), standardized least-squares estimates and maximum-likelihood estimates are identical. The method of least squares can also be derived as a method of moments estimator.

The following discussion is mostly presented in terms of linear functions but the use of least squares is valid and practical for more general families of functions. Also, by iteratively applying local quadratic approximation to the likelihood (through the Fisher information), the least-squares method may be used to fit a generalized linear model.

The least-squares method was officially discovered and published by Adrien-Marie Legendre (1805), though it is usually also co-credited to Carl Friedrich Gauss (1795) who contributed significant theoretical advances to the method and may have previously used it in his work.

### The method

The first clear and concise exposition of the method of least squares was published by Legendre in 1805. The technique is described as an algebraic procedure for fitting linear equations to data and Legendre demonstrates the new method by analyzing the same data as Laplace for the shape of the earth. Within ten years after Legendre's publication, the method of least squares had been adopted as a standard tool in astronomy and geodesy in France, Italy, and Prussia, which constitutes an extraordinarily rapid acceptance of a scientific technique.

In 1809 Carl Friedrich Gauss published his method of calculating the orbits of celestial bodies. In that work he claimed to have been in possession of the method of least squares since 1795. This naturally led to a priority dispute with Legendre. However, to Gauss's credit, he went beyond Legendre and succeeded in connecting the method of least squares with the principles of probability and to the normal distribution. He had managed to complete Laplace's program of specifying a mathematical form of the probability density for the observations, depending on a finite number of unknown parameters, and define a method of estimation that minimizes the error of estimation. Gauss showed that the arithmetic mean is indeed the best estimate of the location parameter by changing both the probability density and the method of estimation. He then turned the problem around by asking what form the density should have and what method of estimation should be used to get the arithmetic mean as estimate of the location parameter. In this attempt, he invented the normal distribution.

An early demonstration of the strength of Gauss's method came when it was used to predict the future location of the newly discovered asteroid Ceres. On 1 January 1801, the Italian astronomer Giuseppe Piazzi discovered Ceres and was able to track its path for 40 days before it was lost in the glare of the sun. Based on these data, astronomers desired to determine the location of Ceres after it emerged from behind the sun without solving Kepler's complicated nonlinear equations of planetary motion. The only predictions that successfully allowed Hungarian astronomer Franz Xaver von Zach to relocate Ceres were those performed by the 24-year-old Gauss using least-squares analysis.

In 1810, after reading Gauss's work, Laplace, after proving the central limit theorem, used it to give a large sample justification for the method of least squares and the normal distribution. In 1822, Gauss was able to state that the least-squares approach to regression analysis is optimal in the sense that in a linear model where the errors have a mean of zero, are uncorrelated, and have equal variances, the best linear unbiased estimator of the coefficients is the least-squares estimator. This result is known as the Gauss–Markov theorem.

The idea of least-squares analysis was also independently formulated by the American Robert Adrain in 1808. In the next two centuries workers in the theory of errors and in statistics found many different ways of implementing least squares.

## Problem statement

The objective consists of adjusting the parameters of a model function to best fit a data set. A simple data set consists of $n$ points (data pairs) $(x_i,y_i)\!$, $i=1,\ldots,n$, where $x_i$ is an independent variable and $y_i$ is a dependent variable whose value is found by observation. The model function has the form $f(x, \boldsymbol \beta)$, where $m$ adjustable parameters are held in the vector $\boldsymbol \beta$. The goal is to find the parameter values for the model that "best" fits the data. The fit of a model to a data point is measured by its residual, defined as the difference between the observed value of the dependent variable and the value predicted by the model:

[$]r_i = y_i - f(x_i, \boldsymbol \beta).[$] The residuals are plotted against corresponding $x$ values. The random fluctuations about $r_i=0$ indicate a linear model is appropriate.

The least-squares method finds the optimal parameter values by minimizing the sum of squared residuals, $S$:

[$]S=\sum_{i=1}^{n}r_i^2.[$]

An example of a model in two dimensions is that of the straight line. Denoting the y-intercept as $\beta_0$ and the slope as $\beta_1$, the model function is given by $f(x,\boldsymbol \beta)=\beta_0+\beta_1 x$. See linear least squares for a fully worked out example of this model.

A data point may consist of more than one independent variable. For example, when fitting a plane to a set of height measurements, the plane is a function of two independent variables,$X$ and z, say. In the most general case there may be one or more independent variables and one or more dependent variables at each data point.

To the right is a residual plot illustrating random fluctuations about $r_i=0$, indicating that a linear model$(Y_i = \alpha + \beta x_i + U_i)$ is appropriate. $U_i$ is an independent, random variable. The residuals are plotted against the corresponding $x$ values. The parabolic shape of the fluctuations about $r_i=0$ indicate a parabolic model is appropriate.

If the residual points had some sort of a shape and were not randomly fluctuating, a linear model would not be appropriate. For example, if the residual plot had a parabolic shape as seen to the right, a parabolic model$(Y_i = \alpha + \beta x_i + \gamma x_i^2 + U_i)$ would be appropriate for the data. The residuals for a parabolic model can be calculated via $r_i=y_i-\hat{\alpha}-\hat{\beta} x_i-\widehat{\gamma} x_i^2$.

## Limitations

This regression formulation considers only observational errors in the dependent variable (but the alternative total least squares regression can account for errors in both variables). There are two rather different contexts with different implications:

• Regression for prediction. Here a model is fitted to provide a prediction rule for application in a similar situation to which the data used for fitting apply. Here the dependent variables corresponding to such future application would be subject to the same types of observation error as those in the data used for fitting. It is therefore logically consistent to use the least-squares prediction rule for such data.
• Regression for fitting a "true relationship". In standard regression analysis that leads to fitting by least squares there is an implicit assumption that errors in the independent variable are zero or strictly controlled so as to be negligible. When errors in the independent variable are non-negligible, models of measurement error can be used; such methods can lead to parameter estimates, hypothesis testing and confidence intervals that take into account the presence of observation errors in the independent variables. An alternative approach is to fit a model by total least squares; this can be viewed as taking a pragmatic approach to balancing the effects of the different sources of error in formulating an objective function for use in model-fitting.

## Linear Least Squares

Linear least squares (LLS) is the least squares approximation of linear functions to data. It is a set of formulations for solving statistical problems involved in linear regression, including variants for ordinary (unweighted), weighted, and generalized (correlated) residuals. Numerical methods for linear least squares include inverting the matrix of the normal equations and orthogonal decomposition methods.

### Main formulations

The three main linear least squares formulations are:

• Ordinary least squares (OLS) is the most common estimator. OLS estimates are commonly used to analyze both experimental and observational data. The OLS method minimizes the sum of squared residuals, and leads to a closed-form expression for the estimated value of the unknown parameter vector $\beta$:

[$] \hat{\boldsymbol\beta} = (\mathbf{X}^\mathsf{T}\mathbf{X})^{-1} \mathbf{X}^\mathsf{T} \mathbf{y}, [$]

where $\mathbf{y}$ is a vector whose $i$th element is the $i$th observation of the dependent variable, and $\mathbf{X}$ is a matrix whose $ij$ element is the $i$th observation of the $j$th independent variable. The estimator is unbiased and consistent if the errors have finite variance and are uncorrelated with the regressors:

[$] \operatorname{E}[\,\mathbf{x}_i\varepsilon_i\,] = 0, [$]

where $\mathbf{x}_i$ is the transpose of row $i$ of the matrix $\mathbf{X}.$ It is also efficient under the assumption that the errors have finite variance and are homoscedastic, meaning that $\operatorname{E}[\varepsilon_i^2|x_i]$ does not depend on $i$. The condition that the errors are uncorrelated with the regressors will generally be satisfied in an experiment, but in the case of observational data, it is difficult to exclude the possibility of an omitted covariate $z$ that is related to both the observed covariates and the response variable. The existence of such a covariate will generally lead to a correlation between the regressors and the response variable, and hence to an inconsistent estimator of $\beta$. The condition of homoscedasticity can fail with either experimental or observational data. If the goal is either inference or predictive modeling, the performance of OLS estimates can be poor if multicollinearity is present, unless the sample size is large.

• Weighted least squares (WLS) are used when heteroscedasticity is present in the error terms of the model.
• Generalized least squares (GLS) is an extension of the OLS method, that allows efficient estimation of $\beta$ when either heteroscedasticity, or correlations, or both are present among the error terms of the model, as long as the form of heteroscedasticity and correlation is known independently of the data. To handle heteroscedasticity when the error terms are uncorrelated with each other, GLS minimizes a weighted analogue to the sum of squared residuals from OLS regression, where the weight for the $i$th case is inversely proportional to $\operatorname{Var}(\varepsilon_i)$. This special case of GLS is called "weighted least squares". The GLS solution to an estimation problem is
[$] \hat{\boldsymbol\beta} = (\mathbf{X}^\mathsf{T} \boldsymbol\Omega^{-1} \mathbf{X})^{-1}\mathbf{X}^\mathsf{T}\boldsymbol\Omega^{-1}\mathbf{y}, [$]
where $\Omega$ is the covariance matrix of the errors. GLS can be viewed as applying a linear transformation to the data so that the assumptions of OLS are met for the transformed data. For GLS to be applied, the covariance structure of the errors must be known up to a multiplicative constant.

### Properties

If the experimental errors, $\varepsilon$, are uncorrelated, have a mean of zero and a constant variance, $\sigma$, the Gauss–Markov theorem states that the least-squares estimator, $\hat{\boldsymbol{\beta}}$, has the minimum variance of all estimators that are linear combinations of the observations. In this sense it is the best, or optimal, estimator of the parameters. Note particularly that this property is independent of the statistical distribution function of the errors. In other words, the distribution function of the errors need not be a normal distribution. However, for some probability distributions, there is no guarantee that the least-squares solution is even possible given the observations; still, in such cases it is the best estimator that is both linear and unbiased.

For example, it is easy to show that the arithmetic mean of a set of measurements of a quantity is the least-squares estimator of the value of that quantity. If the conditions of the Gauss–Markov theorem apply, the arithmetic mean is optimal, whatever the distribution of errors of the measurements might be.

However, in the case that the experimental errors do belong to a normal distribution, the least-squares estimator is also a maximum likelihood estimator.

These properties underpin the use of the method of least squares for all types of data fitting, even when the assumptions are not strictly valid.

### Limitations

An assumption underlying the treatment given above is that the independent variable,$X$, is free of error. In practice, the errors on the measurements of the independent variable are usually much smaller than the errors on the dependent variable and can therefore be ignored. When this is not the case, total least squares or more generally errors-in-variables models, or rigorous least squares, should be used. This can be done by adjusting the weighting scheme to take into account errors on both the dependent and independent variables and then following the standard procedure.

In some cases the (weighted) normal equations matrix$X^{\mathsf{T}}X$ is ill-conditioned. When fitting polynomials the normal equations matrix is a Vandermonde matrix. Vandermonde matrices become increasingly ill-conditioned as the order of the matrix increases. In these cases, the least squares estimate amplifies the measurement noise and may be grossly inaccurate. Various regularization techniques can be applied in such cases, the most common of which is called ridge regression. If further information about the parameters is known, for example, a range of possible values of $\mathbf{\hat{\boldsymbol{\beta}}}$, then various techniques can be used to increase the stability of the solution. For example, see constrained least squares.

Another drawback of the least squares estimator is the fact that the norm of the residuals, $\| \mathbf y - X\hat{\boldsymbol{\beta}} \|$ is minimized, whereas in some cases one is truly interested in obtaining small error in the parameter $\mathbf{\hat{\boldsymbol{\beta}}}$, e.g., a small value of $\|{\boldsymbol{\beta}}-\hat{\boldsymbol{\beta}}\|$. However, since the true parameter ${\boldsymbol{\beta}}$ is necessarily unknown, this quantity cannot be directly minimized. If a prior probability on $\hat{\boldsymbol{\beta}}$ is known, then a Bayes estimator can be used to minimize the mean squared error, $E \left\{ \| {\boldsymbol{\beta}} - \hat{\boldsymbol{\beta}} \|^2 \right\}$. The least squares method is often applied when no prior is known. Surprisingly, when several parameters are being estimated jointly, better estimators can be constructed, an effect known as Stein's phenomenon. For example, if the measurement error is Gaussian, several estimators are known which dominate, or outperform, the least squares technique; the best known of these is the James–Stein estimator. This is an example of more general shrinkage estimators that have been applied to regression problems.

## Ordinary Least Squares (OLS)

In statistics, ordinary least squares (OLS) is a type of linear least squares method for estimating the unknown parameters in a linear regression model. OLS chooses the parameters of a linear function of a set of explanatory variables by the principle of least squares: minimizing the sum of the squares of the differences between the observed dependent variable (values of the variable being observed) in the given dataset and those predicted by the linear function of the independent variable.

Geometrically, this is seen as the sum of the squared distances, parallel to the axis of the dependent variable, between each data point in the set and the corresponding point on the regression surface—the smaller the differences, the better the model fits the data. The resulting estimator can be expressed by a simple formula, especially in the case of a simple linear regression, in which there is a single regressor on the right side of the regression equation.

The OLS estimator is consistent when the regressors are exogenous, and—by the Gauss–Markov theoremoptimal in the class of linear unbiased estimators when the errors are homoscedastic and serially uncorrelated. Under these conditions, the method of OLS provides minimum-variance mean-unbiased estimation when the errors have finite variances. Under the additional assumption that the errors are normally distributed, OLS is the maximum likelihood estimator.

### Linear model Okun's law in macroeconomics states that in an economy the GDP growth should depend linearly on the changes in the unemployment rate. Here the ordinary least squares method is used to construct the regression line describing this law.

Suppose the data consists of $n$ observations $\left\{\mathbf{x}_i, y_i\right\}_{i=1}^n$. Each observation $i$ includes a scalar response $y_i$ and a column vector $\mathbf{x}_i$ of $p$ parameters (regressors), i.e., $\mathbf{x}_i=\left[x_{i1}, x_{i2}, \dots, x_{ip}\right]^\mathsf{T}$. In a linear regression model, the response variable, $y_i$, is a linear function of the regressors:

[$]y_i = \beta_1\ x_{i1} + \beta_2\ x_{i2} + \cdots + \beta_p\ x_{ip} + \varepsilon_i,[$]

or in vector form,

[$] y_i = \mathbf{x}_i^\mathsf{T} \boldsymbol{\beta} + \varepsilon_i, \, [$]

where $\mathbf{x}_i$, as introduced previously, is a column vector of the $i$-th observation of all the explanatory variables; $\boldsymbol{\beta}$ is a $p \times 1$ vector of unknown parameters; and the scalar $\varepsilon_i$ represents unobserved random variables (errors) of the $i$-th observation. $\varepsilon_i$ accounts for the influences upon the responses $y_i$ from sources other than the explanators $\mathbf{x}_i$. This model can also be written in matrix notation as

[$] \mathbf{y} = \mathrm{X} \boldsymbol{\beta} + \boldsymbol{\varepsilon}, \, [$]

where $\mathbf{y}$ and $\boldsymbol{\varepsilon}$ are $n \times 1$ vectors of the response variables and the errors of the $n$ observations, and $\mathrm{X}$ is an $n \times p$ matrix of regressors, also sometimes called the design matrix, whose row $i$ is $\mathbf{x}_i^\mathsf{T}$ and contains the $i$-th observations on all the explanatory variables.

As a rule, the constant term is always included in the set of regressors $\mathrm{X}$, say, by taking $x_{i1}=1$ for all $i=1, \dots, n$. The coefficient $\beta_1$ corresponding to this regressor is called the intercept.

Regressors do not have to be independent: there can be any desired relationship between the regressors (so long as it is not a linear relationship). For instance, we might suspect the response depends linearly both on a value and its square; in which case we would include one regressor whose value is just the square of another regressor. In that case, the model would be quadratic in the second regressor, but none-the-less is still considered a linear model because the model is still linear in the parameters ($\boldsymbol{\beta}$).

#### Matrix/vector formulation

Consider an overdetermined system

[$]\sum_{j=1}^{p} X_{ij} \beta_j = y_i,\ (i=1, 2, \dots, n),[$]

of $n$ linear equations in $p$ unknown coefficients, $\beta_1, \beta_2, \dots, \beta_p$, with $n \gt p$. (Note: for a linear model as above, not all elements in $\mathrm{X}$ contains information on the data points. The first column is populated with ones, $X_{i1} = 1$. Only the other columns contain actual data. So here $p$ is equal to the number of regressors plus one.) This can be written in matrix form as

[$]\mathrm{X} \boldsymbol{\beta} = \mathbf {y},[$]

where

[$]\mathrm{X}=\begin{bmatrix} X_{11} & X_{12} & \cdots & X_{1p} \\ X_{21} & X_{22} & \cdots & X_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ X_{n1} & X_{n2} & \cdots & X_{np} \end{bmatrix} , \qquad \boldsymbol \beta = \begin{bmatrix} \beta_1 \\ \beta_2 \\ \vdots \\ \beta_p \end{bmatrix} , \qquad \mathbf y = \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix}. [$]

Such a system usually has no exact solution, so the goal is instead to find the coefficients $\boldsymbol{\beta}$ which fit the equations "best", in the sense of solving the quadratic minimization problem

[$]\hat{\boldsymbol{\beta}} = \underset{\boldsymbol{\beta}}{\operatorname{arg\,min}}\,S(\boldsymbol{\beta}), [$]

where the objective function $S$ is given by

[$]S(\boldsymbol{\beta}) = \sum_{i=1}^n \biggl| y_i - \sum_{j=1}^p X_{ij}\beta_j\biggr|^2 = \bigl\|\mathbf y - \mathrm{X} \boldsymbol \beta \bigr\|^2.[$]

A justification for choosing this criterion is given in Properties below. This minimization problem has a unique solution, provided that the $p$ columns of the matrix $\mathrm{X}$ are linearly independent, given by solving the so-called normal equations:

[$](\mathrm{X}^{\mathsf T} \mathrm{X} )\hat{\boldsymbol{\beta}} = \mathrm{X}^{\mathsf T} \mathbf y\ .[$]

The matrix $\mathrm{X}^{\mathsf T} \mathrm X$ is known as the normal matrix or Gram matrix and the matrix $\mathrm{X}^{\mathsf T} \mathbf y$ is known as the moment matrix of regressand by regressors. Finally, $\hat{\boldsymbol{\beta}}$ is the coefficient vector of the least-squares hyperplane, expressed as

[$]\hat{\boldsymbol{\beta}}= \left( \mathrm{X}^{\mathsf T} \mathrm{X} \right)^{-1} \mathrm{X}^{\mathsf T} \mathbf y.[$]

or

[$]\hat{\boldsymbol{\beta}}= \boldsymbol{\beta} + (\mathbf{X}^\top \mathbf{X} )^{-1}\mathbf {X}^\top \boldsymbol{\varepsilon}.[$]

## Estimation

Suppose $b$ is a "candidate" value for the parameter vector $\beta$. The quantity $y_i-x_i^{\mathsf{T}}b$, called the residual for the $i$-th observation, measures the vertical distance between the data point $(x_i,y_i)$ and the hyperplane $y=X^{\mathsf{T}}b$, and thus assesses the degree of fit between the actual data and the model. The sum of squared residuals (SSR) (also called the error sum of squares (ESS) or residual sum of squares (RSS)) is a measure of the overall model fit:

[$] S(b) = \sum_{i=1}^n (y_i - x_i ^\mathrm{T} b)^2 = (y-Xb)^\mathrm{T}(y-Xb), [$]

where $\mathsf{T}$ denotes the matrix transpose, and the rows of$X$, denoting the values of all the independent variables associated with a particular value of the dependent variable, are Xi = xiT. The value of b which minimizes this sum is called the OLS estimator for $\beta$. The function $S(b)$ is quadratic in $b$ with positive-definite Hessian, and therefore this function possesses a unique global minimum at $b =\hat\beta$, which can be given by the explicit formula:

[$] \hat\beta = \operatorname{argmin}_{b\in\mathbb{R}^p} S(b) = (X^\mathrm{T}X)^{-1}X^\mathrm{T}y\ . [$]

The product $N=X^{\mathsf{T}}X$ is a Gram matrix and its inverse, $Q=N^{-1}$, is the cofactor matrix of $\beta$, closely related to its covariance matrix, $C_{\beta}$. The matrix

[$](X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}=QX^{\mathsf{T}}[$]

is called the Moore–Penrose pseudoinverse matrix of $X$. This formulation highlights the point that estimation can be carried out if, and only if, there is no perfect multicollinearity between the explanatory variables (which would cause the gram matrix to have no inverse).

After we have estimated $\beta$, the fitted values (or predicted values) from the regression will be

[$] \hat{y} = X\hat\beta = Py, [$]

where

[$]P=X(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}[$]

is the projection matrix onto the space $V$ spanned by the columns of$X$. This matrix $P$ is also sometimes called the hat matrix because it "puts a hat" onto the variable $y$. Another matrix, closely related to $P$ is the annihilator matrix $M=I_n-P$; this is a projection matrix onto the space orthogonal to $V$. Both matrices $P$ and $M$ are symmetric and idempotent (meaning that $P^2 = P$ and $M^2=M$), and relate to the data matrix $X$ via identities $PX=X$ and $MX=0$. Matrix $M$ creates the residuals from the regression:

[$] \hat\varepsilon = y - \hat y = y - X\hat\beta = My = M(X\beta+\varepsilon) = (MX)\beta + M\varepsilon = M\varepsilon. [$]

Using these residuals we can estimate the value of $\sigma^2$ using the reduced chi-squared statistic:

[$] s^2 = \frac{\hat\varepsilon ^\mathrm{T} \hat\varepsilon}{n-p} = \frac{(My)^\mathrm{T} My}{n-p} = \frac{y^\mathrm{T} M^\mathrm{T}My}{n-p}= \frac{y ^\mathrm{T} My}{n-p} = \frac{S(\hat\beta)}{n-p},\qquad \hat\sigma^2 = \frac{n-p}{n}\;s^2 [$]

The denominator, $n-p$, is the statistical degrees of freedom. The first quantity, $s^2$, is the OLS estimate for $\sigma^2$, whereas the second, $\scriptstyle\hat\sigma^2$, is the MLE estimate for $\sigma^2$. The two estimators are quite similar in large samples; the first estimator is always unbiased, while the second estimator is biased but has a smaller mean squared error. In practice $s^2$ is used more often, since it is more convenient for the hypothesis testing. The square root of $s^2$ is called the regression standard error, standard error of the regression, or standard error of the equation.

It is common to assess the goodness-of-fit of the OLS regression by comparing how much the initial variation in the sample can be reduced by regressing onto$X$. The coefficient of determination $R^2$ is defined as a ratio of "explained" variance to the "total" variance of the dependent variable $y$, in the cases where the regression sum of squares equals the sum of squares of residuals:

[$] R^2 = \frac{\sum(\hat y_i-\overline{y})^2}{\sum(y_i-\overline{y})^2} = \frac{y ^\mathrm{T} P ^\mathrm{T} LPy}{y ^\mathrm{T} Ly} = 1 - \frac{y ^\mathrm{T} My}{y ^\mathrm{T} Ly} = 1 - \frac{\rm RSS}{\rm TSS} [$]

where TSS is the total sum of squares for the dependent variable, $L=I_n-\frac{1}{n}J_n$, and $J_n$ is an $n\times n$ matrix of ones. ($L$ is a centering matrix which is equivalent to regression on a constant; it simply subtracts the mean from a variable.) In order for $R^2$ to be meaningful, the matrix$X$ of data on regressors must contain a column vector of ones to represent the constant whose coefficient is the regression intercept. In that case, $R^2$ will always be a number between 0 and 1, with values close to 1 indicating a good degree of fit.

The variance in the prediction of the independent variable as a function of the dependent variable is given in the article Polynomial least squares.

### Simple linear regression model

If the data matrix$X$ contains only two variables, a constant and a scalar regressor $x_i$, then this is called the "simple regression model". This case is often considered in the beginner statistics classes, as it provides much simpler formulas even suitable for manual calculation. The parameters are commonly denoted as $(\alpha,\beta)$:

[$] y_i = \alpha + \beta x_i + \varepsilon_i. [$]

The least squares estimates in this case are given by simple formulas

[] \begin{align} \hat\beta &= \frac{{n} \sum{x_iy_i} - \sum{x_i}\sum{y_i} } {{n} \sum{x_i^2} - (\sum{x_i})^2 } \\ \hat\alpha &= \overline{y} - \hat\beta\,\overline{x}\ , \end{align} []

### Alternative derivations

In the previous section the least squares estimator $\hat\beta$ was obtained as a value that minimizes the sum of squared residuals of the model. However it is also possible to derive the same estimator from other approaches. In all cases the formula for OLS estimator remains the same: $\hat{\beta}=(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}y$; the only difference is in how we interpret this result.

#### Projection OLS estimation can be viewed as a projection onto the linear space spanned by the regressors. (Here each of $X_1$ and $X_2$ refers to a column of the data matrix.)

For mathematicians, OLS is an approximate solution to an overdetermined system of linear equations $X\beta = y$, where $\beta$ is the unknown. Assuming the system cannot be solved exactly (the number of equations $n$ is much larger than the number of unknowns $p$), we are looking for a solution that could provide the smallest discrepancy between the right- and left- hand sides. In other words, we are looking for the solution that satisfies

[$] \hat\beta = {\rm arg}\min_\beta\,\lVert y - X\beta \rVert, [$]

where $||\cdot||$ is the standard L2 norm in the $n$-dimensional Euclidean space $\mathbb{R}^n$. The predicted quantity $X\beta$ is just a certain linear combination of the vectors of regressors. Thus, the residual vector $y-X\beta$ will have the smallest length when$y$is projected orthogonally onto the linear subspace spanned by the columns of$X$. The OLS estimator $\hat\beta$ in this case can be interpreted as the coefficients of vector decomposition of $\hat{y}=Py$ along the basis of$X$.

In other words, the gradient equations at the minimum can be written as:

[$](\mathbf y - X \hat{\boldsymbol{\beta}})^{\rm T} X=0.[$]

A geometrical interpretation of these equations is that the vector of residuals, $\mathbf y - X \hat{\boldsymbol{\beta}}$ is orthogonal to the column space of$X$, since the dot product $(\mathbf y-X\hat{\boldsymbol{\beta}})\cdot X \mathbf v$ is equal to zero for any conformal vector, $\mathbf{v}$. This means that $\mathbf y - X \boldsymbol{\hat \beta}$ is the shortest of all possible vectors $\mathbf{y}- X \boldsymbol \beta$, that is, the variance of the residuals is the minimum possible. This is illustrated at the right.

Introducing $\hat{\boldsymbol{\gamma}}$ and a matrix $K$ with the assumption that a matrix $[X \ K]$ is non-singular and $K^{\mathsf{T}}X=0$ (cf. Orthogonal projections), the residual vector should satisfy the following equation:

[$]\hat{\mathbf{r}} \triangleq \mathbf{y} - X \hat{\boldsymbol{\beta}} = K \hat{{\boldsymbol{\gamma}}}.[$]

The equation and solution of linear least squares are thus described as follows:

[$] \mathbf{y} = \begin{bmatrix}X & K\end{bmatrix} \begin{pmatrix} \hat{\boldsymbol{\beta}} \\ \hat{\boldsymbol{\gamma}} \end{pmatrix} ,[$]
[$] \begin{pmatrix} \hat{\boldsymbol{\beta}} \\ \hat{\boldsymbol{\gamma}} \end{pmatrix} = \begin{bmatrix}X & K\end{bmatrix}^{-1} \mathbf{y} = \begin{bmatrix} (X^{\rm T} X)^{-1} X^{\rm T} \\ (K^{\rm T} K)^{-1} K^{\rm T} \end{bmatrix} \mathbf{y} .[$]

Another way of looking at it is to consider the regression line to be a weighted average of the lines passing through the combination of any two points in the dataset. Although this way of calculation is more computationally expensive, it provides a better intuition on OLS.

#### Maximum likelihood

The OLS estimator is identical to the maximum likelihood estimator (MLE) under the normality assumption for the error terms. From the properties of MLE, we can infer that the OLS estimator is asymptotically efficient (in the sense of attaining the Cramér–Rao bound for variance) if the normality assumption is satisfied.

### Properties

#### Assumptions

There are several different frameworks in which the linear regression model can be cast in order to make the OLS technique applicable. Each of these settings produces the same formulas and same results. The only difference is the interpretation and the assumptions which have to be imposed in order for the method to give meaningful results. The choice of the applicable framework depends mostly on the nature of data in hand, and on the inference task which has to be performed.

One of the lines of difference in interpretation is whether to treat the regressors as random variables, or as predefined constants. In the first case (random design) the regressors $x_i$ are random and sampled together with the $y_i$'s from some population, as in an observational study. This approach allows for more natural study of the asymptotic properties of the estimators. In the other interpretation (fixed design), the regressors$X$ are treated as known constants set by a design, and$y$is sampled conditionally on the values of$X$ as in an experiment. For practical purposes, this distinction is often unimportant, since estimation and inference is carried out while conditioning on$X$. All results stated in this article are within the random design framework.

##### Classical linear regression model

The classical model focuses on the "finite sample" estimation and inference, meaning that the number of observations n is fixed. This contrasts with the other approaches, which study the asymptotic behavior of OLS, and in which the number of observations is allowed to grow to infinity.

Condition Description
Correct specification The linear functional form must coincide with the form of the actual data-generating process.
Strict exogeneity The errors in the regression should have conditional mean zero:$\operatorname{E}[\,\varepsilon\mid X\,] = 0.$The immediate consequence of the exogeneity assumption is that the errors have mean zero: $\operatorname{E}[\epsilon] = 0$, and that the regressors are uncorrelated with the errors: $\operatorname{E}[X^{\mathsf{T}}\epsilon] = 0$.
Linear Independence The regressors in$X$ must all be linearly independent. Mathematically, this means that the matrix$X$ must have full column rank almost surely:
[$] \Pr\!\big[\,\operatorname{rank}(X) = p\,\big] = 1. [$]
Usually, it is also assumed that the regressors have finite moments up to at least the second moment. Then the matrix $Q_{xx}=\operatorname{E}[X^{\mathsf{T}}X]/n$ is finite and positive semi-definite. When this assumption is violated the regressors are called linearly dependent or perfectly multicollinear. In such case the value of the regression coefficient $\beta$ cannot be learned, although prediction of$y$values is still possible for new values of the regressors that lie in the same linearly dependent subspace.
Spherical Errors
[$] \operatorname{Var}[\,\varepsilon \mid X\,] = \sigma^2 I_n, [$]
where $I_n$ is the identity matrix in dimension $n$, and $\sigma^2$ is a parameter which determines the variance of each observation. This $\sigma^2$ is considered a nuisance parameter in the model, although usually it is also estimated. If this assumption is violated then the OLS estimates are still valid, but no longer efficient.
It is customary to split this assumption into two parts:
• Homoscedasticity: $\operatorname{E}[\epsilon_i^2|X] = \sigma^2$, which means that the error term has the same variance $\sigma^2$ in each observation. When this requirement is violated this is called heteroscedasticity, in such case a more efficient estimator would be weighted least squares. If the errors have infinite variance then the OLS estimates will also have infinite variance (although by the law of large numbers they will nonetheless tend toward the true values so long as the errors have zero mean). In this case, robust estimation techniques are recommended.
• No autocorrelation: the errors are uncorrelated between observations: $\operatorname{E}[\epsilon_i\epsilon_j|X]=0$ for $i\neq j$. This assumption may be violated in the context of time series data, panel data, cluster samples, hierarchical data, repeated measures data, longitudinal data, and other data with dependencies. In such cases generalized least squares provides a better alternative than the OLS. Another expression for autocorrelation is serial correlation.
Normality It is sometimes additionally assumed that the errors have normal distribution conditional on the regressors:

[$] \varepsilon \mid X\sim \mathcal{N}(0, \sigma^2I_n). [$]
This assumption is not needed for the validity of the OLS method, although certain additional finite-sample properties can be established in case when it does (especially in the area of hypotheses testing). Also when the errors are normal, the OLS estimator is equivalent to the maximum likelihood estimator (MLE), and therefore it is asymptotically efficient in the class of all regular estimators. Importantly, the normality assumption applies only to the error terms; contrary to a popular misconception, the response (dependent) variable is not required to be normally distributed.

##### Independent and identically distributed (iid)

In some applications, especially with cross-sectional data, an additional assumption is imposed — that all observations are independent and identically distributed. This means that all observations are taken from a random sample which makes all the assumptions listed earlier simpler and easier to interpret. Also this framework allows one to state asymptotic results (as the sample size $n\rightarrow \infty$), which are understood as a theoretical possibility of fetching new independent observations from the data generating process. The list of assumptions in this case is:

Assumption Description
iid obesrvations $(x_i,y_i)$ is independent from, and has the same distribution as, $(x_j,y_j)$ for all $i\neq j$.
no perfect multicollinearity $Q_{xx}=\operatorname{E}[x_ix_i^{\mathsf{T}}]$ is a positive-definite matrix
Exogeneity $\operatorname{E}[\varepsilon_ix_i]=0$
homoscedasticity $\operatorname{Var}[\varepsilon_ix_i] = \sigma^2$

#### Finite sample properties

First of all, under the strict exogeneity assumption the OLS estimators $\scriptstyle\hat\beta$ and $s^2$ are unbiased, meaning that their expected values coincide with the true values of the parameters:

[$] \operatorname{E}[\, \hat\beta \mid X \,] = \beta, \quad \operatorname{E}[\,s^2 \mid X\,] = \sigma^2. [$]

If the strict exogeneity does not hold (as is the case with many time series models, where exogeneity is assumed only with respect to the past shocks but not the future ones), then these estimators will be biased in finite samples.

The variance-covariance matrix (or simply covariance matrix) of $\scriptstyle\hat\beta$ is equal to

[$] \operatorname{Var}[\, \hat\beta \mid X \,] = \sigma^2(X ^T X)^{-1} = \sigma^2 Q. [$]

In particular, the standard error of each coefficient $\scriptstyle\hat\beta_j$ is equal to square root of the $j$-th diagonal element of this matrix. The estimate of this standard error is obtained by replacing the unknown quantity σ2 with its estimate $s^2$. Thus,

[$] \widehat{\operatorname{s.\!e.}}(\hat{\beta}_j) = \sqrt{s^2 (X ^T X)^{-1}_{jj}} [$]

It can also be easily shown that the estimator $\scriptstyle\hat\beta$ is uncorrelated with the residuals from the model:

[$] \operatorname{Cov}[\, \hat\beta,\hat\varepsilon \mid X\,] = 0. [$]

The Gauss–Markov theorem states that under the spherical errors assumption (that is, the errors should be uncorrelated and homoscedastic) the estimator $\scriptstyle\hat\beta$ is efficient in the class of linear unbiased estimators. This is called the best linear unbiased estimator (BLUE). Efficiency should be understood as if we were to find some other estimator $\scriptstyle\tilde\beta$ which would be linear in$y$and unbiased, then 

[$] \operatorname{Var}[\, \tilde\beta \mid X \,] - \operatorname{Var}[\, \hat\beta \mid X \,] \geq 0 [$]

in the sense that this is a nonnegative-definite matrix. This theorem establishes optimality only in the class of linear unbiased estimators, which is quite restrictive. Depending on the distribution of the error terms ε, other, non-linear estimators may provide better results than OLS.

##### Assuming normality

The properties listed so far are all valid regardless of the underlying distribution of the error terms. However, if you are willing to assume that the normality assumption holds (that is, that $\epsilon \sim N(0,\sigma^2I_n)$, then additional properties of the OLS estimators can be stated.

The estimator $\scriptstyle\hat\beta$ is normally distributed, with mean and variance as given before:

[$] \hat\beta\ \sim\ \mathcal{N}\big(\beta,\ \sigma^2(X ^\mathrm{T} X)^{-1}\big) [$]

where $Q$ is the cofactor matrix. This estimator reaches the Cramér–Rao bound for the model, and thus is optimal in the class of all unbiased estimators. Note that unlike the Gauss–Markov theorem, this result establishes optimality among both linear and non-linear estimators, but only in the case of normally distributed error terms.

The estimator $s^2$ will be proportional to the chi-squared distribution:

[$] s^2\ \sim\ \frac{\sigma^2}{n-p} \cdot \chi^2_{n-p} [$]

The variance of this estimator is equal to $2\sigma^4/(n-p)$, which does not attain the Cramér–Rao bound of $2\sigma^4/n$. However it was shown that there are no unbiased estimators of $\sigma^2$ with variance smaller than that of the estimator $s^2$. If we are willing to allow biased estimators, and consider the class of estimators that are proportional to the sum of squared residuals (SSR) of the model, then the best (in the sense of the mean squared error) estimator in this class will be $\sim \sigma^2/(n-p+2)$, which even beats the Cramér–Rao bound in case when there is only one regressor ($p=1$).

Moreover, the estimators $\hat{\beta}$ and $s^2$ are independent, the fact which comes in useful when constructing the t- and F-tests for the regression.

##### Influential observations

As was mentioned before, the estimator $\hat\beta$ is linear in $y$, meaning that it represents a linear combination of the dependent variables $y_i$. The weights in this linear combination are functions of the regressors $X$, and generally are unequal. The observations with high weights are called influential because they have a more pronounced effect on the value of the estimator.

To analyze which observations are influential we remove a specific $j$-th observation and consider how much the estimated quantities are going to change (similarly to the jackknife method). It can be shown that the change in the OLS estimator for $\beta$ will be equal to 

[$] \hat\beta^{(j)} - \hat\beta = - \frac{1}{1-h_j} (X ^\mathrm{T} X)^{-1}x_j ^\mathrm{T} \hat\varepsilon_j\,, [$]

where $h_j = x_j^{\mathsf{T}}(X^{\mathsf{T}}X)^{-1}x_j$ is the $j$-th diagonal element of the hat matrix $P$, and $x_j$ is the vector of regressors corresponding to the $j$-th observation. Similarly, the change in the predicted value for $j$-th observation resulting from omitting that observation from the dataset will be equal to 

[$] \hat{y}_j^{(j)} - \hat{y}_j = x_j ^\mathrm{T} \hat\beta^{(j)} - x_j ^T \hat\beta = - \frac{h_j}{1-h_j}\,\hat\varepsilon_j [$]

From the properties of the hat matrix, $0\leq h_j \leq 1$, and they sum up to $p$, so that on average $h_j \approx p/n$. These quantities $h_j$ are called the leverages, and observations with high $h_j$ are called leverage points. Usually the observations with high leverage ought to be scrutinized more carefully, in case they are erroneous, or outliers, or in some other way atypical of the rest of the dataset.

##### Partitioned regression

Sometimes the variables and corresponding parameters in the regression can be logically split into two groups, so that the regression takes form

[$] y = X_1\beta_1 + X_2\beta_2 + \varepsilon, [$]

where$X$1 and$X$2 have dimensions $n\times p_1$, $n\times p_2$, and $\beta_1$, $\beta_2$ are $p_1 \times 1$ and $p_2 \times 1$ vectors, with $p = p_1 + p_2 = p$.

The Frisch–Waugh–Lovell theorem states that in this regression the residuals $\hat\varepsilon$ and the OLS estimate $\scriptstyle\hat\beta_2$ will be numerically identical to the residuals and the OLS estimate for $\beta$2 in the following regression:

[$] M_1y = M_1X_2\beta_2 + \eta\,, [$]

where $M_1$ is the annihilator matrix for regressors$X_1$.

The theorem can be used to establish a number of theoretical results. For example, having a regression with a constant and another regressor is equivalent to subtracting the means from the dependent variable and the regressor and then running the regression for the de-meaned variables but without the constant term.

#### Large sample properties

The least squares estimators are point estimates of the linear regression model parameters $\beta$. However, generally we also want to know how close those estimates might be to the true values of parameters. In other words, we want to construct the interval estimates.

Since we haven't made any assumption about the distribution of error term $\epsilon_i$, it is impossible to infer the distribution of the estimators $\hat\beta$ and $\hat\sigma^2$. Nevertheless, we can apply the central limit theorem to derive their asymptotic properties as sample size $n$ goes to infinity. While the sample size is necessarily finite, it is customary to assume that $n$ is "large enough" so that the true distribution of the OLS estimator is close to its asymptotic limit.

We can show that under the model assumptions, the least squares estimator for $\beta$ is consistent (that is $\hat\beta$ converges in probability to $\beta$) and asymptotically normal:

[$](\hat\beta - \beta)\ \xrightarrow{d}\ \mathcal{N}\big(0,\;\sigma^2Q_{xx}^{-1}\big),[$]

where $Q_{xx} = X ^T X.$

##### Intervals

Using this asymptotic distribution, approximate two-sided confidence intervals for the $j$-th component of the vector $\hat{\beta}$ can be constructed as

[$]\beta_j \in \bigg[\ \hat\beta_j \pm q^{\mathcal{N}(0, 1)}_{1 - \frac{\alpha}{2}}\!\sqrt{\hat{\sigma}^2 \left[Q_{xx}^{-1}\right]_{jj}}\ \bigg] [$]

at the $1-\alpha$ confidence level, where $Q$ denotes the quantile function of standard normal distribution, and [·]jj is the $j$-th diagonal element of a matrix.

Similarly, the least squares estimator for $\sigma^2$ is also consistent and asymptotically normal (provided that the fourth moment of $\epsilon_i$ exists) with limiting distribution

[$](\hat{\sigma}^2 - \sigma^2)\ \xrightarrow{d}\ \mathcal{N} \left(0,\;\operatorname{E}\left[\varepsilon_i^4\right] - \sigma^4\right). [$]

These asymptotic distributions can be used for prediction, testing hypotheses, constructing other estimators, etc.. As an example consider the problem of prediction. Suppose $x_0$ is some point within the domain of distribution of the regressors, and one wants to know what the response variable would have been at that point. The mean response is the quantity $y_0 = x_0^\mathrm{T} \beta$, whereas the predicted response is $\hat{y}_0 = x_0^\mathrm{T} \hat\beta$. Clearly the predicted response is a random variable, its distribution can be derived from that of $\hat{\beta}$:

[$]\left(\hat{y}_0 - y_0\right)\ \xrightarrow{d}\ \mathcal{N}\left(0,\;\sigma^2 x_0^\mathrm{T} Q_{xx}^{-1} x_0\right),[$]

which allows construct confidence intervals for mean response $y_0$ to be constructed:

[$]y_0 \in \left[\ x_0^\mathrm{T} \hat{\beta} \pm q^{\mathcal{N}(0, 1)}_{1 - \frac{\alpha}{2}}\!\sqrt{\hat\sigma^2 x_0^\mathrm{T} Q_{xx}^{-1} x_0}\ \right][$]

at the $1-\alpha$ confidence level.