Learn R Programming

rsvd (version 2.0.0)

rqb: Randomized QB decomposition

Description

Project the columns of a matrix onto an orthonormal basis and return the basis and projected coordinates. A basis with few columns gives a smaller matrix for subsequent computations. The basis is found using either randomized range finding or a deterministic QR factorization.

Usage

rqb(A, k = NULL, p = 10, q = 2, sdist = "normal", rand = TRUE)

Value

A list of class "rqb" containing two ordinary R matrices:

Q

An \(m \times \ell\) matrix with orthonormal columns.

B

An \(\ell \times n\) matrix containing the projection \(Q^{H} A\).

The input dimensions are \(m \times n\). The basis width \(\ell\)

is defined in Details and is not automatically reduced to the numerical rank.

Arguments

A

A real or complex numeric matrix or a numeric data frame. Sparse and dense matrices from the Matrix package are supported; see Details. Other matrix-like inputs are converted with as.matrix. The input must have positive dimensions and finite entries.

k

A positive integer giving the target rank before randomized oversampling. Requests larger than min(dim(A)) are capped at that value. The default, NULL, requests the full available basis width. Use a rank much smaller than the matrix dimensions for the main computational savings.

p

A non-negative integer oversampling parameter for the randomized algorithm. The default is 10. The oversampled basis is retained in the result; see Details for its relationship to rsvd. This does not change the basis width when rand = FALSE.

q

A non-negative integer giving the number of randomized power (subspace) iterations. The default is 2; zero skips these iterations. Unused when rand = FALSE.

sdist

A character string selecting the sampling distribution. "normal" (the default) uses standard normal entries, "unif" uses uniform entries on \([0,1]\), and "rademacher" uses equally likely values of -1 and 1. For complex input, the real and imaginary parts are sampled independently. Unused when rand = FALSE.

rand

A logical value. If TRUE (the default), use randomized range finding; otherwise use the deterministic QR basis described in Details.

Author

N. Benjamin Erichson

Details

The QB decomposition uses the same set of orthonormal vectors to approximate every column of A. These vectors form Q, and each column of B gives the coordinates of an input column projected onto their span. Their product gives the approximation $$A \approx Q B, \qquad B = Q^{H} A.$$ The superscript \(H\) denotes the conjugate transpose, which is the ordinary transpose for real input. Thus Q %*% B retains the part of each column of A that lies in the column space of Q. This is an orthogonal projection. The input is used without centering or scaling.

When rand = TRUE, random matrix products identify a candidate subspace. The algorithm refines it with q power iterations and intermediate orthogonalization before projecting the input. The basis width is \(\ell = \min(k+p,m,n)\) for integer k and p, with m rows and n columns in the input. The oversampled columns are retained, so the result generally has k + p basis vectors. A singular value decomposition (SVD) of the projected matrix B can then identify its leading directions and truncate the approximation further. The function rsvd follows this approach and returns k singular triplets with its default vector counts.

Oversampling gives the sampled subspace additional directions, and power iterations can improve how well it captures the input. Both can improve approximation quality, especially when singular values decay slowly, at the cost of more computation. The parameters remain fixed throughout the calculation; there is no adaptive error tolerance or automatic rank selection. The useful rank depends on how much information the application needs to retain. Even a matrix with full algebraic rank may be well represented in a much smaller subspace.

When rand = FALSE, the basis width is \(\ell = \min(k,m,n)\). The result uses the first \(\ell\) columns of qr.Q(qr(as.matrix(A))), with base R's default QR conventions. For real input whose leading block is numerically independent, only that block needs dense factorization. Complex input, dependent leading columns and other cases use the full dense QR fallback. This preserves base QR's ordering, including its pivoting behavior. The resulting subspace follows that ordering and need not minimize the rank-k approximation error.

For randomized decompositions, real-valued sparse Matrix inputs are multiplied by thin dense factors without converting the entire input to a dense matrix. Logical and pattern matrices are treated as zero/one data. Complex input is supported for ordinary R matrices. Samples, orthonormal bases and projected matrices are dense. The deterministic fallback can convert the entire input to dense storage, which limits its usefulness for large sparse matrices. It does not provide a general sparse QR implementation. Constructing Q %*% B also forms a dense matrix.

Setting k = NULL uses the full available basis width and gives up the storage advantage of a small basis. The function does not reduce that width based on an estimated numerical rank.

References

  • N. B. Erichson, S. Voronin, S. L. Brunton and J. N. Kutz. 2019. Randomized Matrix Decompositions Using R. Journal of Statistical Software, 89(11), 1-48. tools:::Rd_expr_doi("10.18637/jss.v089.i11").

  • N. Halko, P. Martinsson and J. Tropp. 2009. Finding structure with randomness: probabilistic algorithms for constructing approximate matrix decompositions. https://arxiv.org/abs/0909.4061.

See Also

rsvd, svd, qr

Examples

Run this code
# An exact rank-three signal.
set.seed(42)
A <- matrix(rnorm(60 * 3), 60, 3) %*% matrix(rnorm(3 * 40), 3, 40)
qb <- rqb(A, k = 3, p = 2, q = 1)
dim(qb$Q)  # 60 by 5: randomized QB retains the two oversampling columns.
norm(A - qb$Q %*% qb$B, "F") / norm(A, "F")

# Further truncation is performed in the projected matrix.
small <- svd(qb$B, nu = 3, nv = 3)
U <- qb$Q %*% small$u
A_approx <- sweep(U, 2, small$d[1:3], "*") %*% Conj(t(small$v))
norm(A - A_approx, "F") / norm(A, "F")

# The deterministic branch returns k columns, without oversampling.
qb_deterministic <- rqb(A, k = 3, rand = FALSE)
dim(qb_deterministic$Q)
norm(A - qb_deterministic$Q %*% qb_deterministic$B, "F") / norm(A, "F")

# Sparse randomized QB still returns ordinary dense factors.
S <- Matrix::Diagonal(x = c(9, 4, 1, rep(0, 17)))
qb_sparse <- rqb(S, k = 3, p = 2, q = 1)
dim(qb_sparse$B)

Run the code above in your browser using DataLab