马尔可夫模型
试想有这么一个场景,每天的天气有联系吗?晴天、多云、雷雨之间的相互转换是存在概率的,也就是转移概率。如图所示,假如今天的天气为晴天,那么明天的情况有三种可能,晴天到晴天的概率为0.5,晴天到多云的概率为0.38,晴天到雷雨的概率为0.13。
数学定义
状态转移的概率矩阵
可以很直观的看到,该矩阵已经包含了所有的状态转换信息。并且该矩阵是不变的。
初始概率
一阶马尔可夫模型,定义了三种状态初始发生的概率,即晴天、多云、雷雨发生的概率。如[0.8, 0.2, 0.1]
计算下一次的状态概率
代码演示
import numpy as np
def markov():
# 初始状态分布
init_status = np.array([0.8, 0.2, 0.1])
# 状态转移矩阵
transfer_matrix = np.array([[0.50, 0.375, 0.125],
[0.25, 0.125, 0.625],
[0.25, 0.375, 0.375]])
temp_status = init_status
for i in range(25):
temp_status = np.dot(temp_status, transfer_matrix)
print('round ', i, end='\t')
print(temp_status)
if __name__ == '__main__':
markov()
D:\Software\anaconda3\envs\env_one\python.exe D:/env_one/markov_demo.py
round 0 [0.475 0.3625 0.2625]
round 1 [0.39375 0.321875 0.384375]
round 2 [0.3734375 0.33203125 0.39453125]
round 3 [0.36835937 0.32949219 0.40214844]
round 4 [0.36708984 0.33012695 0.4027832 ]
round 5 [0.36677246 0.32996826 0.40325928]
round 6 [0.36669312 0.33000793 0.40329895]
round 7 [0.36667328 0.32999802 0.4033287 ]
round 8 [0.36666832 0.3300005 0.40333118]
round 9 [0.36666708 0.32999988 0.40333304]
round 10 [0.36666677 0.33000003 0.4033332 ]
round 11 [0.36666669 0.32999999 0.40333332]
round 12 [0.36666667 0.33 0.40333332]
round 13 [0.36666667 0.33 0.40333333]
round 14 [0.36666667 0.33 0.40333333]
round 15 [0.36666667 0.33 0.40333333]
round 16 [0.36666667 0.33 0.40333333]
round 17 [0.36666667 0.33 0.40333333]
round 18 [0.36666667 0.33 0.40333333]
round 19 [0.36666667 0.33 0.40333333]
round 20 [0.36666667 0.33 0.40333333]
round 21 [0.36666667 0.33 0.40333333]
round 22 [0.36666667 0.33 0.40333333]
round 23 [0.36666667 0.33 0.40333333]
round 24 [0.36666667 0.33 0.40333333]
Process finished with exit code 0