Linear Regression
📈 Regression and Linear Models
Introduction to Regression
Regression is a fundamental statistical tool for predicting one variable based on another. The core idea is to model the relationship between an explained variable (response, dependent variable) y and an explanatory variable (predictor, independent variable) x as y = f(x) + \text{noise}. In practice, the goal is to estimate the function f from data.
Regression is central to prediction, which is a key goal in many fields—economics, finance, medicine, machine learning. The simplest and most widely used form is linear regression, where f is assumed to be linear: y = a + bx + \varepsilon.
Notation and Data
Given n observations (x_i, y_i), i = 1, \dots, n.
Typically, these are assumed to be independent and identically distributed (i.i.d.) pairs from some joint distribution of (X, Y).
A scatterplot of the data points suggests a linear trend plus random scatter.
The line y = a + bx is never exactly followed; the deviation is captured by noise \varepsilon.
Univariate Linear Regression
Model
Y = a + b X + \varepsilon
a: intercept
b: slope
\varepsilon: random error (noise)
The goal is to find the best estimates \hat{a}, \hat{b} from the data.
Measuring Fit: The Least Squares Criterion
To quantify how well a line fits the data, the vertical distance from each point to the line is used. For a candidate line y = a + bx, the vertical distance for point (x_i, y_i) is |y_i - (a + bx_i)|.
The most common measure is the sum of squared vertical distances:
\sum_{i=1}^n (y_i - a - b x_i)^2
Minimizing this sum gives the ordinary least squares (OLS) estimates. The squared distance is mathematically convenient (differentiable, yields closed-form solutions) and corresponds to assuming Gaussian errors under maximum likelihood.
Alternative criteria (e.g., sum of absolute deviations) exist but lack closed forms and are less common.
Population (Theoretical) Least Squares
Before considering data, define the population least squares problem: find the best linear predictor knowing the true distribution of (X, Y).
Minimize the expected squared error:
\min_{a, b} \mathbb{E}\left[(Y - a - b X)^2\right]
This leads to the true parameters a, b that define the best linear approximation.
Derivation of Population Parameters
Take derivatives with respect to a and b and set to zero:
\frac{\partial}{\partial a} \mathbb{E}[(Y - a - bX)^2] = 0 \Rightarrow \mathbb{E}[Y - a - bX] = 0
→ a + b\mathbb{E}[X] = \mathbb{E}[Y]\frac{\partial}{\partial b} \mathbb{E}[(Y - a - bX)^2] = 0 \Rightarrow \mathbb{E}[X(Y - a - bX)] = 0
→ \mathbb{E}[XY] = a\mathbb{E}[X] + b\mathbb{E}[X^2]
Solve the system. Substitute a = \mathbb{E}[Y] - b\mathbb{E}[X] into the second equation:
\mathbb{E}[XY] = (\mathbb{E}[Y] - b\mathbb{E}[X])\mathbb{E}[X] + b\mathbb{E}[X^2]
Rearranging:
\mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] = b\left(\mathbb{E}[X^2] - (\mathbb{E}[X])^2\right)
The left side is the covariance \operatorname{Cov}(X,Y) and the right side is b \cdot \operatorname{Var}(X). Hence:
b = \frac{\operatorname{Cov}(X,Y)}{\operatorname{Var}(X)}, \qquad a = \mathbb{E}[Y] - b\mathbb{E}[X]
Key result: The slope b is the ratio of covariance to variance. The intercept a centers the line through the means.
Requirement: \operatorname{Var}(X) > 0 (otherwise the x-values are constant and no slope can be estimated).
Properties of the Noise
If the true model is Y = a + bX + \varepsilon and a,b are the population least squares parameters, then the error \varepsilon must satisfy:
\mathbb{E}[\varepsilon] = 0
\operatorname{Cov}(X, \varepsilon) = 0
These follow from the first-order conditions above. Often a stronger assumption is made: \varepsilon is independent of X (which implies zero covariance) or that \mathbb{E}[\varepsilon \mid X] = 0. For inference (e.g., t-tests, confidence intervals), \varepsilon is often assumed to be Gaussian (normally distributed).
Estimating the Parameters from Data
Given the sample (x_i, y_i), the population expectations are replaced by sample averages (plug-in principle):
\hat{b} = \frac{\frac{1}{n}\sum x_i y_i - \bar{x}\bar{y}}{\frac{1}{n}\sum x_i^2 - \bar{x}^2} = \frac{\widehat{\operatorname{Cov}}(X,Y)}{\widehat{\operatorname{Var}}(X)}
\hat{a} = \bar{y} - \hat{b}\bar{x}
These are the ordinary least squares estimators. Equivalently, they minimize the sample sum of squared errors:
\min_{a,b} \sum_{i=1}^n (y_i - a - b x_i)^2
Taking derivatives with respect to a and b (now on the sum) yields the same expressions. This is a general principle: minimizing the empirical risk often leads to the same formulas as plugging sample moments into population formulas, when a closed form exists.
Multivariate Linear Regression
Model
When we have multiple explanatory variables, we generalize to:
Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \dots + \beta_{p-1} X_{p-1} + \varepsilon
Here X = (X_1, \dots, X_{p-1}) is a vector of predictors. To include an intercept in a unified notation, we set X_0 \equiv 1. Then define the parameter vector \beta = (\beta_0, \beta_1, \dots, \beta_{p-1})^\top \in \mathbb{R}^p, and the model becomes:
Y = X^\top \beta + \varepsilon
where X \in \mathbb{R}^p now includes the constant term.
Interpretation
The response Y is still a scalar.
The predictors form a vector; the relationship is linear in the coefficients.
Geometrically, this describes a hyperplane in (p+1)-dimensional space.
For p=2 (one predictor + intercept), the hyperplane is a line in 2D; for p=3 (two predictors + intercept), it is a plane in 3D.
Data and Matrix Notation
Let n observations be given:
\mathbf{X} = \begin{pmatrix} 1 & x_{11} & \dots & x_{1,p-1} 1 & x_{21} & \dots & x_{2,p-1} \vdots & \vdots & \ddots & \vdots 1 & x_{n1} & \dots & x_{n,p-1} \end{pmatrix}{n \times p}, \qquad \mathbf{y} = \begin{pmatrix} y_1 y_2 \vdots y_n \end{pmatrix}{n \times 1}, \qquad \boldsymbol{\varepsilon} = \begin{pmatrix} \varepsilon_1 \varepsilon_2 \vdots \varepsilon_n \end{pmatrix}
The model for all observations simultaneously:
\mathbf{y} = \mathbf{X} \beta + \boldsymbol{\varepsilon}
Assumptions about \varepsilon: same as univariate case – mean zero, uncorrelated with each predictor (or independent). For inference, often \varepsilon \sim N(0, \sigma^2 I).
Least Squares for Multivariate Regression
The criterion is the sum of squared residuals:
\min_{\beta \in \mathbb{R}^p} \sum_{i=1}^n \left( y_i - \mathbf{x}i^\top \beta \right)^2 = \min{\beta} \mathbf{y} - \mathbf{X}\beta ^2
where \cdot is the Euclidean norm.
Taking the gradient with respect to \beta and setting to zero gives the normal equations:
\frac{\partial}{\partial \beta} \mathbf{y} - \mathbf{X}\beta ^2 = -2 \mathbf{X}^\top (\mathbf{y} - \mathbf{X}\beta) = 0
\Rightarrow \mathbf{X}^\top \mathbf{X} \beta = \mathbf{X}^\top \mathbf{y}
If \mathbf{X}^\top \mathbf{X} is invertible (requires full column rank, i.e., no perfect multicollinearity and n \ge p), the OLS estimator is:
\hat{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}
This formula compactly solves the system of p equations. Each component of \hat{\beta} is a linear combination of the y_i's.
Key takeaway: Multivariate linear regression generalizes univariate regression by using matrix algebra. The least squares solution has a closed form but requires matrix inversion.
Assumptions for Valid Inference
For the OLS estimator to be unbiased and have desirable properties (e.g., Gauss-Markov theorem), the following are typically assumed:
Linearity: \mathbb{E}[Y \mid X] = X^\top \beta (conditional expectation is linear)
Independence: observations are independent (or at least uncorrelated)
Homoscedasticity: \operatorname{Var}(\varepsilon_i \mid X) = \sigma^2 constant
No perfect multicollinearity: columns of \mathbf{X} are linearly independent
Exogeneity: \mathbb{E}[\varepsilon \mid X] = 0 (implies \operatorname{Cov}(X_j, \varepsilon)=0 for all j)
If errors are also Gaussian, finite-sample inference (t-tests, F-tests) is exact.
Summary
Regression models the relationship between a response and predictors.
Linear regression assumes a linear form: Y = X^\top\beta + \varepsilon.
Least squares minimizes the sum of squared vertical distances, yielding plug-in estimators.
Population parameters are expressed in terms of covariances and variances; sample counterparts give the OLS estimates.
Multivariate regression uses matrix notation to handle multiple predictors simultaneously, with solution \hat{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}.
The method is widely used for prediction and inference, often as a baseline model before more complex methods.

Ask anything
We've read this whole study set, so questions about it get the best answers.