logo
CHEATSHEETSTOOLSABOUT中文

Linear Regression

Linear Regression is a fundamental supervised learning algorithm for predicting a continuous target variable yy as a linear combination of input features xx.

1. Mathematical Formulation

Hypothesis Function

For an input feature vector x=[1,x1,x2,…,xn]Tx = [1, x_1, x_2, \dots, x_n]^T and parameter weight vector θ=[θ0,θ1,…,θn]T\theta = [\theta_0, \theta_1, \dots, \theta_n]^T:

hθ(x)=θTx=θ0+θ1x1+θ2x2+⋯+θnxnh_\theta(x) = \theta^T x = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n

In matrix form for a dataset of mm examples with design matrix X∈Rm×(n+1)X \in \mathbb{R}^{m \times (n+1)}:

y^=Xθ\hat{y} = X\theta

2. Cost Function (Mean Squared Error / OLS)

The parameters θ\theta are chosen to minimize the Ordinary Least Squares (OLS) loss function:

J(θ)=12m∑i=1m(hθ(x(i))−y(i))2=12m∥Xθ−y∥22J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} \big( h_\theta(x^{(i)}) - y^{(i)} \big)^2 = \frac{1}{2m} \| X\theta - y \|_2^2

3. Parameter Optimization

Approach A: Gradient Descent (Iterative)

Simultaneously update all weights θj\theta_j in the direction of the negative gradient:

θj:=θj−α∂∂θjJ(θ)=θj−α1m∑i=1m(hθ(x(i))−y(i))xj(i)\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta) = \theta_j - \alpha \frac{1}{m} \sum_{i=1}^{m} \big( h_\theta(x^{(i)}) - y^{(i)} \big) x_j^{(i)}

In vectorized form:

θ:=θ−α1mXT(Xθ−y)\theta := \theta - \alpha \frac{1}{m} X^T (X\theta - y)

Where α\alpha is the learning rate.

Approach B: Normal Equation (Closed-Form Solution)

Setting the analytical gradient ∇θJ(θ)=0\nabla_\theta J(\theta) = 0 yields the exact closed-form solution:

θ=(XTX)−1XTy\theta = (X^T X)^{-1} X^T y
  • Pros: No need to choose learning rate α\alpha; no iterations needed.
  • Cons: Computing (XTX)−1(X^T X)^{-1} requires O(n3)O(n^3) time, becoming prohibitively slow when feature dimension n>10,000n > 10,000.

4. Regularization

To prevent overfitting and handle multicollinearity, regularization penalties are added to the cost function:

Method Penalty Objective Function Properties
Ridge (L2L_2) λ∑j=1nθj2\lambda \sum_{j=1}^n \theta_j^2 J(θ)+λ2m∥θ∥22J(\theta) + \frac{\lambda}{2m}\|\theta\|_2^2 Shrinks weights toward zero; keeps all features; analytical solution: θ=(XTX+λI)−1XTy\theta = (X^T X + \lambda I)^{-1} X^T y.
Lasso (L1L_1) λ∑j=1n∥θj∥\lambda \sum_{j=1}^n \|\theta_j\| J(θ)+λm∥θ∥1J(\theta) + \frac{\lambda}{m}\|\theta\|_1 Drives irrelevant feature weights exactly to zero; performs automatic feature selection.
Elastic Net Combination of L1+L2L_1 + L_2 J(θ)+rλ∥θ∥1+1−r2λ∥θ∥22J(\theta) + r\lambda \|\theta\|_1 + \frac{1-r}{2}\lambda \|\theta\|_2^2 Balances sparsity of Lasso with feature grouping stability of Ridge.

5. Key Assumptions of OLS Linear Regression

  1. Linearity: The relationship between features and the target is linear in parameters.
  2. Homoscedasticity: The variance of residual errors is constant across all levels of features.
  3. Independence of Residuals: Observations and error terms are mutually independent (no autocorrelation).
  4. Normality of Residuals: The error terms ϵ=y−y^\epsilon = y - \hat{y} are normally distributed N(0,σ2)\mathcal{N}(0, \sigma^2).
  5. No Multicollinearity: Features should not be linearly dependent on each other (det⁡(XTX)≠0\det(X^T X) \neq 0).

6. Evaluation Metrics

  • Mean Squared Error (MSE): MSE=1m∑i=1m(yi−y^i)2\text{MSE} = \frac{1}{m} \sum_{i=1}^m (y_i - \hat{y}_i)^2
  • Root Mean Squared Error (RMSE): RMSE=MSE\text{RMSE} = \sqrt{\text{MSE}} (interpretable in original target units)
  • R2R^2 (Coefficient of Determination): R2=1−∑(yi−y^i)2∑(yi−yˉ)2R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2} Indicates the proportion of variance in yy explained by the model (ranges from 0 to 1).