An ice-cream van has sold ice cream on only a handful of days and wants a line sales ≈ a + b·temperature. With so few days the least-squares slope is unreliable, so the owner prefers slopes near zero unless the data insists, and minimises
loss(a, b) = Σ (a + b·xs[i] - ys[i])² + λ · b²
The penalty is on the slope b only, never on the intercept a.
Write ridge_lines(xs, ys, lams) that returns a list with one tuple (a, b) per value λ in lams, in the same order: the intercept and slope that minimise the loss for that λ.
Examples
Input: xs = [0, 1, 2, 3], ys = [0, 1, 2, 3], lams = [0, 1, 5]
Output: [(0.0, 1.0), (0.25, 0.8333333333333334), (0.75, 0.5)]
Explanation: without a penalty the line is y = x. With λ = 5 the slope is halved, and the
intercept moves so that the line still passes through the mean point (1.5, 1.5).
Input: xs = [2, 2, 2], ys = [1, 4, 7], lams = [0, 3]
Output: [(4.0, 0.0), (4.0, 0.0)]
Explanation: all temperatures are equal, so the slope explains nothing; the best
intercept is the mean sale.
Constraints
1 <= len(xs) <= 10**4,0 <= λ <= 10**6- if all
xsare equal, the answer is the slope 0 and the mean ofys, also forλ = 0 - floats are compared with a tolerance of
1e-6
Goals
- Minimise a squared error plus a penalty on the slope
- See the slope shrink towards zero as the penalty grows while the intercept is left free
- Derive the ridge line from the centred sums of Level 3