Power Iteration
The simplest eigenvalue algorithm multiplies a vector by again and again. The component along the eigenvector with the largest eigenvalue grows fastest and soon dominates. Power iteration needs only matrix-vector products, so it works for huge sparse matrices; it is the idea behind PageRank and the starting point of all Krylov eigenvalue methods.
The algorithm
Start with a random and repeat:
The normalization prevents overflow; the Rayleigh quotient estimates the eigenvalue.
Why it works
Write in the eigenvector basis with . Then
so the direction converges to linearly with rate . For a symmetric matrix the Rayleigh quotient error goes like the square of that, .
import numpy as np
rng = np.random.default_rng(51)
Q, _ = np.linalg.qr(rng.normal(size=(50, 50)))
lams = np.r_[10.0, 9.0, rng.uniform(-5, 5, 48)]
A = Q @ np.diag(lams) @ Q.T # symmetric, lambda1 = 10, lambda2 = 9
x = rng.normal(size=50)
prev = None
for k in range(1, 121):
y = A @ x
x = y / np.linalg.norm(y)
if k % 20 == 0:
err = abs(x @ A @ x - 10)
rate = "" if prev is None else f", error factor per step {(err / prev) ** (1 / 20):.4f}"
print(f"k = {k:3d}: error of Rayleigh quotient {err:.1e}{rate}")
prev = errk = 20: error of Rayleigh quotient 3.1e-05
k = 40: error of Rayleigh quotient 4.6e-07, error factor per step 0.8100
k = 60: error of Rayleigh quotient 6.8e-09, error factor per step 0.8100
k = 80: error of Rayleigh quotient 1.0e-10, error factor per step 0.8100
k = 100: error of Rayleigh quotient 1.5e-12, error factor per step 0.8103
k = 120: error of Rayleigh quotient 1.8e-14, error factor per step 0.8015
The measured factor per step matches the prediction until rounding error takes over. The ratio makes convergence slow; a gap like would converge in a handful of steps.
PageRank
The PageRank vector is the dominant eigenvector (eigenvalue 1) of the "Google matrix" , where is the column-stochastic link matrix. The damping guarantees , so power iteration converges at a known rate.
import numpy as np
links = {0: [1, 2], 1: [2], 2: [0], 3: [0, 2], 4: [3, 2]} # page -> pages it links to
n = 5
P = np.zeros((n, n))
for j, outs in links.items():
P[outs, j] = 1 / len(outs)
alpha = 0.85
x = np.ones(n) / n
for k in range(100):
x_new = alpha * P @ x + (1 - alpha) / n
if np.abs(x_new - x).sum() < 1e-12:
break
x = x_new
print("PageRank:", np.round(x_new, 4), "after", k + 1, "iterations")
print("ranking :", np.argsort(-x_new))PageRank: [0.3667 0.1858 0.3747 0.0428 0.03 ] after 55 iterations
ranking : [2 0 1 3 4]
Limitations
- Finds only the eigenvalue of largest magnitude, and converges slowly if .
- Fails to settle if the two largest have equal magnitude (e.g. or a complex pair).
- The fixes are shifts and inverse iteration (Inverse Iteration) and, for several eigenvalues at once, subspace or Krylov methods (Lanczos, Arnoldi).
In software
scipy.sparse.linalg.eigs/eigsh(ARPACK) use Krylov subspaces, a far better use of the same matrix-vector products; ask forkeigenvalues withwhich="LM"(largest magnitude),"SA", and so on.- Power iteration is used in deep learning to estimate spectral norms cheaply (spectral normalization of GAN discriminators).
Exercises
Exercise 1: Steps needed
. Roughly how many power-iteration steps reduce the eigenvector error by ?
Answer
About 40.
Solution
gives .
Exercise 2: By hand
Do two steps of power iteration on from , without normalizing.
Answer
, then .
Solution
The direction approaches , the eigenvector for ; the ratio .
Exercise 3: Spectral norm by power iteration (code)
Estimate for a random matrix by power iteration on (never form it: compute ) and compare with np.linalg.norm(B, 2).
Answer
After 300 iterations they agree only to about 4 digits, because is close to (the printed ratio): convergence is slow.
Solution
import numpy as np
rng = np.random.default_rng(52)
B = rng.normal(size=(200, 100))
x = rng.normal(size=100)
for _ in range(300):
x = B.T @ (B @ x); x /= np.linalg.norm(x)
s = np.linalg.svd(B, compute_uv=False)
print(f"{np.linalg.norm(B @ x):.10f} {s[0]:.10f}, (sigma2/sigma1)^2 = {(s[1] / s[0]) ** 2:.4f}")23.0375003276 23.0392418338, (sigma2/sigma1)^2 = 0.9885
Test yourself
Power iteration converges to the eigenvector of…
- the smallest eigenvalue
- the eigenvalue of largest magnitude
- eigenvalue 0
- all eigenvalues at once
The convergence rate of power iteration is…
- quadratic
- linear with rate
- linear with rate
- independent of the eigenvalues
Why is power iteration suitable for huge sparse matrices?
- It factorizes them
- It needs only matrix-vector products
- It uses the determinant
- It is exact