如题。
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 20 12:46:42 2019
@author: Administrator
"""
import numpy as np
def backword(A, B, PI, O):
T = O.shape[0]
# initialization
beta = np.ones(shape = PI.shape[0]) # zeros()
# main
for t in range(T - 1, 0, -1): # python 是 start end step!
new_beta = np.ones(shape = PI.shape[0])
for i in range(beta.size):
new_beta[i] = np.dot(A[i,:] * B[:,O[t]], beta)
beta = new_beta
# print("beta = ", beta)
return sum(PI * B[:,O[0]] * beta), beta
def forword(A, B, PI, O):
T = O.shape[0]
alpha = PI * B[:,O[0]]
for t in range(T - 1):
new_alpha = np.zeros(3)
for i in range(3):
new_alpha[i] = sum(alpha*A[:,i])*B[i][O[t+1]]
alpha = new_alpha
return alpha
# const seting
A = np.array([0.5, 0.2, 0.3, 0.3, 0.5,0.2,0.2,0.3,0.5]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.4, 0.4])
O = np.array([0, 1, 0, 1])
res, beta = backword(A, B, PI, O)
print('10.1题的答案: \n', res)
A = np.array([0.5, 0.1, 0.4, 0.3, 0.5,0.2,0.2,0.2,0.6]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.3, 0.5])
O = np.array([0, 1, 0, 0, 1, 0, 1, 1])
print('\n10.2题的答案: ')
res, beta = backword(A, B, PI, O)
print('后向的结果 beta = ', beta)
alpha = forword(A, B, PI, O)
print('前向的结果 alpha = ', alpha)
gamma = alpha[-1] * beta[-1] / (sum(alpha * beta))
print(gamma)
'''
10.1题的答案:
0.06009079999999999
10.2题的答案:
后向的结果 beta = [0.00632569 0.00684706 0.00577855]
前向的结果 alpha = [0.00132502 0.00121063 0.00094105]
0.2459610979231905
'''
维特比算法求最优路径
import numpy as np
# const setting
A = np.array([0.5, 0.2, 0.3, 0.3, 0.5,0.2,0.2,0.3,0.5]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.4, 0.4])
T = 4
O = np.array([0, 1, 0, 1])
# initialization
delta = np.zeros(shape=(O.shape[0], 3))
psis = np.zeros(shape=(O.shape[0], 3))
# main
delta[0,:] = PI * B[:,O[0]]
psis[0,:] = np.zeros(3)
for t in range(1, T):
new_delta = np.zeros(shape=3)
old_delta = delta[t - 1, :]
for i in range(3):
temp = old_delta * A[:, i] * B[i, O[t]]
new_delta[i] = temp.max()
psis[t, i] = temp.argmax()
delta[t, :] = new_delta
print('psis = \n', psis)
idx = []
idx.append(delta[-1,:].argmax())
idx.append(psis[-1, int(idx[-1])])
idx.append(psis[-2, int(idx[-1])])
idx.append(psis[-3, int(idx[-1])])
print(np.array(idx[::-1], dtype=int)+1)
'''
输出:
[3 2 2 2]
'''