Search Knowledge

© 2026 LIBREUNI PROJECT

Machine Learning / Supervised Learning

Linear Regression

Linear Regression and the Normal Equation

Linear Regression models a continuous target yy as a linear combination of input features xx:

y^=θTx\hat{y} = \theta^T \mathbf{x} where θ\theta is the model parameter vector (including the intercept or bias θ0\theta_0), and x\mathbf{x} is the observation vector containing the features (with x0=1x_0 = 1).

Simple vs. Multiple Linear Regression

  • Simple Linear Regression: Models the relationship between a single feature x1x_1 and the target yy. The equation simplifies to finding the best line with a specific slope (θ1\theta_1) and intercept (θ0\theta_0): y^=θ0+θ1x1\hat{y} = \theta_0 + \theta_1 x_1
  • Multiple Linear Regression: Extends the concept to multiple features, where each feature gets its own weight (slope) in a multidimensional space.

Polynomial Regression

If the data is more complex than a simple straight line, we can still use linear models to fit nonlinear data. By adding powers of each feature as new features, we can train a linear model on this extended set of features. This is called Polynomial Regression. Despite fitting a curve to the data, it is still considered a linear model because the prediction is still a linear combination of the (now polynomial) features.

Parameter Estimation: Ordinary Least Squares

To train the model, we find parameters θ\theta that minimize the Mean Squared Error (MSE) over the dataset:

MSE(X,hθ)=1mi=1m(θTx(i)y(i))2\text{MSE}(X, h_{\theta}) = \frac{1}{m} \sum_{i=1}^{m} (\theta^T \mathbf{x}^{(i)} - y^{(i)})^2

The Closed-Form Normal Equation

To minimize the cost function, we solve analytically using the Normal Equation:

θ^=(XTX)1XTy\hat{\theta} = (X^T X)^{-1} X^T y

  • XX: The design matrix of shape (m,n+1)(m, n+1) containing all features.
  • yy: The target vector of shape (m,1)(m, 1).

Multicollinearity and SVD

If the matrix XTXX^T X is singular (non-invertible) due to redundant, highly correlated features, standard inversion fails. Solvers compute the pseudo-inverse X+X^+ using Singular Value Decomposition (SVD) for stability.

Computational Complexity

Computing (XTX)1(X^T X)^{-1} requires inverting an (n+1)×(n+1)(n+1) \times (n+1) matrix. The computational complexity is between O(n2.4)O(n^{2.4}) and O(n3)O(n^3), making the Normal Equation expensive when the number of features is large.

Example: Computing the Normal Equation

The following example demonstrates computing model coefficients using the Normal Equation:

python

Interactive Lab

Compute model parameters analytically using the Normal Equation. Alter the synthetic target equation coefficients to see if the equation adapts and finds the new parameters.

Step 1
Inspect the idea
Step 2
Edit the program
Step 3
Run and compare

Exercise

Test your understanding of the analytical parameter estimation limits:

If your dataset contains 100,000 features and 1,000 samples, what makes the Normal Equation less suitable than gradient descent?

References & Further Reading