隐马尔可夫模型基本问题——概率计算问题详细讲解

概率计算问题又称评价问题(Evaluation Problem)

已知条件:给定模型 λ = [ A , B , π ] \lambda=[A,B,\pi] λ=[A,B,π]和观测序列 O = ( o 1 , o 2 , o 3 , . . . , o n ) O=(o_{1},o_{2},o_{3},...,o_{n}) O=(o1,o2,o3,...,on)
求解目标:计算在模型 λ \lambda λ和观测序列 O O O出现情况下的 P ( O ∣ λ ) P(O|\lambda) P(Oλ),也可以认为是求解观测序列和评估模型之间的匹配程度。
求解方式:直接计算法、前向算法、后向算法

例子:有三个盒子,编号1、2、3,每个盒子都有红、白两种颜色的球,有放回的从盒子里拿1个球,总共进行了四次,出现“红、白、红、白”的概率是多少?(初始状态概率矩阵 π = ( 1 , 0 , 0 ) \pi=(1,0,0) π=(1,0,0)

表1. 盒子之间的转移概率
盒子 盒1 盒 2 盒3
盒1 0.4 0.6 0
盒2 0 0.8 0.2
盒 3 0 0 1
表2. 每个盒子中红、白球的个数
盒子 盒1 盒 2 盒3
红球 7 4 8
白球 3 6 2

解析:由表1和表2,可知状态转移矩阵A和观测概率B,此时“模型 λ = [ A , B , π ] \lambda=[A,B,\pi] λ=[A,B,π]和观测序列 O = ( 红 、 白 、 红 、 白 ) O=(红、白、红、白) O=()”为已知条件,求解目标是:“ O = ( 红 、 白 、 红 、 白 ) O=(红、白、红、白) O=()发生的概率,即 P ( O ∣ λ ) = ? P(O|\lambda)=? P(Oλ)=

直接计算法(不提倡)

通过列举所有可能长度为T的状态序列 S = ( s 1 , s 2 , s 3 , . . . , s T ) S=(s_{1},s_{2},s_{3},...,s_{T}) S=(s1,s2,s3,...,sT),求各个状态序列 S S S和观测序列 O O O的联合概率 P ( O , S ∣ λ ) P(O,S|\lambda) P(O,Sλ),然后对所有可能的状态序列求和,得到 P ( O ∣ λ ) P(O|\lambda) P(Oλ),求解方式如下( a i j 是 状 态 转 移 概 率 , b i j 是 观 测 概 率 a_{ij}是状态转移概率,b_{ij}是观测概率 aijbij):

P ( O ∣ S , λ ) = b 11 b 22 b 33 . . . b T M P(O|S,\lambda)=b_{11}b_{22}b_{33}...b_{TM} P(OSλ)=b11b22b33...bTM

P ( S ∣ λ ) = π 1 a 12 a 23 . . . a ( T − 1 ) T P(S|\lambda)=\pi_{1}a_{12}a_{23}...a_{(T-1)T} P(Sλ)=π1a12a23...a(T1)T

P ( O , S ∣ λ ) = P ( O ∣ S , λ ) P ( S ∣ λ ) = π 1 b 11 a 12 b 22 a 23 b 33 . . . a ( T − 1 ) T b T M P(O,S|\lambda)=P(O|S,\lambda)P(S|\lambda) =\pi_{1}b_{11}a_{12}b_{22}a_{23}b_{33}...a_{(T-1)T}b_{TM} P(O,Sλ)=P(OSλ)P(Sλ)=π1b11a12b22a23b33...a(T1)TbTM

P ( O ∣ λ ) = ∑ S ∈ S T P ( O , S ∣ λ ) = ∑ S 1 , S 2 , S 3 , . . . S T π 1 b 11 a 12 b 22 a 23 b 33 . . . a ( T − 1 ) T b T M P(O|\lambda)=\sum_{S∈S_{T}}P(O,S|\lambda) =\sum_{S_{1},S_{2},S_{3},...S_{T}}\pi_{1}b_{11}a_{12}b_{22}a_{23}b_{33}...a_{(T-1)T}b_{TM} P(Oλ)=SSTP(O,Sλ)=S1,S2,S3,...STπ1b11a12b22a23b33...a(T1)TbTM

理论上可以计算,但计算量很大,时间复杂度是 O ( T N T ) O(TN^T) O(TNT) N N N是状态的个数, T T T是观测列表的长度),因此不可行。

解析:还是上面例子,如果 O = ( 红 、 白 、 红 、 白 ) O=(红、白、红、白) O=(),那么只要枚举 3 4 3^4 34个就行,分别求出对应的概率,再相加就行了,但要是 O = ( 红 、 白 、 红 、 . . . 、 白 ) O=(红、白、红、...、白) O=(...)有1000个,需要枚举 3 1000 3^{1000} 31000,这数目就不少了,因此该方法只要知道是什么就可以了,可行度不高。

前向算法(重要)

前向概率:在 λ \lambda λ给定的情况下,到时刻 t t t时,出现的观测序列为 o 1 , o 2 , o 3 , . . . , o t o_{1},o_{2},o_{3},...,o_{t} o1,o2,o3,...,ot且状态为 s i s_{i} si时的概率,记为:

α t ( i ) = P ( o 1 , o 2 , o 3 , . . . , o t , s i ∣ λ ) \alpha_{t}(i)=P(o_{1},o_{2},o_{3},...,o_{t},s_{i}|\lambda) αt(i)=P(o1,o2,o3,...,ot,siλ)

求解步骤

  1. 起始值: α 1 ( i ) = π i b i ( o 1 ) ,      i = 1 , 2 , 3 , . . . , N \alpha_{1}(i)=\pi_{i}b_{i}(o_{1}),\:\:\:\:i=1,2,3,...,N α1(i)=πibi(o1),i=1,2,3,...,N

  2. 递推:当 t = 1 , 2 , 3 , . . . , T − 1 t=1,2,3,...,T-1 t=1,2,3,...,T1时, α t + 1 ( i ) = [ ∑ j = 1 N α t ( j ) α j i ] b i ( o t + 1 ) ,      i = 1 , 2 , 3 , . . . , N \alpha_{t+1}^{(i)}=[\sum_{j=1}^{N}\alpha_{t}(j)\alpha_{ji}]b_{i}(o_{t+1}), \:\:\:\:i=1,2,3,...,N αt+1(i)=[j=1Nαt(j)αji]bi(ot+1),i=1,2,3,...,N

  3. 执行完步骤1和2,可得 P ( O ∣ λ ) = ∑ i = 1 N α T ( i ) ,      i = 1 , 2 , 3 , . . . , N P(O|\lambda)=\sum_{i=1}^{N}\alpha_{T}(i),\:\:\:\:i=1,2,3,...,N P(Oλ)=i=1NαT(i),i=1,2,3,...,N

此时,求解 P ( O ∣ λ ) P(O|\lambda) P(Oλ)的时间复杂度是 O ( N 2 T ) O(N^2T) O(N2T)

隐马尔可夫模型基本问题——概率计算问题详细讲解_第1张图片

解析:对上面的例子进行求解,现在观测序列O=(红、白、红、白),初始状态概率矩阵 π = ( 1 , 0 , 0 ) \pi=(1,0,0) π=(1,0,0),状态转移矩阵A和观测概率B如下:

A = [ 0.4 0.6 0 0 0.8 0.2 0 0 1 ] A=\begin{bmatrix} 0.4& 0.6 &0 \\ 0&0.8 &0.2 \\ 0 & 0& 1 \end{bmatrix} A=0.4000.60.8000.21        \:\:\:\:\:\: B = [ 0.7 0.3 0.4 0.6 0.8 0.2 ] B=\begin{bmatrix} 0.7& 0.3 \\ 0.4&0.6 \\ 0.8 & 0.2 \end{bmatrix} B=0.70.40.80.30.60.2

1、当 t = 1 t=1 t=1时:
α 1 ( 1 ) = π ∗ b 1 ( o 1 ) = 1 ∗ 0.7 = 0.7 \alpha_{1}(1)=\pi*b_{1}(o_{1})=1*0.7=0.7 α1(1)=πb1(o1)=10.7=0.7
2、当 t = 2 t=2 t=2时:
α 2 ( 1 ) = α 1 ( 1 ) ∗ a 11 ∗ b 1 ( o 2 ) = 0.7 ∗ 0.4 ∗ 0.3 = 0.084 \alpha_{2}(1)=\alpha_{1}(1)*a_{11}*b_{1}(o_{2})=0.7*0.4*0.3=0.084 α2(1)=α1(1)a11b1(o2)=0.70.40.3=0.084
α 2 ( 2 ) = α 1 ( 1 ) ∗ a 12 ∗ b 2 ( o 2 ) = 0.7 ∗ 0.6 ∗ 0.6 = 0.252 \alpha_{2}(2)=\alpha_{1}(1)*a_{12}*b_{2}(o_{2})=0.7*0.6*0.6=0.252 α2(2)=α1(1)a12b2(o2)=0.70.60.6=0.252
3、当 t = 3 t=3 t=3时:
α 3 ( 1 ) = α 2 ( 1 ) ∗ a 11 ∗ b 1 ( o 3 ) = 0.084 ∗ 0.4 ∗ 0.7 = 0.02352 \alpha_{3}(1)=\alpha_{2}(1)*a_{11}*b_{1}(o_{3})=0.084*0.4*0.7=0.02352 α3(1)=α2(1)a11b1(o3)=0.0840.40.7=0.02352
α 3 ( 2 ) = [ α 2 ( 1 ) ∗ a 12 + α 2 ( 2 ) ∗ a 22 ] ∗ b 2 ( o 3 ) = ( 0.084 ∗ 0.6 + 0.252 ∗ 0.8 ) ∗ 0.4 = 0.1008 \alpha_{3}(2)=[\alpha_{2}(1)*a_{12}+\alpha_{2}(2)*a_{22}]*b_{2}(o_{3})=(0.084*0.6+0.252*0.8)*0.4=0.1008 α3(2)=[α2(1)a12+α2(2)a22]b2(o3)=(0.0840.6+0.2520.8)0.4=0.1008
α 3 ( 3 ) = α 2 ( 2 ) ∗ a 23 ∗ b 3 ( o 3 ) = 0.252 ∗ 0.2 ∗ 0.8 = 0.04032 \alpha_{3}(3)=\alpha_{2}(2)*a_{23}*b_{3}(o_{3})=0.252*0.2*0.8=0.04032 α3(3)=α2(2)a23b3(o3)=0.2520.20.8=0.04032
4、当 t = 4 t=4 t=4时:
α 4 ( 1 ) = α 3 ( 1 ) ∗ a 11 ∗ b 1 ( o 4 ) = 0.02352 ∗ 0.4 ∗ 0.3 = 0.0028224 \alpha_{4}(1)=\alpha_{3}(1)*a_{11}*b_{1}(o_{4})=0.02352*0.4*0.3=0.0028224 α4(1)=α3(1)a11b1(o4)=0.023520.40.3=0.0028224
α 4 ( 2 ) = [ α 3 ( 1 ) ∗ a 12 + α 3 ( 2 ) ∗ a 22 ] ∗ b 2 ( o 4 ) = ( 0.02352 ∗ 0.6 + 0.1008 ∗ 0.8 ) ∗ 0.6 = 0.0568512 \alpha_{4}(2)=[\alpha_{3}(1)*a_{12}+\alpha_{3}(2)*a_{22}]*b_{2}(o_{4})=(0.02352*0.6+0.1008*0.8)*0.6=0.0568512 α4(2)=[α3(1)a12+α3(2)a22]b2(o4)=(0.023520.6+0.10080.8)0.6=0.0568512
α 4 ( 3 ) = [ α 3 ( 2 ) ∗ a 23 + α 3 ( 3 ) ∗ a 33 ] ∗ b 3 ( o 4 ) = ( 0.1008 ∗ 0.2 + 0.04032 ∗ 1 ) ∗ 0.2 = 0.012096 \alpha_{4}(3)=[\alpha_{3}(2)*a_{23}+\alpha_{3}(3)*a_{33}]*b_{3}(o_{4})=(0.1008*0.2+0.04032*1)*0.2=0.012096 α4(3)=[α3(2)a23+α3(3)a33]b3(o4)=(0.10080.2+0.040321)0.2=0.012096
此时,可求 P ( O ∣ λ ) = α 4 ( 1 ) + α 4 ( 2 ) + α 4 ( 3 ) = 0.0028224 + 0.0568512 + 0.012096 = 0.0717696 P(O|\lambda)=\alpha_{4}(1)+\alpha_{4}(2)+\alpha_{4}(3)=0.0028224+0.0568512+0.012096=0.0717696 P(Oλ)=α4(1)+α4(2)+α4(3)=0.0028224+0.0568512+0.012096=0.0717696

计算流程图如图2所示,其中初始状态概率、状态转移概率和观测概率为0的线没有画

隐马尔可夫模型基本问题——概率计算问题详细讲解_第2张图片

后向算法(和前向算法差不多)

后向概率:在 λ \lambda λ给定的情况下,并且时刻 t t t的状态为 s i s_{i} si的条件下,观测序列 o t + 1 , o t + 2 , o t + 3 , . . . , o T o_{t+1},o_{t+2},o_{t+3},...,o_{T} ot+1,ot+2,ot+3,...,oT的概率,记为:

β t ( i ) = P ( o t + 1 , o t + 2 , o t + 3 , . . . , o T ∣ λ , s i ) \beta_{t}(i)=P(o_{t+1},o_{t+2},o_{t+3},...,o_{T}|\lambda,s_{i}) βt(i)=P(ot+1,ot+2,ot+3,...,oTλ,si)

求解步骤

  1. 起始值: β 1 ( i ) = 1 ,      i = 1 , 2 , 3 , . . . , N \beta_{1}(i)=1,\:\:\:\:i=1,2,3,...,N β1(i)=1,i=1,2,3,...,N T T T时刻的状态,后向概率都为1)

  2. 递推:当 t = T − 1 , T − 2 , T − 3 , . . . , 1 t=T-1,T-2,T-3,...,1 t=T1,T2,T3,...,1时, β t ( i ) = ∑ j = 1 N α i j b j ( o t + 1 ) β t + 1 ( j ) ,      i = 1 , 2 , 3 , . . . , N \beta_{t}^{(i)}=\sum_{j=1}^{N}\alpha_{ij}b_{j}(o_{t+1})\beta_{t+1}(j), \:\:\:\:i=1,2,3,...,N βt(i)=j=1Nαijbj(ot+1)βt+1(j),i=1,2,3,...,N

  3. 执行完步骤1和2,可得 P ( O ∣ λ ) = ∑ i = 1 N π i b i ( o 1 ) β 1 ( i ) ,      i = 1 , 2 , 3 , . . . , N P(O|\lambda)=\sum_{i=1}^{N}\pi_{i}b_{i}(o_{1})\beta_{1}(i),\:\:\:\:i=1,2,3,...,N P(Oλ)=i=1Nπibi(o1)β1(i),i=1,2,3,...,N

隐马尔可夫模型基本问题——概率计算问题详细讲解_第3张图片

解析:使用后向算法对上面的例子进行求解,观测序列为 O = ( 红 、 白 、 红 、 白 ) O=(红、白、红、白) O=(),初始状态概率矩阵 π = ( 1 , 0 , 0 ) \pi=(1,0,0) π=(1,0,0),状态转移矩阵A和观测概率B如下:

A = [ 0.4 0.6 0 0 0.8 0.2 0 0 1 ] A=\begin{bmatrix} 0.4& 0.6 &0 \\ 0&0.8 &0.2 \\ 0 & 0& 1 \end{bmatrix} A=0.4000.60.8000.21        \:\:\:\:\:\: B = [ 0.7 0.3 0.4 0.6 0.8 0.2 ] B=\begin{bmatrix} 0.7& 0.3 \\ 0.4&0.6 \\ 0.8 & 0.2 \end{bmatrix} B=0.70.40.80.30.60.2

1、当 t = 4 t=4 t=4时:

β 4 ( i ) = 1 ,    i = 1 , 2 , 3 \beta_{4}(i)=1,\:\:i=1,2,3 β4(i)=1,i=1,2,3

2、当 t = 3 t=3 t=3时:

β 3 ( 1 ) = ∑ j = 1 N α 1 j b j ( o 4 ) β 4 ( j ) = 0.48 \beta_{3}(1)=\sum_{j=1}^{N}\alpha_{1j}b_{j}(o_{4})\beta_4(j)=0.48 β3(1)=j=1Nα1jbj(o4)β4(j)=0.48

β 3 ( 2 ) = ∑ j = 1 N α 2 j b j ( o 4 ) β 4 ( j ) = 0.52 \beta_{3}(2)=\sum_{j=1}^{N}\alpha_{2j}b_{j}(o_{4})\beta_4(j)=0.52 β3(2)=j=1Nα2jbj(o4)β4(j)=0.52

β 3 ( 3 ) = ∑ j = 1 N α 3 j b j ( o 4 ) β 4 ( j ) = 0.2 \beta_{3}(3)=\sum_{j=1}^{N}\alpha_{3j}b_{j}(o_{4})\beta_4(j)=0.2 β3(3)=j=1Nα3jbj(o4)β4(j)=0.2

3、当 t = 2 t=2 t=2时:

β 2 ( 1 ) = ∑ j = 1 N α 1 j b j ( o 3 ) β 3 ( j ) = 0.2592 \beta_{2}(1)=\sum_{j=1}^{N}\alpha_{1j}b_{j}(o_{3})\beta_3(j)=0.2592 β2(1)=j=1Nα1jbj(o3)β3(j)=0.2592

β 2 ( 2 ) = ∑ j = 1 N α 2 j b j ( o 3 ) β 3 ( j ) = 0.1984 \beta_{2}(2)=\sum_{j=1}^{N}\alpha_{2j}b_{j}(o_{3})\beta_3(j)=0.1984 β2(2)=j=1Nα2jbj(o3)β3(j)=0.1984

β 2 ( 3 ) = ∑ j = 1 N α 3 j b j ( o 3 ) β 3 ( j ) = 0.16 \beta_{2}(3)=\sum_{j=1}^{N}\alpha_{3j}b_{j}(o_{3})\beta_3(j)=0.16 β2(3)=j=1Nα3jbj(o3)β3(j)=0.16

4、当 t = 1 t=1 t=1时:

β 1 ( 1 ) = ∑ j = 1 N α 1 j b j ( o 2 ) β 2 ( j ) = 0.102528 \beta_{1}(1)=\sum_{j=1}^{N}\alpha_{1j}b_{j}(o_{2})\beta_2(j)=0.102528 β1(1)=j=1Nα1jbj(o2)β2(j)=0.102528

β 1 ( 2 ) = ∑ j = 1 N α 2 j b j ( o 2 ) β 2 ( j ) = 0.132352 \beta_{1}(2)=\sum_{j=1}^{N}\alpha_{2j}b_{j}(o_{2})\beta_2(j)=0.132352 β1(2)=j=1Nα2jbj(o2)β2(j)=0.132352

β 1 ( 3 ) = ∑ j = 1 N α 3 j b j ( o 2 ) β 2 ( j ) = 0.032 \beta_{1}(3)=\sum_{j=1}^{N}\alpha_{3j}b_{j}(o_{2})\beta_2(j)=0.032 β1(3)=j=1Nα3jbj(o2)β2(j)=0.032

此时,可求 P ( O ∣ λ ) = ∑ i = 1 N π i b i ( o 1 ) β 1 ( i ) = 0.0717696 P(O|\lambda)=\sum_{i=1}^{N}\pi_{i}b_{i}(o_{1})\beta_1(i)=0.0717696 P(Oλ)=i=1Nπibi(o1)β1(i)=0.0717696

结论:后向算法和前向算法计算结果完全一样,因此计算 P ( O ∣ λ ) P(O|\lambda) P(Oλ)的时候,可以任意选择一个算法。

你可能感兴趣的:(机器学习)