Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncated SVD implementation #73

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using LinearAlgebra: qr, I, norm
using LowRankApprox: pqrfact
using IterativeSolvers
using .BayesianLinear
using LinearAlgebra: SVD, svd

@doc raw"""
`struct QR` : linear least squares solver, using standard QR factorisation;
Expand Down Expand Up @@ -148,3 +149,39 @@ function SKLEARN_ARD(; n_iter = 300, tol = 1e-3, threshold_lambda = 10000)
end

# solve(solver::SKLEARN_ARD, ...) is implemented in ext/

@doc raw"""
`struct Truncated_SVD` : linear least squares solver
```math
θ = \arg\min \| A P^{-1} \theta - y \|^2
```
Constructor
```julia
ACEfit.Truncated_SVD(; lambda = 0.0, P = nothing)
```
where
* `rtol` : relative tolerance
* `P` : right-preconditioner / tychonov operator
"""
struct Truncated_SVD
rtol::Number
P::Any
end

Truncated_SVD(; rtol = 1e-9, P = I) = Truncated_SVD(rtol, P)

function trunc_svd(USV::SVD, Y, rtol)
U, S, V = USV # svd(A)
Ikeep = findall(x -> x > rtol, S ./ maximum(S))
U1 = @view U[:, Ikeep]
S1 = S[Ikeep]
V1 = @view V[:, Ikeep]
return V1 * (S1 .\ (U1' * Y))
end

function solve(solver::Truncated_SVD, A, y)
AP = A / solver.P
θP = trunc_svd(svd(AP), y, solver.rtol)
return Dict{String, Any}("C" => solver.P \ θP)
end

7 changes: 7 additions & 0 deletions test/test_linearsolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ results = ACEfit.solve(solver, A, y)
C = results["C"]
@show norm(A * C - y)
@show norm(C)

@info(" ... Truncated_SVD")
solver = ACEfit.Truncated_SVD()
results = ACEfit.solve(solver, A, y)
C = results["C"]
@show norm(A * C - y)
@show norm(C)
Loading