Ordinary Least Squares
The Ordinary Least Squares (OLS) method is a technique for estimating the unknown parameters in a linear regression model. OLS chooses the parameters of a linear function of a set of explanatory variables by minimizing the sum of the squares of the differences between the observed dependent variable in the given dataset and those predicted by the linear function. In other words, it tries to find the line (or hyperplane) that minimizes the sum of the squared differences between the observed values and the values predicted by the linear approximation.
Example
Assume you have a set of data points \(\{(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)\}\) and you want to find the best line that fits these points in the plane. The equation for the line is of the form:
where \(\beta_0\) is the intercept, \(\beta_1\) is the slope of the line, \(x\) is the independent variable, and \(y\) is the dependent variable.
The goal of ordinary least squares (OLS) is to find the values of \(\beta_0\) and \(\beta_1\) that minimize the sum of the squared differences between the observed values \(y_i\) and the values predicted by the linear approximation \(\hat{y}_i\).
For each data point \((x_i, y_i)\), the predicted value \(\hat{y}_i\) is given by:
The residual for each data point is the difference between the observed value and the predicted value:
The ordinary least squares method minimizes the sum of the squared residuals:
To minimize this sum, we take the partial derivatives of \(S\) with respect to \(\beta_0\) and \(\beta_1\) and set them to zero:
Solving these equations gives the values of \(\beta_0\) and \(\beta_1\) that minimize the sum of the squared residuals.
where \(\bar{x}\) and \(\bar{y}\) are the means of the independent and dependent variables, respectively. Once we have \(\beta_1\), we can find \(\beta_0\) using the formula:
Intuitively, we can see \(\beta_1\) as the slope of the line, and \(\beta_0\) as the intercept.
You can imagine a cloud of data points scattered on a 2D plot. The OLS method fits a straight line through these points such that the vertical distances (errors) between the points and the line are as small as possible on average, and the squared differences are minimized.
Relationship to the Normal Equations
Equations (2) and (3) can be reformulated as a linear regression problem leading to the so-called normal equations. In linear regression, we aim to find a vector of coefficients \(\mathbf{w}\) that best fits the data. Given:
\(\mathbf{X}\): the design matrix of shape \((n, p)\) where \(n\) is the number of samples and \(p\) is the number of features
\(\mathbf{y}\): the target vector of shape \((n,)\)
\(\mathbf{w}\): the vector of coefficients of shape \((p,)\)
Our goal is to find \(\mathbf{w}\) that minimizes the residual sum of squares (RSS):
Compare this equation with (1). The two equations are equivalent with \(\mathbf{X} = [1, x_1; 1, x_2; \dots; 1, x_n]\) and \(\mathbf{y} = [y_1, y_2, \dots, y_n]\). And the solution to the normal equations is the same as the solution to the OLS problem.
The objective function (cost function) \(J(w)\) represents the sum of squared differences between the observed targets and the predicted values:
The expands to:
Note that \(y^T y\) is a constant with respect to \(w\) and can be ignored during optimization.
To find the minimum of \(J(w)\), we compute its gradient with respect to \(w\) and set it to zero:
Setting the gradient to zero to find the critical points:
Simplify by dividing both sides by 2:
This equation is known as the normal equation.
Assuming \(X^T X\) is invertible (which requires that \(X\) has full rank), we can solve for \(w\):
This is the closed-form solution to the linear regression problem.