Skip to content
DeepForgeby svx
All problems
HardStatisticsst-092

Weighted Least Squares Slope

Problem

Return the weighted least squares slope through the origin, b = sum(w*x*y) / sum(w*x^2). Return 0.0 when the weighted sum of x^2 is zero.

Hint

Each observation contributes in proportion to its weight.

Test cases

case 1
in: [[1,2,3],[2,4,6],[1,1,1]]
expected: 2
case 2
in: [[1,2,3],[1,1,1],[1,2,3]]
expected: 0.3888888888888889
case 3
in: [[1,2],[3,4],[0,0]]
expected: 0
case 4
in: [[2,4],[1,3],[1,1]]
expected: 0.7
case 5
in: [[1,1],[5,5],[2,2]]
expected: 5
Starter codePython
def wls_slope(x, y, w):
    # Your code here
    pass

5 test cases · run your code against them in the editor