- Source: 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.
Basic formulation
Consider the linear equation
where
A
∈
R
m
×
n
{\displaystyle A\in \mathbb {R} ^{m\times n}}
and
b
∈
R
m
{\displaystyle b\in \mathbb {R} ^{m}}
are given and
x
∈
R
n
{\displaystyle x\in \mathbb {R} ^{n}}
is variable to be computed. When
m
>
n
,
{\displaystyle m>n,}
it is generally the case that (1) has no solution.
For example, there is no value of
x
{\displaystyle x}
that satisfies
[
1
0
0
1
1
1
]
x
=
[
1
1
0
]
,
{\displaystyle {\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}}x={\begin{bmatrix}1\\1\\0\end{bmatrix}},}
because the first two rows require that
x
=
(
1
,
1
)
,
{\displaystyle x=(1,1),}
but then the third row is not satisfied.
Thus, for
m
>
n
,
{\displaystyle m>n,}
the goal of solving (1) exactly is typically replaced by finding the value of
x
{\displaystyle 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
‖
A
x
−
b
‖
2
.
{\displaystyle \|Ax-b\|^{2}.}
This produces a minimization problem, called a least squares problem
The solution to the least squares problem (1) is computed by solving the normal equation
where
A
⊤
{\displaystyle A^{\top }}
denotes the transpose of
A
{\displaystyle A}
.
Continuing the example, above, with
A
=
[
1
0
0
1
1
1
]
and
b
=
[
1
1
0
]
,
{\displaystyle A={\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}}\quad {\text{and}}\quad b={\begin{bmatrix}1\\1\\0\end{bmatrix}},}
we find
A
⊤
A
=
[
1
0
1
0
1
1
]
[
1
0
0
1
1
1
]
=
[
2
1
1
2
]
{\displaystyle A^{\top }A={\begin{bmatrix}1&0&1\\0&1&1\end{bmatrix}}{\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}}={\begin{bmatrix}2&1\\1&2\end{bmatrix}}}
and
A
⊤
b
=
[
1
0
1
0
1
1
]
[
1
1
0
]
=
[
1
1
]
.
{\displaystyle A^{\top }b={\begin{bmatrix}1&0&1\\0&1&1\end{bmatrix}}{\begin{bmatrix}1\\1\\0\end{bmatrix}}={\begin{bmatrix}1\\1\end{bmatrix}}.}
Solving the normal equation gives
x
=
(
1
/
3
,
1
/
3
)
.
{\displaystyle x=(1/3,1/3).}
Formulations for Linear Regression
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 β:
β
^
=
(
X
T
X
)
−
1
X
T
y
,
{\displaystyle {\hat {\boldsymbol {\beta }}}=(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}\mathbf {y} ,}
where
y
{\displaystyle \mathbf {y} }
is a vector whose ith element is the ith observation of the dependent variable, and
X
{\displaystyle \mathbf {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:
E
[
x
i
ε
i
]
=
0
,
{\displaystyle \operatorname {E} [\,\mathbf {x} _{i}\varepsilon _{i}\,]=0,}
where
x
i
{\displaystyle \mathbf {x} _{i}}
is the transpose of row i of the matrix
X
.
{\displaystyle \mathbf {X} .}
It is also efficient under the assumption that the errors have finite variance and are homoscedastic, meaning that E[εi2|xi] 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.
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 β 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 ith case is inversely proportional to var(εi). This special case of GLS is called "weighted least squares". The GLS solution to an estimation problem is
β
^
=
(
X
T
Ω
−
1
X
)
−
1
X
T
Ω
−
1
y
,
{\displaystyle {\hat {\boldsymbol {\beta }}}=(\mathbf {X} ^{\mathsf {T}}{\boldsymbol {\Omega }}^{-1}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}{\boldsymbol {\Omega }}^{-1}\mathbf {y} ,}
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:
Iteratively reweighted least squares (IRLS) is used when heteroscedasticity, or correlations, or both are present among the error terms of the model, but where little is known about the covariance structure of the errors independently of the data. In the first iteration, OLS, or GLS with a provisional covariance structure is carried out, and the residuals are obtained from the fit. Based on the residuals, an improved estimate of the covariance structure of the errors can usually be obtained. A subsequent GLS iteration is then performed using this estimate of the error structure to define the weights. The process can be iterated to convergence, but in many cases, only one iteration is sufficient to achieve an efficient estimate of β.
Instrumental variables regression (IV) can be performed when the regressors are correlated with the errors. In this case, we need the existence of some auxiliary instrumental variables zi such that E[ziεi] = 0. If Z is the matrix of instruments, then the estimator can be given in closed form as
β
^
=
(
X
T
Z
(
Z
T
Z
)
−
1
Z
T
X
)
−
1
X
T
Z
(
Z
T
Z
)
−
1
Z
T
y
.
{\displaystyle {\hat {\boldsymbol {\beta }}}=(\mathbf {X} ^{\mathsf {T}}\mathbf {Z} (\mathbf {Z} ^{\mathsf {T}}\mathbf {Z} )^{-1}\mathbf {Z} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}\mathbf {Z} (\mathbf {Z} ^{\mathsf {T}}\mathbf {Z} )^{-1}\mathbf {Z} ^{\mathsf {T}}\mathbf {y} .}
Optimal instruments regression is an extension of classical IV regression to the situation where E[εi | zi] = 0.
Total least squares (TLS) is an approach to least squares estimation of the linear regression model that treats the covariates and response variable in a more geometrically symmetric manner than OLS. It is one approach to handling the "errors in variables" problem, and is also sometimes used even when the covariates are assumed to be error-free.
Linear Template Fit (LTF) combines a linear regression with (generalized) least squares in order to determine the best estimator. The Linear Template Fit addresses the frequent issue, when the residuals cannot be expressed analytically or are too time consuming to be evaluate repeatedly, as it is often the case in iterative minimization algorithms. In the Linear Template Fit, the residuals are estimated from the random variables and from a linear approximation of the underlying true model, while the true model needs to be provided for at least
n
+
1
{\displaystyle n+1}
(were
n
{\displaystyle 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
β
^
=
(
(
Y
M
~
)
T
Ω
−
1
Y
M
~
)
−
1
(
Y
M
~
)
T
Ω
−
1
(
y
−
Y
m
¯
)
,
{\displaystyle {\hat {\boldsymbol {\beta }}}=((\mathbf {Y{\tilde {M}}} )^{\mathsf {T}}{\boldsymbol {\Omega }}^{-1}\mathbf {Y{\tilde {M}}} )^{-1}(\mathbf {Y{\tilde {M}}} )^{\mathsf {T}}{\boldsymbol {\Omega }}^{-1}(\mathbf {y} -\mathbf {Y{\bar {m}})} ,}
where
Y
{\displaystyle \mathbf {Y} }
denotes the template matrix with the values of the known or previously determined model for any of the reference values β,
y
{\displaystyle \mathbf {y} }
are the random variables (e.g. a measurement), and the matrix
M
~
{\displaystyle \mathbf {\tilde {M}} }
and the vector
m
~
{\displaystyle \mathbf {\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
n
2
+
2
n
{\displaystyle n^{2}+2n}
distinct values β, and it finds the best estimator using Newton's method.
Percentage least squares focuses on reducing percentage errors, which is useful in the field of forecasting or time series analysis. It is also useful in situations where the dependent variable has a wide range without constant variance, as here the larger residuals at the upper end of the range would dominate if OLS were used. When the percentage or relative error is normally distributed, least squares percentage regression provides maximum likelihood estimates. Percentage regression is linked to a multiplicative error model, whereas OLS is linked to models containing an additive error term.
Constrained least squares, indicates a linear least squares problem with additional constraints on the solution.
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
=
y
T
(
I
−
H
)
T
(
I
−
H
)
y
=
y
T
(
I
−
H
)
y
,
{\displaystyle S=\mathbf {y} ^{\mathsf {T}}(\mathbf {I} -\mathbf {H} )^{\mathsf {T}}(\mathbf {I} -\mathbf {H} )\mathbf {y} =\mathbf {y} ^{\mathsf {T}}(\mathbf {I} -\mathbf {H} )\mathbf {y} ,}
where
H
=
X
(
X
T
X
)
−
1
X
T
{\displaystyle \mathbf {H} =\mathbf {X} (\mathbf {X} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}}
, the latter equality holding since
(
I
−
H
)
{\displaystyle (\mathbf {I} -\mathbf {H} )}
is symmetric and idempotent. It can be shown from this that under an appropriate assignment of weights the expected value of S is
m
−
n
{\textstyle m-n}
. If instead unit weights are assumed, the expected value of S is
(
m
−
n
)
σ
2
{\displaystyle (m-n)\sigma ^{2}}
, where
σ
2
{\displaystyle \sigma ^{2}}
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 (
χ
2
{\displaystyle \chi ^{2}}
) distribution with m − n degrees of freedom. Some illustrative percentile values of
χ
2
{\displaystyle \chi ^{2}}
are given in the following table.
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,
ε
{\displaystyle \varepsilon }
, are uncorrelated, have a mean of zero and a constant variance,
σ
{\displaystyle \sigma }
, the Gauss–Markov theorem states that the least-squares estimator,
β
^
{\displaystyle {\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 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
β
^
{\displaystyle \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,
‖
y
−
X
β
^
‖
{\displaystyle \|\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}}\|}
is minimized, whereas in some cases one is truly interested in obtaining small error in the parameter
β
^
{\displaystyle \mathbf {\hat {\boldsymbol {\beta }}} }
, e.g., a small value of
‖
β
−
β
^
‖
{\displaystyle \|{\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}}\|}
. However, since the true parameter
β
{\displaystyle {\boldsymbol {\beta }}}
is necessarily unknown, this quantity cannot be directly minimized. If a prior probability on
β
^
{\displaystyle {\hat {\boldsymbol {\beta }}}}
is known, then a Bayes estimator can be used to minimize the mean squared error,
E
{
‖
β
−
β
^
‖
2
}
{\displaystyle 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
Polynomial fitting: models are polynomials in an independent variable, x:
Straight line:
f
(
x
,
β
)
=
β
1
+
β
2
x
{\displaystyle f(x,{\boldsymbol {\beta }})=\beta _{1}+\beta _{2}x}
.
Quadratic:
f
(
x
,
β
)
=
β
1
+
β
2
x
+
β
3
x
2
{\displaystyle f(x,{\boldsymbol {\beta }})=\beta _{1}+\beta _{2}x+\beta _{3}x^{2}}
.
Cubic, quartic and higher polynomials. For regression with high-order polynomials, the use of orthogonal polynomials is recommended.
Numerical smoothing and differentiation — this is an application of polynomial fitting.
Multinomials in more than one independent variable, including surface fitting
Curve fitting with B-splines
Chemometrics, Calibration curve, Standard addition, Gran plot, analysis of mixtures
= Uses in data fitting
=The primary application of linear least squares is in data fitting. Given a set of m data points
y
1
,
y
2
,
…
,
y
m
,
{\displaystyle y_{1},y_{2},\dots ,y_{m},}
consisting of experimentally measured values taken at m values
x
1
,
x
2
,
…
,
x
m
{\displaystyle x_{1},x_{2},\dots ,x_{m}}
of an independent variable (
x
i
{\displaystyle x_{i}}
may be scalar or vector quantities), and given a model function
y
=
f
(
x
,
β
)
,
{\displaystyle y=f(x,{\boldsymbol {\beta }}),}
with
β
=
(
β
1
,
β
2
,
…
,
β
n
)
,
{\displaystyle {\boldsymbol {\beta }}=(\beta _{1},\beta _{2},\dots ,\beta _{n}),}
it is desired to find the parameters
β
j
{\displaystyle \beta _{j}}
such that the model function "best" fits the data. In linear least squares, linearity is meant to be with respect to parameters
β
j
,
{\displaystyle \beta _{j},}
so
f
(
x
,
β
)
=
∑
j
=
1
n
β
j
φ
j
(
x
)
.
{\displaystyle f(x,{\boldsymbol {\beta }})=\sum _{j=1}^{n}\beta _{j}\varphi _{j}(x).}
Here, the functions
φ
j
{\displaystyle \varphi _{j}}
may be nonlinear with respect to the variable x.
Ideally, the model function fits the data exactly, so
y
i
=
f
(
x
i
,
β
)
{\displaystyle y_{i}=f(x_{i},{\boldsymbol {\beta }})}
for all
i
=
1
,
2
,
…
,
m
.
{\displaystyle i=1,2,\dots ,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 residuals
r
i
(
β
)
=
y
i
−
f
(
x
i
,
β
)
,
(
i
=
1
,
2
,
…
,
m
)
{\displaystyle r_{i}({\boldsymbol {\beta }})=y_{i}-f(x_{i},{\boldsymbol {\beta }}),\ (i=1,2,\dots ,m)}
so to minimize the function
S
(
β
)
=
∑
i
=
1
m
r
i
2
(
β
)
.
{\displaystyle S({\boldsymbol {\beta }})=\sum _{i=1}^{m}r_{i}^{2}({\boldsymbol {\beta }}).}
After substituting for
r
i
{\displaystyle r_{i}}
and then for
f
{\displaystyle f}
, this minimization problem becomes the quadratic minimization problem above with
X
i
j
=
φ
j
(
x
i
)
,
{\displaystyle X_{ij}=\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
)
{\displaystyle (x,y)}
data points:
(
1
,
6
)
,
{\displaystyle (1,6),}
(
2
,
5
)
,
{\displaystyle (2,5),}
(
3
,
7
)
,
{\displaystyle (3,7),}
and
(
4
,
10
)
{\displaystyle (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
{\displaystyle y}
-values depend on the
x
{\displaystyle x}
-values systematically. The
x
{\displaystyle x}
-values are assumed to be exact, but the
y
{\displaystyle 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
{\displaystyle x}
and
y
{\displaystyle y}
is a line
y
=
β
1
+
β
2
x
{\displaystyle y=\beta _{1}+\beta _{2}x}
. The intercept
β
1
{\displaystyle \beta _{1}}
and the slope
β
2
{\displaystyle \beta _{2}}
are initially unknown. The researcher would like to find values of
β
1
{\displaystyle \beta _{1}}
and
β
2
{\displaystyle \beta _{2}}
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
β
1
+
1
β
2
=
6
,
β
1
+
2
β
2
=
5
,
β
1
+
3
β
2
=
7
,
β
1
+
4
β
2
=
10.
{\displaystyle {\begin{alignedat}{3}\beta _{1}+1\beta _{2}&&\;=\;&&6,&\\\beta _{1}+2\beta _{2}&&\;=\;&&5,&\\\beta _{1}+3\beta _{2}&&\;=\;&&7,&\\\beta _{1}+4\beta _{2}&&\;=\;&&10.&\\\end{alignedat}}}
With four equations in two unknowns, this system is overdetermined. There is no exact solution. To consider approximate solutions, one introduces residuals
r
1
{\displaystyle r_{1}}
,
r
2
{\displaystyle r_{2}}
,
r
3
{\displaystyle r_{3}}
,
r
4
{\displaystyle r_{4}}
into the equations:
β
1
+
1
β
2
+
r
1
=
6
,
β
1
+
2
β
2
+
r
2
=
5
,
β
1
+
3
β
2
+
r
3
=
7
,
β
1
+
4
β
2
+
r
4
=
10.
{\displaystyle {\begin{alignedat}{3}\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.&\\\end{alignedat}}}
The
i
{\displaystyle i}
th residual
r
i
{\displaystyle r_{i}}
is the misfit between the
i
{\displaystyle i}
th observation
y
i
{\displaystyle y_{i}}
and the
i
{\displaystyle i}
th prediction
β
1
+
β
2
x
i
{\displaystyle \beta _{1}+\beta _{2}x_{i}}
:
r
1
=
6
−
(
β
1
+
1
β
2
)
,
r
2
=
5
−
(
β
1
+
2
β
2
)
,
r
3
=
7
−
(
β
1
+
3
β
2
)
,
r
4
=
10
−
(
β
1
+
4
β
2
)
.
{\displaystyle {\begin{alignedat}{3}r_{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}).&\\\end{alignedat}}}
Among 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
{\displaystyle S}
of the squared residuals:
S
(
β
1
,
β
2
)
=
r
1
2
+
r
2
2
+
r
3
2
+
r
4
2
=
[
6
−
(
β
1
+
1
β
2
)
]
2
+
[
5
−
(
β
1
+
2
β
2
)
]
2
+
[
7
−
(
β
1
+
3
β
2
)
]
2
+
[
10
−
(
β
1
+
4
β
2
)
]
2
=
4
β
1
2
+
30
β
2
2
+
20
β
1
β
2
−
56
β
1
−
154
β
2
+
210.
{\displaystyle {\begin{aligned}S(\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]\end{aligned}}}
The best solution is defined to be the one that minimizes
S
{\displaystyle S}
with respect to
β
1
{\displaystyle \beta _{1}}
and
β
2
{\displaystyle \beta _{2}}
. The minimum can be calculated by setting the partial derivatives of
S
{\displaystyle S}
to zero:
0
=
∂
S
∂
β
1
=
8
β
1
+
20
β
2
−
56
,
{\displaystyle 0={\frac {\partial S}{\partial \beta _{1}}}=8\beta _{1}+20\beta _{2}-56,}
0
=
∂
S
∂
β
2
=
20
β
1
+
60
β
2
−
154.
{\displaystyle 0={\frac {\partial S}{\partial \beta _{2}}}=20\beta _{1}+60\beta _{2}-154.}
These normal equations constitute a system of two linear equations in two unknowns. The solution is
β
1
=
3.5
{\displaystyle \beta _{1}=3.5}
and
β
2
=
1.4
{\displaystyle \beta _{2}=1.4}
, and the best-fit line is therefore
y
=
3.5
+
1.4
x
{\displaystyle y=3.5+1.4x}
.
The residuals are
1.1
,
{\displaystyle 1.1,}
−
1.3
,
{\displaystyle -1.3,}
−
0.7
,
{\displaystyle -0.7,}
and
0.9
{\displaystyle 0.9}
(see the diagram on the right). The minimum value of the sum of squared residuals is
S
(
3.5
,
1.4
)
=
1.1
2
+
(
−
1.3
)
2
+
(
−
0.7
)
2
+
0.9
2
=
4.2.
{\displaystyle S(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
β
{\displaystyle \mathbf {y} =\mathbf {X} \mathbf {\beta } }
, where
y
=
[
6
5
7
10
]
,
X
=
[
1
1
1
2
1
3
1
4
]
,
β
=
[
β
1
β
2
]
.
{\displaystyle \mathbf {y} =\left[{\begin{array}{c}6\\5\\7\\10\end{array}}\right],\;\;\;\;\mathbf {X} =\left[{\begin{array}{cc}1&1\\1&2\\1&3\\1&4\end{array}}\right],\;\;\;\;\mathbf {\beta } =\left[{\begin{array}{c}\beta _{1}\\\beta _{2}\end{array}}\right].}
Intuitively,
y
=
X
β
⇒
X
⊤
y
=
X
⊤
X
β
⇒
β
=
(
X
⊤
X
)
−
1
X
⊤
y
=
[
3.5
1.4
]
.
{\displaystyle \mathbf {y} =\mathbf {X} \mathbf {\beta } \;\;\;\;\Rightarrow \;\;\;\;\mathbf {X} ^{\top }\mathbf {y} =\mathbf {X} ^{\top }\mathbf {X} \mathbf {\beta } \;\;\;\;\Rightarrow \;\;\;\;\mathbf {\beta } =\left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }\mathbf {y} =\left[{\begin{array}{c}3.5\\1.4\end{array}}\right].}
More rigorously, if
X
⊤
X
{\displaystyle \mathbf {X} ^{\top }\mathbf {X} }
is invertible, then the matrix
X
(
X
⊤
X
)
−
1
X
⊤
{\displaystyle \mathbf {X} \left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }}
represents orthogonal projection onto the column space of
X
{\displaystyle \mathbf {X} }
. Therefore, among all vectors of the form
X
β
{\displaystyle \mathbf {X} \mathbf {\beta } }
, the one closest to
y
{\displaystyle \mathbf {y} }
is
X
(
X
⊤
X
)
−
1
X
⊤
y
{\displaystyle \mathbf {X} \left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }\mathbf {y} }
. Setting
X
(
X
⊤
X
)
−
1
X
⊤
y
=
X
β
,
{\displaystyle \mathbf {X} \left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }\mathbf {y} =\mathbf {X} \mathbf {\beta } ,}
it is evident that
β
=
(
X
⊤
X
)
−
1
X
⊤
y
{\displaystyle \mathbf {\beta } =\left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }\mathbf {y} }
is a solution.
= Fitting a parabola
=Suppose that the hypothetical researcher wishes to fit a parabola of the form
y
=
β
1
x
2
{\displaystyle y=\beta _{1}x^{2}}
. Importantly, this model is still linear in the unknown parameters (now just
β
1
{\displaystyle \beta _{1}}
), so linear least squares still applies. The system of equations incorporating residuals is
6
=
β
1
(
1
)
2
+
r
1
5
=
β
1
(
2
)
2
+
r
2
7
=
β
1
(
3
)
2
+
r
3
10
=
β
1
(
4
)
2
+
r
4
{\displaystyle {\begin{alignedat}{2}6&&\;=\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{alignedat}}}
The sum of squared residuals is
S
(
β
1
)
=
(
6
−
β
1
)
2
+
(
5
−
4
β
1
)
2
+
(
7
−
9
β
1
)
2
+
(
10
−
16
β
1
)
2
.
{\displaystyle S(\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
=
∂
S
∂
β
1
=
708
β
1
−
498.
{\displaystyle 0={\frac {\partial S}{\partial \beta _{1}}}=708\beta _{1}-498.}
The solution is
β
1
=
0.703
{\displaystyle \beta _{1}=0.703}
, and the fit model is
y
=
0.703
x
2
{\displaystyle y=0.703x^{2}}
.
In matrix notation, the equations without residuals are again
y
=
X
β
{\displaystyle \mathbf {y} =\mathbf {X} \mathbf {\beta } }
, where now
y
=
[
6
5
7
10
]
,
X
=
[
1
4
9
16
]
,
β
=
[
β
1
]
.
{\displaystyle \mathbf {y} =\left[{\begin{array}{c}6\\5\\7\\10\end{array}}\right],\;\;\;\;\mathbf {X} =\left[{\begin{array}{c}1\\4\\9\\16\end{array}}\right],\;\;\;\;\mathbf {\beta } =\left[{\begin{array}{c}\beta _{1}\end{array}}\right].}
By the same logic as above, the solution is
β
=
(
X
⊤
X
)
−
1
X
⊤
y
=
[
0.703
]
.
{\displaystyle \mathbf {\beta } =\left(\mathbf {X} ^{\top }\mathbf {X} \right)^{-1}\mathbf {X} ^{\top }\mathbf {y} =\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
{\displaystyle \mathbf {X} }
with three columns (one for
x
0
{\displaystyle x^{0}}
,
x
1
{\displaystyle x^{1}}
, and
x
2
{\displaystyle x^{2}}
), and one row for each of the red data points.
= Fitting other curves and surfaces
=More generally, one can have
n
{\displaystyle n}
regressors
x
j
{\displaystyle x_{j}}
, and a linear model
y
=
β
0
+
∑
j
=
1
n
β
j
x
j
.
{\displaystyle y=\beta _{0}+\sum _{j=1}^{n}\beta _{j}x_{j}.}
See also
Line–line intersection
Line fitting
Nonlinear least squares
Regularized least squares
Simple linear regression
Partial least squares regression
Linear function
References
Further reading
Bevington, Philip R.; Robinson, Keith D. (2003). Data Reduction and Error Analysis for the Physical Sciences. McGraw-Hill. ISBN 978-0-07-247227-1.
External links
Least Squares Fitting – From MathWorld
Least Squares Fitting-Polynomial – From MathWorld
Kata Kunci Pencarian:
- Pemelajaran mesin daring
- Eliminasi Gauss
- Metode simpleks
- Spektrofotometer
- Thomas J. Sargent
- Statistika matematika
- Statistika
- Matriks idempoten
- Bentuk turunan
- Pemrograman tujuan
- Linear least squares
- Non-linear least squares
- Least squares
- Weighted least squares
- Ordinary least squares
- Total least squares
- Partial least squares regression
- Constrained least squares
- Iteratively reweighted least squares
- Numerical methods for linear least squares