题目要求使用后向算法计算,程序中分别使用了前向算法和后向算法,最后得到的结果是一样的。
前向算法和后向算法:
输入是:
A: 状态转移概率矩阵
B: 观测概率矩阵
p_i: 初始状态概率向量
O: 观测序列
T: 观测序列的个数
N: 状态序列的个数
输出是:
P: 此模型参数下,得到此观测序列的概率
前向算法:
def fore_algorithm(A, B, p_i, O, T, N):
alpha = np.zeros((T, N))
o = np.zeros(T, np.int)
for i in range(T):
if O[i] == '白':
o[i] = 1
else:
o[i] = 0
for i in range(N):
h = o[0]
alpha[0][i] = p_i[i] * B[i][h]
for t in range(T-1):
h = o[t+1]
for i in range(N):
a = 0
for j in range(N):
a += (alpha[t][j] * A[j][i])
alpha[t+1][i] = a * B[i][h]
P = 0
for i in range(N):
P += alpha[T-1][i]
return P
后向算法:
def back_algorithm(A, B, p_i, O, T, N):
P = 0
o = np.zeros(T, np.int)
beta = np.ones((T, N))
for i in range(T):
if O[i] == '白':
o[i] = 1
else:
o[i] = 0
for t in range(T-1):
t = T - t - 2
h = o[t + 1]
h = int(h)
for i in range(N):
beta[t][i] = 0
for j in range(N):
beta[t][i] += A[i][j] * B[j][h] * beta[t+1][j]
for i in range(N):
h = o[0]
h = int(h)
P += p_i[i] * B[i][h] * beta[0][i]
return P
根据题目的要求,已知条件如下:
T = 4
N = 3
O = ['红', '白', '红', '白']
A = [[0.5, 0.2, 0.3],
[0.3, 0.5, 0.2],
[0.2, 0.3, 0.5]]
B = [[0.5, 0.5],
[0.4, 0.6],
[0.7, 0.3]]
p_i = [0.2, 0.4, 0.4]
将已知条件输入到函数中:
PF = fore_algorithm(A, B, p_i, O, T, N)
print('前向算法所得结果: ')
print(PF)
PB = back_algorithm(A, B, p_i, O, T, N)
print('后向算法所的结果: ')
print(PB)
运行后得到的结果为:
前向算法所得结果:
0.06009079999999999
后向算法所的结果:
0.06009079999999999
欢迎大神批评指正!
菜鸟丁午 原创文章 3获赞 3访问量 130 关注 私信 展开阅读全文