统计学习方法第10章隐马尔可夫模型的概率计算方法的前向算法
from numpy import *
Q=[1,2,3]
V=['红','白']
A=mat([[0.5,0.2,0.3],[0.3,0.5,0.2],[0.2,0.3,0.5]])
B=mat([[0.5,0.5],[0.4,0.6],[0.7,0.3]])
Pi=mat([0.2,0.4,0.4])
O=['红','白','红']
m=len(O)
alpha=multiply(Pi,B[:,V.index(O[0])].T) #找到输出的值对应于B矩阵中的那一列来输出观察序列
#print(alpha)
for i in range(1,m): #因为根据初始状态计算了一次观测这里遍历观测序列的长度减去一
alpha=multiply(alpha*A,B[:,V.index(O[i])].T) #对应书上的公式10.16
print(sum(alpha))
输出:
0.130218
made by zcl at CUMT
I know I can because I have a heart that beats