Skip to content
nonconvexcard

Implement ridge and find the optimal lambda

CodingIntermediate⏱ 1 hourClassical MLridgecross-validationimplementationconditioning

Problem

Implement ridge regression via the SVD and empirically verify the ridge-beats-OLS theorem.

  1. Plot test MSE against λ\lambda on a log axis and confirm the minimum is at λ>0\lambda>0.
  2. Confirm the optimal λ\lambda grows with the noise level and with p/np/n.
  3. Implement leave-one-out cross-validation in closed form using the hat matrix, and check it against brute-force LOOCV.

Implement it, then check the implementation against something you can verify independently.

Write it

Solve it here4 tests · Python runs in your browser
⌘/Ctrl + ↵ to run · limit 30s · draft saved in this browser
4 tests will run against your code. The first run downloads the Python runtime — several megabytes, once per browser.

Solution

Opening this before you have a real attempt on paper is the fastest way to feel like you understand something you cannot reproduce.

Related problems

All problems
SolvedProblemFieldTypeDifficultyTime
Least squares as orthogonal projectionClassical MLProofFoundation15 min
Frobenius versus operator norm for random matricesRandom MatricesNumericalIntermediate30 min
Implement backpropagation from scratch runDeep LearningCodingIntermediate3 hours
Implement kernel ridge regression runClassical MLCodingIntermediate1 hour
Implement multi-head attention from scratch runDeep LearningCodingIntermediate3 hours