The purpose of this page is to provide supplementary materials for the ordinary least squares article, reducing the load of the main article with mathematics and improving its accessibility, while at the same time retaining the completeness of exposition.
Derivation of the normal equations
edit
Define the
i
{\displaystyle i}
th residual to be
r
i
=
y
i
−
∑
j
=
1
n
X
i
j
β
j
.
{\displaystyle r_{i}=y_{i}-\sum _{j=1}^{n}X_{ij}\beta _{j}.}
Then the objective
S
{\displaystyle S}
can be rewritten
S
=
∑
i
=
1
m
r
i
2
.
{\displaystyle S=\sum _{i=1}^{m}r_{i}^{2}.}
Given that S is convex, it is minimized when its gradient vector is zero (This follows by definition: if the gradient vector is not zero, there is a direction in which we can move to minimize it further – see maxima and minima .) The elements of the gradient vector are the partial derivatives of S with respect to the parameters:
∂
S
∂
β
j
=
2
∑
i
=
1
m
r
i
∂
r
i
∂
β
j
(
j
=
1
,
2
,
…
,
n
)
.
{\displaystyle {\frac {\partial S}{\partial \beta _{j}}}=2\sum _{i=1}^{m}r_{i}{\frac {\partial r_{i}}{\partial \beta _{j}}}\qquad (j=1,2,\dots ,n).}
The derivatives are
∂
r
i
∂
β
j
=
−
X
i
j
.
{\displaystyle {\frac {\partial r_{i}}{\partial \beta _{j}}}=-X_{ij}.}
Substitution of the expressions for the residuals and the derivatives into the gradient equations gives
∂
S
∂
β
j
=
2
∑
i
=
1
m
(
y
i
−
∑
k
=
1
n
X
i
k
β
k
)
(
−
X
i
j
)
(
j
=
1
,
2
,
…
,
n
)
.
{\displaystyle {\frac {\partial S}{\partial \beta _{j}}}=2\sum _{i=1}^{m}\left(y_{i}-\sum _{k=1}^{n}X_{ik}\beta _{k}\right)(-X_{ij})\qquad (j=1,2,\dots ,n).}
Thus if
β
^
{\displaystyle {\widehat {\beta }}}
minimizes S , we have
2
∑
i
=
1
m
(
y
i
−
∑
k
=
1
n
X
i
k
β
^
k
)
(
−
X
i
j
)
=
0
(
j
=
1
,
2
,
…
,
n
)
.
{\displaystyle 2\sum _{i=1}^{m}\left(y_{i}-\sum _{k=1}^{n}X_{ik}{\widehat {\beta }}_{k}\right)(-X_{ij})=0\qquad (j=1,2,\dots ,n).}
Upon rearrangement, we obtain the normal equations :
∑
i
=
1
m
∑
k
=
1
n
X
i
j
X
i
k
β
^
k
=
∑
i
=
1
m
X
i
j
y
i
(
j
=
1
,
2
,
…
,
n
)
.
{\displaystyle \sum _{i=1}^{m}\sum _{k=1}^{n}X_{ij}X_{ik}{\widehat {\beta }}_{k}=\sum _{i=1}^{m}X_{ij}y_{i}\qquad (j=1,2,\dots ,n).}
The normal equations are written in matrix notation as
(
X
T
X
)
β
^
=
X
T
y
{\displaystyle (\mathbf {X} ^{\mathrm {T} }\mathbf {X} ){\widehat {\boldsymbol {\beta }}}=\mathbf {X} ^{\mathrm {T} }\mathbf {y} }
(where X T is the matrix transpose of X ).
The solution of the normal equations yields the vector
β
^
{\displaystyle {\widehat {\boldsymbol {\beta }}}}
of the optimal parameter values.
Derivation directly in terms of matrices
edit
The normal equations can be derived directly from a matrix representation of the problem as follows. The objective is to minimize
S
(
β
)
=
‖
y
−
X
β
‖
2
=
(
y
−
X
β
)
T
(
y
−
X
β
)
=
y
T
y
−
β
T
X
T
y
−
y
T
X
β
+
β
T
X
T
X
β
.
{\displaystyle S({\boldsymbol {\beta }})={\bigl \|}\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }}{\bigr \|}^{2}=(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})^{\rm {T}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})=\mathbf {y} ^{\rm {T}}\mathbf {y} -{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {y} -\mathbf {y} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}+{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}.}
Here
(
β
T
X
T
y
)
T
=
y
T
X
β
{\displaystyle ({\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {y} )^{\rm {T}}=\mathbf {y} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}}
has the dimension 1x1 (the number of columns of
y
{\displaystyle \mathbf {y} }
), so it is a scalar and equal to its own transpose, hence
β
T
X
T
y
=
y
T
X
β
{\displaystyle {\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {y} =\mathbf {y} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}}
and the quantity to minimize becomes
S
(
β
)
=
y
T
y
−
2
β
T
X
T
y
+
β
T
X
T
X
β
.
{\displaystyle S({\boldsymbol {\beta }})=\mathbf {y} ^{\rm {T}}\mathbf {y} -2{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {y} +{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}.}
Differentiating this with respect to
β
{\displaystyle {\boldsymbol {\beta }}}
and equating to zero to satisfy the first-order conditions gives
−
X
T
y
+
(
X
T
X
)
β
=
0
,
{\displaystyle -\mathbf {X} ^{\rm {T}}\mathbf {y} +(\mathbf {X} ^{\rm {T}}\mathbf {X} ){\boldsymbol {\beta }}=0,}
which is equivalent to the above-given normal equations. A sufficient condition for satisfaction of the second-order conditions for a minimum is that
X
{\displaystyle \mathbf {X} }
have full column rank, in which case
X
T
X
{\displaystyle \mathbf {X} ^{\rm {T}}\mathbf {X} }
is positive definite .
Derivation without calculus
edit
When
X
T
X
{\displaystyle \mathbf {X} ^{\rm {T}}\mathbf {X} }
is positive definite, the formula for the minimizing value of
β
{\displaystyle {\boldsymbol {\beta }}}
can be derived without the use of derivatives. The quantity
S
(
β
)
=
y
T
y
−
2
β
T
X
T
y
+
β
T
X
T
X
β
{\displaystyle S({\boldsymbol {\beta }})=\mathbf {y} ^{\rm {T}}\mathbf {y} -2{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {y} +{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}\mathbf {X} {\boldsymbol {\beta }}}
can be written as
⟨
β
,
β
⟩
−
2
⟨
β
,
(
X
T
X
)
−
1
X
T
y
⟩
+
⟨
(
X
T
X
)
−
1
X
T
y
,
(
X
T
X
)
−
1
X
T
y
⟩
+
C
,
{\displaystyle \langle {\boldsymbol {\beta }},{\boldsymbol {\beta }}\rangle -2\langle {\boldsymbol {\beta }},(\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} \rangle +\langle (\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} ,(\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} \rangle +C,}
where
C
{\displaystyle C}
depends only on
y
{\displaystyle \mathbf {y} }
and
X
{\displaystyle \mathbf {X} }
, and
⟨
⋅
,
⋅
⟩
{\displaystyle \langle \cdot ,\cdot \rangle }
is the inner product defined by
⟨
x
,
y
⟩
=
x
T
(
X
T
X
)
y
.
{\displaystyle \langle x,y\rangle =x^{\rm {T}}(\mathbf {X} ^{\rm {T}}\mathbf {X} )y.}
It follows that
S
(
β
)
{\displaystyle S({\boldsymbol {\beta }})}
is equal to
⟨
β
−
(
X
T
X
)
−
1
X
T
y
,
β
−
(
X
T
X
)
−
1
X
T
y
⟩
+
C
{\displaystyle \langle {\boldsymbol {\beta }}-(\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} ,{\boldsymbol {\beta }}-(\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} \rangle +C}
and therefore minimized exactly when
β
−
(
X
T
X
)
−
1
X
T
y
=
0.
{\displaystyle {\boldsymbol {\beta }}-(\mathbf {X} ^{\rm {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\rm {T}}\mathbf {y} =0.}
Generalization for complex equations
edit
In general, the coefficients of the matrices
X
,
β
{\displaystyle \mathbf {X} ,{\boldsymbol {\beta }}}
and
y
{\displaystyle \mathbf {y} }
can be complex. By using a Hermitian transpose instead of a simple transpose, it is possible to find a vector
β
^
{\displaystyle {\boldsymbol {\widehat {\beta }}}}
which minimizes
S
(
β
)
{\displaystyle S({\boldsymbol {\beta }})}
, just as for the real matrix case. In order to get the normal equations we follow a similar path as in previous derivations:
S
(
β
)
=
⟨
y
−
X
β
,
y
−
X
β
⟩
=
⟨
y
,
y
⟩
−
⟨
X
β
,
y
⟩
¯
−
⟨
y
,
X
β
⟩
¯
+
⟨
X
β
,
X
β
⟩
=
y
T
y
¯
−
β
†
X
†
y
−
y
†
X
β
+
β
T
X
T
X
¯
β
¯
,
{\displaystyle \displaystyle S({\boldsymbol {\beta }})=\langle \mathbf {y} -\mathbf {X} {\boldsymbol {\beta }},\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }}\rangle =\langle \mathbf {y} ,\mathbf {y} \rangle -{\overline {\langle \mathbf {X} {\boldsymbol {\beta }},\mathbf {y} \rangle }}-{\overline {\langle \mathbf {y} ,\mathbf {X} {\boldsymbol {\beta }}\rangle }}+\langle \mathbf {X} {\boldsymbol {\beta }},\mathbf {X} {\boldsymbol {\beta }}\rangle =\mathbf {y} ^{\rm {T}}{\overline {\mathbf {y} }}-{\boldsymbol {\beta }}^{\dagger }\mathbf {X} ^{\dagger }\mathbf {y} -\mathbf {y} ^{\dagger }\mathbf {X} {\boldsymbol {\beta }}+{\boldsymbol {\beta }}^{\rm {T}}\mathbf {X} ^{\rm {T}}{\overline {\mathbf {X} }}{\overline {\boldsymbol {\beta }}},}
where
†
{\displaystyle \dagger }
stands for Hermitian transpose.
We should now take derivatives of
S
(
β
)
{\displaystyle S({\boldsymbol {\beta }})}
with respect to each of the coefficients
β
j
{\displaystyle \beta _{j}}
, but first we separate real and imaginary parts to deal with the conjugate factors in above expression. For the
β
j
{\displaystyle \beta _{j}}
we have
β
j
=
β
j
R
+
i
β
j
I
{\displaystyle \beta _{j}=\beta _{j}^{R}+i\beta _{j}^{I}}
and the derivatives change into
∂
S
∂
β
j
=
∂
S
∂
β
j
R
∂
β
j
R
∂
β
j
+
∂
S
∂
β
j
I
∂
β
j
I
∂
β
j
=
∂
S
∂
β
j
R
−
i
∂
S
∂
β
j
I
(
j
=
1
,
2
,
3
,
…
,
n
)
.
{\displaystyle {\frac {\partial S}{\partial \beta _{j}}}={\frac {\partial S}{\partial \beta _{j}^{R}}}{\frac {\partial \beta _{j}^{R}}{\partial \beta _{j}}}+{\frac {\partial S}{\partial \beta _{j}^{I}}}{\frac {\partial \beta _{j}^{I}}{\partial \beta _{j}}}={\frac {\partial S}{\partial \beta _{j}^{R}}}-i{\frac {\partial S}{\partial \beta _{j}^{I}}}\quad (j=1,2,3,\ldots ,n).}
After rewriting
S
(
β
)
{\displaystyle S({\boldsymbol {\beta }})}
in the summation form and writing
β
j
{\displaystyle \beta _{j}}
explicitly, we can calculate both partial derivatives with result:
∂
S
∂
β
j
R
=
−
∑
i
=
1
m
(
X
¯
i
j
y
i
+
y
¯
i
X
i
j
)
+
2
∑
i
=
1
m
X
i
j
X
¯
i
j
β
j
R
+
∑
i
=
1
m
∑
k
≠
j
n
(
X
i
j
X
¯
i
k
β
¯
k
+
β
k
X
i
k
X
¯
i
j
)
,
−
i
∂
S
∂
β
j
I
=
∑
i
=
1
m
(
X
¯
i
j
y
i
−
y
¯
i
X
i
j
)
−
2
i
∑
i
=
1
m
X
i
j
X
¯
i
j
β
j
I
+
∑
i
=
1
m
∑
k
≠
j
n
(
X
i
j
X
¯
i
k
β
¯
k
−
β
k
X
i
k
X
¯
i
j
)
,
{\displaystyle {\begin{aligned}{\frac {\partial S}{\partial \beta _{j}^{R}}}={}&-\sum _{i=1}^{m}{\Big (}{\overline {X}}_{ij}y_{i}+{\overline {y}}_{i}X_{ij}{\Big )}+2\sum _{i=1}^{m}X_{ij}{\overline {X}}_{ij}\beta _{j}^{R}+\sum _{i=1}^{m}\sum _{k\neq j}^{n}{\Big (}X_{ij}{\overline {X}}_{ik}{\overline {\beta }}_{k}+\beta _{k}X_{ik}{\overline {X}}_{ij}{\Big )},\\[8pt]&{}-i{\frac {\partial S}{\partial \beta _{j}^{I}}}=\sum _{i=1}^{m}{\Big (}{\overline {X}}_{ij}y_{i}-{\overline {y}}_{i}X_{ij}{\Big )}-2i\sum _{i=1}^{m}X_{ij}{\overline {X}}_{ij}\beta _{j}^{I}+\sum _{i=1}^{m}\sum _{k\neq j}^{n}{\Big (}X_{ij}{\overline {X}}_{ik}{\overline {\beta }}_{k}-\beta _{k}X_{ik}{\overline {X}}_{ij}{\Big )},\end{aligned}}}
which, after adding it together and comparing to zero (minimization condition for
β
^
{\displaystyle {\boldsymbol {\widehat {\beta }}}}
) yields
∑
i
=
1
m
X
i
j
y
¯
i
=
∑
i
=
1
m
∑
k
=
1
n
X
i
j
X
¯
i
k
β
^
¯
k
(
j
=
1
,
2
,
3
,
…
,
n
)
.
{\displaystyle \sum _{i=1}^{m}X_{ij}{\overline {y}}_{i}=\sum _{i=1}^{m}\sum _{k=1}^{n}X_{ij}{\overline {X}}_{ik}{\overline {\widehat {\beta }}}_{k}\qquad (j=1,2,3,\ldots ,n).}
In matrix form:
X
T
y
¯
=
X
T
(
X
β
^
)
¯
or
(
X
†
X
)
β
^
=
X
†
y
.
{\displaystyle {\textbf {X}}^{\rm {T}}{\overline {\textbf {y}}}={\textbf {X}}^{\rm {T}}{\overline {{\big (}{\textbf {X}}{\boldsymbol {\widehat {\beta }}}{\big )}}}\quad {\text{ or }}\quad {\big (}{\textbf {X}}^{\dagger }{\textbf {X}}{\big )}{\boldsymbol {\widehat {\beta }}}={\textbf {X}}^{\dagger }{\textbf {y}}.}
Least squares estimator for β
edit
Using matrix notation, the sum of squared residuals is given by
S
(
β
)
=
(
y
−
X
β
)
T
(
y
−
X
β
)
.
{\displaystyle S(\beta )=(y-X\beta )^{T}(y-X\beta ).}
Since this is a quadratic expression, the vector which gives the global minimum may be found via matrix calculus by differentiating with respect to the vector
β
{\displaystyle \beta }
(using denominator layout) and setting equal to zero:
0
=
d
S
d
β
(
β
^
)
=
d
d
β
(
y
T
y
−
β
T
X
T
y
−
y
T
X
β
+
β
T
X
T
X
β
)
|
β
=
β
^
=
−
2
X
T
y
+
2
X
T
X
β
^
{\displaystyle 0={\frac {dS}{d\beta }}({\widehat {\beta }})={\frac {d}{d\beta }}{\bigg (}y^{T}y-\beta ^{T}X^{T}y-y^{T}X\beta +\beta ^{T}X^{T}X\beta {\bigg )}{\bigg |}_{\beta ={\widehat {\beta }}}=-2X^{T}y+2X^{T}X{\widehat {\beta }}}
By assumption matrix X has full column rank, and therefore XT X is invertible and the least squares estimator for β is given by
β
^
=
(
X
T
X
)
−
1
X
T
y
{\displaystyle {\widehat {\beta }}=(X^{T}X)^{-1}X^{T}y}
Unbiasedness and variance of
β
^
{\displaystyle {\widehat {\beta }}}
edit
Plug y = Xβ + ε into the formula for
β
^
{\displaystyle {\widehat {\beta }}}
and then use the law of total expectation :
E
[
β
^
]
=
E
[
(
X
T
X
)
−
1
X
T
(
X
β
+
ε
)
]
=
β
+
E
[
(
X
T
X
)
−
1
X
T
ε
]
=
β
+
E
[
E
[
(
X
T
X
)
−
1
X
T
ε
∣
X
]
]
=
β
+
E
[
(
X
T
X
)
−
1
X
T
E
[
ε
∣
X
]
]
=
β
,
{\displaystyle {\begin{aligned}\operatorname {E} [\,{\widehat {\beta }}]&=\operatorname {E} {\Big [}(X^{T}X)^{-1}X^{T}(X\beta +\varepsilon ){\Big ]}\\&=\beta +\operatorname {E} {\Big [}(X^{T}X)^{-1}X^{T}\varepsilon {\Big ]}\\&=\beta +\operatorname {E} {\Big [}\operatorname {E} {\Big [}(X^{T}X)^{-1}X^{T}\varepsilon \mid X{\Big ]}{\Big ]}\\&=\beta +\operatorname {E} {\Big [}(X^{T}X)^{-1}X^{T}\operatorname {E} [\varepsilon \mid X]{\Big ]}&=\beta ,\end{aligned}}}
where E[ε |X ] = 0 by assumptions of the model. Since the expected value of
β
^
{\displaystyle {\widehat {\beta }}}
equals the parameter it estimates,
β
{\displaystyle \beta }
, it is an unbiased estimator of
β
{\displaystyle \beta }
.
For the variance, let the covariance matrix of
ε
{\displaystyle \varepsilon }
be
E
[
ε
ε
T
]
=
σ
2
I
{\displaystyle \operatorname {E} [\,\varepsilon \varepsilon ^{T}\,]=\sigma ^{2}I}
(where
I
{\displaystyle I}
is the identity
m
×
m
{\displaystyle m\,\times \,m}
matrix), and let X be a known constant.
Then,
E
[
(
β
^
−
β
)
(
β
^
−
β
)
T
]
=
E
[
(
(
X
T
X
)
−
1
X
T
ε
)
(
(
X
T
X
)
−
1
X
T
ε
)
T
]
=
E
[
(
X
T
X
)
−
1
X
T
ε
ε
T
X
(
X
T
X
)
−
1
]
=
(
X
T
X
)
−
1
X
T
E
[
ε
ε
T
]
X
(
X
T
X
)
−
1
=
(
X
T
X
)
−
1
X
T
σ
2
X
(
X
T
X
)
−
1
=
σ
2
(
X
T
X
)
−
1
X
T
X
(
X
T
X
)
−
1
=
σ
2
(
X
T
X
)
−
1
,
{\displaystyle {\begin{aligned}\operatorname {E} [\,({\widehat {\beta }}-\beta )({\widehat {\beta }}-\beta )^{T}]&=\operatorname {E} {\Big [}((X^{T}X)^{-1}X^{T}\varepsilon )((X^{T}X)^{-1}X^{T}\varepsilon )^{T}{\Big ]}\\&=\operatorname {E} {\Big [}(X^{T}X)^{-1}X^{T}\varepsilon \varepsilon ^{T}X(X^{T}X)^{-1}{\Big ]}\\&=(X^{T}X)^{-1}X^{T}\operatorname {E} {\Big [}\varepsilon \varepsilon ^{T}{\Big ]}X(X^{T}X)^{-1}\\&=(X^{T}X)^{-1}X^{T}\sigma ^{2}X(X^{T}X)^{-1}\\&=\sigma ^{2}(X^{T}X)^{-1}X^{T}X(X^{T}X)^{-1}\\&=\sigma ^{2}(X^{T}X)^{-1},\end{aligned}}}
where we used the fact that
β
^
−
β
{\displaystyle {\widehat {\beta }}-\beta }
is just an affine transformation of
ε
{\displaystyle \varepsilon }
by the matrix
(
X
T
X
)
−
1
X
T
{\displaystyle (X^{T}X)^{-1}X^{T}}
.
For a simple linear regression model, where
β
=
[
β
0
,
β
1
]
T
{\displaystyle \beta =[\beta _{0},\beta _{1}]^{T}}
(
β
0
{\displaystyle \beta _{0}}
is the y -intercept and
β
1
{\displaystyle \beta _{1}}
is the slope), one obtains
σ
2
(
X
T
X
)
−
1
=
σ
2
(
(
1
1
⋯
x
1
x
2
⋯
)
(
1
x
1
1
x
2
⋮
⋮
)
)
−
1
=
σ
2
(
∑
i
=
1
m
(
1
x
i
x
i
x
i
2
)
)
−
1
=
σ
2
(
m
∑
x
i
∑
x
i
∑
x
i
2
)
−
1
=
σ
2
⋅
1
m
∑
x
i
2
−
(
∑
x
i
)
2
(
∑
x
i
2
−
∑
x
i
−
∑
x
i
m
)
=
σ
2
⋅
1
m
∑
(
x
i
−
x
¯
)
2
(
∑
x
i
2
−
∑
x
i
−
∑
x
i
m
)
Var
(
β
^
1
)
=
σ
2
∑
i
=
1
m
(
x
i
−
x
¯
)
2
.
{\displaystyle {\begin{aligned}\sigma ^{2}(X^{T}X)^{-1}&=\sigma ^{2}\left({\begin{pmatrix}1&1&\cdots \\x_{1}&x_{2}&\cdots \end{pmatrix}}{\begin{pmatrix}1&x_{1}\\1&x_{2}\\\vdots &\vdots \,\,\,\end{pmatrix}}\right)^{-1}\\[6pt]&=\sigma ^{2}\left(\sum _{i=1}^{m}{\begin{pmatrix}1&x_{i}\\x_{i}&x_{i}^{2}\end{pmatrix}}\right)^{-1}\\[6pt]&=\sigma ^{2}{\begin{pmatrix}m&\sum x_{i}\\\sum x_{i}&\sum x_{i}^{2}\end{pmatrix}}^{-1}\\[6pt]&=\sigma ^{2}\cdot {\frac {1}{m\sum x_{i}^{2}-(\sum x_{i})^{2}}}{\begin{pmatrix}\sum x_{i}^{2}&-\sum x_{i}\\-\sum x_{i}&m\end{pmatrix}}\\[6pt]&=\sigma ^{2}\cdot {\frac {1}{m\sum {(x_{i}-{\bar {x}})^{2}}}}{\begin{pmatrix}\sum x_{i}^{2}&-\sum x_{i}\\-\sum x_{i}&m\end{pmatrix}}\\[8pt]\operatorname {Var} ({\widehat {\beta }}_{1})&={\frac {\sigma ^{2}}{\sum _{i=1}^{m}(x_{i}-{\bar {x}})^{2}}}.\end{aligned}}}
Expected value and biasedness of
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
edit
First we will plug in the expression for y into the estimator, and use the fact that X'M = MX = 0 (matrix M projects onto the space orthogonal to X ):
σ
^
2
=
1
n
y
′
M
y
=
1
n
(
X
β
+
ε
)
′
M
(
X
β
+
ε
)
=
1
n
ε
′
M
ε
{\displaystyle {\widehat {\sigma }}^{\,2}={\tfrac {1}{n}}y'My={\tfrac {1}{n}}(X\beta +\varepsilon )'M(X\beta +\varepsilon )={\tfrac {1}{n}}\varepsilon 'M\varepsilon }
Now we can recognize ε ′Mε as a 1×1 matrix, such matrix is equal to its own trace . This is useful because by properties of trace operator, tr (AB ) = tr (BA ), and we can use this to separate disturbance ε from matrix M which is a function of regressors X :
E
σ
^
2
=
1
n
E
[
tr
(
ε
′
M
ε
)
]
=
1
n
tr
(
E
[
M
ε
ε
′
]
)
{\displaystyle \operatorname {E} \,{\widehat {\sigma }}^{\,2}={\tfrac {1}{n}}\operatorname {E} {\big [}\operatorname {tr} (\varepsilon 'M\varepsilon ){\big ]}={\tfrac {1}{n}}\operatorname {tr} {\big (}\operatorname {E} [M\varepsilon \varepsilon ']{\big )}}
Using the Law of iterated expectation this can be written as
E
σ
^
2
=
1
n
tr
(
E
[
M
E
[
ε
ε
′
|
X
]
]
)
=
1
n
tr
(
E
[
σ
2
M
I
]
)
=
1
n
σ
2
E
[
tr
M
]
{\displaystyle \operatorname {E} \,{\widehat {\sigma }}^{\,2}={\tfrac {1}{n}}\operatorname {tr} {\Big (}\operatorname {E} {\big [}M\,\operatorname {E} [\varepsilon \varepsilon '|X]{\big ]}{\Big )}={\tfrac {1}{n}}\operatorname {tr} {\big (}\operatorname {E} [\sigma ^{2}MI]{\big )}={\tfrac {1}{n}}\sigma ^{2}\operatorname {E} {\big [}\operatorname {tr} \,M{\big ]}}
Recall that M = I − P where P is the projection onto linear space spanned by columns of matrix X . By properties of a projection matrix , it has p = rank(X ) eigenvalues equal to 1, and all other eigenvalues are equal to 0. Trace of a matrix is equal to the sum of its characteristic values, thus tr(P ) = p , and tr(M ) = n − p . Therefore,
E
σ
^
2
=
n
−
p
n
σ
2
{\displaystyle \operatorname {E} \,{\widehat {\sigma }}^{\,2}={\frac {n-p}{n}}\sigma ^{2}}
Since the expected value of
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
does not equal the parameter it estimates,
σ
2
{\displaystyle \sigma ^{\,2}}
, it is a biased estimator of
σ
2
{\displaystyle \sigma ^{\,2}}
. Note in the later section “Maximum likelihood” we show that under the additional assumption that errors are distributed normally, the estimator
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
is proportional to a chi-squared distribution with n – p degrees of freedom, from which the formula for expected value would immediately follow. However the result we have shown in this section is valid regardless of the distribution of the errors, and thus has importance on its own.
Consistency and asymptotic normality of
β
^
{\displaystyle {\widehat {\beta }}}
edit
Estimator
β
^
{\displaystyle {\widehat {\beta }}}
can be written as
β
^
=
(
1
n
X
′
X
)
−
1
1
n
X
′
y
=
β
+
(
1
n
X
′
X
)
−
1
1
n
X
′
ε
=
β
+
(
1
n
∑
i
=
1
n
x
i
x
i
′
)
−
1
(
1
n
∑
i
=
1
n
x
i
ε
i
)
{\displaystyle {\widehat {\beta }}={\big (}{\tfrac {1}{n}}X'X{\big )}^{-1}{\tfrac {1}{n}}X'y=\beta +{\big (}{\tfrac {1}{n}}X'X{\big )}^{-1}{\tfrac {1}{n}}X'\varepsilon =\beta \;+\;{\bigg (}{\frac {1}{n}}\sum _{i=1}^{n}x_{i}x'_{i}{\bigg )}^{\!\!-1}{\bigg (}{\frac {1}{n}}\sum _{i=1}^{n}x_{i}\varepsilon _{i}{\bigg )}}
We can use the law of large numbers to establish that
1
n
∑
i
=
1
n
x
i
x
i
′
→
p
E
[
x
i
x
i
′
]
=
Q
x
x
n
,
1
n
∑
i
=
1
n
x
i
ε
i
→
p
E
[
x
i
ε
i
]
=
0
{\displaystyle {\frac {1}{n}}\sum _{i=1}^{n}x_{i}x'_{i}\ {\xrightarrow {p}}\ \operatorname {E} [x_{i}x_{i}']={\frac {Q_{xx}}{n}},\qquad {\frac {1}{n}}\sum _{i=1}^{n}x_{i}\varepsilon _{i}\ {\xrightarrow {p}}\ \operatorname {E} [x_{i}\varepsilon _{i}]=0}
By Slutsky's theorem and continuous mapping theorem these results can be combined to establish consistency of estimator
β
^
{\displaystyle {\widehat {\beta }}}
:
β
^
→
p
β
+
n
Q
x
x
−
1
⋅
0
=
β
{\displaystyle {\widehat {\beta }}\ {\xrightarrow {p}}\ \beta +nQ_{xx}^{-1}\cdot 0=\beta }
The central limit theorem tells us that
1
n
∑
i
=
1
n
x
i
ε
i
→
d
N
(
0
,
V
)
,
{\displaystyle {\frac {1}{\sqrt {n}}}\sum _{i=1}^{n}x_{i}\varepsilon _{i}\ {\xrightarrow {d}}\ {\mathcal {N}}{\big (}0,\,V{\big )},}
where
V
=
Var
[
x
i
ε
i
]
=
E
[
ε
i
2
x
i
x
i
′
]
=
E
[
E
[
ε
i
2
∣
x
i
]
x
i
x
i
′
]
=
σ
2
Q
x
x
n
{\displaystyle V=\operatorname {Var} [x_{i}\varepsilon _{i}]=\operatorname {E} [\,\varepsilon _{i}^{2}x_{i}x'_{i}\,]=\operatorname {E} {\big [}\,\operatorname {E} [\varepsilon _{i}^{2}\mid x_{i}]\;x_{i}x'_{i}\,{\big ]}=\sigma ^{2}{\frac {Q_{xx}}{n}}}
Applying Slutsky's theorem again we'll have
n
(
β
^
−
β
)
=
(
1
n
∑
i
=
1
n
x
i
x
i
′
)
−
1
(
1
n
∑
i
=
1
n
x
i
ε
i
)
→
d
Q
x
x
−
1
n
⋅
N
(
0
,
σ
2
Q
x
x
n
)
=
N
(
0
,
σ
2
Q
x
x
−
1
n
)
{\displaystyle {\sqrt {n}}({\widehat {\beta }}-\beta )={\bigg (}{\frac {1}{n}}\sum _{i=1}^{n}x_{i}x'_{i}{\bigg )}^{\!\!-1}{\bigg (}{\frac {1}{\sqrt {n}}}\sum _{i=1}^{n}x_{i}\varepsilon _{i}{\bigg )}\ {\xrightarrow {d}}\ Q_{xx}^{-1}n\cdot {\mathcal {N}}{\big (}0,\sigma ^{2}{\frac {Q_{xx}}{n}}{\big )}={\mathcal {N}}{\big (}0,\sigma ^{2}Q_{xx}^{-1}n{\big )}}
Maximum likelihood approach
edit
Maximum likelihood estimation is a generic technique for estimating the unknown parameters in a statistical model by constructing a log-likelihood function corresponding to the joint distribution of the data, then maximizing this function over all possible parameter values. In order to apply this method, we have to make an assumption about the distribution of y given X so that the log-likelihood function can be constructed. The connection of maximum likelihood estimation to OLS arises when this distribution is modeled as a multivariate normal .
Specifically, assume that the errors ε have multivariate normal distribution with mean 0 and variance matrix σ 2 I . Then the distribution of y conditionally on X is
y
∣
X
∼
N
(
X
β
,
σ
2
I
)
{\displaystyle y\mid X\ \sim \ {\mathcal {N}}(X\beta ,\,\sigma ^{2}I)}
and the log-likelihood function of the data will be
L
(
β
,
σ
2
∣
X
)
=
ln
(
1
(
2
π
)
n
/
2
(
σ
2
)
n
/
2
e
−
1
2
(
y
−
X
β
)
′
(
σ
2
I
)
−
1
(
y
−
X
β
)
)
=
−
n
2
ln
2
π
−
n
2
ln
σ
2
−
1
2
σ
2
(
y
−
X
β
)
′
(
y
−
X
β
)
{\displaystyle {\begin{aligned}{\mathcal {L}}(\beta ,\sigma ^{2}\mid X)&=\ln {\bigg (}{\frac {1}{(2\pi )^{n/2}(\sigma ^{2})^{n/2}}}e^{-{\frac {1}{2}}(y-X\beta )'(\sigma ^{2}I)^{-1}(y-X\beta )}{\bigg )}\\[6pt]&=-{\frac {n}{2}}\ln 2\pi -{\frac {n}{2}}\ln \sigma ^{2}-{\frac {1}{2\sigma ^{2}}}(y-X\beta )'(y-X\beta )\end{aligned}}}
Differentiating this expression with respect to β and σ 2 we'll find the ML estimates of these parameters:
∂
L
∂
β
′
=
−
1
2
σ
2
(
−
2
X
′
y
+
2
X
′
X
β
)
=
0
⇒
β
^
=
(
X
′
X
)
−
1
X
′
y
∂
L
∂
σ
2
=
−
n
2
1
σ
2
+
1
2
σ
4
(
y
−
X
β
)
′
(
y
−
X
β
)
=
0
⇒
σ
^
2
=
1
n
(
y
−
X
β
^
)
′
(
y
−
X
β
^
)
=
1
n
S
(
β
^
)
{\displaystyle {\begin{aligned}{\frac {\partial {\mathcal {L}}}{\partial \beta '}}&=-{\frac {1}{2\sigma ^{2}}}{\Big (}-2X'y+2X'X\beta {\Big )}=0\quad \Rightarrow \quad {\widehat {\beta }}=(X'X)^{-1}X'y\\[6pt]{\frac {\partial {\mathcal {L}}}{\partial \sigma ^{2}}}&=-{\frac {n}{2}}{\frac {1}{\sigma ^{2}}}+{\frac {1}{2\sigma ^{4}}}(y-X\beta )'(y-X\beta )=0\quad \Rightarrow \quad {\widehat {\sigma }}^{\,2}={\frac {1}{n}}(y-X{\widehat {\beta }})'(y-X{\widehat {\beta }})={\frac {1}{n}}S({\widehat {\beta }})\end{aligned}}}
We can check that this is indeed a maximum by looking at the Hessian matrix of the log-likelihood function.
Finite-sample distribution
edit
Since we have assumed in this section that the distribution of error terms is known to be normal, it becomes possible to derive the explicit expressions for the distributions of estimators
β
^
{\displaystyle {\widehat {\beta }}}
and
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
:
β
^
=
(
X
′
X
)
−
1
X
′
y
=
(
X
′
X
)
−
1
X
′
(
X
β
+
ε
)
=
β
+
(
X
′
X
)
−
1
X
′
N
(
0
,
σ
2
I
)
{\displaystyle {\widehat {\beta }}=(X'X)^{-1}X'y=(X'X)^{-1}X'(X\beta +\varepsilon )=\beta +(X'X)^{-1}X'{\mathcal {N}}(0,\sigma ^{2}I)}
so that by the affine transformation properties of multivariate normal distribution
β
^
∣
X
∼
N
(
β
,
σ
2
(
X
′
X
)
−
1
)
.
{\displaystyle {\widehat {\beta }}\mid X\ \sim \ {\mathcal {N}}(\beta ,\,\sigma ^{2}(X'X)^{-1}).}
Similarly the distribution of
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
follows from
σ
^
2
=
1
n
(
y
−
X
(
X
′
X
)
−
1
X
′
y
)
′
(
y
−
X
(
X
′
X
)
−
1
X
′
y
)
=
1
n
(
M
y
)
′
M
y
=
1
n
(
X
β
+
ε
)
′
M
(
X
β
+
ε
)
=
1
n
ε
′
M
ε
,
{\displaystyle {\begin{aligned}{\widehat {\sigma }}^{\,2}&={\tfrac {1}{n}}(y-X(X'X)^{-1}X'y)'(y-X(X'X)^{-1}X'y)\\[5pt]&={\tfrac {1}{n}}(My)'My\\[5pt]&={\tfrac {1}{n}}(X\beta +\varepsilon )'M(X\beta +\varepsilon )\\[5pt]&={\tfrac {1}{n}}\varepsilon 'M\varepsilon ,\end{aligned}}}
where
M
=
I
−
X
(
X
′
X
)
−
1
X
′
{\displaystyle M=I-X(X'X)^{-1}X'}
is the symmetric projection matrix onto subspace orthogonal to X , and thus MX = X ′M = 0. We have argued before that this matrix rank n – p , and thus by properties of chi-squared distribution ,
n
σ
2
σ
^
2
∣
X
=
(
ε
/
σ
)
′
M
(
ε
/
σ
)
∼
χ
n
−
p
2
{\displaystyle {\tfrac {n}{\sigma ^{2}}}{\widehat {\sigma }}^{\,2}\mid X=(\varepsilon /\sigma )'M(\varepsilon /\sigma )\ \sim \ \chi _{n-p}^{2}}
Moreover, the estimators
β
^
{\displaystyle {\widehat {\beta }}}
and
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
turn out to be independent (conditional on X ), a fact which is fundamental for construction of the classical t- and F-tests. The independence can be easily seen from following: the estimator
β
^
{\displaystyle {\widehat {\beta }}}
represents coefficients of vector decomposition of
y
^
=
X
β
^
=
P
y
=
X
β
+
P
ε
{\displaystyle {\widehat {y}}=X{\widehat {\beta }}=Py=X\beta +P\varepsilon }
by the basis of columns of X , as such
β
^
{\displaystyle {\widehat {\beta }}}
is a function of Pε . At the same time, the estimator
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
is a norm of vector Mε divided by n , and thus this estimator is a function of Mε . Now, random variables (Pε , Mε ) are jointly normal as a linear transformation of ε , and they are also uncorrelated because PM = 0. By properties of multivariate normal distribution, this means that Pε and Mε are independent, and therefore estimators
β
^
{\displaystyle {\widehat {\beta }}}
and
σ
^
2
{\displaystyle {\widehat {\sigma }}^{\,2}}
will be independent as well.
Derivation of simple linear regression estimators
edit
We look for
α
^
{\displaystyle {\widehat {\alpha }}}
and
β
^
{\displaystyle {\widehat {\beta }}}
that minimize the sum of squared errors (SSE):
min
α
^
,
β
^
SSE
(
α
^
,
β
^
)
≡
min
α
^
,
β
^
∑
i
=
1
n
(
y
i
−
α
^
−
β
^
x
i
)
2
{\displaystyle \min _{{\widehat {\alpha }},{\widehat {\beta }}}\,\operatorname {SSE} \left({\widehat {\alpha }},{\widehat {\beta }}\right)\equiv \min _{{\widehat {\alpha }},{\widehat {\beta }}}\sum _{i=1}^{n}\left(y_{i}-{\widehat {\alpha }}-{\widehat {\beta }}x_{i}\right)^{2}}
To find a minimum take partial derivatives with respect to
α
^
{\displaystyle {\widehat {\alpha }}}
and
β
^
{\displaystyle {\widehat {\beta }}}
∂
∂
α
^
(
SSE
(
α
^
,
β
^
)
)
=
−
2
∑
i
=
1
n
(
y
i
−
α
^
−
β
^
x
i
)
=
0
⇒
∑
i
=
1
n
(
y
i
−
α
^
−
β
^
x
i
)
=
0
⇒
∑
i
=
1
n
y
i
=
∑
i
=
1
n
α
^
+
β
^
∑
i
=
1
n
x
i
⇒
∑
i
=
1
n
y
i
=
n
α
^
+
β
^
∑
i
=
1
n
x
i
⇒
1
n
∑
i
=
1
n
y
i
=
α
^
+
1
n
β
^
∑
i
=
1
n
x
i
⇒
y
¯
=
α
^
+
β
^
x
¯
{\displaystyle {\begin{aligned}&{\frac {\partial }{\partial {\widehat {\alpha }}}}\left(\operatorname {SSE} \left({\widehat {\alpha }},{\widehat {\beta }}\right)\right)=-2\sum _{i=1}^{n}\left(y_{i}-{\widehat {\alpha }}-{\widehat {\beta }}x_{i}\right)=0\\[4pt]\Rightarrow {}&\sum _{i=1}^{n}\left(y_{i}-{\widehat {\alpha }}-{\widehat {\beta }}x_{i}\right)=0\\[4pt]\Rightarrow {}&\sum _{i=1}^{n}y_{i}=\sum _{i=1}^{n}{\widehat {\alpha }}+{\widehat {\beta }}\sum _{i=1}^{n}x_{i}\\[4pt]\Rightarrow {}&\sum _{i=1}^{n}y_{i}=n{\widehat {\alpha }}+{\widehat {\beta }}\sum _{i=1}^{n}x_{i}\\[4pt]\Rightarrow {}&{\frac {1}{n}}\sum _{i=1}^{n}y_{i}={\widehat {\alpha }}+{\frac {1}{n}}{\widehat {\beta }}\sum _{i=1}^{n}x_{i}\\[4pt]\Rightarrow {}&{\bar {y}}={\widehat {\alpha }}+{\widehat {\beta }}{\bar {x}}\end{aligned}}}
Before taking partial derivative with respect to
β
^
{\displaystyle {\widehat {\beta }}}
, substitute the previous result for
α
^
.
{\displaystyle {\widehat {\alpha }}.}
min
α
^
,
β
^
∑
i
=
1
n
[
y
i
−
(
y
¯
−
β
^
x
¯
)
−
β
^
x
i
]
2
=
min
α
^
,
β
^
∑
i
=
1
n
[
(
y
i
−
y
¯
)
−
β
^
(
x
i
−
x
¯
)
]
2
{\displaystyle \min _{{\widehat {\alpha }},{\widehat {\beta }}}\sum _{i=1}^{n}\left[y_{i}-\left({\bar {y}}-{\widehat {\beta }}{\bar {x}}\right)-{\widehat {\beta }}x_{i}\right]^{2}=\min _{{\widehat {\alpha }},{\widehat {\beta }}}\sum _{i=1}^{n}\left[\left(y_{i}-{\bar {y}}\right)-{\widehat {\beta }}\left(x_{i}-{\bar {x}}\right)\right]^{2}}
Now, take the derivative with respect to
β
^
{\displaystyle {\widehat {\beta }}}
:
∂
∂
β
^
(
SSE
(
α
^
,
β
^
)
)
=
−
2
∑
i
=
1
n
[
(
y
i
−
y
¯
)
−
β
^
(
x
i
−
x
¯
)
]
(
x
i
−
x
¯
)
=
0
⇒
∑
i
=
1
n
(
y
i
−
y
¯
)
(
x
i
−
x
¯
)
−
β
^
∑
i
=
1
n
(
x
i
−
x
¯
)
2
=
0
⇒
β
^
=
∑
i
=
1
n
(
y
i
−
y
¯
)
(
x
i
−
x
¯
)
∑
i
=
1
n
(
x
i
−
x
¯
)
2
=
Cov
(
x
,
y
)
Var
(
x
)
{\displaystyle {\begin{aligned}&{\frac {\partial }{\partial {\widehat {\beta }}}}\left(\operatorname {SSE} \left({\widehat {\alpha }},{\widehat {\beta }}\right)\right)=-2\sum _{i=1}^{n}\left[\left(y_{i}-{\bar {y}}\right)-{\widehat {\beta }}\left(x_{i}-{\bar {x}}\right)\right]\left(x_{i}-{\bar {x}}\right)=0\\\Rightarrow {}&\sum _{i=1}^{n}\left(y_{i}-{\bar {y}}\right)\left(x_{i}-{\bar {x}}\right)-{\widehat {\beta }}\sum _{i=1}^{n}\left(x_{i}-{\bar {x}}\right)^{2}=0\\\Rightarrow {}&{\widehat {\beta }}={\frac {\sum _{i=1}^{n}\left(y_{i}-{\bar {y}}\right)\left(x_{i}-{\bar {x}}\right)}{\sum _{i=1}^{n}\left(x_{i}-{\bar {x}}\right)^{2}}}={\frac {\operatorname {Cov} (x,y)}{\operatorname {Var} (x)}}\end{aligned}}}
And finally substitute
β
^
{\displaystyle {\widehat {\beta }}}
to determine
α
^
{\displaystyle {\widehat {\alpha }}}
α
^
=
y
¯
−
β
^
x
¯
{\displaystyle {\widehat {\alpha }}={\bar {y}}-{\widehat {\beta }}{\bar {x}}}