Randomized Matrix Algorithms¶
Sketching Matrix¶
You have an ultra-large matrix and you need to conduct linear algebra tasks for it, however, you only need an approximate solution in exchange for a faster computation. This is when randomized methods come in handy.
The high level idea of randomized methods is to use random sketching matrix
Matrix Multiplication¶
In many applications the full matrix product
may be too costly to compute. We can instead compute an approximate product by "sketching" the inner dimension. The idea is to choose a random projection matrix
Then the approximate product is given by:
The key property of the sketching matrix
which means we need to design
For example, we can choose
This is the key idea of randomized methods: even though the random matrix
Below is a Python example code to illustrate the idea.
import numpy as np
from numpy.linalg import norm
# Set dimensions: m x n times n x p
m, n, p = 500, 1000, 300
# Generate random matrices A and B
A = np.random.randn(m, n)
B = np.random.randn(n, p)
# Choose a sketch size s (s << n)
s = 100
# Create a random projection matrix S of size n x s (Gaussian random matrix)
S = np.random.randn(n, s) / np.sqrt(s) # scaling to preserve norm in expectation
# Compute the sketched matrices
Y = A @ S # Y has shape m x s
Z = S.T @ B # Z has shape s x p
# Approximate product
C_approx = Y @ Z
# Compute the true product for comparison
C_true = A @ B
# Measure the relative error (Frobenius norm)
error = norm(C_true - C_approx, 'fro') / norm(C_true, 'fro')
print("Relative error in the approximate product:", error)
The figure below shows the trade-off between the running time and the error for choosing different sketch sizes for 1000 x 1000 matrix multiplication.
Randomized SVD¶
Given a large matrix
therefore, the left singular vectors of
We formalize the idea of randomized SVD by the following steps:
-
Step 1: Form a sketch
where is a sketching matrix of size (often for desired eigenvalues). -
Step 2: QR decomposition of
to get . ( spans approximately the same subspace as , i.e., there exists an orthogonal matrix such that ) -
Step 3: Project
into this lower-dimensional subspace: . ( ) -
Step 4: Compute the SVD of the small matrix
. (As , and , we have )
The approximate left singular vectors of
import numpy as np
def randomized_svd(A, rank):
m, n = A.shape
# Generate a random Gaussian matrix (Sketching size s = rank + 10)
S = np.random.randn(n, rank+10)
# Form the sample matrix Z, which is m x k
Y = A @ S
# Orthonormalize Y using QR decomposition
Q, _ = np.linalg.qr(Y)
# Obtain the low-rank approximation of A
B = Q.T @ A
# Perform SVD on the low-rank approximation
U_tilde, Sigma, Vt = np.linalg.svd(B, full_matrices=False)
# Obtain the final singular vectors
U = Q @ U_tilde
return U, Sigma, Vt
Below is an illustration of the recovery of the randomized SVD of a tiger image. The left is the original image and the right is the recovery by the randomized SVD with different sketching rates.