Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Created May 20, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bicycle1885/aeab104df88677a84f1e194734c0d362 to your computer and use it in GitHub Desktop.
Save bicycle1885/aeab104df88677a84f1e194734c0d362 to your computer and use it in GitHub Desktop.
Randomized SVD
# Randomized SVD
# --------------
#
# H. Li, G. C. Linderman, et al. "Algorithm 971: An Implementation of a
# Randomized Algorithm for Principal Component Analysis", ACM Transactions on
# Mathematical Software (TOMS), 2017
# DOI: https://doi.org/10.1145/3004053
#
# N. Halko, P. G. Martinsson, and J. A. Tropp, "Finding Structure with
# Randomness: Probabilistic Algorithms for Constructing Approximate Matrix
# Decompositions", SIAM Rev., 53(2), 217–288., 2011
# DOI: https://doi.org/10.1137/090771806
function rsvd(A::Matrix{T}, k::Integer; its=3, l=k+5) where T
m, n = size(A)
l = min(l, m, n)
@assert 0 < k ≤ l ≤ min(m, n)
Q::Matrix{T} = rand(eltype(T), n, l) .- T(0.5)
Y = A*Q
F = lufact(Y)
for i in 1:its
Y = A'F[:L]
F = lufact!(Y)
Y = A*F[:L]
if i < its
F = lufact!(Y)
else
F = qrfact!(Y)
end
end
Q = full(F[:Q])
B = Q'A
W, Σ, V = svd(B)
U = Q*W
return U[:,1:k], Σ[1:k], V[:,1:k]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment