All problems
Least Squares Through the Origin
Problem
Fit the model y = k*x through the origin to paired data by least squares, where
k = (x . y) / (x . x)
Return the predicted values [k*x[0], k*x[1], ...]. If x is the zero vector, return zeros.
Hint
The slope estimate is the projection of y onto the direction x.
Test cases
case 1
in: [[1,2],[2,4]]
expected: [2,4]
case 2
in: [[1,2,3],[1,2,3]]
expected: [1,2,3]
case 3
in: [[1,1],[1,2]]
expected: [1.5,1.5]
case 4
in: [[0,0],[1,2]]
expected: [0,0]
Starter codePython
def least_squares_1d(x, y):
# Your code here
pass4 test cases · run your code against them in the editor