In [1]:
from stochmatrix import StochMatrix
In [2]:
import numpy as np
In [3]:
# Taken from www.math.wustl.edu/~feres/Math450Lect04.pdf
P = np.zeros((10, 10))
P[[0, 0], [0, 2]] = 1./2
P[1, 1], P[1, 6] = 1./3, 2./3
P[2, 0] = 1
P[3, 4] = 1
P[[4, 4, 4], [3, 4, 8]] = 1./3
P[5, 5] = 1
P[6, 6], P[6, 8] = 1./4, 3./4
P[[7, 7, 7, 7], [2, 3, 7, 9]] = 1./4
P[8, 1] = 1
P[[9, 9, 9], [1, 4, 9]] = 1./3
In [4]:
np.get_printoptions()
Out[4]:
In [5]:
np.set_printoptions(precision=3)
In [6]:
print P
StochMatrix
class
In [7]:
P = StochMatrix(P)
Whether P
is irreducible
In [8]:
P.is_irreducible
Out[8]:
Communication classes (strongly connected components) of P
In [9]:
P.comm_classes()
Out[9]:
Recurrent classes of P
In [10]:
P.rec_classes()
Out[10]:
Recurrent states of P
In [11]:
rec_states = [i for rec_class in P.rec_classes() for i in rec_class]
In [12]:
print rec_states
Transient states of P
In [13]:
trans_states = [i for i in range(10) if i not in rec_states]
In [14]:
print trans_states
Canonical form of P
In [15]:
permutation = rec_states + trans_states
In [16]:
permutation
Out[16]:
In [17]:
print P[permutation, :][:, permutation]
Decomposing P
to irreducible submatrices
In [18]:
for i, rec_class in enumerate(P.rec_classes()):
print 'P_{0} ='.format(i)
print P[rec_class, :][:, rec_class]
In [18]: