Application: Matrix Powers
Many processes apply the same matrix again and again: one step of a simulation, one day of a weather model, one click of a random web surfer. After steps the state is . Diagonalization gives a closed formula for and, more importantly, tells you what happens as grows: which part survives, which dies out, and how fast.
Powers of a diagonalizable matrix
If (Diagonalization), the inner pairs cancel:
Powers of a diagonal matrix are just powers of its entries. For with , and :
For this gives back . SymPy can do the same with a symbolic , and we can check it against direct multiplication:
from sympy import Matrix, symbols, simplify
k = symbols("k", integer=True, nonnegative=True)
A = Matrix([[4, -2], [1, 1]])
P, D = A.diagonalize()
Ak = simplify(P * D**k * P.inv())
print("A^k =", Ak.tolist())
print("formula at k=10:", Ak.subs(k, 10).tolist())
print("A**10 directly: ", (A**10).tolist())A^k = [[-2**k + 2*3**k, 2*2**k - 2*3**k], [-2**k + 3**k, 2**(k + 1) - 3**k]]
formula at k=10: [[117074, -116050], [58025, -57001]]
A**10 directly: [[117074, -116050], [58025, -57001]]
The long run: the dominant eigenvalue wins
Write the starting vector in eigenvector coordinates, . Then
Each eigen-direction evolves on its own. Terms with decay to 0, terms with blow up, and terms stay as they are. If one eigenvalue is strictly largest in absolute value, ends up pointing along its eigenvector (provided its ), which is why power iteration works. The ratio of the two largest sets the speed.
Markov chains and their steady state
A Markov chain moves between states with fixed probabilities. Store them in a matrix whose entry is the probability of going from state to state . Every column then adds up to 1 (a column-stochastic matrix), and if holds the probabilities of each state today, then .
A toy weather model: a sunny day is followed by sun with probability 0.9, a rainy day by rain with probability 0.5.
from sunny from rainy
M = [[ 0.9, 0.5 ], to sunny
[ 0.1, 0.5 ]] to rainyBecause the columns add up to 1, is always an eigenvalue (the rows of add up to the zero row). Its eigenvector, scaled so the entries add up to 1, is the steady state with :
Starting from a rainy day, , so
In the long run of days are sunny, whatever the start, and the memory of the start fades like .
import numpy as np
M = np.array([[0.9, 0.5], [0.1, 0.5]])
x = np.array([0.0, 1.0]) # start: rainy
for day in range(1, 8):
x = M @ x
formula = 5 / 6 - 5 / 6 * 0.4**day
print(f"day {day}: P(sun) = {x[0]:.6f} formula {formula:.6f}")
values, vectors = np.linalg.eig(M)
i = int(np.argmin(abs(values - 1))) # the eigenvalue closest to 1
s = vectors[:, i] / vectors[:, i].sum() # scale so the entries add up to 1
print("eigenvalues:", [round(float(v), 6) for v in values])
print("steady state:", [round(float(v), 6) for v in s])day 1: P(sun) = 0.500000 formula 0.500000
day 2: P(sun) = 0.700000 formula 0.700000
day 3: P(sun) = 0.780000 formula 0.780000
day 4: P(sun) = 0.812000 formula 0.812000
day 5: P(sun) = 0.824800 formula 0.824800
day 6: P(sun) = 0.829920 formula 0.829920
day 7: P(sun) = 0.831968 formula 0.831968
eigenvalues: [1.0, 0.4]
steady state: [0.833333, 0.166667]
The same structure, at a scale of billions of states, is PageRank: a random surfer follows links, the transition matrix is column-stochastic, and the steady state ranks pages (PageRank and Markov Chains).
In software
- Ranking and recommendation: PageRank, random-walk recommenders, and graph embeddings use the steady state or the dominant eigenvectors of a transition matrix.
- Text generation: a simple -gram language model is a Markov chain over words; its steady state is the long-run word frequency.
- Simulations and games: a population or economy model grows at the rate of the dominant eigenvalue, and its eigenvector is the long-run mix.
- Counting paths: entry of for a graph's adjacency matrix counts walks of length from to . Fast exponentiation by squaring computes it in matrix products (Application: Solving Recurrences).
Tip: A Markov chain can have more than one steady state (if it splits into separate pieces) or oscillate forever (if it is periodic, like ). PageRank's "teleport" probability exists precisely to rule both out.
Exercises
Exercise 1: A closed form
Use with , to find , then .
Answer
, so .
Solution
Exercise 2: Steady state by hand
Find the steady state and the second eigenvalue of .
Answer
; the second eigenvalue is 0.5.
Solution
Exercise 3: Powers of a symmetric matrix
Find a formula for when .
Answer
.
Solution
Exercise 4: A three-state chain
is column-stochastic. Find its steady state exactly with SymPy.
Answer
.
Solution
From : the first row gives , so ; the last row gives , so . Then gives .
from sympy import Matrix, Rational as R, eye
M = Matrix([[R(1, 2), R(1, 3), 0], [R(1, 2), R(1, 3), R(1, 2)], [0, R(1, 3), R(1, 2)]])
v = (M - eye(3)).nullspace()[0]
s = v / sum(v)
print("steady state:", list(s))
print("M s == s:", M * s == s)steady state: [2/7, 3/7, 2/7]
M s == s: True
Test yourself
If , what is ?
Why does every column-stochastic matrix have eigenvalue 1?
- Because its trace is 1
- Because its rows of add up to the zero row, so is singular
- Because it is symmetric
- Because all its entries are positive
A chain has eigenvalues 1 and 0.9; another has 1 and 0.2. Which forgets its starting state faster?
- The first
- The second
- Both equally fast
- Neither ever forgets