Skip to content

Instantly share code, notes, and snippets.

@jseabold
Created September 20, 2012 18:01
Show Gist options
  • Save jseabold/3757398 to your computer and use it in GitHub Desktop.
Save jseabold/3757398 to your computer and use it in GitHub Desktop.
Perron-Frobenius theorem
T = np.array([[0,1,0],[0, .1, .9],[.6, .4, 0]])
p = np.array([.5, .2, .3])
ps = [np.inf, p]
while np.all(np.abs(ps[-1] - ps[-2]) > 1e-8):
ps.append(np.dot(ps[-1], T))
from scipy import linalg
# get the spectrum
eigs, evecs = linalg.eig(T, left=True, right=False)
print eigs[2]
print ps[-1]
# stochastic eigenvector
print evecs[:,2]/evecs[:,2].sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment