ML Foundations

Learn / ML Foundations

ML Foundations: Least Squares and Linear Regression Math

Advanced quiz on residuals, normal equations, design matrices, projections, and regularized linear regression.

Learning path

0%

0 of 4 sections marked complete · about 31 minutes

Learning objectives

What you will be able to explain

  • What does least squares minimize?
  • Fit a line through two points
  • Predict with the fitted line
  • Compute a residual
  • Compute the gradient of a small squared-error objective
  • Which formula gives the normal-equation solution?

Section 01

What does least squares minimize? to Predict with the fitted line

01

What does least squares minimize?

Least squares minimizes squared errors between predictions and targets. This produces a convex objective and leads to the normal equations in the linear case.

Guided checkpoint

Choose the standard objective.

02

Fit a line through two points

The point with x=0x = 0 gives w0=1w_0 = 1. Then 1+2w1=51 + 2w_1 = 5 implies w1=2w_1 = 2. With two points and two parameters, the line can match them exactly unless they are degenerate.

Guided checkpoint

Consider the model y^=w0+w1x\hat{y} = w_0 + w_1 x. Find parameters that exactly fit the two points (0,1)(0, 1) and (2,5)(2, 5).

03

Predict with the fitted line

Substituting x=3x = 3 gives y^=1+23=7\hat{y} = 1 + 2 \cdot 3 = 7.

Guided checkpoint

Using y^=1+2x\hat{y} = 1 + 2x, compute the prediction for x=3x = 3.

Section 02

Compute a residual to Which formula gives the normal-equation solution?

01

Compute a residual

The residual is the observed value minus the predicted value, so r=87=1r = 8 - 7 = 1. Squaring residuals gives the least-squares objective.

Guided checkpoint

Define the residual as r=yy^r = y - \hat{y}. If the true target is y=8y = 8 and the prediction is y^=7\hat{y} = 7, what is the residual?

02

Compute the gradient of a small squared-error objective

At (0,0)(0, 0) the predictions are (0,0)(0, 0), so the errors (y^y)(\hat{y} - y) are (1,3)(-1, -3). Summing derivatives gives L/w0=1+(3)=4\partial L/\partial w_0 = -1 + (-3) = -4 and L/w1=(1)0+(3)1=3\partial L/\partial w_1 = (-1)\cdot 0 + (-3)\cdot 1 = -3.

Guided checkpoint

Use the model y^=w0+w1x\hat{y} = w_0 + w_1 x and the loss L=12i(y^iyi)2L = \frac{1}{2}\sum_i (\hat{y}_i - y_i)^2 on data (x,y)=(0,1)(x, y) = (0, 1) and (1,3)(1, 3). Compute the gradient at (w0,w1)=(0,0)(w_0, w_1) = (0, 0).

03

Which formula gives the normal-equation solution?

Setting the gradient of the least-squares objective to zero gives the normal equations XXw=XyX^\top X w = X^\top y, whose solution is (XX)1Xy(X^\top X)^{-1}X^\top y when invertible.

Guided checkpoint

Choose the closed-form least-squares solution when XXX^\top X is invertible.

Section 03

Compute entries of XXX^\top X to Linear regression facts: true or false

01

Compute entries of XXX^\top X

The first column is (1,1,1)(1,1,1)^\top and the second is (0,1,2)(0,1,2)^\top. Their inner products produce XX=[3335]X^\top X = \begin{bmatrix} 3 & 3 \\ 3 & 5 \end{bmatrix}.

Guided checkpoint

Let the design matrix be X=[101112]X = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}. Compute the distinct entries of XXX^\top X.

02

What is the geometric meaning of least squares?

The least-squares prediction is the orthogonal projection of yy onto the span of the columns of XX. The residual is orthogonal to that column space at the optimum.

Guided checkpoint

Choose the best interpretation.

03

Linear regression facts: true or false

Least squares remains well defined even when exact fitting is impossible. Rank deficiency breaks uniqueness, ridge stabilizes the system, and the linear-regression objective is convex in the parameters.

Guided checkpoint

Mark each statement as true or false.

Section 04

Differentiate an L2L_2 penalty to What issue appears when the columns of XX are linearly dependent?

01

Differentiate an L2L_2 penalty

Because R(w)=λiwi2R(w) = \lambda \sum_i w_i^2, its gradient is 2λw2\lambda w. Here that gives 20.1(2,1)=(0.4,0.2)2 \cdot 0.1 \cdot (2, -1) = (0.4, -0.2).

Guided checkpoint

Let the regularization term be R(w)=λw22R(w) = \lambda \|w\|_2^2 with w=(2,1)w = (2, -1) and λ=0.1\lambda = 0.1. Compute R(w)\nabla R(w).

02

What issue appears when the columns of XX are linearly dependent?

Linear dependence means some columns do not add new information, so different parameter vectors can produce the same predictions. This is why rank and regularization matter.

Guided checkpoint

Choose the best answer.

Knowledge check

Turn understanding into recall.

The quiz now follows the same concepts in scored form. You can return to this lesson from the quiz whenever a gap appears.