DAY 3
0
AI & Data

## 小明的狀態轉移 (續)

• Ubuntu 18.04 LTS
• Python 3.6.6
• numpy

### 實作

``````import numpy as np
Matrix_trans = np.array([[0.9,0.1],[0.5,0.5]])

# after n steps
Matrix_trans_step3 = np.linalg.matrix_power(Matrix_trans, 3)
Matrix_trans_step5 = np.linalg.matrix_power(Matrix_trans, 5)
Matrix_trans_step10 = np.linalg.matrix_power(Matrix_trans, 10)
Matrix_trans_step50 = np.linalg.matrix_power(Matrix_trans, 50)
Matrix_trans_step100 = np.linalg.matrix_power(Matrix_trans, 100)

# print the result
print('After 3 steps: \n', Matrix_trans_step3, '\n')
print('After 5 steps: \n', Matrix_trans_step5, '\n')
print('After 10 steps: \n', Matrix_trans_step10, '\n')
print('After 50 steps: \n', Matrix_trans_step50, '\n')
print('After 100 steps: \n', Matrix_trans_step100, '\n')
``````

``````After 3 steps:
[[0.844 0.156]
[0.78  0.22 ]]

After 5 steps:
[[0.83504 0.16496]
[0.8248  0.1752 ]]

After 10 steps:
[[0.83335081 0.16664919]
[0.83324595 0.16675405]]

After 50 steps:
[[0.83333333 0.16666667]
[0.83333333 0.16666667]]

After 100 steps:
[[0.83333333 0.16666667]
[0.83333333 0.16666667]]
``````

``````# set s0 = [1, 0]
s0 = np.array([1.0, 0.0])

# print the result
print('Initial state: \n', s0, '\n')
print('After 3 steps: \n', np.dot(s0, Matrix_trans_step3), '\n')
print('After 5 steps: \n', np.dot(s0, Matrix_trans_step5), '\n')
print('After 10 steps: \n', np.dot(s0, Matrix_trans_step10), '\n')
print('After 50 steps: \n', np.dot(s0, Matrix_trans_step50), '\n')
print('After 100 steps: \n', np.dot(s0, Matrix_trans_step100), '\n')
``````
`````` Initial state:
[1. 0.]

After 3 steps:
[0.844 0.156]

After 5 steps:
[0.83504 0.16496]

After 10 steps:
[0.83335081 0.16664919]

After 50 steps:
[0.83333333 0.16666667]

After 100 steps:
[0.83333333 0.16666667]
``````
``````# random initial state
rand_num = np.random.rand(1)
random_s0 = np.array([rand_num, 1-rand_num]).reshape(2)

# print the result
print('Initial state: \n', random_s0, '\n')
print('After 3 steps: \n', np.dot(random_s0, Matrix_trans_step3), '\n')
print('After 5 steps: \n', np.dot(random_s0, Matrix_trans_step5), '\n')
print('After 10 steps: \n', np.dot(random_s0, Matrix_trans_step10), '\n')
print('After 50 steps: \n', np.dot(random_s0, Matrix_trans_step50), '\n')
print('After 100 steps: \n', np.dot(random_s0, Matrix_trans_step100), '\n')
``````
``````Initial state:
[0.6913491 0.3086509]

After 3 steps:
[0.82424634 0.17575366]

After 5 steps:
[0.83187941 0.16812059]

After 10 steps:
[0.83331845 0.16668155]

After 50 steps:
[0.83333333 0.16666667]

After 100 steps:
[0.83333333 0.16666667]
``````

### 限制

• 任意狀態能夠轉移到任意其他狀態：狀態之間不一定要可以直接轉移，但必須能夠到達那個狀態。
• 狀態轉移不是固定的週期 (ex: 1 -> 3 -> 2 -> 4 -> 1 ...)

## Reference

[1] How do I tell whether a Markov chain can converge to equilibrium? [link]