All problems
Weighted Least Squares
Problem
Fit y = a + b*x by weighted least squares, minimizing sum(w_i * (y_i - a - b*x_i)^2).
With W = sum(w_i), use the weighted means mx = sum(w_i*x_i)/W and my = sum(w_i*y_i)/W, then b = sum(w_i*(x_i - mx)*(y_i - my)) / sum(w_i*(x_i - mx)^2) and a = my - b*mx. Return [a, b]; if W is 0 or the input is empty return [0.0, 0.0], and if the weighted denominator is 0 return [my, 0.0].
Hint
Everything is the simple linear regression formula with each observation scaled by its weight.
Test cases
case 1
in: [[1,2,3,4,5],[3,5,7,9,11],[1,1,1,1,1]]
expected: [1,2]
case 2
in: [[1,2,3],[1,2,10],[1,1,1]]
expected: [-4.666666666666667,4.5]
case 3
in: [[1,2,3],[1,2,10],[1,1,9]]
expected: [-5.478260869565219,5.108695652173914]
case 4
in: [[2],[5],[3]]
expected: [5,0]
Starter codePython
def weighted_least_squares(X, y, weights):
# Returns [intercept, slope]
# Your code here
pass4 test cases · run your code against them in the editor