Linear Regression
Linear Regression is a fundamental supervised learning algorithm for predicting a continuous target variable as a linear combination of input features .
1. Mathematical Formulation
Hypothesis Function
For an input feature vector and parameter weight vector :
In matrix form for a dataset of examples with design matrix :
2. Cost Function (Mean Squared Error / OLS)
The parameters are chosen to minimize the Ordinary Least Squares (OLS) loss function:
3. Parameter Optimization
Approach A: Gradient Descent (Iterative)
Simultaneously update all weights in the direction of the negative gradient:
In vectorized form:
Where is the learning rate.
Approach B: Normal Equation (Closed-Form Solution)
Setting the analytical gradient yields the exact closed-form solution:
- Pros: No need to choose learning rate ; no iterations needed.
- Cons: Computing requires time, becoming prohibitively slow when feature dimension .
4. Regularization
To prevent overfitting and handle multicollinearity, regularization penalties are added to the cost function:
| Method | Penalty | Objective Function | Properties |
|---|---|---|---|
| Ridge () | Shrinks weights toward zero; keeps all features; analytical solution: . | ||
| Lasso () | Drives irrelevant feature weights exactly to zero; performs automatic feature selection. | ||
| Elastic Net | Combination of | 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 are normally distributed .
- No Multicollinearity: Features should not be linearly dependent on each other ().
6. Evaluation Metrics
- Mean Squared Error (MSE):
- Root Mean Squared Error (RMSE): (interpretable in original target units)
- (Coefficient of Determination): Indicates the proportion of variance in explained by the model (ranges from 0 to 1).