JuliaLinearAlgebra / NonNegLeastSquares.jl

Some nonnegative least squares solvers in Julia
MIT License
46 stars 11 forks source link

Coordinate Descent? #22

Closed caseykneale closed 3 years ago

caseykneale commented 3 years ago

Some people in julia slack helped me tune a coordinate descent NNLS. Not sure if this interests you, but, here's some code,

function NNLS_CD(X, Y; ϵ = 1e-9, max_iters = 300)
    rows,vars = size(X)
    XTX,XTY = transpose(X) * X, transpose(X) * Y
    x  = zeros(vars)
    μ = -XTY
    Hxf = similar(XTY)
    @inbounds for iter in 1:max_iters
        Hxf .= XTX * x .- XTY
        all(>=(-ϵ), Hxf) && break
        for v in 1:vars
            initial = x[v]
            x[v,1]  = max(x[v] - μ[v] / XTX[v,v], 0.0)
            ∇       = x[v] - initial
            μ      .+= ∇ .* @views XTX[:,v]
        end
    end

    return x
end

variables = 100
X    = randn(200, variables)
beta = rand(variables, 1)
Y    = X * beta
r = NNLS_CD(X, Y; ϵ=1e-9, max_iters=300)
sum(abs,beta .- r)
JeffFessler commented 3 years ago

could be useful. do you want to make a PR? it would be important to test it too.

caseykneale commented 3 years ago

No problem I'll close this out and make a PR real quick