概率图模型一:隐马尔可夫模型
两个基本假设:
- 齐次马尔科夫假设:隐藏的马尔科夫链在任意时刻的状态只依赖于前一时刻的状态,与其他时刻的状态及观测无关,也与时刻无关。
- 观测独立性假设:任意时刻的观测只依赖于该时刻的马尔科夫链状态,与其他观测及状态无关。
公式部分总是整不好,就不写了。参考相国大人的博客
跑了一下博客的代码部分:
观测序列概率的前向算法
import numpy as np
#马尔科夫前向算法
def forward_prob(model,Observe,States):
A,B,pi=model
N=States.size
T=Observe.size
alpha=pi*B[:,Observe[0]]
print "(1)计算初值alpha_1(i): ",alpha
print "(2)递推..."
for t in xrange(0,T-1):
print "t=",t+1," alpha_",t+1,"(i):",alpha
alpha=alpha.dot(A)*B[:,Observe[t+1]]
print "(3)终止。alpha_",T,"(i): ", alpha
print "输出Prob: ",alpha.sum()
return alpha.sum()
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])
model = (A, B, pi)
Observe = np.array([0, 1, 0])
States = np.array([1, 2, 3])
forward_prob(model,Observe,States)
运行结果:
观测序列概率的后向算法
import numpy as np
def backward_prob(model,Observe,States):
'''
马尔科夫后向算法
'''
A, B, pi = model
N = States.size
T = Observe.size
beta = np.ones((N,)) # beta_T
print "(1)计算初值beta_",T,"(i): ", beta
print "(2) 递推..."
for t in xrange(T - 2, -1, -1): # t=T-2,...,0
beta = A.dot(B[:, Observe[t + 1]] * beta)
print "t=", t + 1, " beta_", t + 1, "(i):", beta
print "(3)终止。alpha_", 1, "(i): ", beta
prob = pi.dot(beta * B[:, Observe[0]])
print "输出Prob: ", prob
return prob
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])
model = (A, B, pi)
Observe = np.array([0, 1, 0])
States = np.array([1, 2, 3])
backward_prob(model, Observe, States)
运行结果: