import numpy as np
# 状态转移矩阵
A = np.array([[0.5,0.2,0.3],[0.3,0.5,0.2],[0.2,0.3,0.5]])
# 观测矩阵
B = np.array([[0.5,0.5],[0.4,0.6],[0.7,0.3]])
# 初始矩阵
pi = np.array([0.2,0.4,0.4])
# 序列
O = np.array([0,1,0])
def backward(A,B,pi,O):
m=np.shape(A)[0]
T=len(O)
belta=np.zeros((T, m))
# 假设最后一个时刻的值均为1
for i in range(m):
belta[0][i]=1
# 递推
for t in range(1,T):
for i in range(m):
sum=0
for j in range(m):
sum+=A[i][j]*B[j][O[T-t]]*belta[t-1][j]
belta[t][i]=sum
# 计算最后的概率值
P=0
for i in range(m):
P+=pi[i]*B[i][O[0]]*belta[-1][i]
return P
print(backward(A,B,pi,O))
后向算法理论部分在李航统计学习方法上有详细的介绍,实践部分采用python实现
李航统计学习方法 隐马尔科夫 用后向算法实现例10.2
最新推荐文章于 2021-06-27 23:54:44 发布