S14 · Lesson 13 · Regression lab
A linear solver with pivoting, ridge regression from it, leave-one-out residuals by the leverage shortcut checked against refitting, Bayesian posteriors with predictive bands, and prior precisions chosen by the evidence
Lessons 7 to 12 worked out regression with one or two weights by hand. Real fits have dozens, and the formulas only pay off as code. In this lab you'll build the pieces from scratch, without a library: a solver for the normal equations, ridge regression, leave-one-out cross-validation from a single fit, the Bayesian posterior with its predictive band, and the evidence's choice of prior. Each result gets checked against an independent calculation.
- The data are three points, (1, 1), (2, 2.5) and (3, 2.5), fitted by a line through the origin with noise precision β = 4.
- The curves are the Bayesian predictive mean and a band of ±2 predictive standard deviations, for the prior precision α on the slider.
- A broad prior (small α) gives the least-squares slope; a tight one pulls the line toward zero. The band never gets narrower than the noise, and widens with x because the slope is uncertain. The evidence's choice, α = 1.10, sits where the prior's width matches the slope.
Predict
A ridge fit leaves a residual of 0.3 at a point with leverage 0.6. You refit without that point. Its residual becomes
For least squares and ridge regression with a fixed λ, the shortcut is exact, not an approximation: 0.75. Task 2 checks it against refitting the model N times. One fit gives the whole leave-one-out cross-validation.
Name it
What the lab computes
- Solving: Gaussian elimination with partial pivoting (choose the largest remaining entry in each column as the pivot) solves ; solving for each column of the identity gives .
- Ridge regression: , here penalizing every weight, the intercept included.
- Leave-one-out residuals: with .
- Bayesian posterior: , ; predictive mean and variance .
- The evidence's α: iterate and , recomputing the posterior each time. The trace form gives the same γ as lesson 12's eigenvalue sum, without computing eigenvalues.
Check
Write solve(A, b) by Gaussian elimination with partial pivoting, inverse(A), and ridge_fit(X, t, lam), where X is a list of feature rows.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write loo_residuals(X, t, lam): every example's leave-one-out residual from one ridge fit, using the leverage shortcut.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write posterior(X, t, alpha, beta) returning (m, S), and predictive(x, m, S, beta) returning (mean, variance).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write evidence_alpha(X, t, beta, alpha=1.0, iters=200): re-estimate α by the evidence and return (α, γ).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the leave-one-out residual larger than the ordinary residual, and by exactly 1/(1 − h)?
A fitted model is pulled toward every training point, most strongly toward points with high leverage, those far from the others in input space whose own targets weigh heavily in their own predictions. Its ordinary residual at such a point is therefore smaller than its error on a genuinely new point. For a linear smoother the fitted value is a weighted sum of the targets, with weight on the point's own target, so removing the point and refitting shifts the prediction by an amount proportional to the residual itself. Working this through, the residual grows by the factor : modest for typical points, large for points that the fit was bending to reach.