limit behavior of transition probability of a given markov chain
P.S. Note that below code use @ operator which requires python 3.5 or above, see matrix_operator.
>>> import numpy as np
>>> np.set_printoptions(precision=4)
>>> g = lambda trans_mat, cnt=20: reduce(lambda x1, x2: print(x1) or x1@x2, (trans_mat for _ in range(cnt)), np.eye(len(trans_mat)))
>>> t, t1 = np.array([[.5,.4,.1],[.3,.4,.3],[.2,.3,.5]]), \
np.array([[.7,0,.3,0],[.5,0,.5,0],[0,.4,0,.6],[0,.2,0,.8]])
>>> g(t,10)
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
[[ 0.5 0.4 0.1]
[ 0.3 0.4 0.3]
[ 0.2 0.3 0.5]]
[[ 0.39 0.39 0.22]
[ 0.33 0.37 0.3 ]
[ 0.29 0.35 0.36]]
[[ 0.356 0.378 0.266]
[ 0.336 0.37 0.294]
[ 0.322 0.364 0.314]]
[[ 0.3446 0.3734 0.282 ]
[ 0.3378 0.3706 0.2916]
[ 0.333 0.3686 0.2984]]
[[ 0.3407 0.3718 0.2875]
[ 0.3384 0.3708 0.2908]
[ 0.3368 0.3702 0.2931]]
[[ 0.3394 0.3713 0.2894]
[ 0.3386 0.3709 0.2905]
[ 0.338 0.3707 0.2913]]
[[ 0.3389 0.3711 0.29 ]
[ 0.3387 0.371 0.2904]
[ 0.3385 0.3709 0.2906]]
[[ 0.3388 0.371 0.2902]
[ 0.3387 0.371 0.2903]
[ 0.3386 0.3709 0.2904]]
[[ 0.3387 0.371 0.2903]
[ 0.3387 0.371 0.2903]
[ 0.3387 0.371 0.2904]]
Out[20]:
array([[ 0.3387, 0.371 , 0.2903],
[ 0.3387, 0.371 , 0.2903],
[ 0.3387, 0.371 , 0.2903]])
- plot a sequence of function with different hyperparameter
Given f(x)=xa−1e−x/b (which is integrand of gamma function when b=1, see Gamma Function, we can plot a series of plots of functions.
import numpy as np
from sympy import *
%matplotlib inline
x = Symbol('x')
def g(a=2.,b=1.):
return x**(a-1) * exp(-x/b)
for v in np.linspace(0.5,2,6):
plot(g(v),(x,0,v-1+4),title = 'a=%s, b=1.0' % v)
the output is show as