Ridge Regression
A closed-form one-feature ridge fit shrinks the slope. Centre the data so the intercept is not penalized; lambda zero recovers least squares.
numerator, spread = 4, 2
print(numerator / spread)
print(numerator / (spread+2))From arithmetic to an experiment
A closed-form one-feature ridge fit shrinks the slope. Centre the data so the intercept is not penalized; lambda zero recovers least squares. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
ridge(xs, ys, penalty) returns (slope, intercept) minimizing sum squared error + penalty*slope**2. Inputs are nonempty and penalty >= 0. Return slope zero when the denominator is zero.
The explanation opens here once every check passes.