All problems
Hat Matrix Leverage
Problem
For simple linear regression with an intercept, compute the leverage (hat matrix diagonal) of each observation: h_i = 1/n + (x_i - mean_x)^2 / Sxx, where Sxx = sum((x_j - mean_x)^2).
If Sxx is 0, return 1/n for every point. Empty input returns [].
Hint
Leverage grows with the squared distance of x_i from the mean; all leverages sum to the number of parameters.
Test cases
case 1
in: [[1,2,3,4]]
expected: [0.7,0.3,0.3,0.7]
case 2
in: [[5,5,5]]
expected: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
case 3
in: [[0,1]]
expected: [1,1]
case 4
in: [[7]]
expected: [1]
Starter codePython
def leverage_values(X):
# Your code here
pass4 test cases · run your code against them in the editor