Linear Regression is a fundamental supervised learning algorithm for predicting a continuous target variable y as a linear combination of input features x.
1. Mathematical Formulation
Hypothesis Function
For an input feature vector x=[1,x1,x2,…,xn]T and parameter weight vector θ=[θ0,θ1,…,θn]T:
hθ(x)=θTx=θ0+θ1x1+θ2x2+⋯+θnxn
In matrix form for a dataset of m examples with design matrix X∈Rm×(n+1):
y^=Xθ
2. Cost Function (Mean Squared Error / OLS)
The parameters θ are chosen to minimize the Ordinary Least Squares (OLS) loss function:
J(θ)=2m1i=1∑m(hθ(x(i))−y(i))2=2m1∥Xθ−y∥22
3. Parameter Optimization
Approach A: Gradient Descent (Iterative)
Simultaneously update all weights θj in the direction of the negative gradient:
Balances sparsity of Lasso with feature grouping stability of Ridge.
5. Key Assumptions of OLS Linear Regression
Linearity: The relationship between features and the target is linear in parameters.
Homoscedasticity: The variance of residual errors is constant across all levels of features.
Independence of Residuals: Observations and error terms are mutually independent (no autocorrelation).
Normality of Residuals: The error terms ϵ=y−y^ are normally distributed N(0,σ2).
No Multicollinearity: Features should not be linearly dependent on each other (det(XTX)=0).
6. Evaluation Metrics
Mean Squared Error (MSE): MSE=m1∑i=1m(yi−y^i)2
Root Mean Squared Error (RMSE): RMSE=MSE (interpretable in original target units)
R2 (Coefficient of Determination):
R2=1−∑(yi−yˉ)2∑(yi−y^i)2
Indicates the proportion of variance in y explained by the model (ranges from 0 to 1).