Last chance! 50% off unlimited learning
Sale ends in
The augmented implicitly restarted Lanczos bidiagonalization algorithm (IRLBA) finds a few approximate largest (or, optionally, smallest) singular values and corresponding singular vectors of a sparse or dense matrix using a method of Baglama and Reichel. It is a fast and memory-efficient way to compute a partial SVD.
irlba(A, nv = 5, nu = nv, maxit = 1000, work = nv + 7, reorth = TRUE,
tol = 1e-05, v = NULL, right_only = FALSE, verbose = FALSE,
scale = NULL, center = NULL, shift = NULL, mult = NULL,
fastpath = TRUE, svtol = tol, smallest = FALSE, ...)
Returns a list with entries:
max(nu, nv) approximate singular values
nu approximate left singular vectors (only when right_only=FALSE)
nv approximate right singular vectors
The number of Lanczos iterations carried out
The total number of matrix vector products carried out
numeric real- or complex-valued matrix or real-valued sparse matrix.
number of right singular vectors to estimate.
number of left singular vectors to estimate (defaults to nv
).
maximum number of iterations.
working subspace dimension, larger values can speed convergence at the cost of more memory use.
if TRUE
, apply full reorthogonalization to both SVD bases, otherwise
only apply reorthogonalization to the right SVD basis vectors; the latter case is cheaper per
iteration but, overall, may require more iterations for convergence. Automatically TRUE
when fastpath=TRUE
(see below).
convergence is determined when svtol = tol
(see svtol
below),
where the spectral norm ||A|| is approximated by the
largest estimated singular value, and U, V, S are the matrices corresponding
to the estimated left and right singular vectors, and diagonal matrix of
estimated singular values, respectively.
optional starting vector or output from a previous run of irlba
used
to restart the algorithm from where it left off (see the notes).
logical value indicating return only the right singular vectors
(TRUE
) or both sets of vectors (FALSE
). The right_only option can be
cheaper to compute and use much less memory when nrow(A) >> ncol(A)
but note
that obtained solutions typically lose accuracy due to lack of re-orthogonalization in the
algorithm and that right_only = TRUE
sets fastpath = FALSE
(only use this option
for really large problems that run out of memory and when nrow(A) >> ncol(A)
).
Consider increasing the work
option to improve accuracy with right_only=TRUE
.
logical value that when TRUE
prints status messages during the computation.
optional column scaling vector whose values divide each column of A
;
must be as long as the number of columns of A
(see notes).
optional column centering vector whose values are subtracted from each
column of A
; must be as long as the number of columns of A
and may
not be used together with the deflation options below (see notes).
optional shift value (square matrices only, see notes).
DEPRECATED optional custom matrix multiplication function (default is %*%
, see notes).
try a fast C algorithm implementation if possible; set fastpath=FALSE
to use the
reference R implementation. See the notes for more details.
additional stopping tolerance on maximum allowed absolute relative change across each
estimated singular value between iterations.
The default value of this parameter is to set it to tol
. You can set svtol=Inf
to
effectively disable this stopping criterion. Setting svtol=Inf
allows the method to
terminate on the first Lanczos iteration if it finds an invariant subspace, but with less certainty
that the converged subspace is the desired one. (It may, for instance, miss some of the largest
singular values in difficult problems.)
set smallest=TRUE
to estimate the smallest singular values and associated
singular vectors. WARNING: this option is somewhat experimental, and may produce poor
estimates for ill-conditioned matrices.
optional additional arguments used to support experimental and deprecated features.
Baglama, James, and Lothar Reichel. "Augmented implicitly restarted Lanczos bidiagonalization methods." SIAM Journal on Scientific Computing 27.1 (2005): 19-42.
set.seed(1)
A <- matrix(runif(400), nrow=20)
S <- irlba(A, 3)
S$d
# Compare with svd
svd(A)$d[1:3]
# Restart the algorithm to compute more singular values
# (starting with an existing solution S)
S1 <- irlba(A, 5, v=S)
# Estimate smallest singular values
irlba(A, 3, smallest=TRUE)$d
#Compare with
tail(svd(A)$d, 3)
# Principal components (see also prcomp_irlba)
P <- irlba(A, nv=1, center=colMeans(A))
# Compare with prcomp and prcomp_irlba (might vary up to sign)
cbind(P$v,
prcomp(A)$rotation[, 1],
prcomp_irlba(A)$rotation[, 1])
# A custom matrix multiplication function that scales the columns of A
# (cf the scale option). This function scales the columns of A to unit norm.
col_scale <- sqrt(apply(A, 2, crossprod))
setClass("scaled_matrix", contains="matrix", slots=c(scale="numeric"))
setMethod("%*%", signature(x="scaled_matrix", y="numeric"),
function(x ,y) x@.Data %*% (y / x@scale))
setMethod("%*%", signature(x="numeric", y="scaled_matrix"),
function(x ,y) (x %*% y@.Data) / y@scale)
a <- new("scaled_matrix", A, scale=col_scale)
irlba(a, 3)$d
# Compare with:
svd(sweep(A, 2, col_scale, FUN=`/`))$d[1:3]
Run the code above in your browser using DataLab