Deriving and Analyzing Ordinary Least Squares Linear Regression
Machine LearningModel answerLast updated
Explain the mathematical derivation, key theoretical assumptions, numerical considerations, and regularized extensions of ordinary least squares (OLS).
Explain the foundation, derivation, and practical mechanics of the Ordinary Least Squares (OLS) method for linear regression.
Specifically, address the following points:
- Optimization Objective: Write out the mathematical objective function that OLS minimizes. Explain why the squared Euclidean norm (L2 loss) is standard from a probabilistic perspective.
- Matrix Derivation: Derive the closed-form normal equation matrix solution ($\hat{\beta}$) step by step, using matrix calculus notation.
- Core Assumptions: Describe the standard theoretical assumptions (Gauss-Markov assumptions) required for OLS to be the Best Linear Unbiased Estimator (BLUE).
- Numerical Stability & Singularities: Discuss what happens when feature matrices are collinear or rank-deficient, and explain how techniques like Ridge Regularization (L2 penalty) solve these issues mathematically and practically.
Hints
Hint 1
Consider expressing the sum of squared errors in matrix vector form using transpose properties before taking derivatives.
Hint 2
Remember the linear algebra rule for differentiating vector quadratic forms: $\frac{\partial}{\partial \beta} (\beta^T A \beta) = 2 A \beta$ when $A$ is symmetric.
Hint 3
Relate rank deficiency to the invertibility of $X^T X$ and explain how adding $\lambda I$ guarantees invertibility.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
1. Mathematical Formulation & Probabilistic Intuition
Ordinary Least Squares (OLS) aims to find a linear combination of input features $X$ that best predicts target vector $y$.
Given a dataset of $n$ samples and $p$ features represented as a matrix $X \in \mathbb{R}^{n \times p}$ (where a column of $1$s is typically included for the intercept) and a target vector $y \in \mathbb{R}^n$, we model the output as:
$$y = X\beta + \epsilon$$
where $\beta \in \mathbb{R}^p$ is the parameter vector, and $\epsilon \in \mathbb{R}^n$ represents residual errors.
Optimization Objective
The OLS objective function minimizes the Residual Sum of Squares (RSS):
$$\text{Loss}(\beta) = \frac{1}{2} |y - X\beta|_2^2 = \frac{1}{2} (y - X\beta)^T (y - X\beta)$$
Maximum Likelihood Connection
Minimizing squared residual errors directly corresponds to Maximum Likelihood Estimation (MLE) under the assumption that the noise terms are independently and identically distributed (i.i.d.) Gaussian random variables:
$$\epsilon_i \sim \mathcal{N}(0, \sigma^2)$$
Under this assumption, the likelihood function for the observed target $y$ given features $X$ and parameters $\beta$ is:
$$P(y | X; \beta) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left( -\frac{(y_i - x_i^T\beta)^2}{2\sigma^2} \right)$$
Taking the negative log-likelihood (NLL):
$$\text{NLL}(\beta) = \frac{n}{2}\ln(2\pi\sigma^2) + \frac{1}{2\sigma^2} \sum_{i=1}^n (y_i - x_i^T\beta)^2$$
Minimizing NLL with respect to $\beta$ drops the constant scale factors, yielding the exact OLS mean squared error formulation.
2. Derivation of the Closed-Form Solution
To find the optimal parameter vector $\hat{\beta}$, we expand the matrix objective function and set its gradient relative to $\beta$ to zero.
Step 1: Expand the Loss Function
$$\text{Loss}(\beta) = \frac{1}{2} (y^T - \beta^T X^T)(y - X\beta)$$ $$\text{Loss}(\beta) = \frac{1}{2} \left( y^T y - y^T X \beta - \beta^T X^T y + \beta^T X^T X \beta \right)$$
Because $y^T X \beta$ is a scalar, it equals its transpose $\beta^T X^T y$. Thus:
$$\text{Loss}(\beta) = \frac{1}{2} \left( y^T y - 2 \beta^T X^T y + \beta^T X^T X \beta \right)$$
Step 2: Compute the Gradient
Using standard matrix calculus rules ($\frac{\partial}{\partial \beta}(a^T \beta) = a$ and $\frac{\partial}{\partial \beta}(\beta^T A \beta) = 2 A \beta$ for symmetric $A$):
$$\nabla_\beta \text{Loss}(\beta) = \frac{1}{2} \left( -2 X^T y + 2 X^T X \beta \right) = -X^T y + X^T X \beta$$
Step 3: Solve the Normal Equations
Setting the gradient to zero vector $\mathbf{0}$:
$$-X^T y + X^T X \beta = 0 \implies X^T X \beta = X^T y$$
Assuming $X^T X$ is invertible (i.e., $X$ has full column rank $p$):
$$\hat{\beta} = (X^T X)^{-1} X^T y$$
3. Core Theoretical Assumptions (Gauss-Markov Theorem)
The Gauss-Markov Theorem states that OLS is the Best Linear Unbiased Estimator (BLUE)—meaning it has the lowest variance among all linear unbiased estimators—if the following core conditions hold:
- Linearity in Parameters: The relationship between $X$ and $y$ is strictly linear in parameters: $y = X\beta + \epsilon$.
- Strict Exogeneity: The conditional expectation of errors given $X$ is zero: $\mathbb{E}[\epsilon | X] = 0$.
- No Multicollinearity (Full Rank): Matrix $X$ has full column rank ($\text{rank}(X) = p$), meaning features are linearly independent.
- Spherical Errors (Homoscedasticity & No Autocorrelation):
- Homoscedasticity: $\text{Var}(\epsilon_i | X) = \sigma^2$ (constant variance).
- No Autocorrelation: $\text{Cov}(\epsilon_i, \epsilon_j | X) = 0$ for $i \neq j$.
- Combined covariance matrix: $\text{Var}(\epsilon | X) = \sigma^2 I_n$.
Failure Modes When Assumptions Break
- Heteroscedasticity (non-constant variance): OLS remains unbiased, but standard errors are incorrect, rendering hypothesis tests ($t$-tests, $p$-values) invalid. Fix: Use Generalized Least Squares (GLS) or robust standard errors (White/Huber sandwich estimators).
- Autocorrelation (common in time-series data): Standard errors are underestimated. Fix: Use Newey-West standard errors or autoregressive models.
4. Numerical Stability and Ridge Regularization
Linear Dependence & Rank Deficiency
When features exhibit strong multicollinearity or when sample size $n < p$, $X^T X$ becomes singular (non-invertible) or ill-conditioned.
- An ill-conditioned $X^T X$ has a high condition number $\kappa = \frac{\sigma_{\max}}{\sigma_{\min}}$.
- Small perturbations in data cause huge spikes in variance for coefficients $\hat{\beta}$, leading to overfitting and unstable predictions.
Ridge Regression (L2 Regularization) Solution
To resolve numerical instability and rank deficiency, we add an $L2$ norm penalty term to the objective:
$$\text{Loss}_{\text{ridge}}(\beta) = \frac{1}{2} |y - X\beta|_2^2 + \frac{\lambda}{2} |\beta|_2^2$$
Taking the gradient and setting it to zero yields:
$$\nabla_\beta \text{Loss}{\text{ridge}}(\beta) = -X^T y + X^T X \beta + \lambda \beta = 0$$ $$(X^T X + \lambda I) \hat{\beta}{\text{ridge}} = X^T y$$ $$\hat{\beta}_{\text{ridge}} = (X^T X + \lambda I)^{-1} X^T y$$
Why Ridge Regularization Solves Numerical Instability
- Guaranteed Invertibility: For any $\lambda > 0$, the matrix $(X^T X + \lambda I)$ is strictly positive definite, guaranteeing that its inverse always exists regardless of rank issues in $X$.
- Variance Reduction: Adding $\lambda I$ shifts all singular values of $X^T X$ up by $\lambda$, bounding the condition number and substantially reducing variance at the cost of introducing a small bias (Bias-Variance Trade-off).