CS 460G Midterm Hub / Lectures / Week 3 Lecture 1
Week 3 · Lecture 1

Linear Regression

Least squares, the normal equations, and the regression metrics.

mathshort answer

The regression twin of W2L3. Same skeleton — model, loss, optimisation, metrics — with a continuous target.

What a regression problem needs

The deck builds the list one bullet at a time:

  1. Features (inputs)
  2. Training examples — pairs of inputs and outputs
  3. A model — a function approximating the relationship between $x$ and $y$
  4. A loss — how well the model predicts on the training examples
  5. Optimisation — finding the parameters where the loss is minimal
The five ingredients of a regression problem
The five ingredients of a regression problem · slide 19

Any "explain the regression pipeline" question is answered by walking that list.

The model

$$\hat y = w_0 + w_1 x$$

or, in the multi-dimensional case,

$$\hat y(x) = w_0 + w_1x_1 + w_2x_2 + \dots + w_dx_d = w^T x \text{ with } x_0=1$$

Multi-dimensional linear regression
Multi-dimensional linear regression · slide 46

Loss: squared error

Least squares is the default because it is differentiable, convex in $w$, and its optimum has a closed form.

$$L(h(x),y) = \big(h(x)-y\big)^2, \qquad J(w) = \frac{1}{2N}\sum_{n=1}^{N}\big(h(x_n)-y_n\big)^2$$

Squared loss for linear regression
Squared loss for linear regression · slide 30

The $\tfrac12$ is cosmetic — it cancels the 2 that comes down from the derivative.

Optimisation, two routes

Iterative — gradient descent, exactly as in classification:

$$w \leftarrow w - \alpha \nabla_w J(w)$$

Gradient descent for regression
Gradient descent for regression · slide 32

Closed form — write the problem as $Aw = b$ and solve:

$$A = \begin{bmatrix} 1 & x_{11} & \cdots & x_{1d} \\ \vdots & & & \vdots \\ 1 & x_{N1} & \cdots & x_{Nd}\end{bmatrix},\quad w = \begin{bmatrix}w_0\\ \vdots \\ w_d\end{bmatrix},\quad b=\begin{bmatrix}y_1\\ \vdots \\ y_N\end{bmatrix}$$

If $A$ is invertible the ERM solution is $w = (A^TA)^{-1}A^Tb$ — the normal equations.

Matrix form and the closed-form solution
Matrix form and the closed-form solution · slide 44

$(A^TA)$ is invertible only when the columns are linearly independent. If features are collinear (or $d > N$) you need the pseudo-inverse, or — which is the connection to W6L3 — ridge regularisation, because adding $\lambda I$ makes the matrix invertible again.

Gradient descent variants — a guaranteed definitional question

VariantWhat one update usesCost per update
Batchthe error over all training samplesfull pass, exact gradient
Stochastic (SGD)the error over one training samplecheapest, noisiest
Mini-batchthe error over a subsetthe middle ground, and what every deep-learning notebook uses

And the two words students mix up:

  • Epoch — processing the entire training set once.
  • Iteration — processing the current training samples once.
Batch, stochastic and mini-batch; epoch vs iteration
Batch, stochastic and mini-batch; epoch vs iteration · slide 42

Regression metrics

$$MSE = \frac{1}{N}\sum_{i=1}^{N}(y_i-\hat y_i)^2 \qquad RMSE = \sqrt{MSE} \qquad MAE = \frac{1}{N}\sum_{i=1}^{N}|y_i-\hat y_i|$$

$$R^2 = 1 - \frac{RSS}{TSS}, \qquad RSS=\sum (y_i-\hat y_i)^2,\quad TSS=\sum(y_i-\bar y)^2$$

The six regression metrics
The six regression metrics · slide 47

Reading of $R^2$: the fraction of the variance in $y$ explained by the model. $R^2=1$ perfect, $R^2=0$ no better than predicting the mean, negative = worse than the mean. It is unitless, which is why it is comparable across datasets while MSE is not.

"Name typical loss functions for classification and regression." Classification: 0-1 loss, cross-entropy / logistic (negative log-likelihood), hinge loss (SVM). Regression: squared error (L2 / least squares), absolute error (L1 / MAE), Huber. Say one sentence on why each: cross-entropy is the negative log-likelihood of a Bernoulli/Categorical model and is smooth where 0-1 is not; hinge is the tightest convex upper bound on 0-1 that also enforces a margin; squared error is differentiable and gives a closed form; absolute error is robust to outliers but non-differentiable at zero.