1. Linear Modeling & Matrix Formulation
Linear regression is the foundational workhorse of statistical modeling and supervised machine learning. It models the relationship between a continuous dependent response variable $y \in \mathbb{R}$ and a vector of $p$ explanatory features $\mathbf{x} = (x_1, x_2, \dots, x_p)^T$:
$$y_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \dots + \beta_p x_{ip} + \epsilon_i, \quad i = 1, \dots, n$$
Matrix Formulation
Stacking all $n$ observations into vector and matrix notation:
$$\mathbf{y} = \mathbf{X}\mathbf{\beta} + \mathbf{\epsilon}$$
$$\begin{bmatrix} y_1 \ y_2 \ \vdots \ y_n \end{bmatrix} = \begin{bmatrix} 1 & x_{11} & \cdots & x_{1p} \ 1 & x_{21} & \cdots & x_{2p} \ \vdots & \vdots & \ddots & \vdots \ 1 & x_{n1} & \cdots & x_{np} \end{bmatrix} \begin{bmatrix} \beta_0 \ \beta_1 \ \vdots \ \beta_p \end{bmatrix} + \begin{bmatrix} \epsilon_1 \ \epsilon_2 \ \vdots \ \epsilon_n \end{bmatrix}$$
where $\mathbf{X} \in \mathbb{R}^{n \times (p+1)}$ is the Design Matrix (including a column of ones for the intercept), $\mathbf{\beta} \in \mathbb{R}^{p+1}$ is the parameter vector, and $\mathbf{\epsilon} \sim \mathcal{N}(\mathbf{0}, \sigma^2 \mathbf{I})$ is unobserved noise.
2. Derivation of Ordinary Least Squares (OLS)
We seek the parameter vector $\hat{\mathbf{\beta}}$ that minimizes the Sum of Squared Residuals (SSR):
$$S(\mathbf{\beta}) = |\mathbf{y} - \mathbf{X}\mathbf{\beta}|_2^2 = (\mathbf{y} - \mathbf{X}\mathbf{\beta})^T (\mathbf{y} - \mathbf{X}\mathbf{\beta})$$
Expanding the quadratic matrix product:
$$S(\mathbf{\beta}) = \mathbf{y}^T \mathbf{y} - \mathbf{y}^T \mathbf{X}\mathbf{\beta} - \mathbf{\beta}^T \mathbf{X}^T \mathbf{y} + \mathbf{\beta}^T \mathbf{X}^T \mathbf{X}\mathbf{\beta} = \mathbf{y}^T \mathbf{y} - 2\mathbf{\beta}^T \mathbf{X}^T \mathbf{y} + \mathbf{\beta}^T \mathbf{X}^T \mathbf{X}\mathbf{\beta}$$
Taking the Gradient with Respect to $\mathbf{\beta}$:
$$\nabla_\mathbf{\beta} S(\mathbf{\beta}) = -2\mathbf{X}^T \mathbf{y} + 2\mathbf{X}^T \mathbf{X}\mathbf{\beta}$$
Setting the gradient to zero yields the celebrated Normal Equations:
$$\mathbf{X}^T \mathbf{X} \mathbf{\beta} = \mathbf{X}^T \mathbf{y}$$
Assuming $\mathbf{X}$ has full column rank ($p+1 \le n$ and columns are linearly independent), the Gram matrix $\mathbf{X}^T \mathbf{X}$ is invertible:
$$\hat{\mathbf{\beta}} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}$$
text
3. Geometric Interpretation: Orthogonal Projections
The fitted predictions are:
$$\hat{\mathbf{y}} = \mathbf{X}\hat{\mathbf{\beta}} = \mathbf{X}(\mathbf{X}^T \mathbf{X})^{-1}\mathbf{X}^T \mathbf{y} = \mathbf{H}\mathbf{y}$$
The matrix $\mathbf{H} = \mathbf{X}(\mathbf{X}^T \mathbf{X})^{-1}\mathbf{X}^T \in \mathbb{R}^{n \times n}$ is called the Hat Matrix (it "puts the hat on $y$").
Properties of the Hat Matrix:
- Symmetric: $\mathbf{H}^T = \mathbf{H}$.
- Idempotent: $\mathbf{H}^2 = \mathbf{H}$ (projecting twice is identical to projecting once).
- Orthogonal Projection: $\mathbf{H}$ projects the observed vector $\mathbf{y} \in \mathbb{R}^n$ orthogonally onto the subspace spanned by the columns of $\mathbf{X}$ ($C(\mathbf{X})$).
text
The residual vector $\mathbf{e} = \mathbf{y} - \hat{\mathbf{y}} = (\mathbf{I} - \mathbf{H})\mathbf{y}$ is strictly perpendicular to every column of $\mathbf{X}$:
$$\mathbf{X}^T \mathbf{e} = \mathbf{0}$$
4. The Gauss-Markov Theorem (BLUE)
Why is OLS the universal choice over other regression techniques?
Theorem (Gauss-Markov)
Under the classical linear regression assumptions:
- Strict exogeneity: $E[\mathbf{\epsilon} \mid \mathbf{X}] = \mathbf{0}$.
- Homoscedasticity and uncorrelated errors: $\text{Var}(\mathbf{\epsilon} \mid \mathbf{X}) = \sigma^2 \mathbf{I}$.
The OLS estimator $\hat{\mathbf{\beta}}$ is BLUE: the Best Linear Unbiased Estimator.
That is, for any other linear unbiased estimator $\tilde{\mathbf{\beta}}$, its covariance matrix satisfies:
$$\text{Cov}(\tilde{\mathbf{\beta}}) - \text{Cov}(\hat{\mathbf{\beta}}) \ge \mathbf{0} \quad \text{(Positive Semi-Definite)}$$
No linear unbiased estimator can ever achieve lower variance than OLS!
5. Goodness of Fit & Diagnostic Metrics
5.1 Coefficient of Determination ($R^2$)
Measures the proportion of total variance in the dependent variable explained by the model:
$$R^2 = 1 - \frac{\text{SS}{\text{res}}}{\text{SS}{\text{tot}}} = 1 - \frac{\sum_{i=1}^n (y_i - \hat{y}i)^2}{\sum{i=1}^n (y_i - \bar{y})^2}$$
- $R^2 = 1.0$: Perfect fit.
- $R^2 = 0.0$: Model predicts no better than the sample mean $\bar{y}$.
5.2 Adjusted $R^2$
Adding random noise features to $\mathbf{X}$ can never decrease $R^2$. Adjusted $R^2$ penalizes model complexity:
$$R^2_{\text{adj}} = 1 - \frac{\text{SS}{\text{res}} / (n - p - 1)}{\text{SS}{\text{tot}} / (n - 1)}$$
6. Classical Assumptions & Diagnostic Pitfalls
text
Multicollinearity & Variance Inflation Factor (VIF)
When features are strongly correlated, $\mathbf{X}^T \mathbf{X}$ becomes near-singular (ill-conditioned). Parameter estimates $\hat{\mathbf{\beta}}$ suffer massive variance and wild instability.
The Variance Inflation Factor (VIF) for feature $j$ is:
$$\text{VIF}_j = \frac{1}{1 - R_j^2}$$
where $R_j^2$ is the $R^2$ from regressing feature $x_j$ against all other remaining features.
- $\text{VIF} = 1$: Completely orthogonal (no collinearity).
- $\text{VIF} > 5$ or $10$: Severe multicollinearity; features must be pruned or regularized via Ridge ($L_2$).
7. Python Implementation: OLS from Scratch via NumPy
python
8. Key Takeaways
- Normal Equations: $\hat{\mathbf{\beta}} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}$ is the global minimum of the quadratic residual surface.
- Orthogonal Projection: The prediction $\hat{\mathbf{y}} = \mathbf{H}\mathbf{y}$ is the orthogonal projection of $\mathbf{y}$ onto the column space of $\mathbf{X}$.
- Gauss-Markov Optimality: OLS is the Best Linear Unbiased Estimator (BLUE) under spherical errors.