Linear least squares explained

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.

Basic formulation

Consider the linear equation where

A\inRm x

and

b\inRm

are given and

x\inRn

is variable to be computed. When

m>n,

it is generally the case that has no solution. For example, there is no value of

x

that satisfies\begin1 & 0 \\ 0 & 1 \\1 & 1 \end x = \begin1 \\ 1 \\ 0\end,because the first two rows require that

x=(1,1),

but then the third row is not satisfied.Thus, for

m>n,

the goal of solving exactly is typically replaced by finding the value of

x

that minimizes some error. There are many ways that the error can be defined, but one of the most common is to define it as

\|Ax-b\|2.

This produces a minimization problem, called a least squares problem

The solution to the least squares problem is computed by solving the normal equation[1] where

A\top

denotes the transpose of

A

.

Continuing the example, above, with A = \begin1 & 0 \\ 0 & 1 \\1 & 1 \end \quad \text \quadb = \begin1 \\ 1 \\ 0\end,we find A^\top A = \begin1 & 0 & 1\\ 0 & 1 & 1\end\begin1 & 0 \\ 0 & 1 \\1 & 1 \end =\begin2 & 1 \\ 1 & 2\endand A^\top b = \begin1 & 0 & 1\\ 0 & 1 & 1\end\begin1 \\ 1 \\ 0\end= \begin1 \\ 1\end.Solving the normal equation gives

x=(1/3,1/3).

Formulations for Linear Regression

The three main linear least squares formulations are:

\hat = (\mathbf^\mathsf\mathbf)^ \mathbf^\mathsf \mathbf, where

y

is a vector whose ith element is the ith observation of the dependent variable, and

X

is a matrix whose ij element is the ith observation of the jth independent variable. The estimator is unbiased and consistent if the errors have finite variance and are uncorrelated with the regressors:[2] \operatorname[\,\mathbf{x}_i\varepsilon_i\,] = 0, where

xi

is the transpose of row i of the matrix

X.

It is also efficient under the assumption that the errors have finite variance and are homoscedastic, meaning that E[''ε''<sub>''i''</sub><sup>2</sup>{{!}}''x''<sub>''i''</sub>] 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 β. 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.

\hat = (\mathbf^\mathsf \boldsymbol\Omega^ \mathbf)^\mathbf^\mathsf\boldsymbol\Omega^\mathbf, where Ω 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.

Alternative formulations

Other formulations include:

\hat = (\mathbf^\mathsf\mathbf(\mathbf^\mathsf\mathbf)^\mathbf^\mathsf\mathbf)^\mathbf^\mathsf\mathbf(\mathbf^\mathsf\mathbf)^\mathbf^\mathsf\mathbf. Optimal instruments regression is an extension of classical IV regression to the situation where .

n+1

(were

n

is the number of estimators) distinct reference values β. The true distribution is then approximated by a linear regression, and the best estimators are obtained in closed form as \hat = ((\mathbf)^\mathsf \boldsymbol\Omega^ \mathbf)^(\mathbf)^\mathsf\boldsymbol\Omega^(\mathbf-\mathbf, where

Y

denotes the template matrix with the values of the known or previously determined model for any of the reference values β,

y

are the random variables (e.g. a measurement), and the matrix

\tilde{M

} and the vector

\tilde{m

} are calculated from the values of β. The LTF can also be expressed for Log-normal distribution distributed random variables. A generalization of the LTF is the Quadratic Template Fit, which assumes a second order regression of the model, requires predictions for at least

n2+2n

distinct values β, and it finds the best estimator using Newton's method.

Objective function

In OLS (i.e., assuming unweighted observations), the optimal value of the objective function is found by substituting the optimal expression for the coefficient vector:S=\mathbf y^\mathsf (\mathbf - \mathbf)^\mathsf (\mathbf - \mathbf) \mathbf y = \mathbf y^\mathsf (\mathbf - \mathbf) \mathbf y,where

H=X(XTX)-1XT

, the latter equality holding since

(I-H)

is symmetric and idempotent. It can be shown from this[9] that under an appropriate assignment of weights the expected value of S is m-n. If instead unit weights are assumed, the expected value of S is

(m-n)\sigma2

, where

\sigma2

is the variance of each observation.

If it is assumed that the residuals belong to a normal distribution, the objective function, being a sum of weighted squared residuals, will belong to a chi-squared distribution with m − n degrees of freedom. Some illustrative percentile values of

\chi2

are given in the following table.[10]

m-n

\chi

2
0.50

\chi

2
0.95

\chi

2
0.99
10 9.34 18.3 23.2
25 24.3 37.7 44.3
100 99.3 124 136
These values can be used for a statistical criterion as to the goodness of fit. When unit weights are used, the numbers should be divided by the variance of an observation.

For WLS, the ordinary objective function above is replaced for a weighted average of residuals.

Discussion

In statistics and mathematics, linear least squares is an approach to fitting a mathematical or statistical model to data in cases where the idealized value provided by the model for any data point is expressed linearly in terms of the unknown parameters of the model. The resulting fitted model can be used to summarize the data, to predict unobserved values from the same system, and to understand the mechanisms that may underlie the system.

Mathematically, linear least squares is the problem of approximately solving an overdetermined system of linear equations A x = b, where b is not an element of the column space of the matrix A. The approximate solution is realized as an exact solution to A x = b', where b' is the projection of b onto the column space of A. The best approximation is then that which minimizes the sum of squared differences between the data values and their corresponding modeled values. The approach is called linear least squares since the assumed function is linear in the parameters to be estimated. Linear least squares problems are convex and have a closed-form solution that is unique, provided that the number of data points used for fitting equals or exceeds the number of unknown parameters, except in special degenerate situations. In contrast, non-linear least squares problems generally must be solved by an iterative procedure, and the problems can be non-convex with multiple optima for the objective function. If prior distributions are available, then even an underdetermined system can be solved using the Bayesian MMSE estimator.

In statistics, linear least squares problems correspond to a particularly important type of statistical model called linear regression which arises as a particular form of regression analysis. One basic form of such a model is an ordinary least squares model. The present article concentrates on the mathematical aspects of linear least squares problems, with discussion of the formulation and interpretation of statistical regression models and statistical inferences related to these being dealt with in the articles just mentioned. See outline of regression analysis for an outline of the topic.

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.[11]

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.[12] [13]

In some cases the (weighted) normal equations matrix XTX 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

\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,

\|y-X\hat{\boldsymbol{\beta}}\|

is minimized, whereas in some cases one is truly interested in obtaining small error in the parameter

\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. 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.

Applications

f(x,\boldsymbol\beta)=\beta1+\beta2x

.[14]

f(x,\boldsymbol\beta)=\beta1+\beta2x+\beta3x2

.

Uses in data fitting

The primary application of linear least squares is in data fitting. Given a set of m data points

y1,y2,...,ym,

consisting of experimentally measured values taken at m values

x1,x2,...,xm

of an independent variable (

xi

may be scalar or vector quantities), and given a model function

y=f(x,\boldsymbol\beta),

with

\boldsymbol\beta=(\beta1,\beta2,...,\betan),

it is desired to find the parameters

\betaj

such that the model function "best" fits the data. In linear least squares, linearity is meant to be with respect to parameters

\betaj,

sof(x, \boldsymbol \beta) = \sum_^ \beta_j \varphi_j(x).

Here, the functions

\varphij

may be nonlinear with respect to the variable x.

Ideally, the model function fits the data exactly, soy_i = f(x_i, \boldsymbol \beta)for all

i=1,2,...,m.

This is usually not possible in practice, as there are more data points than there are parameters to be determined. The approach chosen then is to find the minimal possible value of the sum of squares of the residualsr_i(\boldsymbol \beta)= y_i - f(x_i, \boldsymbol \beta),\ (i=1, 2, \dots, m) so to minimize the functionS(\boldsymbol \beta)=\sum_^r_i^2(\boldsymbol \beta).

After substituting for

ri

and then for

f

, this minimization problem becomes the quadratic minimization problem above withX_ = \varphi_j(x_i),and the best fit can be found by solving the normal equations.

Example

A hypothetical researcher conducts an experiment and obtains four

(x,y)

data points:

(1,6),

(2,5),

(3,7),

and

(4,10)

(shown in red in the diagram on the right). Because of exploratory data analysis or prior knowledge of the subject matter, the researcher suspects that the

y

-values depend on the

x

-values systematically. The

x

-values are assumed to be exact, but the

y

-values contain some uncertainty or "noise", because of the phenomenon being studied, imperfections in the measurements, etc.

Fitting a line

One of the simplest possible relationships between

x

and

y

is a line

y=\beta1+\beta2x

. The intercept

\beta1

and the slope

\beta2

are initially unknown. The researcher would like to find values of

\beta1

and

\beta2

that cause the line to pass through the four data points. In other words, the researcher would like to solve the system of linear equations\begin\beta_1 + 1 \beta_2 &&\; = \;&& 6, & \\\beta_1 + 2 \beta_2 &&\; = \;&& 5, & \\\beta_1 + 3 \beta_2 &&\; = \;&& 7, & \\\beta_1 + 4 \beta_2 &&\; = \;&& 10. & \\\endWith four equations in two unknowns, this system is overdetermined. There is no exact solution. To consider approximate solutions, one introduces residuals

r1

,

r2

,

r3

,

r4

into the equations:\begin\beta_1 + 1 \beta_2 + r_1 &&\; = \;&& 6, & \\\beta_1 + 2 \beta_2 + r_2 &&\; = \;&& 5, & \\\beta_1 + 3 \beta_2 + r_3 &&\; = \;&& 7, & \\\beta_1 + 4 \beta_2 + r_4 &&\; = \;&& 10. & \\\endThe

i

th residual

ri

is the misfit between the

i

th observation

yi

and the

i

th prediction

\beta1+\beta2xi

:\beginr_1 &&\; = \;&& 6 - (\beta_1 + 1\beta_2), & \\r_2 &&\; = \;&& 5 - (\beta_1 + 2\beta_2), & \\r_3 &&\; = \;&& 7 - (\beta_1 + 3\beta_2), & \\r_4 &&\; = \;&& 10 - (\beta_1 + 4\beta_2). & \\\endAmong all approximate solutions, the researcher would like to find the one that is "best" in some sense.

In least squares, one focuses on the sum

S

of the squared residuals:\beginS(\beta_1, \beta_2) &= r_1^2 + r_2^2 + r_3^2 + r_4^2 \\[6pt]&= [6-(\beta_1+1\beta_2)]^2 + [5-(\beta_1+2\beta_2)]^2 + [7-(\beta_1+3\beta_2)]^2 + [10-(\beta_1+4\beta_2)]^2 \\[6pt]&= 4\beta_1^2 + 30\beta_2^2 + 20\beta_1\beta_2 - 56\beta_1 - 154\beta_2 + 210. \\[6pt]\endThe best solution is defined to be the one that minimizes

S

with respect to

\beta1

and

\beta2

. The minimum can be calculated by setting the partial derivatives of

S

to zero:0 = \frac = 8 \beta_1 + 20 \beta_2 -56,0 = \frac = 20 \beta_1 + 60 \beta_2 -154.These normal equations constitute a system of two linear equations in two unknowns. The solution is

\beta1=3.5

and

\beta2=1.4

, and the best-fit line is therefore

y=3.5+1.4x

.The residuals are

1.1,

-1.3,

-0.7,

and

0.9

(see the diagram on the right). The minimum value of the sum of squared residuals isS(3.5, 1.4) = 1.1^2+(-1.3)^2+(-0.7)^2+0.9^2 = 4.2.

This calculation can be expressed in matrix notation as follows. The original system of equations is

y=X\beta

, where\mathbf = \left[\begin{array}{c} 6 \\ 5 \\ 7 \\ 10 \end{array}\right], \;\;\;\; \mathbf = \left[\begin{array}{cc}1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \end{array}\right], \;\;\;\; \mathbf = \left[\begin{array}{c} \beta_1 \\ \beta_2 \end{array}\right].Intuitively,\mathbf = \mathbf \mathbf \;\;\;\; \Rightarrow \;\;\;\; \mathbf^\top \mathbf = \mathbf^\top \mathbf \mathbf \;\;\;\; \Rightarrow \;\;\;\; \mathbf = \left(\mathbf^\top \mathbf\right)^ \mathbf^\top \mathbf = \left[\begin{array}{c} 3.5 \\ 1.4 \end{array}\right].More rigorously, if

X\topX

is invertible, then the matrix

X\left(X\topX\right)-1X\top

represents orthogonal projection onto the column space of

X

. Therefore, among all vectors of the form

X\beta

, the one closest to

y

is

X\left(X\topX\right)-1X\topy

. Setting\mathbf \left(\mathbf^\top \mathbf\right)^ \mathbf^\top \mathbf = \mathbf \mathbf,it is evident that

\beta=\left(X\topX\right)-1X\topy

is a solution.

Fitting a parabola

Suppose that the hypothetical researcher wishes to fit a parabola of the form

y=\beta1x2

. Importantly, this model is still linear in the unknown parameters (now just

\beta1

), so linear least squares still applies. The system of equations incorporating residuals is\begin6 &&\; = \beta_1 (1)^2 + r_1 \\5 &&\; = \beta_1 (2)^2 + r_2 \\7 &&\; = \beta_1 (3)^2 + r_3 \\10 &&\; = \beta_1 (4)^2 + r_4 \\\end

The sum of squared residuals isS(\beta_1) = (6 - \beta_1)^2 + (5 - 4 \beta_1)^2 + (7 - 9 \beta_1)^2 + (10 - 16 \beta_1)^2.There is just one partial derivative to set to 0:0 = \frac = 708 \beta_1 - 498.The solution is

\beta1=0.703

, and the fit model is

y=0.703x2

.

In matrix notation, the equations without residuals are again

y=X\beta

, where now\mathbf = \left[\begin{array}{c} 6 \\ 5 \\ 7 \\ 10 \end{array}\right], \;\;\;\; \mathbf = \left[\begin{array}{c}1 \\ 4 \\ 9 \\ 16 \end{array}\right], \;\;\;\; \mathbf = \left[\begin{array}{c} \beta_1 \end{array}\right].By the same logic as above, the solution is\mathbf = \left(\mathbf^\top \mathbf\right)^ \mathbf^\top \mathbf = \left[\begin{array}{c} 0.703 \end{array}\right].

The figure shows an extension to fitting the three parameter parabola using a design matrix

X

with three columns (one for

x0

,

x1

, and

x2

), and one row for each of the red data points.

Fitting other curves and surfaces

More generally, one can have

n

regressors

xj

, and a linear modely = \beta_0 + \sum_^ \beta_ x_.

See also

Further reading

External links

Notes and References

  1. Web site: Normal Equation . Weisstein . Eric W . MathWorld . Wolfram . December 18, 2023.
  2. Lai . T.L. . Robbins . H. . Wei . C.Z. . . 1978 . 75 . Strong consistency of least squares estimates in multiple regression . 7 . 3034–3036 . 10.1073/pnas.75.7.3034 . 16592540 . 68164 . 1978PNAS...75.3034L . 392707 . free .
  3. The Unifying Role of Iterative Generalized Least Squares in Statistical Algorithms . del Pino . Guido . Statistical Science . 4 . 1989 . 394–403 . 10.1214/ss/1177012408 . 4 . 2245853. free .
  4. Adapting for Heteroscedasticity in Linear Models . Carroll . Raymond J. . The Annals of Statistics . 10 . 1982 . 1224–1233 . 10.1214/aos/1176345987 . 4 . 2240725. free .
  5. Robust, Smoothly Heterogeneous Variance Regression . Cohen . Michael . Dalal, Siddhartha R. . Tukey, John W. . Journal of the Royal Statistical Society, Series C . 42 . 1993 . 339–353 . 2 . 2986237.
  6. Total Least Squares: State-of-the-Art Regression in Numerical Analysis . Nievergelt . Yves . SIAM Review . 36 . 1994 . 258–264 . 10.1137/1036055 . 2 . 2132463.
  7. The Linear Template Fit . Britzger . Daniel . Eur. Phys. J. C . 82 . 2022 . 8 . 731 . 10.1140/epjc/s10052-022-10581-w . 2112.01548. 2022EPJC...82..731B . 244896511 .
  8. 1406472 . Least Squares Percentage Regression . Tofallis, C . Journal of Modern Applied Statistical Methods . 7 . 2009 . 526–534 . 10.2139/ssrn.1406472 . 2299/965 . free .
  9. Book: Hamilton, W. C. . Statistics in Physical Science . 1964 . Ronald Press . New York . registration .
  10. Book: Spiegel, Murray R. . Schaum's outline of theory and problems of probability and statistics . 1975 . McGraw-Hill . New York . 978-0-585-26739-5 .
  11. Book: Margenau, Henry . The Mathematics of Physics and Chemistry . Murphy, George Moseley . 1956 . Van Nostrand . Princeton . registration .
  12. Book: Gans, Peter . Data fitting in the Chemical Sciences . 1992 . Wiley . New York . 978-0-471-93412-7 .
  13. Book: Deming, W. E. . Statistical adjustment of Data . 1943 . Wiley . New York .
  14. Book: Acton, F. S. . Analysis of Straight-Line Data . 1959 . Wiley . New York .
  15. Book: Guest, P. G. . Numerical Methods of Curve Fitting . 1961 . Cambridge University Press . Cambridge .