import numpy as np
import skimage.data
rng = np.random.default_rng(3)
def range_finder(A, ell, q=0):
"""Orthonormal basis Q for range((A A^T)^q A Omega), re-orthonormalizing after each product."""
Omega = rng.standard_normal((A.shape[1], ell))
Q, _ = np.linalg.qr(A @ Omega)
for _ in range(q):
Q, _ = np.linalg.qr(A.T @ Q)
Q, _ = np.linalg.qr(A @ Q)
return Q
def randomized_svd(A, k, p=10, q=0):
"""Rank-k randomized SVD with oversampling p and q power iterations."""
Q = range_finder(A, k + p, q)
U_tilde, s, Vt = np.linalg.svd(Q.T @ A, full_matrices=False)
return (Q @ U_tilde)[:, :k], s[:k], Vt[:k]Power Iterations for the Randomized SVD
Math 123: Mathematical Aspects of Data Analysis — Fall 2026
The average error bound for the randomized SVD compares its error with the Eckart–Young optimum \(\big(\sum_{j>k}\sigma_j^2\big)^{1/2}\), which is small only if the tail is small. When the singular values decay slowly, as they do for many images and for noisy data, the tail in each sample \(\bfA\bfomega\) is large, and the range finder is noticeably worse than the exact SVD. This page explains the standard fix, power iterations, and tries it on a photograph. The basic algorithm and its experiments are in the randomized SVD example.
Sharpening the spectrum
The idea is to sample a matrix with the same singular vectors as \(\bfA\) but faster-decaying singular values. As in the lecture, \(\bfA\in\Rbb^{n\times d}\) has rank \(r\) and SVD \(\bfA=\bfU_r\Sigma_r\bfV_r^\top\).
Proof. By the SVD and eigendecomposition, \(\bfA\bfA^\top=\bfU_r\Sigma_r^2\bfU_r^\top\). Since \(\bfU_r^\top\bfU_r=\bfI_r\), the inner factors cancel in a product, so \((\bfA\bfA^\top)^q=\bfU_r\Sigma_r^{2q}\bfU_r^\top\) for \(q\geq1\), and \[ (\bfA\bfA^\top)^q\bfA=\bfU_r\Sigma_r^{2q}\bfU_r^\top\bfU_r\Sigma_r\bfV_r^\top=\bfU_r\Sigma_r^{2q+1}\bfV_r^\top. \]
So \((\bfA\bfA^\top)^q\bfA\) has the same left singular vectors as \(\bfA\), and singular values \(\sigma_i^{2q+1}\). The ratio between a tail singular value and a top one shrinks from \(\sigma_{k+1}/\sigma_k\) to \((\sigma_{k+1}/\sigma_k)^{2q+1}\). For instance, a ratio of \(0.9\) becomes \(0.9^5\approx0.59\) with \(q=2\).
The randomized SVD with \(q\) power iterations replaces step 2 of the algorithm by \[ \bfY=(\bfA\bfA^\top)^q\bfA\,\bfOmega, \] and leaves step 3 alone: we still form \(\bfB=\bfQ^\top\bfA\) with the original \(\bfA\), so stage B and the cost of truncating apply unchanged. The powered matrix is only used to find a better subspace, and is never formed explicitly: \(\bfY\) is computed by \(2q+1\) successive products with \(\bfA\) and \(\bfA^\top\), each costing \(nd\ell\).
In floating point arithmetic, with about \(16\) significant digits, computing \((\bfA\bfA^\top)^q\bfA\bfOmega\) directly is a mistake. The component along \(\bfu_j\) is scaled by \(\sigma_j^{2q+1}\), so once \((\sigma_j/\sigma_1)^{2q+1}<10^{-16}\) it is lost to rounding, and every column of \(\bfY\) becomes numerically parallel to the top few singular vectors. The fix is to orthonormalize after every product: compute \(\bfQ\) from \(\bfA\bfOmega\), then repeat \(q\) times: \(\bfQ\leftarrow\operatorname{orth}(\bfA^\top\bfQ)\), then \(\bfQ\leftarrow\operatorname{orth}(\bfA\bfQ)\). Orthonormalizing does not change the range, so in exact arithmetic the final \(\bfQ\) spans the same subspace as \((\bfA\bfA^\top)^q\bfA\bfOmega\).
The total cost is \(2q+2\) products with \(\bfA\) or \(\bfA^\top\), so each power iteration costs about as much as the whole basic algorithm. In practice \(q=1\) or \(2\) is usually enough, as the experiment below shows.
sklearn.utils.extmath.randomized_svd(A, k) implements the randomized SVD with power iterations, using \(p=10\) and, by default, \(q=4\), or \(q=7\) when \(k<0.1\min(n,d)\). PCA(svd_solver="randomized") uses it to compute principal components.
The algorithm
The range finder implements stage A with \(q\) power iterations, re-orthonormalizing after each product as in the warning above. The randomized SVD adds stage B.
Figure style
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
INK = "#7c7c78"
GRID = "rgba(128, 128, 128, 0.28)"
BLUE, ORANGE, TEAL = "#2a78d6", "#eb6834", "#27a9a0"
CONFIG = dict(displayModeBar=False, responsive=True)
def style(fig, height=460, title=None, showlegend=None):
"""Apply the shared layout to a plot with axes."""
fig.update_layout(
template="simple_white",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font=dict(color=INK, size=13),
title=dict(text=title, x=0, xanchor="left", font=dict(size=15)),
margin=dict(l=60, r=30, t=60 if title else 40, b=50),
height=height,
showlegend=showlegend,
hovermode="closest",
)
fig.update_xaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)
fig.update_yaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)
return figA photograph
The \(512\times512\) camera image from scikit-image, viewed as a matrix \(\bfA\) with entries in \([0,1]\). Its singular values decay slowly, which is the hard case for the basic algorithm. We use \(p=10\) throughout and compare \(q=0,1,2\) power iterations against the exact SVD.
A = skimage.data.camera().astype(float) / 255
U, S, Vt = np.linalg.svd(A)
qs = [0, 1, 2]
ks = np.arange(5, 101, 5)
optimum = np.array([np.sqrt(np.sum(S[k:] ** 2)) for k in ks]) / np.linalg.norm(A)
errors = {q: [] for q in qs}
for q in qs:
for k in ks:
U_k, s_k, Vt_k = randomized_svd(A, k, q=q)
errors[q].append(np.linalg.norm(A - (U_k * s_k) @ Vt_k) / np.linalg.norm(A))
errors = {q: np.array(e) for q, e in errors.items()}
a = list(ks).index(40)
print(f"rank 40: optimum {optimum[a]:.4f}")
for q in qs:
print(f" q={q}: error {errors[q][a]:.4f}, {errors[q][a] / optimum[a] - 1:+.1%} relative to optimum")rank 40: optimum 0.0719
q=0: error 0.0998, +38.7% relative to optimum
q=1: error 0.0737, +2.5% relative to optimum
q=2: error 0.0725, +0.7% relative to optimum
Plot the singular values and errors
q_colors = {0: ORANGE, 1: BLUE, 2: TEAL}
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.1,
subplot_titles=("top 40 singular values σⱼ", "relative rank-k error"))
idx = np.arange(1, 41)
fig.add_trace(go.Scatter(x=idx, y=S[:40], mode="lines", name="exact",
line=dict(color=INK, width=3)), row=1, col=1)
fig.add_trace(go.Scatter(x=ks, y=optimum, mode="lines", name="Eckart–Young optimum",
line=dict(color=INK, width=3, dash="dash")), row=1, col=2)
for q in qs:
fig.add_trace(go.Scatter(x=idx, y=randomized_svd(A, 40, q=q)[1], mode="markers", name=f"q = {q}",
marker=dict(color=q_colors[q], size=6), legendgroup=str(q)), row=1, col=1)
fig.add_trace(go.Scatter(x=ks, y=errors[q], mode="lines+markers", name=f"q = {q}",
line=dict(color=q_colors[q], width=2), marker=dict(size=5),
legendgroup=str(q), showlegend=False), row=1, col=2)
style(fig, height=440, showlegend=True)
fig.update_xaxes(title_text="index j", row=1, col=1)
fig.update_yaxes(title_text="σⱼ", type="log", row=1, col=1)
fig.update_xaxes(title_text="rank k", row=1, col=2)
fig.update_yaxes(title_text="‖A − UΣVᵀ‖_F / ‖A‖_F", row=1, col=2)
fig.show(config=CONFIG)Without power iterations the estimated singular values sag below the truth, and the error sits well above the optimum at every rank. For rank \(40\), the error is nearly \(40\%\) above the optimum with \(q=0\), about \(3\%\) above with \(q=1\), and within \(1\%\) with \(q=2\). So one power iteration closes most of the gap, and two close almost all of it. Each iteration costs two more products with \(\bfA\), so \(q=2\) costs three times as much as \(q=0\), which is still far cheaper than a full SVD for a large matrix.
What the error looks like
Plot the images
k = 40
panels = {"original": A, f"exact, rank {k}": (U[:, :k] * S[:k]) @ Vt[:k]}
for q in [0, 1]:
U_k, s_k, Vt_k = randomized_svd(A, k, q=q)
panels[f"randomized, q = {q}"] = (U_k * s_k) @ Vt_k
titles = [name if name == "original" else f"{name}<br>error {np.linalg.norm(A - B) / np.linalg.norm(A):.3f}"
for name, B in panels.items()]
fig = px.imshow(np.clip(np.stack(list(panels.values())), 0, 1), facet_col=0, binary_string=True,
zmin=0, zmax=1)
for annotation, title in zip(fig.layout.annotations, titles):
annotation.text = title
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)",
font=dict(color=INK, size=13), margin=dict(l=10, r=10, t=60, b=10), height=380)
fig.show(config=CONFIG)Without power iterations, the sketch blurs the fine detail and adds speckle. With \(q=1\) the result is visually indistinguishable from the exact truncated SVD.