S1413 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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

Name it

What the lab computes

  • Solving: Gaussian elimination with partial pivoting (choose the largest remaining entry in each column as the pivot) solves A𝐱=𝐛; solving for each column of the identity gives A−1.
  • Ridge regression: 𝐰=(XTX+λI)−1XT𝐭, here penalizing every weight, the intercept included.
  • Leave-one-out residuals: ei/(1−hii) with hii=𝐱iT(XTX+λI)−1𝐱i.
  • Bayesian posterior: SN=(αI+βXTX)−1, 𝐦N=βSNXT𝐭; predictive mean 𝐱T𝐦N and variance 1/β+𝐱TSN𝐱.
  • The evidence's α: iterate γ=M−αtr(SN) and α=γ/𝐦NT𝐦N, 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)?