University

Econometrics basics · technical

The OLS estimator,
derived

Free study notes from Econometrics basics (first-year foundations).

With several explanatory variables, each OLS coefficient is no longer the sample covariance of its regressor and y over that regressor's sample variance, so we need a formula that handles all the regressors at once. Writing the data as a vector and a matrix gives it: \(\hat{\boldsymbol\beta}=(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{y}\). These notes derive it from the least-squares problem, show that it gives the minimum, recover the one-regressor slope from it, and prove that OLS is unbiased and find its variance, naming the assumption each step uses. If you want the one-variable version first, start with the plain-English guide to OLS.

The model in matrix form

Write the linear model for \(n\) observations and \(k\) regressors (including the constant) stacked into vectors and matrices:

The linear model

$$\mathbf{y}=\mathbf{X}\boldsymbol\beta+\mathbf{u},$$

where \(\mathbf{y}\) is \(n\times 1\), \(\mathbf{X}\) is \(n\times k\) (each row an observation, its first column a column of ones when the model has a constant), \(\boldsymbol\beta\) is \(k\times 1\), and \(\mathbf{u}\) is the \(n\times 1\) vector of unobserved errors.

The least-squares problem and the normal equations

For a trial vector \(\mathbf b\) of coefficients, the gaps between the data and the trial fitted values \(\mathbf X\mathbf b\) are \(\mathbf y-\mathbf X\mathbf b\), and OLS chooses the \(\mathbf b\) that makes the sum of their squares as small as possible. As on the plain-English guide, \(\boldsymbol\beta\) is the population vector, which is not ours to vary, \(\mathbf b\) is a trial value and \(\hat{\boldsymbol\beta}\) is the minimiser. Since \(\mathbf y'\mathbf X\mathbf b\) is a scalar, it equals its transpose \(\mathbf b'\mathbf X'\mathbf y\), so

$$\begin{aligned}S(\mathbf b)&=(\mathbf{y}-\mathbf{X}\mathbf b)'(\mathbf{y}-\mathbf{X}\mathbf b)\\ &=\mathbf{y}'\mathbf{y}-2\mathbf b'\mathbf{X}'\mathbf{y}+\mathbf b'\mathbf{X}'\mathbf{X}\mathbf b.\end{aligned}$$

Differentiate with respect to the vector \(\mathbf b\) and set the gradient to zero. Using \(\partial(\mathbf{a}'\mathbf b)/\partial\mathbf b=\mathbf{a}\) and \(\partial(\mathbf b'\mathbf{A}\mathbf b)/\partial\mathbf b=2\mathbf{A}\mathbf b\) for symmetric \(\mathbf{A}\), with \(\mathbf X'\mathbf X\) symmetric:

First-order conditions (normal equations)

$$\frac{\partial S}{\partial\mathbf b}=-2\mathbf{X}'\mathbf{y}+2\mathbf{X}'\mathbf{X}\mathbf b ,$$

which is zero at \(\mathbf b=\hat{\boldsymbol\beta}\):

$$\mathbf{X}'\mathbf{X}\,\hat{\boldsymbol\beta}=\mathbf{X}'\mathbf{y}.$$

Provided \(\mathbf X\) has full column rank \(k\) (no column, the constant included, an exact linear combination of the others), \(\mathbf{X}'\mathbf{X}\) is invertible; pre-multiply by its inverse:

$$\boxed{\ \hat{\boldsymbol\beta}=(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{y}.\ }$$

This is the minimum, and the only one. The matrix of second derivatives is \(2\mathbf X'\mathbf X\). For any \(\mathbf c\neq\mathbf 0\), \(\mathbf c'\mathbf X'\mathbf X\mathbf c=(\mathbf X\mathbf c)'(\mathbf X\mathbf c)\) is a sum of squares, and it is positive because full column rank makes \(\mathbf X\mathbf c\neq\mathbf 0\). So \(S\) is a bowl (strictly convex) and \(\hat{\boldsymbol\beta}\) is its lowest point. At \(\mathbf b=\hat{\boldsymbol\beta}\) the gaps are the residuals \(\hat{\mathbf u}=\mathbf y-\mathbf X\hat{\boldsymbol\beta}\), and \(S(\hat{\boldsymbol\beta})=\hat{\mathbf u}'\hat{\mathbf u}\); the errors \(\mathbf u\) are never observed.

Intuition

The normal equations say \(\mathbf{X}'\hat{\mathbf u}=\mathbf 0\): the residual vector is orthogonal to every regressor. Geometrically, \(S(\mathbf b)\) is the squared distance between \(\mathbf y\) and the point \(\mathbf X\mathbf b\), which can be anywhere in the column space of \(\mathbf X\). The closest such point, \(\hat{\mathbf{y}}=\mathbf{X}\hat{\boldsymbol\beta}\), is the one whose residual is perpendicular to that space: OLS projects \(\mathbf{y}\) onto the column space of \(\mathbf{X}\).

What students say

“Nicky is an excellent econometrics tutor. He explains complex concepts clearly and makes them much easier to understand.”
Charles GardnerGoogle review
“I was completely lost in my economics class, and then I started working Nicky and he totally saved me. He was excellent at explaining the material and extremely accommodating to meet with me many times despite us being in different time zones! Highly recommend working with Nicky!”
Claire ChouGoogle review
“Nicky is a very good and thorough teacher. He has excellent knowledge of econometrics and takes the time to work through your questions. Highly recommended!”
ChrisEconometrics · 22/08
“Really great tutor! Encouraging, knowledgeable, friendly”
JedEconometrics · 09/02
“Very good lesson, Nicky is very helpful.”
ChrisEconometrics · 19/08
“Very helpful and clear.”
ClaireEconometrics · 22/01
“I gather it was very useful, thank you!”
ClaireEconometrics · 12/01
“Fantastic!”
JedEconometrics · 11/02

Recovering the one-regressor formula

With a single regressor \(x\) plus a constant, each row of \(\mathbf X\) is \((1, x_i)\), so

$$\mathbf X'\mathbf X=\begin{pmatrix}n&\sum x_i\\ \sum x_i&\sum x_i^2\end{pmatrix},$$

$$\mathbf X'\mathbf y=\begin{pmatrix}\sum y_i\\ \sum x_iy_i\end{pmatrix}.$$

The determinant of \(\mathbf X'\mathbf X\) is

$$n\sum x_i^2-\Bigl(\sum x_i\Bigr)^2=n\sum(x_i-\bar x)^2,$$

and the second entry of \((\mathbf X'\mathbf X)^{-1}\mathbf X'\mathbf y\) is

$$\frac{n\sum x_iy_i-\sum x_i\sum y_i}{n\sum(x_i-\bar x)^2}.$$

Since

$$\begin{aligned}&n\sum x_iy_i-\sum x_i\sum y_i\\ &\quad=n\sum(x_i-\bar x)(y_i-\bar y),\end{aligned}$$

the \(n\) cancels and this gives

$$\hat\beta_1=\frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sum_i (x_i-\bar x)^2}=\frac{\widehat{\operatorname{Cov}}(x,y)}{\widehat{\operatorname{Var}}(x)},$$

with \(\hat\beta_0=\bar y-\hat\beta_1\bar x\) from the first entry.

So the slope is the sample covariance of \(x\) and \(y\) over the sample variance of \(x\); the matrix form handles many regressors at once. The same formula follows from the two scalar first-order conditions, worked line by line in the plain-English guide to OLS.

Worked example: five workers

The plain-English guide's five workers have \(x = 0, 1, 2, 3, 4\) (years of education after 16) and \(y = 10, 12, 13, 16, 19\) (hourly pay in pounds). Then \(n=5\), \(\sum x_i=10\), \(\sum x_i^2=30\), \(\sum y_i=70\) and \(\sum x_iy_i=162\), so

$$\mathbf X'\mathbf X=\begin{pmatrix}5&10\\ 10&30\end{pmatrix},$$

$$\mathbf X'\mathbf y=\begin{pmatrix}70\\ 162\end{pmatrix},$$

$$(\mathbf X'\mathbf X)^{-1}=\frac{1}{50}\begin{pmatrix}30&-10\\ -10&5\end{pmatrix},$$

$$\begin{aligned}\hat{\boldsymbol\beta}&=\frac{1}{50}\begin{pmatrix}30\times 70-10\times 162\\ -10\times 70+5\times 162\end{pmatrix}\\ &=\frac{1}{50}\begin{pmatrix}480\\ 110\end{pmatrix}=\begin{pmatrix}9.6\\ 2.2\end{pmatrix}.\end{aligned}$$

The determinant, 50, is \(n\sum(x_i-\bar x)^2=5\times 10\). The residuals are 0.4, 0.2, -1.0, -0.2 and 0.6. They sum to zero, the first row of \(\mathbf X'\hat{\mathbf u}=\mathbf 0\). The second row is

$$\begin{aligned}&0(0.4)+1(0.2)+2(-1.0)\\ &\quad+3(-0.2)+4(0.6)=0:\end{aligned}$$

the residuals are orthogonal to \(x\), as the normal equations require.

Is OLS unbiased?

Substitute the population model \(\mathbf{y}=\mathbf{X}\boldsymbol\beta+\mathbf{u}\) into the estimator:

$$\begin{aligned}\hat{\boldsymbol\beta}&=(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'(\mathbf{X}\boldsymbol\beta+\mathbf{u})\\ &=\boldsymbol\beta+(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{u}.\end{aligned}$$

Unbiasedness

Taking expectations conditional on \(\mathbf{X}\), using that \((\mathbf X'\mathbf X)^{-1}\mathbf X'\) is fixed once \(\mathbf X\) is known, and the exogeneity assumption \(\mathbb{E}[\mathbf{u}\mid\mathbf{X}]=\mathbf 0\) (under random sampling this follows from \(\mathbb E[u_i\mid\mathbf x_i]=0\) for each observation, with \(\mathbf x_i\) row \(i\) of \(\mathbf X\), the form used on the plain-English guide; in time series it is the stronger condition of strict exogeneity),

$$\begin{aligned}\mathbb{E}[\hat{\boldsymbol\beta}\mid\mathbf{X}]&=\boldsymbol\beta+(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\,\mathbb{E}[\mathbf{u}\mid\mathbf{X}]\\ &=\boldsymbol\beta.\end{aligned}$$

The result rests on \(\mathbb{E}[\mathbf{u}\mid\mathbf{X}]=\mathbf 0\), with the linear model and full-rank \(\mathbf X\). If it fails, for example through an omitted variable correlated with a regressor, reverse causality or measurement error in a regressor, the conditional mean of the second term, \((\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\,\mathbb{E}[\mathbf{u}\mid\mathbf{X}]\), is in general not zero and OLS is biased. Instrumental variables are one response.

The variance of the estimator

Add the assumption that the errors have the same variance and are uncorrelated across observations (homoskedasticity and no serial correlation), \(\operatorname{Var}(\mathbf{u}\mid\mathbf{X})=\sigma^2\mathbf{I}_n\). Since \(\hat{\boldsymbol\beta}-\boldsymbol\beta=(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{u}\), and \(\operatorname{Var}(\mathbf A\mathbf u\mid\mathbf X)=\mathbf A\operatorname{Var}(\mathbf u\mid\mathbf X)\mathbf A'\) for any \(\mathbf A\) fixed once \(\mathbf X\) is known,

Variance-covariance matrix

$$\begin{aligned}\operatorname{Var}(\hat{\boldsymbol\beta}\mid\mathbf{X})&=(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\,(\sigma^2\mathbf{I})\\ &\qquad\times\mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\\ &=\sigma^2(\mathbf{X}'\mathbf{X})^{-1}.\end{aligned}$$

\(\sigma^2\) is unknown. Software replaces it with \(s^2=\hat{\mathbf u}'\hat{\mathbf u}/(n-k)\), and the square roots of the diagonal entries of \(s^2(\mathbf X'\mathbf X)^{-1}\) are the standard errors reported next to each coefficient. They shrink as the sample grows (\(\mathbf{X}'\mathbf{X}\) grows with \(n\)) and as the regressors vary more: more spread in \(x\) means a more precisely estimated slope. When the constant-variance assumption fails, this formula is wrong and we use heteroskedasticity-robust (White) standard errors instead; the point estimates are unchanged.

Gauss-Markov: OLS is BLUE

Gauss-Markov theorem

Under linearity, \(\mathbb{E}[\mathbf{u}\mid\mathbf{X}]=\mathbf 0\), \(\operatorname{Var}(\mathbf{u}\mid\mathbf{X})=\sigma^2\mathbf{I}\), and full-rank \(\mathbf{X}\), OLS is the Best Linear Unbiased Estimator: among all estimators that are linear in \(\mathbf{y}\) and unbiased, it has the smallest variance-covariance matrix. Adding normal errors makes OLS coincide with maximum likelihood and efficient among all unbiased estimators, and justifies the exact \(t\) and \(F\) distributions used for inference.

Check your understanding

Which single assumption is doing the work in the unbiasedness proof, and what breaks if it fails?

\(\mathbb{E}[\mathbf{u}\mid\mathbf{X}]=\mathbf 0\) (exogeneity), with the linear model and full-rank \(\mathbf X\). If it fails, \(\mathbb{E}[(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{u}\mid\mathbf X]\) is in general not zero, so \(\hat{\boldsymbol\beta}\) is biased. If the regressors are also correlated with the error in the population, as with an omitted variable correlated with a regressor, OLS is inconsistent too, and that is the case IV/2SLS is designed for.

Why do standard errors shrink when the regressor \(x\) is more spread out?

With one regressor and a constant, the slope's variance, the second diagonal entry of \(\sigma^2(\mathbf X'\mathbf X)^{-1}\), is \(\sigma^2/\sum(x_i-\bar x)^2\). A wider spread of \(x\) about its mean makes the denominator larger and the variance smaller: the slope is pinned down more precisely when the data cover a wider range of \(x\).

What extra assumption turns the Gauss-Markov "smallest variance among linear unbiased" into exact \(t\)-tests?

Normally distributed errors, \(\mathbf u\mid\mathbf X\sim N(\mathbf 0,\sigma^2\mathbf I)\). Then \(\hat{\boldsymbol\beta}\) is exactly normal and the studentised coefficients, using \(s\), follow a \(t\) distribution with \(n-k\) degrees of freedom in finite samples.

Learning this for a
course or exam?

I tutor statistics and econometrics from first principles through to postgraduate level, with worked examples at your pace. The first consultation is free.

Book a consultation How sessions work & pricing
Free 30-min consultationNo obligation · reply within one business day
Book now