Gradient Descent for Least Squares
Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables by fitting a linear equation to observed data. The simplest form of the linear equation with one dependent and one independent variable is represented as , where is the slope of the line and is the y-intercept. This method is widely used in predictive modeling and quantitative forecasting.
Gradient Descent for Linear Regression
Gradient Descent is an optimization algorithm used to minimize some function by iteratively moving in the direction of steepest descent as defined by the negative of the gradient. In the context of linear regression, gradient descent is used to find the values of and that minimize the cost function, which is typically the sum of squared differences between the observed values and the values predicted by the model.
Cost Function
The mean squared error cost function for linear regression quantifies the difference between the observed values and the values predicted by the linear model. It is given by:
where:
- is the number of observations,
- is the observed value,
- is the independent variable,
- is the slope, and
- is the y-intercept.
Gradient Descent Algorithm
The gradient descent algorithm updates the parameters and iteratively to minimize the cost function . The update rules for and at each iteration are:
where is the learning rate, a hyperparameter that controls the size of the steps taken towards the minimum of the cost function.
Partial Derivatives of the Cost Function
The partial derivatives of with respect to and are:
These gradients are used to update the values of and iteratively along the negative gradient; a suitable step size is needed to decrease the cost function.
Implementation Steps
- Initialization: Start with initial guesses for the values of and .
- Gradient Calculation: Compute the gradients of the cost function with respect to and .
- Update Parameters: Update the values of and using the gradient descent update rules.
- Iteration: Repeat steps 2 and 3 until the gradient tolerance is met or the iteration budget is exhausted; report which stopping condition was reached.
Try moving points and adjusting the line in Explained Visually’s least-squares demonstration. Compare the residual squares as the slope and intercept change; their sum differs from the mean-squared-error objective above only by the constant number of observations.
Exact solution and a reproducible update
This note concerns the optimization calculation; the assumptions and interpretation of the model belong to Linear Regression. With , the chain rule gives and , explaining the signs and factor above. Because of , is mean squared error, not the residual sum of squares. Scaling a loss preserves its minimizer but scales its gradient and changes a suitable learning rate.
Use the three observations . Their sum of squared errors is exactly the quadratic from the gradient note:
Here , , , and . Solving and gives , . The residuals are , so and .
At , . A simultaneous update with gives and lowers from to . Repeat by computing both gradient sums before assigning either parameter.
Collect the parameters in and the observations in . Let row of the design matrix be , so entry of is the prediction . Then , where the squared Euclidean norm sums the squared residuals, and the Hessian is . For every , , proving convexity. The solution is unique only when the columns of are linearly independent (full column rank); for slope plus intercept this requires at least two distinct . If all , only is identifiable and many parameter pairs fit equally well.
The step-size bound follows from the error dynamics. Here is symmetric positive definite and is the unique minimizer. For , the quadratic gradient is , hence
Along an eigenvector of with eigenvalue , the error component is multiplied by . Convergence from every starting point requires for every eigenvalue, giving . At the upper boundary, the largest-eigenvalue component oscillates instead of shrinking. Convexity identifies global minima; it does not make every step size converge.
For this dataset, , so fixed-step descent converges for . Feature scaling changes this bound. Stop with a gradient-norm tolerance and an iteration cap, and compare with the exact solution; unchanged loss alone can mean numerical stagnation. For small dense problems, QR or SVD least-squares solvers are often preferable to hand-iterating or explicitly inverting .