TExcelTAD
Home › Linear Algebra & Matrix Theory › Application: Matrix Powers

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 kk steps the state is Akx0A^k\mathbf{x}_0. Diagonalization gives a closed formula for AkA^k and, more importantly, tells you what happens as kk grows: which part survives, which dies out, and how fast.

Powers of a diagonalizable matrix

If A=PDP−1A = PDP^{-1} (Diagonalization), the inner P−1PP^{-1}P pairs cancel:

A2=PDP−1⋅PDP−1=PD2P−1Ak=PDkP−1with Dk=diag⁡(λ1k,…,λnk)\begin{aligned} A^2 &= PDP^{-1} \cdot PDP^{-1} = PD^2P^{-1} \\ A^k &= PD^kP^{-1} \qquad \text{with } D^k = \operatorname{diag}(\lambda_1^k, \ldots, \lambda_n^k) \end{aligned}

Powers of a diagonal matrix are just powers of its entries. For A=[4−211]A = \begin{bmatrix} 4 & -2 \\ 1 & 1 \end{bmatrix} with P=[1211]P = \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix}, D=diag⁡(2,3)D = \operatorname{diag}(2, 3) and P−1=[−121−1]P^{-1} = \begin{bmatrix} -1 & 2 \\ 1 & -1 \end{bmatrix}:

Ak=[1211]⋅diag⁡(2k,3k)⋅[−121−1]=[2⋅3k−2k2⋅2k−2⋅3k3k−2k2⋅2k−3k]\begin{aligned} A^k &= \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix} \cdot \operatorname{diag}(2^k, 3^k) \cdot \begin{bmatrix} -1 & 2 \\ 1 & -1 \end{bmatrix} \\ &= \begin{bmatrix} 2 \cdot 3^k - 2^k & 2 \cdot 2^k - 2 \cdot 3^k \\ 3^k - 2^k & 2 \cdot 2^k - 3^k \end{bmatrix} \end{aligned}

For k=1k = 1 this gives back [4−211]\begin{bmatrix} 4 & -2 \\ 1 & 1 \end{bmatrix}. SymPy can do the same with a symbolic kk, and we can check it against direct multiplication:

python · try it yourself
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, x0=c1v1+⋯+cnvn\mathbf{x}_0 = c_1\mathbf{v}_1 + \cdots + c_n\mathbf{v}_n. Then

Akx0=c1λ1kv1+c2λ2kv2+⋯+cnλnkvnA^k\mathbf{x}_0 = c_1\lambda_1^k\mathbf{v}_1 + c_2\lambda_2^k\mathbf{v}_2 + \cdots + c_n\lambda_n^k\mathbf{v}_n

Each eigen-direction evolves on its own. Terms with ∣λ∣<1\lvert\lambda\rvert < 1 decay to 0, terms with ∣λ∣>1\lvert\lambda\rvert > 1 blow up, and λ=1\lambda = 1 terms stay as they are. If one eigenvalue is strictly largest in absolute value, Akx0A^k\mathbf{x}_0 ends up pointing along its eigenvector (provided its ci≠0c_i \ne 0), which is why power iteration works. The ratio ∣λ2/λ1∣\lvert\lambda_2/\lambda_1\rvert 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 MM whose entry MijM_{ij} is the probability of going from state jj to state ii. Every column then adds up to 1 (a column-stochastic matrix), and if xk\mathbf{x}_k holds the probabilities of each state today, then xk+1=Mxk\mathbf{x}_{k+1} = M\mathbf{x}_k.

A toy weather model: a sunny day is followed by sun with probability 0.9, a rainy day by rain with probability 0.5.

text
            from sunny  from rainy
M = [[ 0.9,        0.5 ],     to sunny
     [ 0.1,        0.5 ]]     to rainy

Because the columns add up to 1, λ=1\lambda = 1 is always an eigenvalue (the rows of M−IM - I add up to the zero row). Its eigenvector, scaled so the entries add up to 1, is the steady state s\mathbf{s} with Ms=sM\mathbf{s} = \mathbf{s}:

(M−I)=[−0.10.50.1−0.5] → −0.1x+0.5y=0 → (5,1) → s=(56,16)second eigenvalue=trace−1=1.4−1=0.4,eigenvector (1,−1)\begin{aligned} &(M - I) = \begin{bmatrix} -0.1 & 0.5 \\ 0.1 & -0.5 \end{bmatrix} \ \to\ -0.1x + 0.5y = 0 \ \to\ (5, 1) \ \to\ \mathbf{s} = \left(\tfrac{5}{6}, \tfrac{1}{6}\right) \\ &\text{second eigenvalue} = \text{trace} - 1 = 1.4 - 1 = 0.4, \quad \text{eigenvector } (1, -1) \end{aligned}

Starting from a rainy day, x0=(0,1)=s−56(1,−1)\mathbf{x}_0 = (0, 1) = \mathbf{s} - \frac{5}{6}(1, -1), so

xk=s−56⋅0.4k⋅(1,−1)P(sunny on day k)=56−56⋅0.4k\mathbf{x}_k = \mathbf{s} - \tfrac{5}{6} \cdot 0.4^k \cdot (1, -1) \qquad P(\text{sunny on day } k) = \tfrac{5}{6} - \tfrac{5}{6} \cdot 0.4^k

In the long run 56≈83.3%\frac{5}{6} \approx 83.3\% of days are sunny, whatever the start, and the memory of the start fades like 0.4k0.4^k.

python · try it yourself
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 nn-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 xk+1=Axk\mathbf{x}_{k+1} = A\mathbf{x}_k grows at the rate of the dominant eigenvalue, and its eigenvector is the long-run mix.
  • Counting paths: entry (i,j)(i, j) of GkG^k for a graph's adjacency matrix GG counts walks of length kk from jj to ii. Fast exponentiation by squaring computes it in O(log⁡k)O(\log k) 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 [0110]\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}). PageRank's "teleport" probability exists precisely to rule both out.

Exercises

Exercise 1: A closed form

Use A=[1102]=PDP−1A = \begin{bmatrix} 1 & 1 \\ 0 & 2 \end{bmatrix} = PDP^{-1} with P=[1101]P = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, D=diag⁡(1,2)D = \operatorname{diag}(1, 2) to find AkA^k, then A10A^{10}.

Answer

Ak=[12k−102k]A^k = \begin{bmatrix} 1 & 2^k - 1 \\ 0 & 2^k \end{bmatrix}, so A10=[1102301024]A^{10} = \begin{bmatrix} 1 & 1023 \\ 0 & 1024 \end{bmatrix}.

Solution
P−1=[1−101]PDk=[12k02k]PDkP−1=[1−1+2k02k]k=10: 210=1024\begin{aligned} P^{-1} &= \begin{bmatrix} 1 & -1 \\ 0 & 1 \end{bmatrix} \\ PD^k &= \begin{bmatrix} 1 & 2^k \\ 0 & 2^k \end{bmatrix} \\ PD^kP^{-1} &= \begin{bmatrix} 1 & -1 + 2^k \\ 0 & 2^k \end{bmatrix} \\ k = 10&: \ 2^{10} = 1024 \end{aligned}

Exercise 2: Steady state by hand

Find the steady state and the second eigenvalue of M=[0.80.30.20.7]M = \begin{bmatrix} 0.8 & 0.3 \\ 0.2 & 0.7 \end{bmatrix}.

Answer

s=(0.6,0.4)\mathbf{s} = (0.6, 0.4); the second eigenvalue is 0.5.

Solution
M−I=[−0.20.30.2−0.3] → −0.2x+0.3y=0 → (3,2) → s=(35,25)λ1+λ2=trace=1.5,λ1=1 → λ2=0.5check det: 0.56−0.06=0.5=1⋅0.5\begin{aligned} &M - I = \begin{bmatrix} -0.2 & 0.3 \\ 0.2 & -0.3 \end{bmatrix} \ \to\ -0.2x + 0.3y = 0 \ \to\ (3, 2) \ \to\ \mathbf{s} = \left(\tfrac{3}{5}, \tfrac{2}{5}\right) \\ &\lambda_1 + \lambda_2 = \text{trace} = 1.5, \quad \lambda_1 = 1 \ \to\ \lambda_2 = 0.5 \\ &\text{check det: } 0.56 - 0.06 = 0.5 = 1 \cdot 0.5 \end{aligned}

Exercise 3: Powers of a symmetric matrix

Find a formula for AkA^k when A=[2112]A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}.

Answer

Ak=12⋅[3k+13k−13k−13k+1]A^k = \frac{1}{2} \cdot \begin{bmatrix} 3^k + 1 & 3^k - 1 \\ 3^k - 1 & 3^k + 1 \end{bmatrix}.

Solution
λ=3:(1,1);λ=1:(1,−1)P=[111−1],P−1=12⋅[111−1]PDk=[3k13k−1]Ak=12⋅[3k+13k−13k−13k+1]k=1: 12⋅[4224]=[2112] ✓\begin{aligned} &\lambda = 3: (1, 1); \quad \lambda = 1: (1, -1) \\ &P = \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}, \quad P^{-1} = \tfrac{1}{2} \cdot \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \\ &PD^k = \begin{bmatrix} 3^k & 1 \\ 3^k & -1 \end{bmatrix} \\ &A^k = \tfrac{1}{2} \cdot \begin{bmatrix} 3^k + 1 & 3^k - 1 \\ 3^k - 1 & 3^k + 1 \end{bmatrix} \\ &k = 1: \ \tfrac{1}{2} \cdot \begin{bmatrix} 4 & 2 \\ 2 & 4 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} \ \checkmark \end{aligned}

Exercise 4: A three-state chain

M=[1/21/301/21/31/201/31/2]M = \begin{bmatrix} 1/2 & 1/3 & 0 \\ 1/2 & 1/3 & 1/2 \\ 0 & 1/3 & 1/2 \end{bmatrix} is column-stochastic. Find its steady state exactly with SymPy.

Answer

s=(27,37,27)\mathbf{s} = \left(\frac{2}{7}, \frac{3}{7}, \frac{2}{7}\right).

Solution

From (M−I)x=0(M - I)\mathbf{x} = \mathbf{0}: the first row gives −x/2+y/3=0-x/2 + y/3 = 0, so y=3x/2y = 3x/2; the last row gives y/3−z/2=0y/3 - z/2 = 0, so z=2y/3=xz = 2y/3 = x. Then x+3x/2+x=7x/2=1x + 3x/2 + x = 7x/2 = 1 gives x=27x = \frac{2}{7}.

python · try it yourself
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 A=PDP−1A = PDP^{-1}, what is A5A^5?

  1. P5D5P−5P^5D^5P^{-5}
  2. PD5P−1PD^5P^{-1}
  3. 5PDP−15PDP^{-1}
  4. P−1D5PP^{-1}D^5P

Why does every column-stochastic matrix have eigenvalue 1?

  1. Because its trace is 1
  2. Because its rows of M−IM - I add up to the zero row, so M−IM - I is singular
  3. Because it is symmetric
  4. 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?

  1. The first
  2. The second
  3. Both equally fast
  4. Neither ever forgets