All problems
Weighted Least Squares Slope Estimate
Problem
The weighted least squares slope is sum(w_i (x_i - xbar_w)(y_i - ybar_w)) / sum(w_i (x_i - xbar_w)^2), where the weighted means use the same weights.
Given the predictor values, responses, and weights, return the slope.
Hint
Center both variables with weighted means before the ratio.
Test cases
case 1
in: [[1,2,3],[2,4,6],[1,1,1]]
expected: 2
case 2
in: [[0,1,2],[1,1,3],[1,2,1]]
expected: 1
case 3
in: [[1,2],[3,5],[2,3]]
expected: 2
Starter codePython
def weighted_least_squares_slope(xs, ys, weights):
# Your code here
pass3 test cases · run your code against them in the editor