All problems
Non-Negative Least Squares Step
Problem
Take one projected gradient step for non-negative least squares with a scalar weight.
Compute grad = sum(xi * (w * xi - yi)), set w = w - lr * grad, then project back onto w >= 0. Return the new weight.
Hint
Projection onto the non-negative orthant is just max(0, w).
Test cases
case 1
in: [0,[1,2],[2,4],0.1]
expected: 1
case 2
in: [1,[1,1],[0,0],0.5]
expected: 0
case 3
in: [-0.5,[1],[2],0.1]
expected: 0
case 4
in: [2,[],[],0.1]
expected: 2
Starter codePython
def nnls_step(w, X, y, lr):
# Your code here
pass4 test cases · run your code against them in the editor