{
"cells": [
{
"cell_type": "markdown",
"id": "8d688d3d",
"metadata": {},
"source": [
"# The Perron-Frobenius Theorem\n",
"\n",
"\n",
"\n",
"In addition to what’s in Anaconda, this lecture will need the following libraries:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcf5a296",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"!pip install quantecon"
]
},
{
"cell_type": "markdown",
"id": "509e9b58",
"metadata": {},
"source": [
"In this lecture we will begin with the foundational concepts in spectral theory.\n",
"\n",
"Then we will explore the Perron-Frobenius Theorem and connect it to applications in Markov chains and networks.\n",
"\n",
"We will use the following imports:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13bb5603",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.linalg import eig\n",
"import scipy as sp\n",
"import quantecon as qe"
]
},
{
"cell_type": "markdown",
"id": "538df5c0",
"metadata": {},
"source": [
"## Nonnegative matrices\n",
"\n",
"Often, in economics, the matrix that we are dealing with is nonnegative.\n",
"\n",
"Nonnegative matrices have several special and useful properties.\n",
"\n",
"In this section we will discuss some of them — in particular, the connection\n",
"between nonnegativity and eigenvalues.\n",
"\n",
"An $ n \\times m $ matrix $ A $ is called **nonnegative** if every element of $ A $\n",
"is nonnegative, i.e., $ a_{ij} \\geq 0 $ for every $ i,j $.\n",
"\n",
"We denote this as $ A \\geq 0 $.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "e7b6c166",
"metadata": {},
"source": [
"### Irreducible matrices\n",
"\n",
"We introduced irreducible matrices in the [Markov chain lecture](https://intro.quantecon.org/markov_chains_II.html#mc-irreducible).\n",
"\n",
"Here we generalize this concept:\n",
"\n",
"Let $ a^{k}_{ij} $ be element $ (i,j) $ of $ A^k $.\n",
"\n",
"An $ n \\times n $ nonnegative matrix $ A $ is called irreducible if $ A + A^2 + A^3 + \\cdots \\gg 0 $, where $ \\gg 0 $ indicates that every element in $ A $ is strictly positive.\n",
"\n",
"In other words, for each $ i,j $ with $ 1 \\leq i, j \\leq n $, there exists a $ k \\geq 0 $ such that $ a^{k}_{ij} > 0 $.\n",
"\n",
"Here are some examples to illustrate this further:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 0.5 & 0.1 \\\\ \n",
" 0.2 & 0.2 \n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$ A $ is irreducible since $ a_{ij}>0 $ for all $ (i,j) $.\n",
"\n",
"$$\n",
"B = \\begin{bmatrix} 0 & 1 \\\\ \n",
" 1 & 0 \n",
"\\end{bmatrix}\n",
", \\quad\n",
"B^2 = \\begin{bmatrix} 1 & 0 \\\\ \n",
" 0 & 1\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$ B $ is irreducible since $ B + B^2 $ is a matrix of ones.\n",
"\n",
"$$\n",
"C = \\begin{bmatrix} 1 & 0 \\\\ \n",
" 0 & 1 \n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$ C $ is not irreducible since $ C^k = C $ for all $ k \\geq 0 $ and thus\n",
"$ c^{k}_{12},c^{k}_{21} = 0 $ for all $ k \\geq 0 $."
]
},
{
"cell_type": "markdown",
"id": "b634144e",
"metadata": {},
"source": [
"### Left eigenvectors\n",
"\n",
"Recall that we previously discussed eigenvectors in [Eigenvalues and Eigenvectors](https://intro.quantecon.org/eigen_I.html#la-eigenvalues).\n",
"\n",
"In particular, $ \\lambda $ is an eigenvalue of $ A $ and $ v $ is an eigenvector of $ A $ if $ v $ is nonzero and satisfy\n",
"\n",
"$$\n",
"Av = \\lambda v.\n",
"$$\n",
"\n",
"In this section we introduce left eigenvectors.\n",
"\n",
"To avoid confusion, what we previously referred to as “eigenvectors” will be called “right eigenvectors”.\n",
"\n",
"Left eigenvectors will play important roles in what follows, including that of stochastic steady states for dynamic models under a Markov assumption.\n",
"\n",
"A vector $ w $ is called a left eigenvector of $ A $ if $ w $ is a right eigenvector of $ A^\\top $.\n",
"\n",
"In other words, if $ w $ is a left eigenvector of matrix $ A $, then $ A^\\top w = \\lambda w $, where $ \\lambda $ is the eigenvalue associated with the left eigenvector $ v $.\n",
"\n",
"This hints at how to compute left eigenvectors"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3835ce98",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"A = np.array([[3, 2],\n",
" [1, 4]])\n",
"\n",
"# Compute eigenvalues and right eigenvectors\n",
"λ, v = eig(A)\n",
"\n",
"# Compute eigenvalues and left eigenvectors\n",
"λ, w = eig(A.T)\n",
"\n",
"# Keep 5 decimals\n",
"np.set_printoptions(precision=5)\n",
"\n",
"print(f\"The eigenvalues of A are:\\n {λ}\\n\")\n",
"print(f\"The corresponding right eigenvectors are: \\n {v[:,0]} and {-v[:,1]}\\n\")\n",
"print(f\"The corresponding left eigenvectors are: \\n {w[:,0]} and {-w[:,1]}\\n\")"
]
},
{
"cell_type": "markdown",
"id": "f394a72d",
"metadata": {},
"source": [
"We can also use `scipy.linalg.eig` with argument `left=True` to find left eigenvectors directly"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17ff4541",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"eigenvals, ε, e = sp.linalg.eig(A, left=True)\n",
"\n",
"print(f\"The eigenvalues of A are:\\n {eigenvals.real}\\n\")\n",
"print(f\"The corresponding right eigenvectors are: \\n {e[:,0]} and {-e[:,1]}\\n\")\n",
"print(f\"The corresponding left eigenvectors are: \\n {ε[:,0]} and {-ε[:,1]}\\n\")"
]
},
{
"cell_type": "markdown",
"id": "228857c2",
"metadata": {},
"source": [
"The eigenvalues are the same while the eigenvectors themselves are different.\n",
"\n",
"(Also note that we are taking the nonnegative value of the eigenvector of [dominant eigenvalue](#perron-frobe), this is because `eig` automatically normalizes the eigenvectors.)\n",
"\n",
"We can then take transpose to obtain $ A^\\top w = \\lambda w $ and obtain $ w^\\top A= \\lambda w^\\top $.\n",
"\n",
"This is a more common expression and where the name left eigenvectors originates.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "a38f9816",
"metadata": {},
"source": [
"### The Perron-Frobenius theorem\n",
"\n",
"For a square nonnegative matrix $ A $, the behavior of $ A^k $ as $ k \\to \\infty $ is controlled by the eigenvalue with the largest\n",
"absolute value, often called the **dominant eigenvalue**.\n",
"\n",
"For any such matrix $ A $, the Perron-Frobenius Theorem characterizes certain\n",
"properties of the dominant eigenvalue and its corresponding eigenvector."
]
},
{
"cell_type": "markdown",
"id": "7c162fc5",
"metadata": {},
"source": [
"### (Perron-Frobenius Theorem)\n",
"\n",
"If a matrix $ A \\geq 0 $ then,\n",
"\n",
"1. the dominant eigenvalue of $ A $, $ r(A) $, is real-valued and nonnegative. \n",
"1. for any other eigenvalue (possibly complex) $ \\lambda $ of $ A $, $ |\\lambda| \\leq r(A) $. \n",
"1. we can find a nonnegative and nonzero eigenvector $ v $ such that $ Av = r(A)v $. \n",
"\n",
"\n",
"Moreover if $ A $ is also irreducible then,\n",
"\n",
"1. the eigenvector $ v $ associated with the eigenvalue $ r(A) $ is strictly positive. \n",
"1. there exists no other positive eigenvector $ v $ (except scalar multiples of $ v $) associated with $ r(A) $. \n",
"\n",
"\n",
"(More of the Perron-Frobenius theorem about primitive matrices will be introduced [below](#prim-matrices).)\n",
"\n",
"(This is a relatively simple version of the theorem — for more details see\n",
"[here](https://en.wikipedia.org/wiki/Perron%E2%80%93Frobenius_theorem)).\n",
"\n",
"We will see applications of the theorem below.\n",
"\n",
"Let’s build our intuition for the theorem using a simple example we have seen [before](https://intro.quantecon.org/markov_chains_I.html#mc-eg1).\n",
"\n",
"Now let’s consider examples for each case."
]
},
{
"cell_type": "markdown",
"id": "a056e56d",
"metadata": {},
"source": [
"#### Example: Irreducible matrix\n",
"\n",
"Consider the following irreducible matrix $ A $:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "839b26a2",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"A = np.array([[0, 1, 0],\n",
" [.5, 0, .5],\n",
" [0, 1, 0]])"
]
},
{
"cell_type": "markdown",
"id": "318750e9",
"metadata": {},
"source": [
"We can compute the dominant eigenvalue and the corresponding eigenvector"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86a1925b",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"eig(A)"
]
},
{
"cell_type": "markdown",
"id": "438193e5",
"metadata": {},
"source": [
"Now we can see the claims of the Perron-Frobenius Theorem holds for the irreducible matrix $ A $:\n",
"\n",
"1. The dominant eigenvalue is real-valued and non-negative. \n",
"1. All other eigenvalues have absolute values less than or equal to the dominant eigenvalue. \n",
"1. A non-negative and nonzero eigenvector is associated with the dominant eigenvalue. \n",
"1. As the matrix is irreducible, the eigenvector associated with the dominant eigenvalue is strictly positive. \n",
"1. There exists no other positive eigenvector associated with the dominant eigenvalue. \n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "ae554d23",
"metadata": {},
"source": [
"### Primitive matrices\n",
"\n",
"We know that in real world situations it’s hard for a matrix to be everywhere positive (although they have nice properties).\n",
"\n",
"The primitive matrices, however, can still give us helpful properties with looser definitions.\n",
"\n",
"Let $ A $ be a square nonnegative matrix and let $ A^k $ be the $ k^{th} $ power of $ A $.\n",
"\n",
"A matrix is called **primitive** if there exists a $ k \\in \\mathbb{N} $ such that $ A^k $ is everywhere positive.\n",
"\n",
"Recall the examples given in irreducible matrices:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 0.5 & 0.1 \\\\ \n",
" 0.2 & 0.2 \n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$ A $ here is also a primitive matrix since $ A^k $ is everywhere nonnegative for $ k \\in \\mathbb{N} $.\n",
"\n",
"$$\n",
"B = \\begin{bmatrix} 0 & 1 \\\\ \n",
" 1 & 0 \n",
"\\end{bmatrix}\n",
", \\quad\n",
"B^2 = \\begin{bmatrix} 1 & 0 \\\\ \n",
" 0 & 1\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$ B $ is irreducible but not primitive since there are always zeros in either principal diagonal or secondary diagonal.\n",
"\n",
"We can see that if a matrix is primitive, then it implies the matrix is irreducible but not vice versa.\n",
"\n",
"Now let’s step back to the primitive matrices part of the Perron-Frobenius Theorem"
]
},
{
"cell_type": "markdown",
"id": "b87e0036",
"metadata": {},
"source": [
"### (Continous of Perron-Frobenius Theorem)\n",
"\n",
"If $ A $ is primitive then,\n",
"\n",
"1. the inequality $ |\\lambda| \\leq r(A) $ is **strict** for all eigenvalues $ \\lambda $ of $ A $ distinct from $ r(A) $, and \n",
"1. with $ v $ and $ w $ normalized so that the inner product of $ w $ and $ v = 1 $, we have\n",
" $ r(A)^{-m} A^m $ converges to $ v w^{\\top} $ when $ m \\rightarrow \\infty $. The matrix $ v w^{\\top} $ is called the **Perron projection** of $ A $. "
]
},
{
"cell_type": "markdown",
"id": "f181a630",
"metadata": {},
"source": [
"#### Example 1: Primitive matrix\n",
"\n",
"Consider the following primitive matrix $ B $:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0667ab09",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"B = np.array([[0, 1, 1],\n",
" [1, 0, 1],\n",
" [1, 1, 0]])\n",
"\n",
"np.linalg.matrix_power(B, 2)"
]
},
{
"cell_type": "markdown",
"id": "1f1a61cd",
"metadata": {},
"source": [
"We compute the dominant eigenvalue and the corresponding eigenvector"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05453022",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"eig(B)"
]
},
{
"cell_type": "markdown",
"id": "a42a1b9c",
"metadata": {},
"source": [
"Now let’s give some examples to see if the claims of the Perron-Frobenius Theorem hold for the primitive matrix $ B $:\n",
"\n",
"1. The dominant eigenvalue is real-valued and non-negative. \n",
"1. All other eigenvalues have absolute values strictly less than the dominant eigenvalue. \n",
"1. A non-negative and nonzero eigenvector is associated with the dominant eigenvalue. \n",
"1. The eigenvector associated with the dominant eigenvalue is strictly positive. \n",
"1. There exists no other positive eigenvector associated with the dominant eigenvalue. \n",
"1. The inequality $ |\\lambda| < r(B) $ holds for all eigenvalues $ \\lambda $ of $ B $ distinct from the dominant eigenvalue. \n",
"\n",
"\n",
"Furthermore, we can verify the convergence property (7) of the theorem on the following examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "129d9e84",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def compute_perron_projection(M):\n",
"\n",
" eigval, v = eig(M)\n",
" eigval, w = eig(M.T)\n",
"\n",
" r = np.max(eigval)\n",
"\n",
" # Find the index of the dominant (Perron) eigenvalue\n",
" i = np.argmax(eigval)\n",
"\n",
" # Get the Perron eigenvectors\n",
" v_P = v[:, i].reshape(-1, 1)\n",
" w_P = w[:, i].reshape(-1, 1)\n",
"\n",
" # Normalize the left and right eigenvectors\n",
" norm_factor = w_P.T @ v_P\n",
" v_norm = v_P / norm_factor\n",
"\n",
" # Compute the Perron projection matrix\n",
" P = v_norm @ w_P.T\n",
" return P, r\n",
"\n",
"def check_convergence(M):\n",
" P, r = compute_perron_projection(M)\n",
" print(\"Perron projection:\")\n",
" print(P)\n",
"\n",
" # Define a list of values for n\n",
" n_list = [1, 10, 100, 1000, 10000]\n",
"\n",
" for n in n_list:\n",
"\n",
" # Compute (A/r)^n\n",
" M_n = np.linalg.matrix_power(M/r, n)\n",
"\n",
" # Compute the difference between A^n / r^n and the Perron projection\n",
" diff = np.abs(M_n - P)\n",
"\n",
" # Calculate the norm of the difference matrix\n",
" diff_norm = np.linalg.norm(diff, 'fro')\n",
" print(f\"n = {n}, error = {diff_norm:.10f}\")\n",
"\n",
"\n",
"A1 = np.array([[1, 2],\n",
" [1, 4]])\n",
"\n",
"A2 = np.array([[0, 1, 1],\n",
" [1, 0, 1],\n",
" [1, 1, 0]])\n",
"\n",
"A3 = np.array([[0.971, 0.029, 0.1, 1],\n",
" [0.145, 0.778, 0.077, 0.59],\n",
" [0.1, 0.508, 0.492, 1.12],\n",
" [0.2, 0.8, 0.71, 0.95]])\n",
"\n",
"for M in A1, A2, A3:\n",
" print(\"Matrix:\")\n",
" print(M)\n",
" check_convergence(M)\n",
" print()\n",
" print(\"-\"*36)\n",
" print()"
]
},
{
"cell_type": "markdown",
"id": "a5f248c9",
"metadata": {},
"source": [
"The convergence is not observed in cases of non-primitive matrices.\n",
"\n",
"Let’s go through an example"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29ae15c5",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"B = np.array([[0, 1, 1],\n",
" [1, 0, 0],\n",
" [1, 0, 0]])\n",
"\n",
"# This shows that the matrix is not primitive\n",
"print(\"Matrix:\")\n",
"print(B)\n",
"print(\"100th power of matrix B:\")\n",
"print(np.linalg.matrix_power(B, 100))\n",
"\n",
"check_convergence(B)"
]
},
{
"cell_type": "markdown",
"id": "ce3974e9",
"metadata": {},
"source": [
"The result shows that the matrix is not primitive as it is not everywhere positive.\n",
"\n",
"These examples show how the Perron-Frobenius Theorem relates to the eigenvalues and eigenvectors of positive matrices and the convergence of the power of matrices.\n",
"\n",
"In fact we have already seen the theorem in action before in [the Markov chain lecture](https://intro.quantecon.org/markov_chains_I.html#mc1_ex_1).\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "5f85af88",
"metadata": {},
"source": [
"#### Example 2: Connection to Markov chains\n",
"\n",
"We are now prepared to bridge the languages spoken in the two lectures.\n",
"\n",
"A primitive matrix is both irreducible and aperiodic.\n",
"\n",
"So Perron-Frobenius Theorem explains why both [Imam and Temple matrix](https://intro.quantecon.org/markov_chains_I.html#mc-eg3) and [Hamilton matrix](https://en.wikipedia.org/wiki/Hamiltonian_matrix) converge to a stationary distribution, which is the Perron projection of the two matrices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48508f3a",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"P = np.array([[0.68, 0.12, 0.20],\n",
" [0.50, 0.24, 0.26],\n",
" [0.36, 0.18, 0.46]])\n",
"\n",
"print(compute_perron_projection(P)[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "656c8619",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"mc = qe.MarkovChain(P)\n",
"ψ_star = mc.stationary_distributions[0]\n",
"ψ_star"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6455727c",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"P_hamilton = np.array([[0.971, 0.029, 0.000],\n",
" [0.145, 0.778, 0.077],\n",
" [0.000, 0.508, 0.492]])\n",
"\n",
"print(compute_perron_projection(P_hamilton)[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d791a17",
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"mc = qe.MarkovChain(P_hamilton)\n",
"ψ_star = mc.stationary_distributions[0]\n",
"ψ_star"
]
},
{
"cell_type": "markdown",
"id": "743014cb",
"metadata": {},
"source": [
"We can also verify other properties hinted by Perron-Frobenius in these stochastic matrices.\n",
"\n",
"Another example is the relationship between convergence gap and convergence rate.\n",
"\n",
"In the [exercise](https://intro.quantecon.org/markov_chains_I.html#mc1_ex_1), we stated that the convergence rate is determined by the spectral gap, the difference between the largest and the second largest eigenvalue.\n",
"\n",
"This can be proven using what we have learned here.\n",
"\n",
"Please note that we use $ \\mathbb{1} $ for a vector of ones in this lecture.\n",
"\n",
"With Markov model $ M $ with state space $ S $ and transition matrix $ P $, we can write $ P^t $ as\n",
"\n",
"$$\n",
"P^t=\\sum_{i=1}^{n-1} \\lambda_i^t v_i w_i^{\\top}+\\mathbb{1} \\psi^*,\n",
"$$\n",
"\n",
"This is proven in [[Sargent and Stachurski, 2023](https://intro.quantecon.org/zreferences.html#id19)] and a nice discussion can be found [here](https://math.stackexchange.com/questions/2433997/can-all-matrices-be-decomposed-as-product-of-right-and-left-eigenvector).\n",
"\n",
"In this formula $ \\lambda_i $ is an eigenvalue of $ P $ with corresponding right and left eigenvectors $ v_i $ and $ w_i $ .\n",
"\n",
"Premultiplying $ P^t $ by arbitrary $ \\psi \\in \\mathscr{D}(S) $ and rearranging now gives\n",
"\n",
"$$\n",
"\\psi P^t-\\psi^*=\\sum_{i=1}^{n-1} \\lambda_i^t \\psi v_i w_i^{\\top}\n",
"$$\n",
"\n",
"Recall that eigenvalues are ordered from smallest to largest from $ i = 1 ... n $.\n",
"\n",
"As we have seen, the largest eigenvalue for a primitive stochastic matrix is one.\n",
"\n",
"This can be proven using [Gershgorin Circle Theorem](https://en.wikipedia.org/wiki/Gershgorin_circle_theorem),\n",
"but it is out of the scope of this lecture.\n",
"\n",
"So by the statement (6) of Perron-Frobenius Theorem, $ \\lambda_i<1 $ for all $ i