In [1]:
import numpy as np

Stochastic Matrices

These guys are matrices that predict a probability distribution iteratively. You present a "current" distribution, then you multiply by the "transition matrix" and it tells you the "next" distribution.


In [55]:
#
# define a transition matrix
#
T = np.array([[0.1, 0.2, 0.3],
             [0.5,0.3,0.6],
             [0.4,0.5,0.1]])
T


Out[55]:
array([[0.1, 0.2, 0.3],
       [0.5, 0.3, 0.6],
       [0.4, 0.5, 0.1]])

In [56]:
#
# Start out with a random prob distribution vector
#

x0 = np.array([np.random.rand(3)]).T
x0 = x0/x0.sum()
x0


Out[56]:
array([[0.65827967],
       [0.29726598],
       [0.04445434]])

In [57]:
x1 = T.dot(x0)
x1


Out[57]:
array([[0.13861747],
       [0.44499224],
       [0.4163903 ]])

In [58]:
x2 = T.dot(x1)
x2


Out[58]:
array([[0.22777728],
       [0.45264058],
       [0.31958214]])

In [59]:
x3 = T.dot(x2)
x3


Out[59]:
array([[0.20918049],
       [0.4414301 ],
       [0.34938942]])

In [60]:
xn = x3
for i in range(100):
    xn = T.dot(xn)
    
xn


Out[60]:
array([[0.21290323],
       [0.44516129],
       [0.34193548]])

In [61]:
vals, vecs = np.linalg.eig(T)
vals


Out[61]:
array([ 1.       , -0.1381966, -0.3618034])

In [62]:
vecs


Out[62]:
array([[ 0.35463375,  0.80901699, -0.30901699],
       [ 0.74150694, -0.5       , -0.5       ],
       [ 0.5695633 , -0.30901699,  0.80901699]])

In [63]:
vecs.sum(axis=0)


Out[63]:
array([ 1.66570400e+00, -8.32667268e-16,  0.00000000e+00])

In [64]:
vecs/vecs.sum(axis=0)[0]


Out[64]:
array([[ 0.21290323,  0.48569073, -0.18551735],
       [ 0.44516129, -0.30017338, -0.30017338],
       [ 0.34193548, -0.18551735,  0.48569073]])

In [ ]: