MIT 18.650 — Lecture 24

Linear Regression II

§ Variance of the LS Solution

Here's an alternative way of presenting the assumptions we made about the relationship between feature vectors XX and response variables YY from Lecture 23.

Assumption. For some true unknown β\beta^*, we have Yi=Xiβ+ϵiY_i = X_i^{\top}\beta^* + \epsilon_i, where ϵiN(0,σ2)\epsilon_i \sim \mathcal{N}(0, \sigma^2) for some (unknown) constant σ\sigma.

Under this model, the maximum-likelihood estimator is β^MLE=β^LS=(XX)1XY\hat{\beta}^{\mathrm{MLE}} = \hat{\beta}^{\mathrm{LS}} = (\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}Y. But what's the variance of this estimator? How far does it tend to be off from the true value β\beta^*?

Remark. Recall that the estimator β^LS\hat{\beta}^{\mathrm{LS}} is still an MLE, so all the properties of MLE—consistency, asymptotic normality—still hold. (But finding the asymptotic variance isn't as straightforward as computing the Fisher information.)

Theorem. (LS Variance) We have β^LSNk(β,σ2(XX)1)\hat{\beta}^{\mathrm{LS}} \sim \mathcal{N}_k(\beta^*, \sigma^2(\mathbb{X}^{\top}\mathbb{X})^{-1}).

Proof: More generally, it is true that the vector YRnY \in \mathbb{R}^n satisfies Y=Xβ+ϵY = \mathbb{X}\beta^* + \epsilon, where ϵNn(0,σ2In)\epsilon \sim \mathcal{N}_n(0, \sigma^2 I_n). Thus,

β^LS=(XX)1X(Xβ+ϵ)=β+(XX)1Xϵ.\hat{\beta}^{\mathrm{LS}} = (\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}( \mathbb{X}\beta^* + \epsilon ) = \beta^* + (\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}\epsilon.

The rest of the proof is just multivariate Gaussian properties; recall Lecture 6, or more fundamentally, the properties of covariance matrices from Lecture 5.

The mean of the RHS is clearly β\beta^*, whereas the covariance is:

Cov([(XX)1X]ϵ)=[(XX)1X]Cov(ϵ)[(XX)1X]=σ2(XX)1XX(XX)1=σ2(XX)1.\begin{align*}\Cov\left( \left[(\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}\right] \epsilon \right) & = \left[(\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}\right] \Cov(\epsilon) \left[(\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}\right]^{\top} \\ & = \sigma^2 \cdot (\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top} \mathbb{X}(\mathbb{X}^{\top}\mathbb{X})^{-1} \\ & = \sigma^2 (\mathbb{X}^{\top}\mathbb{X})^{-1}.\end{align*}

Yeah, the computations look terrible, but at least it matches what the theorem promised.   \blacksquare

§ Linear Regression Confidence Intervals

We know the variance now, so surely computing confidence intervals and performing tests should be easy, right?

There's still a problem: we don't know σ\sigma, so the variance being σ2(XX)1\sigma^2(\mathbb{X}^{\top}\mathbb{X})^{-1} still doesn't tell us anything yet. But—in a similar fashion as bootstrapping—there's a trick to use the training data to estimate σ\sigma.

Theorem. (Estimating Regression Variance) Given the training data and β^LS\hat{\beta}^{\mathrm{LS}}, we may compute the vector ϵ^:=YXβ^LS\hat{\epsilon} := Y - \mathbb{X}\hat{\beta}^{\mathrm{LS}}. Then the estimator σ^2:=1nkϵ^2\hat{\sigma}^2 := \frac{1}{n - k} \|\hat{\epsilon}\|^2 is an unbiased estimator for σ2\sigma^2.

Remark. This is a very technically dense proof, but the fundamental ideas are relatively straightforward.

The above is an image outlining the ideas of the proof for n=5n = 5 and k=2k = 2.

That's why we might expect E[ϵ^2]=3σ2\mathbb{E}\left[\|\hat{\epsilon}\|^2\right] = 3\sigma^2; in general, the coefficient of 33 would be nkn - k. The technical complexity of the proof comes from having to formalize the notion of “directions of noise”.

Proof: The first step is to rewrite ϵ^\hat{\epsilon} as the “orthogonal component” of the overall Gaussian noise ϵ\epsilon.

Lemma 1. Let HH denote the RnRn\mathbb{R}^n \to \mathbb{R}^n map given by projecting vectors onto the column space of X\mathbb{X}. Then ϵ^=(InH)ϵ\hat{\epsilon} = (I_n - H)\epsilon.

Proof: Recalling from Lecture 23 that HY=Xβ^LSHY = \mathbb{X}\hat{\beta}^{\mathrm{LS}}, we can rewrite the definition of ϵ^\hat{\epsilon} like so:

ϵ^=YXβ^LS=(InH)Y=(InH)(Xβ+ϵ)=(InH)(Xβ)+(InH)ϵ.\begin{align*}\hat{\epsilon} = Y - \mathbb{X}\hat{\beta}^{\mathrm{LS}} & = (I_n - H)Y \\ & = (I_n - H)(\mathbb{X}\beta^* + \epsilon) \\ & = (I_n - H)(\mathbb{X}\beta^*) + (I_n - H)\epsilon.\end{align*}

However, (InH)(Xβ)=0(I_n - H)(\mathbb{X}\beta^*) = 0 because Xβ\mathbb{X}\beta^* already lies on the column space of X\mathbb{X}—that is, because H(Xβ)=XβH(\mathbb{X}\beta^*) = \mathbb{X}\beta^*. Then all that remains is the ϵ^=(InH)ϵ\hat{\epsilon} = (I_n - H)\epsilon term, as promised.   \square

Define the matrix M:=InHM := I_n - H; think of MM not as a projection map, but as an “altitude” map. Then we seek to show that E[Mϵ2]=(nk)σ2\mathbb{E}\left[\left\|M\epsilon\right\|^2\right] = (n - k) \sigma^2, where ϵNn(0,σ2In)\epsilon \sim \mathcal{N}_n(0, \sigma^2 I_n).

To proceed, we'll need some deeper linear algebra tricks; refer to 18.701 Notes for reference.

Lemma 2. The matrix MM diagonalizes as M=UΛUM = U\Lambda U^{\top}, where:

  • The matrix UU is orthogonal.

  • The matrix Λ\Lambda is diagonal with kk zeroes and nkn - k ones.

Proof: Note that MM is a real symmetric matrix, so the Spectral theorem applies. This, alone, already guarantees us that MM is expressible in the form M=UΛUM = U \Lambda U^{\top} with UU orthogonal and Λ\Lambda diagonal.

It remains to study Λ\Lambda, or equivalently, to study the spectrum of MM. Recall that for the kk-dimensional subspace W:=colspan(X)W := \mathrm{colspan}(\mathbb{X}) of Rn\mathbb{R}^n, the map MM takes vectors in Rn\mathbb{R}^n to their “altitudes” onto WW. Therefore,

  • Every vector along WW has eigenvalue zero.

  • Every vector along WW^{\perp} (the (nk)(n - k)-dimensional subspace of vectors orthogonal to WW) has eigenvalue one.

This implies the spectrum of MM contains kk zeroes and (nk)(n - k) ones, proving the lemma's promise about Λ\Lambda.   \square

Denote η:=Uϵ\eta := U^{\top}\epsilon (that is, ϵ\epsilon expressed in the eigenbasis of MM). Using Lemma 2, we can rewrite Mϵ2\|M\epsilon\|^2 like so:

Mϵ2=UΛUϵ2=ΛUϵ2=Λη2=i=1nΛi,i2ηi2.\| M\epsilon \|^2 = \| U\Lambda U^{\top} \epsilon \|^2 = \| \Lambda U^{\top} \epsilon \|^2 = \| \Lambda \eta \|^2 = \sum_{i = 1}^n \Lambda_{i, i}^2 \eta_i^2.

Note that the step UΛUϵ2=ΛUϵ2\|U\Lambda U^{\top}\epsilon\|^2 = \|\Lambda U^{\top}\epsilon\|^2 comes from the orthogonality of UU; rotating ΛUϵ\Lambda U^{\top}\epsilon via multiplication by UU does not change its magnitude. Now just one more lemma.

Lemma 3. We have E[η]=0\mathbb{E}[\eta] = 0 and Cov(η)=σ2In\Cov(\eta) = \sigma^2 I_n (the same moments as ϵ\epsilon). By corollary, E[ηi2]=σ2\mathbb{E}[\eta_i^2] = \sigma^2 for all ii.

Proof: The point is that UU is orthogonal, so UU=InU^{\top}U = I_n, making the covariance unchanged.

E[η]=UE[ϵ]=0    and    Cov(η)=UCov(ϵ)U=σ2InUU=σ2In.   \mathbb{E}[\eta] = U^{\top} \mathbb{E}[\epsilon] = 0 ~~~ \text{ and } ~~~ \Cov(\eta) = U^{\top}\Cov(\epsilon)U = \sigma^2 I_n \cdot U^{\top}U = \sigma^2 I_n. ~~~ \square

By Lemma 3, taking expectations on both sides yields:

E[Mϵ2]=E[i=1nΛi,i2ηi2]=i=1nΛi,i2E[ηi2]=i=1nΛi,i2σ2.\mathbb{E}\left[ \|M\epsilon\|^2 \right] = \mathbb{E}\left[ \sum_{i = 1}^n \Lambda_{i, i}^2 \eta_i^2 \right] = \sum_{i = 1}^n \Lambda_{i, i}^2 \mathbb{E}\left[\eta_i^2\right] = \sum_{i = 1}^n \Lambda_{i, i}^2 \sigma^2.

Exactly nkn - k terms have Λi,i=1\Lambda_{i, i} = 1, and the rest have Λi,i=0\Lambda_{i, i} = 0, so the summation evaluates to (nk)σ2(n - k) \sigma^2, as promised.   \blacksquare

With all that out of the way… we can now construct confidence intervals and perform hypothesis tests with β^LS\hat{\beta}^{\mathrm{LS}}.

Remark. Most of the time, these tests will ask for whether βi=0\beta_i^* = 0 or βi0\beta_i^* \neq 0 for some particular entry ii of β\beta^*. This translates to asking whether the information communicated by the ithi^{\text{th}} entry of a feature vector says anything about the response that the other entries do not already say.

§ Affine Linear Regression

Suppose I have a collection of data points {(Xi,Yi)}i=1n\{(X_i, Y_i)\}_{i = 1}^n on the coordinate plane, and I'd like to draw their line of best fit. In other words, I would like to find parameters (β^1LS,β^0LS)(\hat{\beta}_1^{\mathrm{LS}}, \hat{\beta}_0^{\mathrm{LS}}) for which Yiβ^1LSXi+β^0LSY_i \approx \hat{\beta}^{\mathrm{LS}}_1X_i + \hat{\beta}^{\mathrm{LS}}_0.

This is not the same as a one-dimensional linear regression, as that would only produce a model Yiβ^LSXiY_i \approx \hat{\beta}^{\mathrm{LS}}X_i. Fortunately, there's an easy adjustment we can make to linear regression to generalize it to affine models as well.

Idea. Pad every feature vector XiRkX_i \in \mathbb{R}^k with an extra 11, extending it to Rk+1\mathbb{R}^{k + 1}. Then:

(Y1Y2Yn)Y(1X11X21Xn)X(β0β1βk)β^LS=(X1Xn)(β1βk)+(β0β0).\underbrace{ \begin{pmatrix} Y_1 \\ Y_2 \\ \vdots \\ Y_n \end{pmatrix} }_{Y} \approx \underbrace{ \begin{pmatrix} 1 & \text{---} & X_1^{\top} & \text{---} \\ 1 & \text{---} & X_2^{\top} & \text{---} \\ \vdots & & \vdots & \\ 1 & \text{---} & X_n^{\top} & \text{---} \\ \end{pmatrix} }_{\mathbb{X}} \underbrace{ \begin{pmatrix} \beta_0 \\ \beta_1 \\ \vdots \\ \beta_k \end{pmatrix} }_{\hat{\beta}^{\mathrm{LS}}} = \begin{pmatrix} \text{---} & X_1^{\top} & \text{---} \\ & \vdots & \\ \text{---} & X_n^{\top} & \text{---} \\ \end{pmatrix} \begin{pmatrix} \beta_1 \\ \vdots \\ \beta_k \end{pmatrix} + \begin{pmatrix} \beta_0 \\ \vdots \\ \beta_0 \end{pmatrix}.

So drawing a line of best fit when the feature dimension is k=1k = 1 is secretly a two-dimensional linear regression, where the first entry of β^LS\hat{\beta}^{\mathrm{LS}} communicates the yy-intercept, and the second entry communicates the slope.

§ 1D Linear Regression and Correlation

Definition. Given one-dimensional data X1,,XnRX_1, \dots, X_n \in \mathbb{R} and Y1,,YnRY_1, \dots, Y_n \in \mathbb{R}, we may compute their sample variances and sample covariances like so:

Sxx:=1ni=1n(XiXˉn)2      Syy:=1ni=1n(YiYˉn)2      Sxy:=1ni=1n(XiXˉn)(YiYˉn)S_{xx} := \frac{1}{n} \sum_{i = 1}^n (X_i - \bar{X}_n)^2 ~~~ \parallel ~~~ S_{yy} := \frac{1}{n} \sum_{i = 1}^n (Y_i - \bar{Y}_n)^2 ~~~ \parallel ~~~ S_{xy} := \frac{1}{n} \sum_{i = 1}^n (X_i - \bar{X}_n)(Y_i - \bar{Y}_n)

Definition. The correlation coefficient between the {Xi}i=1n\{X_i\}_{i = 1}^n and the {Yi}i=1n\{Y_i\}_{i = 1}^n is defined as r:=SxySxxSyyr := \frac{S_{xy}}{\sqrt{S_{xx}S_{yy}}}.

It turns out that in the case of one-dimensional line-of-best-fit (or 2D linear regression), we have the following:

Theorem. (1D Linear Regression Slope) In a one-dimensional line-of-best-fit Yiβ^1LSXi+β^0LSY_i \approx \hat{\beta}^{\mathrm{LS}}_1X_i + \hat{\beta}^{\mathrm{LS}}_0, we have β^1LS=SxySxx\hat{\beta}^{\mathrm{LS}}_1 = \frac{S_{xy}}{S_{xx}}.

Proof: Details omitted; the proof is just unenlightening computation.   \blacksquare

Now let Y^i:=β^1LSXi+β^0LS\hat{Y}_i := \hat{\beta}^{\mathrm{LS}}_1X_i + \hat{\beta}^{\mathrm{LS}}_0 denote the value of YiY_i predicted by the line-of-best-fit (in contrast to the true value YiY_i).

Theorem. (Correlation and Regression) We have that Y^iYˉnSyy=rXiXˉnSxx\frac{\hat{Y}_i - \bar{Y}_n}{\sqrt{S_{yy}}} = r \cdot \frac{X_i - \bar{X}_n}{\sqrt{S_{xx}}}.

Proof: More computation; omitted.   \blacksquare

And that's why AP Statistics makes you interpret the correlation coefficient by writing, “The constant r2r^2 represents the percent of the variation in the response that is explained by the linear relationship with the feature vector.”