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$.
Notes19 past questions from this week
Kernel regression
Kernels, alpha, predictions
- Orientation: what you will learn and why
- Big picture: concept map, three regressions compared, and the recipe
- Start here: inner products, expanding squares, a $3\times3$ system
- Inner product $x^Tx'=\sum_jx_jx'_j$.
- $(a+b)^2=a^2+2ab+b^2$.
- Solve $K\alpha=y$ by elimination, then substitute back to check.
- The idea: feature maps, kernels and the prediction formula
- $k(x,x')=\phi(x)^T\phi(x')$ is a similarity score.
- Polynomial kernel: $k(x,x')=(x^Tx'+c)^p$: dot product, add $c$, raise to $p$.
- The lifted space can be huge, $\dim\phi=\binom{d+p}{p}$, which is why computing $k$ directly is cheaper.
- A kernel changes how similarity is measured, not the data itself.
- The kernel matrix $K$ and solving for $\alpha$
- $K_{ij}=k(x_i,x_j)$. $K$ is $n\times n$, symmetric and positive semi-definite.
- $\alpha=K^{-1}y$, which needs $K$ to be invertible.
- If $K$ is invertible, the model reproduces every training label: $\hat y(x_j)=y_j$.
- Fill the matrix entry by entry, then check symmetry before solving.
- Computing predictions when $\alpha$ is given
- $\hat y(x)=\sum_i\alpha_i\,k(x_i,x)$.
- Every training point contributes, weighted by $\alpha_i$.
- Zero test vector with the polynomial kernel: every kernel value is $c^p$, so $\hat y(0)=c^p\sum_i\alpha_i$.
- Make a small table of $k(x_i,x)$ values first, then multiply and add.
- Parametric or not, and the cost of kernel regression
- Non-parametric: the number of coefficients grows with $n$.
- Prediction needs all training points.
- Training builds and solves with an $n\times n$ matrix.
- Linear regression on fixed features is parametric: a weight vector of fixed length.
Notes9 past questions from this week
Week 6: Probabilistic view, Ridge, Lasso and cross-validationAll MLT topics and weeks