Tutorials › Econometrics (Graduate) › Deriving the OLS Sandwich Covariance

Heteroskedasticity-Robust Inference · Tutorial 103 of 952

Deriving the OLS Sandwich Covariance

See how OLS uncertainty becomes a sandwich, why squared residuals form its empirical meat, and how the covariance scales with sample size.

Intermediate 12 min read

What You'll Learn

  • Write the finite-sample decomposition of the OLS estimation error.
  • Derive its conditional covariance using the conditional covariance matrix of the errors.
  • Identify the bread, variance-weighted meat, and residual-based HC0 meat.
  • Explain the conditions needed for the asymptotic sandwich covariance to be defined.
  • Calculate an HC0 covariance estimate from a small hypothetical regression.
  • Distinguish heteroskedasticity robustness from robustness to dependence between observations.

Why OLS Needs a Different Covariance Formula

OLS coefficients can remain useful when error variance changes across observations, but the familiar homoskedastic covariance formula no longer describes their sampling uncertainty. The key is to start with the exact finite-sample error in the OLS estimator. That decomposition reveals which part of the covariance depends on the design and which part depends on the errors’ conditional variances.

The resulting formula has a recognizable structure: a matrix determined by the design on each side, and a variance-weighted matrix in the middle. Since the conditional variances are unknown, the usual heteroskedasticity-robust OLS covariance estimator replaces that middle matrix with one built from squared OLS residuals. This tutorial derives that structure and its scaling; it does not require equal error variances.

Model and Finite-Sample Assumptions

Let \(y\) be an \(n\times 1\) outcome vector, \(X\) an \(n\times k\) design matrix, and \(\beta\) a \(k\times 1\) coefficient vector. The \(i\)th row of \(X\) is \(x_i'\), where \(x_i\) is a \(k\times 1\) vector of regressors. Write the linear model as \(y=X\beta+u\), where \(u\) is the \(n\times 1\) error vector.

Assumptions for the finite-sample covariance derivation: The columns of \(X\) are linearly independent, so \(X'X\) is invertible. Conditional on \(X\), the errors have mean zero, \(E[u\mid X]=0\), and conditional covariance matrix \(\operatorname{Var}(u\mid X)=\Omega\). Heteroskedasticity is allowed: the diagonal elements of \(\Omega\) need not be equal. The standard observation-by-observation robust formula additionally assumes that errors are conditionally uncorrelated across observations, so \(\Omega\) is diagonal.

These are assumptions about the data-generating process, not facts established by calculating a covariance estimator. In particular, a residual plot or a sample calculation cannot prove that the conditional mean is correctly specified or that observations are conditionally uncorrelated.

Under full column rank, the OLS estimator and its residual vector are

$$ \hat{\beta}=(X'X)^{-1}X'y, \qquad \hat{u}=y-X\hat{\beta}. $$

Conditional on \(X\), the design matrix is treated as fixed. Substituting \(y=X\beta+u\) into the estimator gives its finite-sample error exactly.

$$ \hat{\beta}-\beta =(X'X)^{-1}X'(X\beta+u)-\beta =(X'X)^{-1}X'u. $$

This is the starting point for the sandwich formula: OLS estimation error is the error vector transformed by \((X'X)^{-1}X'\).

Deriving the Finite-Sample Sandwich

Because \(E[u\mid X]=0\), the conditional mean of \(\hat{\beta}-\beta\) is zero. For any fixed matrix \(A\), the covariance of \(Au\), conditional on \(X\), is \(A\Omega A'\). Apply this rule with \(A=(X'X)^{-1}X'\). The matrix \(X'X\) is symmetric, so its inverse is symmetric as well.

$$ \begin{aligned} \operatorname{Var}(\hat{\beta}\mid X) &=\operatorname{Var}\big((X'X)^{-1}X'u\mid X\big)\\ &=(X'X)^{-1}X'\Omega X(X'X)^{-1}. \end{aligned} $$

The two copies of \((X'X)^{-1}\) are the bread. The middle matrix, \(X'\Omega X\), is the variance-weighted meat. These names describe the arrangement of the matrices; the derivation above, rather than the names, establishes the formula.

If errors are conditionally uncorrelated across observations, write \(\Omega=\operatorname{diag}(\sigma_1^2,\ldots,\sigma_n^2)\), where \(\sigma_i^2=\operatorname{Var}(u_i\mid X)\). Since \(X'\) has columns \(x_i\) in the corresponding rows of \(X\), the meat can then be written as a sum:

$$ X'\Omega X=\sum_{i=1}^{n}\sigma_i^2 x_i x_i'. $$

Thus heteroskedasticity changes the contribution each observation makes to the meat. If errors are conditionally correlated across observations, the full expression \(X'\Omega X\) still applies, but it contains cross-observation terms. The sum of individual \(x_i x_i'\) contributions by itself does not account for those terms.

From Unknown Variances to the HC0 Estimate

In practice, the individual conditional variances \(\sigma_i^2\) are unknown. A direct substitution of the OLS residual square \(\hat{u}_i^2\) for each \(\sigma_i^2\) gives the usual HC0 covariance estimator:

Definition: Under the observation-level, conditionally uncorrelated error setup, the HC0 heteroskedasticity-robust OLS covariance estimator is
$$ \widehat{\operatorname{Var}}_{\mathrm{HC0}}(\hat{\beta}) =(X'X)^{-1} \left(\sum_{i=1}^{n}\hat{u}_i^{\,2}x_i x_i'\right) (X'X)^{-1}. $$
The residual-based middle matrix is called the HC0 meat. The formula estimates the covariance; it is not generally the exact finite-sample covariance, because residual squares are not the unknown conditional variances.

The name HC0 identifies the unadjusted residual-square version of the estimator. This derivation neither adds a small-sample correction nor assumes that one is needed. It also does not make the estimator robust to arbitrary dependence between observations: the displayed residual meat is designed for heteroskedasticity with conditional lack of cross-observation correlation.

Worked Example: An Intercept-Only HC0 Calculation

Suppose a hypothetical sample has three outcomes, \(2,4,8\), and the regression contains only an intercept. Assume the intercept-only conditional mean is correctly specified, \(E[u\mid X]=0\), and errors are conditionally uncorrelated; their variances may differ. Here each \(x_i=1\), and \(X'X=3\).

The fitted intercept is the sample mean, \(\hat{\beta}=14/3\). The three residuals are \(-8/3,-2/3,10/3\). The HC0 meat and covariance estimate are therefore

$$ \begin{aligned} \sum_{i=1}^{3}\hat{u}_i^{\,2}x_i x_i' &=\left(-\frac{8}{3}\right)^2 +\left(-\frac{2}{3}\right)^2 +\left(\frac{10}{3}\right)^2\\ &=\frac{64+4+100}{9} =\frac{56}{3},\\[4pt] \widehat{\operatorname{Var}}_{\mathrm{HC0}}(\hat{\beta}) &=\frac{1}{3}\left(\frac{56}{3}\right)\frac{1}{3} =\frac{56}{27} \approx 2.074. \end{aligned} $$

The estimated variance of the intercept is \(2.074\) in squared outcome units. The computation uses residual squares as variance proxies; it does not show that those squares equal the three true conditional variances.

Worked Example: Building the Matrix Meat in a Two-Regressor Model

Consider hypothetical observations \((x_i,y_i)=(0,1),(1,2),(2,4)\), with a constant and one slope regressor. Assume the linear conditional mean, zero conditional error mean, and conditional lack of correlation across observations. Variances may differ. In the order constant, slope, the rows of \(X\) are \((1,0),(1,1),(1,2)\).

The OLS estimates are \(\hat{\beta}_0=5/6\) and \(\hat{\beta}_1=3/2\), giving fitted values \(5/6,7/3,23/6\). The residuals are \(1/6,-1/3,1/6\). First calculate the bread:

$$ X'X= \begin{pmatrix} 3&3\\ 3&5 \end{pmatrix}, \qquad (X'X)^{-1} =\frac{1}{6} \begin{pmatrix} 5&-3\\ -3&3 \end{pmatrix}. $$

For each observation, the contribution to the meat is \(\hat{u}_i^2x_ix_i'\). Adding the three contributions gives

$$ \begin{aligned} \sum_{i=1}^{3}\hat{u}_i^{\,2}x_ix_i' &=\frac{1}{36} \begin{pmatrix}1&0\\0&0\end{pmatrix} +\frac{1}{9} \begin{pmatrix}1&1\\1&1\end{pmatrix} +\frac{1}{36} \begin{pmatrix}1&2\\2&4\end{pmatrix}\\ &= \begin{pmatrix} 1/6&1/6\\ 1/6&2/9 \end{pmatrix}. \end{aligned} $$

Premultiplying and postmultiplying by the bread yields

$$ \widehat{\operatorname{Var}}_{\mathrm{HC0}}(\hat{\beta}) = \begin{pmatrix} 5/6&-1/2\\ -1/2&1/2 \end{pmatrix} \begin{pmatrix} 1/6&1/6\\ 1/6&2/9 \end{pmatrix} \begin{pmatrix} 5/6&-1/2\\ -1/2&1/2 \end{pmatrix} = \begin{pmatrix} 7/216&-1/72\\ -1/72&1/72 \end{pmatrix}. $$

The diagonal entries estimate the variances of the intercept and slope, respectively; the off-diagonal entries estimate their covariance. Since the outcome and slope regressor in this hypothetical example have specified units, these entries have the corresponding squared or product units. The matrix is symmetric, as a covariance matrix must be.

Asymptotic Scaling and the Nonsingularity Condition

The finite-sample formula also reveals how uncertainty scales with \(n\). Define the normalized design matrix \(Q_n=n^{-1}X'X\) and the normalized variance-weighted design matrix \(B_n=n^{-1}X'\Omega X\). Whenever \(Q_n\) is invertible, substituting \(X'X=nQ_n\) and \(X'\Omega X=nB_n\) into the exact covariance formula gives

$$ \operatorname{Var}(\hat{\beta}\mid X) =\frac{1}{n}Q_n^{-1}B_nQ_n^{-1}. $$

This identity explains the \(1/n\) covariance scale when the normalized matrices remain well behaved. To state a limiting covariance, however, convergence of the matrices alone is not enough: the limit of the normalized design must be invertible.

Asymptotic covariance scaling: If \(Q_n\to Q\), \(B_n\to B\), and \(Q\) is positive definite, then
$$ n\,\operatorname{Var}(\hat{\beta}\mid X) =Q_n^{-1}B_nQ_n^{-1} \longrightarrow Q^{-1}BQ^{-1}. $$
For random designs, the convergence can be understood in probability. Positive definiteness of \(Q\) ensures that \(Q^{-1}\) exists and that the inverses \(Q_n^{-1}\) approach it under the stated convergence.

The displayed limit is the limit of the scaled conditional covariance. Interpreting it as the covariance in a limiting distribution for \(\sqrt{n}(\hat{\beta}-\beta)\) also requires an appropriate central limit theorem. The covariance algebra by itself does not establish that distributional result.

Worked Example: Checking the Limiting Design Condition

Suppose a hypothetical intercept-only design has \(x_i=1\) for every observation, and the average conditional error variance is \(4\) at each sample size. Then \(Q_n=n^{-1}\sum_i 1=1\) and \(B_n=n^{-1}\sum_i\sigma_i^2=4\). Thus \(Q=1\) is positive definite and \(B=4\), so

$$ n\,\operatorname{Var}(\hat{\beta}\mid X) =Q_n^{-1}B_nQ_n^{-1} =1^{-1}(4)1^{-1} =4. $$

At \(n=100\), the conditional variance is \(4/100=0.04\), and the conditional standard deviation is \(\sqrt{0.04}=0.2\) outcome units. The limiting covariance calculation is defined because \(Q\) is nonsingular. If instead the normalized design converged to a singular matrix, the expression \(Q^{-1}BQ^{-1}\) would not be defined, even if every finite-sample \(X'X\) happened to be invertible.

Common Mistakes and Why the Derivation Matters

  • Using the homoskedastic formula by default. The expression proportional to \((X'X)^{-1}\) follows only under a common conditional error variance. With varying variances, the middle matrix must reflect observation-specific contributions.
  • Calling the residual meat the true variance matrix. The true finite-sample meat is \(X'\Omega X\). HC0 substitutes residual squares because the conditional variances are unknown; the substitution makes it an estimator.
  • Ignoring the required limiting rank condition. Finite-sample full column rank does not guarantee that \(Q_n\) converges to a nonsingular \(Q\). The inverse in the limiting covariance requires \(Q\) to be positive definite.
  • Treating heteroskedasticity robustness as dependence robustness. The observation-level HC0 meat does not include cross-observation covariance terms. Conditional correlation requires a covariance formula that accounts for the relevant dependence structure.

The sandwich derivation matters because it separates two issues: how the regressors transmit error variation into coefficient variation, and how much error variation the data-generating process supplies. That separation makes the formula interpretable and clarifies which assumptions are needed before using it.

Check Your Understanding

Answer each question by referring to the finite-sample decomposition or its assumptions.

  1. Starting from \(y=X\beta+u\), write \(\hat{\beta}-\beta\) in terms of \(X\) and \(u\).
  2. Which matrix is the meat in the exact conditional covariance formula, and what does it become when \(\Omega\) is diagonal?
  3. Why does HC0 use \(\hat{u}_i^2\), and why is that substitution not an identity for the true finite-sample covariance?
  4. What condition on the limit \(Q\) is needed to define \(Q^{-1}BQ^{-1}\)?
  5. Which assumption behind the usual observation-level HC0 meat would fail if errors were conditionally correlated across observations?
Clinical Biostats