All problems
Leave-One-Out MSE
Problem
Compute the leave-one-out mean squared error of a simple linear regression without refitting. Fit the OLS line, form residuals e_i, and use the PRESS shortcut sum((e_i / (1 - h_i))^2) / n with leverage h_i = 1/n + (x_i - mean_x)^2 / Sxx. Fewer than two points returns 0.0.
Hint
The PRESS formula deletes each point analytically, so no OLS refits are needed.
Test cases
case 1
in: [[1,2,3,4],[2,4,6,8]]
expected: 0
case 2
in: [[1,2,3,4],[1,3,5,8]]
expected: 0.44784580498866317
case 3
in: [[5,5,5],[1,2,4]]
expected: 3.4999999999999987
case 4
in: [[2,4,6,8],[1,5,4,8]]
expected: 3.684807256235827
case 5
in: [[1],[2]]
expected: 0
Starter codePython
def loo_mse(x, y):
# Your code here
pass5 test cases · run your code against them in the editor