隐马尔可夫模型(二)
概率计算算法
这里介绍计算观测序列概率 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ)的前向与后向算法。
直接计算法
给定模型 λ = ( A , B , π ) \lambda = \left( {A,B,\pi } \right) λ=(A,B,π)和观测序列 O O O,计算观测序列 O O O出现的概率 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ),最直接的方法是按概率公式直接计算。
通过列举所有可能的长度为 T T T的状态序列 I = ( i 1 , i 2 , ⋯   , i T ) I=\left( {{i_1},{i_2}, \cdots ,{i_T}} \right) I=(i1,i2,⋯,iT),求各个状态序列 I I I与观测序列 O O O的联合概率 P ( O , I ∣ λ ) P\left( {O,I\left| \lambda \right.} \right) P(O,I∣λ),然后对所有可能的状态序列求和,得到 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ)。
状态序列
I
=
(
i
1
,
i
2
,
⋯
 
,
i
T
)
I=\left( {{i_1},{i_2}, \cdots ,{i_T}} \right)
I=(i1,i2,⋯,iT)的概率是:
P
(
I
∣
λ
)
=
π
i
1
a
i
1
i
2
a
i
2
i
3
⋯
a
i
T
−
1
i
T
P\left( {I\left| \lambda \right.} \right) = {\pi _{{i_1}}}{a_{{i_1}{i_2}}}{a_{{i_2}{i_3}}} \cdots {a_{{i_{T - 1}}{i_T}}}
P(I∣λ)=πi1ai1i2ai2i3⋯aiT−1iT
对固定的状态序列
I
=
(
i
1
,
i
2
,
⋯
 
,
i
T
)
I=\left( {{i_1},{i_2}, \cdots ,{i_T}} \right)
I=(i1,i2,⋯,iT),观测序列
O
O
O的概率是
P
(
O
∣
I
,
λ
)
P\left( {O\left| {I,\lambda } \right.} \right)
P(O∣I,λ):
P
(
O
∣
I
,
λ
)
=
b
i
1
(
o
1
)
b
i
2
(
o
2
)
⋯
b
i
T
(
o
T
)
P\left( {O\left| {I,\lambda } \right.} \right) = {b_{{i_1}}}\left( {{o_1}} \right){b_{{i_2}}}\left( {{o_2}} \right) \cdots {b_{{i_T}}}\left( {{o_T}} \right)
P(O∣I,λ)=bi1(o1)bi2(o2)⋯biT(oT)
O
O
O和
I
I
I同时出现的联合概率为:
P
(
O
,
I
∣
λ
)
=
P
(
O
∣
I
,
λ
)
P
(
I
∣
λ
)
=
π
i
1
b
i
1
(
o
1
)
a
i
1
i
2
b
i
2
(
o
2
)
⋯
a
i
T
−
1
i
T
b
i
T
(
o
T
)
P\left( {O,I\left| \lambda \right.} \right) = P\left( {O\left| {I,\lambda } \right.} \right)P\left( {I\left| \lambda \right.} \right) = {\pi _{{i_1}}}{b_{{i_1}}}\left( {{o_1}} \right){a_{{i_1}{i_2}}}{b_{i2}}\left( {{o_2}} \right) \cdots {a_{{i_{T - 1}}{i_T}}}{b_{{i_T}}}\left( {{o_T}} \right)
P(O,I∣λ)=P(O∣I,λ)P(I∣λ)=πi1bi1(o1)ai1i2bi2(o2)⋯aiT−1iTbiT(oT)
然后,对所有可能的状态序列
I
I
I求和,得到的观测序列
O
O
O的概率
P
(
O
∣
λ
)
P\left( {O\left| \lambda \right.} \right)
P(O∣λ),即
P
(
O
∣
λ
)
=
∑
I
P
(
O
∣
I
,
λ
)
P
(
I
∣
λ
)
=
∑
i
1
,
i
2
,
⋯
 
,
i
T
π
i
1
b
i
1
(
o
1
)
a
i
1
i
2
b
i
2
(
o
2
)
⋯
a
i
T
−
1
i
T
b
i
T
(
o
T
)
P\left( {O\left| \lambda \right.} \right) = \sum\limits_I {P\left( {O\left| {I,\lambda } \right.} \right)P\left( {I\left| \lambda \right.} \right)} = \sum\limits_{{i_1},{i_2}, \cdots ,{i_T}} {{\pi _{{i_1}}}{b_{{i_1}}}\left( {{o_1}} \right){a_{{i_1}{i_2}}}{b_{{i_2}}}\left( {{o_2}} \right) \cdots {a_{{i_{T - 1}}{i_T}}}{b_{{i_T}}}\left( {{o_T}} \right)}
P(O∣λ)=I∑P(O∣I,λ)P(I∣λ)=i1,i2,⋯,iT∑πi1bi1(o1)ai1i2bi2(o2)⋯aiT−1iTbiT(oT)
此算法 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ)的计算量很大,是 O ( T N T ) O\left(TN^T\right) O(TNT)阶的,不可行。
前向算法
前向概率
给定隐马尔可夫模型
λ
\lambda
λ,定义到时刻
t
t
t部分观测序列为
o
1
,
o
2
,
⋯
 
,
o
t
{o_1},{o_2}, \cdots ,{o_t}
o1,o2,⋯,ot,且状态为
q
i
q_i
qi的概率为前向概率,记作:
α
t
(
i
)
=
P
(
o
1
,
⋯
 
,
o
T
,
i
t
=
q
i
∣
λ
)
{\alpha _t}\left( i \right) = P\left( {{o_1}, \cdots ,{o_T},{i_t} = {q_i}\left| \lambda \right.} \right)
αt(i)=P(o1,⋯,oT,it=qi∣λ)
可以递推地求得向前概率
α
t
(
i
)
{\alpha _t}\left( i \right)
αt(i)及观测序列概率
P
(
O
∣
λ
)
P\left( {O\left| \lambda \right.} \right)
P(O∣λ)。
算法
- 输入:隐马尔科夫流程 λ \lambda λ,观测序列 O O O
- 输出:观测序列概率 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ)
- 流程:
- 初值
α t ( i ) = π i b i ( o 1 ) , i = 1 , 2 , ⋯   , N {\alpha _t}\left( i \right) = {\pi _i}{b_i}\left( {{o_1}} \right),i = 1,2, \cdots ,N αt(i)=πibi(o1),i=1,2,⋯,N - 递推
对 t = 1 , 2 , ⋯   , T − 1 t = 1,2, \cdots ,T - 1 t=1,2,⋯,T−1,
α t + 1 ( i ) = [ ∑ j = 1 N α t ( j ) a j i ] b i ( o t + 1 ) , i = 1 , 2 , ⋯   , N {\alpha _{t + 1}}\left( i \right) = \left[ {\sum\limits_{j = 1}^N {{\alpha _t}\left( j \right){a_{ji}}} } \right]{b_i}\left( {{o_{t + 1}}} \right),i = 1,2, \cdots ,N αt+1(i)=[j=1∑Nαt(j)aji]bi(ot+1),i=1,2,⋯,N - 终止
P ( O ∣ λ ) = ∑ i = 1 N α T ( i ) P\left( {O\left| \lambda \right.} \right) = \sum\limits_{i = 1}^N {{\alpha _T}\left( i \right)} P(O∣λ)=i=1∑NαT(i)
- 初值
后向算法
后向概率
给定隐马尔科夫模型
λ
\lambda
λ,定义在时刻
t
t
t状态为
q
i
q_i
qi的条件下,从
t
+
1
t+1
t+1到
T
T
T的部分观测序列为
o
t
+
1
,
o
t
+
2
,
⋯
 
,
o
T
{o_{t + 1}},{o_{t + 2}}, \cdots ,{o_T}
ot+1,ot+2,⋯,oT的概率为后向概率,记作:
β
t
(
i
)
=
P
(
o
t
+
1
,
o
t
+
2
,
⋯
 
,
o
T
∣
i
t
=
q
i
,
λ
)
{\beta _t}\left( i \right) = P\left( {{o_{t + 1}},{o_{t + 2}}, \cdots ,{o_T}\left| {{i_t} = {q_i},\lambda } \right.} \right)
βt(i)=P(ot+1,ot+2,⋯,oT∣it=qi,λ)
算法
- 输入:隐马尔可夫模型 λ \lambda λ,观测序列 O O O;
- 输出:观测序列概率 P ( O ∣ λ ) P\left( {O\left| \lambda \right.} \right) P(O∣λ)
- 流程:
- 初始化后向概率
β T ( i ) = 1 , i = 1 , 2 , ⋯ N {\beta _T}\left( i \right) = 1,i = 1,2, \cdots N βT(i)=1,i=1,2,⋯N - 对
t
=
T
−
1
,
⋯
 
,
1
t=T-1,\cdots,1
t=T−1,⋯,1
β t ( i ) = ∑ j = 1 N a i j b j ( o i + 1 ) β t + 1 ( j ) , i = 1 , 2 , ⋯   , N {\beta _t}\left( i \right) = \sum\limits_{j = 1}^N {{a_{ij}}{b_j}\left( {{o_{i + 1}}} \right){\beta _{t + 1}}\left( j \right)} ,i = 1,2, \cdots ,N βt(i)=j=1∑Naijbj(oi+1)βt+1(j),i=1,2,⋯,N - P ( O ∣ λ ) = ∑ i = 1 N π i b i ( o 1 ) β 1 ( i ) P\left( {O\left| \lambda \right.} \right) = \sum\limits_{i = 1}^N {{\pi _i}{b_i}\left( {{o_1}} \right){\beta _1}\left( i \right)} P(O∣λ)=i=1∑Nπibi(o1)β1(i)
- 初始化后向概率
一些概率和期望
给定模型
λ
\lambda
λ和观测
O
O
O,在时刻
t
t
t处于状态
q
i
q_i
qi的概率记为:
γ
t
(
i
)
=
α
t
(
i
)
β
t
(
i
)
∑
j
=
i
N
α
t
(
j
)
β
t
(
j
)
{\gamma _t}\left( i \right) = \frac{{{\alpha _t}\left( i \right){\beta _t}\left( i \right)}}{{\sum\limits_{j = i}^N {{\alpha _t}\left( j \right){\beta _t}\left( j \right)} }}
γt(i)=j=i∑Nαt(j)βt(j)αt(i)βt(i)
在时刻
t
t
t处于状态
q
i
q_i
qi且在时刻
t
+
1
t+1
t+1处于状态
q
j
q_j
qj的概率记为:
ξ
t
(
i
,
j
)
=
α
t
(
i
)
a
i
j
b
j
(
o
t
+
1
)
β
t
+
1
(
j
)
∑
i
=
1
N
∑
j
=
1
N
α
t
(
i
)
a
i
j
b
j
(
o
t
+
1
)
β
t
+
1
(
j
)
{\xi _t}\left( {i,j} \right) = \frac{{{\alpha _t}\left( i \right){a_{ij}}{b_j}\left( {{o_{t + 1}}} \right){\beta _{t + 1}}\left( j \right)}}{{\sum\limits_{i = 1}^N {\sum\limits_{j = 1}^N {{\alpha _t}\left( i \right){a_{ij}}{b_j}\left( {{o_{t + 1}}} \right){\beta _{t + 1}}\left( j \right)} } }}
ξt(i,j)=i=1∑Nj=1∑Nαt(i)aijbj(ot+1)βt+1(j)αt(i)aijbj(ot+1)βt+1(j)
- 在观测状态
O
O
O下状态
i
i
i出现的期望值:
∑ t = 1 T γ t ( i ) \sum\limits_{t = 1}^T {{\gamma _t}\left( i \right)} t=1∑Tγt(i) - 在观测状态
O
O
O下由状态
i
i
i转移的期望值:
∑ t = 1 T − 1 γ t ( i ) \sum\limits_{t = 1}^{T - 1} {{\gamma _t}\left( i \right)} t=1∑T−1γt(i) - 在观测状态
O
O
O下由状态
i
i
i转移到状态
j
j
j的期望值:
∑ t = 1 T − 1 ξ t ( i , j ) \sum\limits_{t = 1}^{T - 1} {{\xi _t}\left( {i,j} \right)} t=1∑T−1ξt(i,j)
参考文献
《统计学习方法》