Skip to main content

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 y=mx+by = mx + b, where mm is the slope of the line and bb 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 mm and bb 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:

E(m,b)=1n∑i=1n(yi−(mxi+b))2E(m, b) = \frac{1}{n} \sum_{i=1}^{n} (y_i - (mx_i + b))^2

where:

  • nn is the number of observations,
  • yiy_i is the observed value,
  • xix_i is the independent variable,
  • mm is the slope, and
  • bb is the y-intercept.

Gradient Descent Algorithm​

The gradient descent algorithm updates the parameters mm and bb iteratively to minimize the cost function E(m,b)E(m, b). The update rules for mm and bb at each iteration are:

mk+1=mk−α∂E∂mm_{k+1} = m_k - \alpha \frac{\partial E}{\partial m} bk+1=bk−α∂E∂bb_{k+1} = b_k - \alpha \frac{\partial E}{\partial b}

where α\alpha 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 E(m,b)E(m, b) with respect to mm and bb are:

∂E∂m=−2n∑i=1nxi(yi−(mxi+b))\frac{\partial E}{\partial m} = -\frac{2}{n} \sum_{i=1}^{n} x_i(y_i - (mx_i + b)) ∂E∂b=−2n∑i=1n(yi−(mxi+b))\frac{\partial E}{\partial b} = -\frac{2}{n} \sum_{i=1}^{n} (y_i - (mx_i + b))

These gradients are used to update the values of mm and bb iteratively along the negative gradient; a suitable step size is needed to decrease the cost function.

Implementation Steps​

  1. Initialization: Start with initial guesses for the values of mm and bb.
  2. Gradient Calculation: Compute the gradients of the cost function with respect to mm and bb.
  3. Update Parameters: Update the values of mm and bb using the gradient descent update rules.
  4. 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 ri=mxi+b−yir_i=mx_i+b-y_i, the chain rule gives ∂ri2/∂m=2rixi\partial r_i^2/\partial m=2r_ix_i and ∂ri2/∂b=2ri\partial r_i^2/\partial b=2r_i, explaining the signs and factor 22 above. Because of 1/n1/n, EE 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 (1,2),(2,5),(3,3)(1,2),(2,5),(3,3). Their sum of squared errors is exactly the quadratic from the gradient note:

S=3E=14m2+12mb+3b2−42m−20b+38.S=3E=14m^2+12mb+3b^2-42m-20b+38.

Here ∑xi=6\sum x_i=6, ∑yi=10\sum y_i=10, ∑xi2=14\sum x_i^2=14, and ∑xiyi=21\sum x_iy_i=21. Solving 28m+12b−42=028m+12b-42=0 and 12m+6b−20=012m+6b-20=0 gives m=1/2m=1/2, b=7/3b=7/3. The residuals yi−(mxi+b)y_i-(mx_i+b) are −5/6,5/3,−5/6-5/6,5/3,-5/6, so S=25/6S=25/6 and E=25/18E=25/18.

At (m,b)=(0,0)(m,b)=(0,0), ∇E=(−14,−20/3)\nabla E=(-14,-20/3). A simultaneous update with α=0.05\alpha=0.05 gives (7/10,1/3)(7/10,1/3) and lowers EE from 38/338/3 to 1789/450≈3.9755561789/450\approx3.975556. Repeat by computing both gradient sums before assigning either parameter.

Collect the parameters in θ=(m,b)T\theta=(m,b)^T and the observations in y=(y1,…,yn)Ty=(y_1,\ldots,y_n)^T. Let row ii of the design matrix XX be (xi,1)(x_i,1), so entry ii of XθX\theta is the prediction mxi+bmx_i+b. Then E=∥Xθ−y∥22/nE=\|X\theta-y\|_2^2/n, where the squared Euclidean norm sums the squared residuals, and the Hessian is H=2XTX/nH=2X^TX/n. For every vv, vTHv=2∥Xv∥22/n≥0v^THv=2\|Xv\|_2^2/n\ge0, proving convexity. The solution is unique only when the columns of XX are linearly independent (full column rank); for slope plus intercept this requires at least two distinct xix_i. If all xi=cx_i=c, only mc+bmc+b is identifiable and many parameter pairs fit equally well.

The step-size bound follows from the error dynamics. Here HH is symmetric positive definite and θ∗\theta_* is the unique minimizer. For ek=θk−θ∗e_k=\theta_k-\theta_*, the quadratic gradient is HekHe_k, hence

ek+1=(I−αH)ek.e_{k+1}=(I-\alpha H)e_k.

Along an eigenvector of HH with eigenvalue λ>0\lambda>0, the error component is multiplied by 1−αλ1-\alpha\lambda. Convergence from every starting point requires ∣1−αλ∣<1|1-\alpha\lambda|<1 for every eigenvalue, giving 0<α<2/λmax⁡(H)0<\alpha<2/\lambda_{\max}(H). 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, λmax⁡(H)=(17+265)/3≈11.092940\lambda_{\max}(H)=(17+\sqrt{265})/3\approx11.092940, so fixed-step descent converges for 0<α<2/λmax⁡≈0.1802950<\alpha<2/\lambda_{\max}\approx0.180295. 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 XTXX^TX.

Explore connectionsOpen network