Eigenvectors at work: Markov chains, PCA, SVD
Eigenvectors doing real jobs — a Markov chain editor with its steady state and spectral gap (and PageRank), power iteration, Fibonacci by matrix powers against Binet's formula, the Leslie population model, PCA by rotating a line through a cloud, SVD image compression of your own photo with its rank-1 layers, and latent semantic indexing.
Added
How to use it
Seven tabs, each an eigenvector (or singular vector) doing a job. Every setting can be put in a link, and every book example has a preset — see Linking to an example.
⛶ Full screen (or F) gives the Markov state diagram, the PCA scatter or the LSI concept space
the whole screen — the browser’s full screen where it is allowed, otherwise a full-window layer
(iPhone); it is redrawn as the window changes, and ◫ Panels hides the tab’s other panels. Escape leaves.
Markov chains
Pick 2–6 states, name them, and type the transition matrix. Column j lists where you go from state j (the book’s convention: columns sum to 1 and distributions update as x ↦ Px; Transpose converts a row-stochastic matrix). The state diagram shows each arrow’s probability, and the day slider fills each state by the chance of being there that day.
-
The distribution, day by day: each state’s probability, with its steady state dashed.
-
Distance from the steady state on a log scale, next to the prediction |λ₂|ⁿ: the gap shrinks by the second-largest eigenvalue size every day.
-
Eigenvalues and steady state: all eigenvalues, the spectral gap 1 − |λ₂|, the steady state (eigenvector for eigenvalue 1, scaled to sum 1) and how many days it takes to get within 0.001. A chain that cycles (|λ₂| = 1) or splits into pieces (eigenvalue 1 twice) is flagged.
-
Damping d below 1 turns the chain into PageRank’s teleporting surfer, G = dP + (1 − d)/n·J, and reports how many power-iteration steps PageRank needs. A column of zeros is a dead end.
Power iteration and matrix powers
Enter a 2 × 2 matrix and a start vector. The picture shows v₀ … v_k, each renormalised, swinging onto the dominant eigenvector; the chart compares the error with |λ₂/λ₁|ᵏ, and the table gives the Rayleigh quotient vᵀAv converging to λ₁. Below, Aᵏ is computed exactly (repeated squaring with big integers when the entries are whole numbers) and as P Dᵏ P⁻¹ from the eigenvalues and eigenvectors.
Fibonacci and Binet’s formula
Fₙ exactly as the corner of [[1,1],[1,0]]ⁿ by repeated squaring (every step listed), against
Binet’s formula evaluated as the book’s code does, round(φn / 50.5) in 64-bit floating
point. Matching leading digits are green, the rest red; the chart counts the agreeing digits for
every n up to 1474, after which φⁿ overflows. At n = 100 the two numbers share 14 leading digits.
Leslie model
A projection matrix by age class: births along the top row, survival below the diagonal. The tool runs the population year by year, shows each class and the total (log scale optional), the share of each class settling on the stable age distribution (the dominant eigenvector), the growth rate λ, and the harvest h = 1 − 1/λ that would hold the population steady. The harvest slider applies (1 − h)L.
PCA: rotate the cloud
Drag on the scatter plot (or use ← →) to turn a line through the centre. Every point’s projection is ticked onto the line and the variance of the projections, uᵀCu, updates live; the dial plots that variance for every direction. Hunt for the maximum, then press Reveal PC1: the first principal component, the top eigenvector of the covariance matrix, lands on your best line. Sliders change the correlation, the noise and the number of points, and the eigenvalue ratio tracks how elongated the cloud is. Height & arm span is the book’s 500 people (scaled so the covariance matrix is exactly the book’s); switching height to millimetres shows mixed units hijacking PC1.
SVD compression
Choose the book’s 200 × 200 image (smooth waves, a disc and a little noise), another built-in sample, or your own photo — it is turned into greyscale and processed only in this page. The SVD is computed by one-sided Jacobi rotations in a background Web Worker. The rank k slider redraws the rank-k image beside the original, with the numbers stored, k(m + n + 1) against mn, and the relative error, both measured and from the singular-value tail (Eckart–Young). The singular values are plotted on a log scale with your cut-off, and the first eight rank-1 layers σᵢuᵢvᵢᵀ are shown as images — stripes that add up to the picture.
The last panel takes any 2 × 2 matrix and shows the unit circle after Vᵀ, Σ Vᵀ and U Σ Vᵀ (the step slider), with v₁ and v₂ and where they go, σ₁σ₂ = |det A|, and U and V.
Latent semantic indexing
A term–document matrix (rows are terms, columns documents), a query, and the number of concepts k. The table compares each document’s raw cosine with the query against its cosine in the k-dimensional concept space; the plot shows documents, terms and the query in the first two concepts. With the book’s five documents, “car” finds the document that only says “automobile”.
Limits
-
Markov chains up to 6 states, Leslie matrices up to 5 classes, power iteration for 2 × 2 matrices. Eigenvalues come from the matrix calculator’s shifted QR iteration.
-
Photos are scaled so the longer side is 120–400 pixels (200 by default); 400 × 400 takes a few seconds. Colour is dropped.
-
The book’s synthetic image and its PCA sample come from NumPy’s random generator, which this page does not reproduce: the noise here is different but statistically the same, so the large singular values and the errors match the book to about two decimals, and the height / arm-span sample is rescaled to have exactly the book’s covariance matrix.
-
Digits are counted from the left: F₁₀₀ = 354224848179261915075 and its floating-point Binet value 354224848179263111168 agree in 14 (35422484817926), then differ.
Related: the Matrix calculator (eigenvalues, eigenvectors, decompositions), Matrix transformations (eigenvectors and the SVD as a moving picture), Linear systems, visually, Statistics, and The maths of machine learning.
Companion to chapter 28, Eigenvectors, Bases, and Applications, of Mathematics, Reconnected: Fibonacci via matrix powers, Markov chains, PageRank, PCA and the SVD.
Linking to an example
A link opens the tool with the given settings: /tools/pca-svd/?<parameters>#<tab>, where the tab
is markov, power, fib, leslie, pca, svd or lsi. Share link (top right) builds one
from whatever is on screen. Matrices are written row by row: entries separated by commas, rows by
semicolons (0.9,0.5;0.1,0.5); fractions such as 1/3 work. A + typed straight into a link
means a space to browsers, so leave it out (3 not +3) or write %2B.
| Parameter | Meaning | Example |
|---|---|---|
|
a book preset (list below); other parameters in the same link override it |
|
|
Markov transition matrix, column j = from state j |
|
|
start distribution |
|
|
state names, comma separated |
|
|
damping factor (1 = none; 0.85 for PageRank) |
|
|
days plotted |
|
|
day shown in the state diagram |
|
|
2 × 2 matrix for power iteration and Aᵏ |
|
|
start vector |
|
|
iteration steps, and the power in Aᵏ |
|
|
Fibonacci index |
|
|
Leslie matrix |
|
|
starting population by class |
|
|
years to run |
|
|
harvest fraction |
|
|
PCA data: |
|
|
correlation of the synthetic cloud (−0.95 to 0.95) |
|
|
noise of the synthetic cloud |
|
|
number of points |
|
|
random seed of the synthetic cloud |
|
|
angle of the line, degrees |
|
|
show PC1 and PC2 ( |
|
|
height units for the book data: |
|
|
image: |
|
|
rank k of the approximation |
|
|
longest side (pixels) for your own photos |
|
|
2 × 2 matrix for the circle-to-ellipse panel |
|
|
0 circle, 1 after Vᵀ, 2 after ΣVᵀ, 3 after UΣVᵀ |
|
|
LSI terms (rows), comma separated |
|
|
term–document matrix, one row per term |
|
|
LSI query words |
|
|
number of concepts k |
|
Presets for example=:
| Name | Tab | Shows |
|---|---|---|
|
markov |
The sunny/rainy chain from a rainy day: 0.5, 0.7, 0.78 …, steady state 5/6, eigenvalues 1 and 0.4, spectral gap 0.6 |
|
markov |
Brand switching: the first network keeps 1/3 of the market |
|
markov |
The gym exercise: 4/7 of days working out |
|
markov |
A chain that cycles: eigenvalue −1, the distribution never settles |
|
markov |
A chain in two separate pieces: eigenvalue 1 twice, several steady states |
|
markov |
The four-page web with damping 0.85: 47 iterations, C 0.3941, A 0.3725, B 0.1958, D 0.0375 |
|
markov |
The exercise: the same web plus a link B → D |
|
power |
Power iteration on [[4,1],[2,3]]: the error shrinks by 2/5 a step |
|
power |
A formula for Aⁿ: A³ = [[86,39],[78,47]] directly and as P D³ P⁻¹ |
|
fib |
F₁₀₀ exactly against Binet in floating point — 14 digits agree |
|
leslie |
Juveniles and adults from 10 adults: totals 35, 32.5, 68.75 …, λ = 1.5, two juveniles per adult |
|
pca |
The lab: turn the line to the maximum variance, then reveal PC1 |
|
pca |
Height and arm span: eigenvalues 176.4 and 8.4, PC1 (0.669, 0.744) explains 95.4% |
|
pca |
The same data with height in millimetres: PC1 becomes “height” |
|
svd |
The book’s 200 × 200 image at rank 5, its layers, and [[3,0],[4,5]] as an ellipse |
|
svd |
The 2 × 2 worked example: σ = 6.708 and 2.236, σ₁σ₂ = 15 = det A |
|
lsi |
Latent semantic indexing: “car” scores 1.00 against the “automobile” document with k = 2 |
For example, https://geekychris.github.io/tools/pca-svd/?example=ch28-pagerank#markov opens the PageRank example, and https://geekychris.github.io/tools/pca-svd/?P=0.7,0.4;0.3,0.6&x0=0,1&names=Work%20out,Skip#markov the gym chain starting from a skipped day.
Feedback is a gift. Something to improve, a feature to add, or a bug? Tell me.
Runs entirely in your browser; nothing you type leaves this page.