Skip to content
GATEiit

GATE DA · Machine Learning

Simple linear regression, multiple linear regression and ridge regression

Fitting a line or hyperplane by least squares: the closed-form slope and intercept, the normal equations for many features, and ridge regression, which adds an L2 penalty to shrink coefficients.

Before you start

This topic comes before: Bias–variance trade-off, Logistic regression

Concept notes

1. Simple linear regression

The model is y=β0+β1x+ε. Ordinary least squares (OLS) picks β0,β1 to minimise the residual sum of squares RSS=∑i=1n(yi−β0−β1xi)2. Setting both partial derivatives to zero gives β^1=SxySxx=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2,β^0=yˉ−β^1xˉ.

Key facts:

  • The fitted line always passes through (xˉ,yˉ).
  • Equivalently β^1=rsysx, where r is the correlation; the slope has the sign of r.
  • With an intercept, the residuals sum to zero and are uncorrelated with x.
  • In simple regression R2=r2.
  • Swapping the roles of x and y gives a different line (slope rsx/sy) unless ∣r∣=1.

Interpretation: β^1 is the change in predicted y for a one-unit increase in x. Predicting far outside the range of the training x (extrapolation) is unreliable.

2. Multiple linear regression and the normal equations

With d features, stack the data in a design matrix X of size n×(d+1) whose first column is all ones. The model is y=Xβ+ε and OLS minimises ∥y−Xβ∥2.

The gradient −2X⊤(y−Xβ) set to zero gives the normal equations X⊤Xβ^=X⊤y⇒β^=(X⊤X)−1X⊤y.

  • The solution is unique exactly when X has full column rank (X⊤X invertible). Perfectly collinear features, or d+1>n, break this.
  • The residual e=y−Xβ^ is orthogonal to every column of X: X⊤e=0.
  • Each coefficient β^j is the effect of feature j holding the other features fixed; it can differ in sign from the simple-regression slope on xj alone.
  • Building X⊤X costs O(nd2) and inverting it O(d3); for very large d, gradient descent is used instead. The squared-error loss is convex, so gradient descent with a suitable step reaches the same minimum.
y ŷ = Xβ̂ e = y − ŷ column space of X
Least squares as projection: the residual is perpendicular to the column space of X

3. The hat matrix

The fitted values are y^=Xβ^=Hy with the hat matrix H=X(X⊤X)−1X⊤.

H is the orthogonal projection onto the column space of X, so:

  • H is symmetric (H⊤=H) and idempotent (H2=H).
  • I−H is also a projection; it maps y to the residual vector e=(I−H)y.
  • trace(H)=rank(X)=d+1 for full column rank; this is the number of fitted parameters.
  • Its eigenvalues are only 0 and 1.
  • The diagonal entry hii is the leverage of point i, with 0≤hii≤1 and ∑ihii=d+1.

The decomposition ∥y−yˉ1∥2=∥y^−yˉ1∥2+∥e∥2 (total = explained + residual) holds because y^−yˉ1 and e are orthogonal when the model has an intercept. This is why R2=1−RSS/TSS lies between 0 and 1 on training data.

4. Ridge regression

Ridge regression adds an L2 penalty to least squares: min⁡β  ∥y−Xβ∥2+λ∥β∥2,λ≥0. Setting the gradient to zero gives β^ridge=(X⊤X+λI)−1X⊤y.

  • For λ>0, X⊤X+λI is always invertible (its eigenvalues are those of X⊤X plus λ, all positive). Ridge therefore works with collinear features and with d>n.
  • λ=0 gives OLS. As λ→∞, all penalised coefficients shrink towards 0, but none becomes exactly zero (unlike lasso, which uses an L1 penalty).
  • Ridge adds bias but reduces variance; a well-chosen λ (by cross-validation) lowers test error.
  • The intercept is usually not penalised, and features should be standardised first, because the penalty depends on the scale of each feature.
  • One feature, no intercept: β^=∑xiyi∑xi2+λ, versus OLS ∑xiyi∑xi2.
OLS Ridge
Penalty none λ∥β∥2
Needs X⊤X invertible yes no (λ>0)
Bias zero (under the model) some
Variance higher lower

Formulas to remember

OLS slope
β^1=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2=r sysx
OLS intercept
β^0=yˉ−β^1xˉ
Normal equations
X⊤X β^=X⊤y
OLS solutionNeeds X to have full column rank
β^=(X⊤X)−1X⊤y
Hat matrix
H=X(X⊤X)−1X⊤,H2=H,tr⁡(H)=d+1
Ridge solution
β^ridge=(X⊤X+λI)−1X⊤y
Ridge, one feature, no intercept
β^=∑xiyi∑xi2+λ

Common mistakes

  • Dividing Sxy by Syy instead of Sxx; that gives the slope of x on y, not y on x.
  • Penalising the intercept in ridge regression, or applying ridge to unscaled features.
  • Saying ridge performs feature selection; it shrinks but does not zero out coefficients (lasso does).
  • Reading a multiple-regression coefficient as the simple-regression slope; it is the effect with the other features held fixed.
  • Forgetting the column of ones in X when the model has an intercept; it changes X⊤X and the answer.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Fit a simple regression line

numerical

Data: x=(1,2,3,4,5), y=(2,4,5,4,5). Find the least-squares line and predict y at x=6.

  1. Step 1: Find the means.

    Try it on paper first.

Example 2: Normal equations with two features

numerical

No intercept. Rows of X: (1,0), (0,1), (1,1); y=(1,2,4). Find β^ by OLS and by ridge with λ=1.

  1. Step 1: Form $X^\top X$ and $X^\top y$.

    Try it on paper first.

Example 3: Ridge with one feature

numerical

No intercept. x=(1,2,3), y=(2,4,7). Find the OLS slope and the ridge slope for λ=1.

  1. Step 1: Compute the two sums.

    Try it on paper first.

Example 4: When does OLS fail and ridge rescue it?

conceptual

A dataset has 50 rows and features height in cm and height in inches, plus an intercept. Why is OLS not unique, and does ridge fix it?

  1. Step 1: Check the rank of $X$.

    Try it on paper first.

Previous year questions

4 official GATE DA questions on this topic, newest first.

Practise these 4 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.