Key points, notes and past questions
Linear regression and gradient descent
Least squares, projections, MLE view, gradient descent
- Orientation: what you will learn and why
- Big picture: concept map, four roads and the shape table
- Start here: vectors, matrices and slopes, from zero
- $X$ is $d\times n$: columns are data points.
- Residual $=w^Tx_i-y_i$. The loss is the sum of squared residuals.
- Positive slope: the function rises to the right. To go down, step the opposite way.
- $\nabla L$ points in the direction of fastest increase.
- The model, the loss and how to read a fitted line
- $L(w)=\sum_i(w^Tx_i-y_i)^2=\|X^Tw-y\|^2$.
- 'Linear' means linear in the weights $w$, not in $x$.
- Squared loss punishes big errors much more than small ones.
- A least-squares line balances its positive and negative residuals.
- The optimal weight: normal equations, intercept and scaling
- $XX^Tw=Xy$, so $w^*=(XX^T)^{-1}Xy$ when $XX^T$ is invertible.
- One feature, no intercept: $w^*=\sum x_iy_i/\sum x_i^2$.
- With an intercept: $w_1=\sum(x_i-\bar x)(y_i-\bar y)/\sum(x_i-\bar x)^2$ and $w_0=\bar y-w_1\bar x$. The residuals then sum to 0.
- Scaling: $y\to cy$ gives $w^*\to cw^*$. $x\to cx$ gives $w^*\to w^*/c$.
- At the optimum, $X(X^Tw^*-y)=0$.
- Geometry: projection, orthogonality and spans
- $\hat y=X^Tw^*$ is the projection of $y$ onto the span of the columns of $X^T$.
- The residual $y-X^Tw^*$ is perpendicular to every feature column.
- Zero training error happens exactly when $y$ already lies in that span.
- Pythagoras: $\|y\|^2=\|X^Tw^*\|^2+\|y-X^Tw^*\|^2$.
- Which model can lower the training loss?
- A bigger (nested) model has best training loss that is lower or equal.
- Lower training loss does not mean better predictions on new data.
- Zero training error can still give bad predictions.
- Compare models by checking whether one contains the other.
- Gradient descent: updates, learning rate, SSE versus MSE
- $w^{(t+1)}=w^{(t)}-\eta\nabla L(w^{(t)})$ with $\nabla L=2X(X^Tw-y)$.
- If the loss has a $\frac12$ in front, the $2$ disappears.
- One feature, plain sum loss: converges iff $0<\eta<1/\sum x_i^2$.
- Each step multiplies the distance to the optimum by $(1-2\eta\sum x_i^2)$.
- SSE sums the errors and MSE averages them: same minimiser, different gradient size.
- Probabilistic view: why least squares is maximum likelihood
- Gaussian noise plus maximum likelihood gives least squares.
- The noise variance $\sigma^2$ does <b>not</b> change $\hat w$.
- $\hat\sigma^2_{ML}=\text{SSE}/n$: divide by $n$ (not $n-1$) and fit $w$ first.
- The negative log-likelihood is a constant plus $\frac1{2\sigma^2}\sum(y_i-w^Tx_i)^2$.
Notes4 past questions from this week
Ridge, Lasso and MAP
Regularisation, priors, sparsity
- Orientation: what you will learn and why
- Big picture: one idea in three forms, and the comparison table
- Start here: the pieces you need
- $\|w\|^2=\sum w_j^2$ and $\|w\|_1=\sum|w_j|$.
- $\begin{bmatrix}a&b\\c&d\end{bmatrix}^{-1}=\frac1{ad-bc}\begin{bmatrix}d&-b\\-c&a\end{bmatrix}$.
- Bayes: posterior $\propto$ likelihood $\times$ prior.
- Gaussian prior is bell-shaped (smooth); Laplace prior has a sharp peak at 0.
- Ridge regression: the formula, the arithmetic and what $\lambda$ does
- $L_R(w)=\|X^Tw-y\|^2+\lambda\|w\|^2$ and $\hat w_R=(XX^T+\lambda I)^{-1}Xy$.
- For $\lambda>0$, $XX^T+\lambda I$ is always invertible, so $\hat w_R$ always exists.
- One feature: $\hat w_R/\hat w_{MLE}=\sum x_i^2/(\sum x_i^2+\lambda)$. The label vector $y$ cancels.
- $\lambda=0$ gives plain least squares; large $\lambda$ shrinks strongly (risk of underfitting); $\lambda\to\infty$ gives $w\to0$.
- If many $w$ give the same minimal error, ridge picks the one with the smallest norm.
- The constrained view and choosing a weight vector
- Constrained ridge: $\min\|X^Tw-y\|^2$ subject to $\|w\|^2\le\theta$.
- Procedure: discard candidates that break the norm limit, then take the smallest error among the rest.
- A larger $\lambda$ corresponds to a smaller $\theta$ (a tighter limit).
- Penalty form and constraint form describe the same family of solutions.
- Lasso ($L_1$): sparsity, the diamond picture and comparing weights
- Lasso loss: SSE $+\lambda\|w\|_1$.
- The $L_1$ ball is a diamond; its corners lie on the axes, so the optimum often has some weights exactly $0$.
- Lasso is sparse (built-in feature selection). Ridge shrinks all weights but rarely makes one exactly $0$.
- One feature: ridge scales the weight down; lasso subtracts an amount and clips at $0$.
- Total loss at the optimum $=$ the error term plus $\lambda\|w\|_1$ at that $w$.
- MAP: regularisation as a prior
- MAP maximises likelihood $\times$ prior, i.e. posterior $\propto P(y|X,w)P(w)$.
- Gaussian prior (variance $\gamma^2$) with Gaussian noise (variance $\sigma^2$) gives ridge with $\lambda=\sigma^2/\gamma^2$.
- Laplace prior gives the $L_1$ penalty, i.e. lasso.
- MLE is the case with no prior (no regularisation).
- With plenty of data the likelihood dominates and MAP moves toward MLE.
- Check the convention in the question: where a $\frac12$ appears changes what number $\lambda$ equals.
Notes21 past questions from this week
Cross-validation and model comparison
k-fold, LOOCV, choosing hyperparameters
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: a line through points
- $w_1=S_{xy}/S_{xx}$ and $w_0=\bar y-w_1\bar x$, where $S$ are sums of centred products.
- Training data fits the model, validation data chooses hyperparameters, test data reports the final number once.
- Never choose a model by looking at the test data.
- Reading training and validation errors; choosing a hyperparameter
- Choose the hyperparameter with the smallest <b>validation</b> error.
- Training error usually keeps decreasing as the model gets more complex.
- Validation error is typically U-shaped: too simple underfits, too complex overfits.
- Large gap between low training error and high validation error means overfitting.
- How k-fold cross-validation works: counting models and sizes
- $k$ models are trained per candidate setting.
- Training size $=n\frac{k-1}{k}$, validation size $=\frac nk$.
- Total models in a grid search $=(\text{number of settings})\times k$.
- Every point is used for validation exactly once and for training $k-1$ times.
- Leave-one-out cross-validation for a line
- LOOCV means $k=n$: $n$ models, each trained on $n-1$ points.
- Refit the line every time. Do not reuse the full-data line.
- Error for each left-out point = (actual − predicted) squared, then average over $n$.
- Write the remaining points and their $\bar x,\bar y$ before computing each fit.
- Model comparison: zero training error, geometry and true/false facts
- Guaranteed zero training error (distinct points): look-up rule, fully grown decision tree, 1-NN.
- Not guaranteed: $k$-NN with $k=5$, naive Bayes, a linear model, predicting the mean.
- Tree boundaries are axis-parallel; ridge and lasso are linear; 1-NN is piecewise non-linear.
- Depth of a tree is a hyperparameter chosen by cross-validation.
- k-NN is lazy and non-parametric, and its $k$ counts neighbours.
Notes13 past questions from this week
Week 5: Linear regression and kernel regressionWeek 7: k-NN and decision treesAll MLT topics and weeks