用簡單易懂的例子解釋隱馬爾可夫模型

Jeff-Chow000發表於2020-10-24

用簡單易懂的例子解釋隱馬爾可夫模型

隱含的身體狀態 = { 健康 , 發燒 }

可觀察的感覺狀態 = { 正常 , 冷 , 頭暈 }

預判的身體狀態初始概率分佈 = { 健康:0.6 , 發燒:0.4 }

身體健康狀態的轉換概率分佈 = {
健康->健康:0.7 ,
健康->發燒:0.3 ,
發燒->健康:0.4 ,
發燒->發燒:0.6
}

在相應健康狀況條件下,感覺狀態的概率分佈 = {
健康,正常:0.5 ,冷 :0.4 ,頭暈: 0.1 ;
發燒,正常:0.1 ,冷 :0.3 ,頭暈: 0.6
}
連續三天的身體感覺依次是: 正常、冷、頭暈 。

求:這三天的身體健康狀態變化的過程是怎麼樣的?

(1) 初始情況:

P(第一天健康) = 0.6,P(第一天發燒)=0.4。

(2) 求第一天的身體情況:

計算在感覺正常的情況下最可能的身體狀態:

P(第一天健康,感覺正常) = P(正常|第一天健康) * P(第一天健康) = 0.5 * 0.6 = 0.3

P(第一天發燒,感覺正常) = P(正常|第一天發燒) * P(第一天發燒) = 0.1 * 0.4 = 0.04

那麼就可以認為第一天最可能的身體狀態是:健康。

(3) 求第二天的身體狀況:

計算在感覺冷的情況下最可能的身體狀態。

P(第二天健康,感覺冷) = P(感覺冷|第二天健康) * P(第二天健康|第一天健康) * P(第一天健康) + P(感覺冷|第二天健康) * P(第二天健康|第一天發燒) * P(第一天發燒) = 0.4 * 0.7 * 0.6 + 0.4 * 0.4 * 0.4 = 0.232

P(第二天發燒,感覺冷) = P(感覺冷|第二天發燒) * P(第二天發燒|第一天健康) * P(第一天健康) + P(感覺冷|第二天發燒) * P(第二天發燒|第一天發燒) * P(第一天發燒) = 0.3 * 0.3 * 0.6 + 0.3 * 0.6 * 0.4 = 0.126

那麼可以認為,第二天最可能的狀態是:健康。

(4) 求第三天的身體狀態:

計算在感覺頭暈的情況下最可能的身體狀態。

P(第三天健康,感覺頭暈) = P(感覺頭暈|第三天健康) * P(第三天健康|第二天健康) * P(第二天健康|第一天健康) * P(第一天健康) + P(感覺頭暈|第三天健康) * P(第三天健康|第二天健康) * P(第二天健康|第一天發燒) * P(第一天發燒) + P(感覺頭暈|第三天健康) * P(第三天健康|第二天發燒) * P(第二天發燒|第一天健康) * P(第一天健康) + P(感覺頭暈|第三天健康) * P(第三天健康|第二天發燒) * P(第二天發燒|第一天發燒) * P(第一天發燒) = 0.1 * 0.7 * 0.7 * 0.6 + 0.1 * 0.7 * 0.4 * 0.4 + 0.1 * 0.4 * 0.3 * 0.6 + 0.1 * 0.4 * 0.6 * 0.4 = 0.0574

P(第三天發燒,感覺頭暈) = P(感覺頭暈|第三天發燒) * P(第三天發燒|第二天健康) * P(第二天健康|第一天健康) * P(第一天健康) + P(感覺頭暈|第三天發燒) * P(第三天發燒|第二天健康) * P(第二天健康|第一天發燒) * P(第一天發燒) + P(感覺頭暈|第三天發燒) * P(第三天發燒|第二天發燒) * P(第二天發燒|第一天健康) * P(第一天健康) + P(感覺頭暈|第三天發燒) * P(第三天發燒|第二天發燒) * P(第二天發燒|第一天發燒) * P(第一天發燒) = 0.6 * 0.3 * 0.7 * 0.6 + 0.6 * 0.3 * 0.4 * 0.4 + 0.6 * 0.6 * 0.3 * 0.6 + 0.6 * 0.6 * 0.6 * 0.4 = 0.2556

那麼可以認為,第三天最可能的狀態是:發燒。

用數學公式表達

初始概率分佈:

π ( 0 ) = [ 0.6 0.4 ] T \pi(0) = \left[ \begin{array}{cc} 0.6 & 0.4 \end{array} \right]^T π(0)=[0.60.4]T

狀態轉移概率矩陣:

A = [ 0.7 0.3 0.4 0.6 ] A = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] A=[0.70.40.30.6]

觀測概率矩陣:

B = [ 0.5 0.4 0.1 0.1 0.3 0.6 ] B = \left[ \begin{array}{ccc} 0.5 & 0.4 & 0.1 \\ 0.1 & 0.3 & 0.6 \end{array} \right] B=[0.50.10.40.30.10.6]

實際運算:

π ( i + 1 ) = A π ( i ) B ( i + 1 ) = A B ( i ) \pi(i+1) = A \pi(i) \\ B(i+1) = A B(i) π(i+1)=Aπ(i)B(i+1)=AB(i)

例:

π ( 1 ) = A π ( 0 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.6 0.4 ] = [ 0.54 0.48 ] π ( 2 ) = A π ( 1 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.54 0.48 ] = [ 0.522 0.504 ] π ( 3 ) = A π ( 2 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.522 0.504 ] = [ 0.5166 0.5112 ] \pi(1) = A \pi(0) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{c} 0.6 \\ 0.4 \end{array} \right] =\left[ \begin{array}{cc} 0.54 \\ 0.48 \end{array} \right] \\ \pi(2) = A \pi(1) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{c} 0.54 \\ 0.48 \end{array} \right] =\left[ \begin{array}{cc} 0.522 \\ 0.504 \end{array} \right] \\ \pi(3) = A \pi(2) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{c} 0.522 \\ 0.504 \end{array} \right] =\left[ \begin{array}{cc} 0.5166 \\ 0.5112 \end{array} \right] π(1)=Aπ(0)=[0.70.40.30.6][0.60.4]=[0.540.48]π(2)=Aπ(1)=[0.70.40.30.6][0.540.48]=[0.5220.504]π(3)=Aπ(2)=[0.70.40.30.6][0.5220.504]=[0.51660.5112]


B ( 1 ) = A B ( 0 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.5 0.4 0.1 0.1 0.3 0.6 ] = [ 0.38 0.37 0.25 0.26 0.34 0.4 ] B ( 2 ) = A B ( 1 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.38 0.37 0.25 0.26 0.34 0.4 ] = [ 0.344 0.361 0.295 0.308 0.352 0.34 ] B ( 3 ) = A B ( 2 ) = [ 0.7 0.3 0.4 0.6 ] [ 0.344 0.361 0.295 0.308 0.352 0.34 ] = [ 0.3332 0.3583 0.3085 0.3224 0.3556 0.322 ] B(1) = A B(0) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{ccc} 0.5 & 0.4 & 0.1 \\ 0.1 & 0.3 & 0.6 \end{array} \right]= \left[ \begin{array}{cc} 0.38 & 0.37 & 0.25 \\ 0.26 & 0.34 & 0.4 \end{array} \right] \\ B(2) = A B(1) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{cc} 0.38 & 0.37 & 0.25 \\ 0.26 & 0.34 & 0.4 \end{array} \right] =\left[ \begin{array}{cc} 0.344 & 0.361 & 0.295 \\ 0.308 & 0.352 & 0.34 \end{array} \right] \\ B(3) = A B(2) = \left[ \begin{array}{cc} 0.7 & 0.3 \\ 0.4 & 0.6 \end{array} \right] \left[ \begin{array}{cc} 0.344 & 0.361 & 0.295 \\ 0.308 & 0.352 & 0.34 \end{array} \right] =\left[ \begin{array}{cc} 0.3332 & 0.3583 & 0.3085 \\ 0.3224 & 0.3556 & 0.322 \end{array} \right] B(1)=AB(0)=[0.70.40.30.6][0.50.10.40.30.10.6]=[0.380.260.370.340.250.4]B(2)=AB(1)=[0.70.40.30.6][0.380.260.370.340.250.4]=[0.3440.3080.3610.3520.2950.34]B(3)=AB(2)=[0.70.40.30.6][0.3440.3080.3610.3520.2950.34]=[0.33320.32240.35830.35560.30850.322]

狀態集合 Q = { 1 , 2 , 3 } Q=\{1,2,3\} Q={1,2,3},觀測集合 V = { 紅 , 白 } V=\{紅,白\} V={,}

初始概率分佈:
π ( 0 ) = [ 0.2 0.4 0.4 ] T \pi(0) = \left[ \begin{array}{cc} 0.2 & 0.4 & 0.4 \end{array} \right]^T π(0)=[0.20.40.4]T
狀態轉移概率矩陣:
A = [ 0.5 0.2 0.3 0.3 0.5 0.2 0.2 0.3 0.5 ] A = \left[ \begin{array}{cc} 0.5 & 0.2 & 0.3 \\ 0.3 & 0.5 & 0.2 \\ 0.2 & 0.3 & 0.5 \end{array} \right] A=0.50.30.20.20.50.30.30.20.5
觀測概率矩陣:
B = [ 0.5 0.5 0.4 0.6 0.7 0.3 ] B = \left[ \begin{array}{ccc} 0.5 & 0.5 \\ 0.4 & 0.6 \\ 0.7 & 0.3 \end{array} \right] B=0.50.40.70.50.60.3
T = 3 T=3 T=3 O = ( 紅 , 白 , 紅 ) O=(紅,白,紅) O=(,,),試用前向演算法計算 P ( O ∣ λ ) P(O|\lambda) P(Oλ)

前向演算法計算:

第一個觀測值是紅,計算在初始概率分佈下,三個狀態觀測到紅的概率分別是:
α 1 ( 1 ) = π 1 b 1 ( o 1 ) = 0.2 ∗ 0.5 = 0.1 α 1 ( 2 ) = π 2 b 2 ( o 1 ) = 0.4 ∗ 0.4 = 0.16 α 1 ( 3 ) = π 3 b 3 ( o 1 ) = 0.4 ∗ 0.7 = 0.28 \alpha_1(1) = \pi_1 b_1(o_1) = 0.2 * 0.5 = 0.1 \\ \alpha_1(2) = \pi_2 b_2(o_1) = 0.4 * 0.4 = 0.16 \\ \alpha_1(3) = \pi_3 b_3(o_1) = 0.4 * 0.7 = 0.28 α1(1)=π1b1(o1)=0.20.5=0.1α1(2)=π2b2(o1)=0.40.4=0.16α1(3)=π3b3(o1)=0.40.7=0.28

P ( 第 一 個 觀 測 值 是 紅 , 初 始 狀 態 1 ) = P ( 第 一 個 觀 測 值 是 紅 ∣ 初 始 狀 態 1 ) P ( 初 始 狀 態 1 ) = P ( 觀 測 值 是 紅 ∣ 狀 態 1 ) P ( 初 始 狀 態 1 ) P ( 第 一 個 觀 測 值 是 紅 , 初 始 狀 態 2 ) = P ( 第 一 個 觀 測 值 是 紅 ∣ 初 始 狀 態 2 ) P ( 初 始 狀 態 2 ) = P ( 觀 測 值 是 紅 ∣ 狀 態 2 ) P ( 初 始 狀 態 2 ) P ( 第 一 個 觀 測 值 是 紅 , 初 始 狀 態 3 ) = P ( 第 一 個 觀 測 值 是 紅 ∣ 初 始 狀 態 3 ) P ( 初 始 狀 態 3 ) = P ( 觀 測 值 是 紅 ∣ 狀 態 3 ) P ( 初 始 狀 態 3 ) P(第一個觀測值是紅,初始狀態1) = P(第一個觀測值是紅|初始狀態1) P(初始狀態1) = P(觀測值是紅|狀態1) P(初始狀態1) \\ P(第一個觀測值是紅,初始狀態2) = P(第一個觀測值是紅|初始狀態2) P(初始狀態2) = P(觀測值是紅|狀態2) P(初始狀態2) \\ P(第一個觀測值是紅,初始狀態3) = P(第一個觀測值是紅|初始狀態3) P(初始狀態3) = P(觀測值是紅|狀態3) P(初始狀態3) P(1)=P(1)P(1)=P(1)P(1)P(2)=P(2)P(2)=P(2)P(2)P(3)=P(3)P(3)=P(3)P(3)

P ( 第 一 個 觀 測 值 是 紅 ) = ∑ i = 1 3 P ( 第 一 個 觀 測 值 是 紅 , 初 始 狀 態 i ) = 0.54 P(第一個觀測值是紅) = \sum_{i=1}^3 P(第一個觀測值是紅,初始狀態i) = 0.54 P()=i=13P(i)=0.54
在第一個觀測值為紅的前提下,轉為狀態1的概率:
β 1 ( 1 ) = ∑ i = 1 3 α 1 ( i ) a i 1 = 0.1 ∗ 0.5 + 0.16 ∗ 0.3 + 0.28 ∗ 0.2 = 0.154 \beta_1(1) = \sum_{i=1}^3 \alpha_1(i) a_{i1} = 0.1 * 0.5 + 0.16 * 0.3 + 0.28 * 0.2 = 0.154 β1(1)=i=13α1(i)ai1=0.10.5+0.160.3+0.280.2=0.154

P ( 第 一 個 觀 測 值 是 紅 , 第 一 個 狀 態 是 1 ) = ∑ i = 1 3 P ( 第 一 個 觀 測 值 是 紅 ∣ 第 一 個 狀 態 是 1 ) P ( 第 一 個 狀 態 是 1 ∣ 初 始 狀 態 i ) P ( 初 始 狀 態 i ) = ∑ i = 1 3 P ( 第 一 個 觀 測 值 是 紅 ∣ 第 一 個 狀 態 是 1 ) P ( 初 始 狀 態 i ) P ( 第 一 個 狀 態 是 1 ∣ 初 始 狀 態 i ) = ∑ i = 1 3 P ( 觀 測 值 是 紅 ∣ 狀 態 1 ) P ( 初 始 狀 態 i ) P ( 下 一 個 狀 態 是 1 ∣ 狀 態 i ) = ∑ i = 1 3 P ( 觀 測 值 是 紅 , 初 始 狀 態 i ) P ( 下 一 個 狀 態 是 1 ∣ 狀 態 i ) \begin{aligned} P(第一個觀測值是紅,第一個狀態是1) &= \sum_{i=1}^3 P(第一個觀測值是紅|第一個狀態是1) P(第一個狀態是1|初始狀態i) P(初始狀態i) \\ &= \sum_{i=1}^3 P(第一個觀測值是紅|第一個狀態是1) P(初始狀態i) P(第一個狀態是1|初始狀態i) \\ &= \sum_{i=1}^3 P(觀測值是紅|狀態1) P(初始狀態i) P(下一個狀態是1|狀態i) \\ &= \sum_{i=1}^3 P(觀測值是紅, 初始狀態i) P(下一個狀態是1|狀態i) \end{aligned} P(1)=i=13P(1)P(1i)P(i)=i=13P(1)P(i)P(1i)=i=13P(1)P(i)P(1i)=i=13P(,i)P(1i)

P ( 第 一 個 觀 測 值 是 紅 , 初 始 狀 態 i ) P ( 第 一 個 狀 態 是 1 ∣ 初 始 狀 態 i ) = P(第一個觀測值是紅,初始狀態i) P(第一個狀態是1|初始狀態i) = P(i)P(1i)=

在第一個觀測值為紅的前提下,轉為狀態2的概率:
β 1 ( 2 ) = ∑ i = 1 3 α 1 ( i ) a i 2 = 0.1 ∗ 0.2 + 0.16 ∗ 0.5 + 0.28 ∗ 0.3 = 0.184 \beta_1(2) = \sum_{i=1}^3 \alpha_1(i) a_{i2} = 0.1 * 0.2 + 0.16 * 0.5 + 0.28 * 0.3 = 0.184 β1(2)=i=13α1(i)ai2=0.10.2+0.160.5+0.280.3=0.184
在第一個觀測值為紅的前提下,轉為狀態3的概率:
β 1 ( 2 ) = ∑ i = 1 3 α 1 ( i ) a i 2 = 0.1 ∗ 0.3 + 0.16 ∗ 0.2 + 0.28 ∗ 0.5 = 0.202 \beta_1(2) = \sum_{i=1}^3 \alpha_1(i) a_{i2} = 0.1 * 0.3 + 0.16 * 0.2 + 0.28 * 0.5 = 0.202 β1(2)=i=13α1(i)ai2=0.10.3+0.160.2+0.280.5=0.202
所以
π ( 1 ) = [ 0.154 0.184 0.202 ] T \pi(1) = \left[ \begin{array}{cc} 0.154 & 0.184 & 0.202 \end{array} \right]^T π(1)=[0.1540.1840.202]T
第二個觀測值為白,計算在上述概率分佈下,三個狀態觀測到白的概率分別是:
α 2 ( 1 ) = β 1 ( 1 ) b 1 ( o 2 ) = 0.154 ∗ 0.5 = 0.077 α 2 ( 2 ) = β 1 ( 2 ) b 2 ( o 2 ) = 0.184 ∗ 0.6 = 0.1104 α 2 ( 3 ) = β 1 ( 3 ) b 3 ( o 2 ) = 0.202 ∗ 0.3 = 0.0606 \alpha_2(1) = \beta_1(1) b_1(o_2) = 0.154 * 0.5 = 0.077 \\ \alpha_2(2) = \beta_1(2) b_2(o_2) = 0.184 * 0.6 = 0.1104 \\ \alpha_2(3) = \beta_1(3) b_3(o_2) = 0.202 * 0.3 = 0.0606 α2(1)=β1(1)b1(o2)=0.1540.5=0.077α2(2)=β1(2)b2(o2)=0.1840.6=0.1104α2(3)=β1(3)b3(o2)=0.2020.3=0.0606
在第一、二個觀測值為紅、白的前提下,轉為狀態1的概率:
β 2 ( 1 ) = ∑ i = 1 3 α 2 ( i ) a i 1 = 0.077 ∗ 0.5 + 0.1104 ∗ 0.3 + 0.0606 ∗ 0.2 = 0.08374 \beta_2(1) = \sum_{i=1}^3 \alpha_2(i) a_{i1} = 0.077 * 0.5 + 0.1104 * 0.3 + 0.0606 * 0.2 = 0.08374 β2(1)=i=13α2(i)ai1=0.0770.5+0.11040.3+0.06060.2=0.08374
在第一、二個觀測值為紅、白的前提下,轉為狀態2的概率:
β 2 ( 2 ) = ∑ i = 1 3 α 2 ( i ) a i 2 = 0.077 ∗ 0.2 + 0.1104 ∗ 0.5 + 0.0606 ∗ 0.3 = 0.08878 \beta_2(2) = \sum_{i=1}^3 \alpha_2(i) a_{i2} = 0.077 * 0.2 + 0.1104 * 0.5 + 0.0606 * 0.3 = 0.08878 β2(2)=i=13α2(i)ai2=0.0770.2+0.11040.5+0.06060.3=0.08878
在第一、二個觀測值為紅、白的前提下,轉為狀態3的概率:
β 2 ( 2 ) = ∑ i = 1 3 α 2 ( i ) a i 2 = 0.077 ∗ 0.3 + 0.1104 ∗ 0.2 + 0.0606 ∗ 0.5 = 0.07548 \beta_2(2) = \sum_{i=1}^3 \alpha_2(i) a_{i2} = 0.077 * 0.3 + 0.1104 * 0.2 + 0.0606 * 0.5 = 0.07548 β2(2)=i=13α2(i)ai2=0.0770.3+0.11040.2+0.06060.5=0.07548
所以
π ( 2 ) = [ 0.08374 0.08878 0.07548 ] T \pi(2) =\left[ \begin{array}{cc} 0.08374 & 0.08878 & 0.07548\end{array} \right]^T π(2)=[0.083740.088780.07548]T
第三個觀測值為紅,計算在上述概率分佈下,三個狀態觀測到紅的概率分別是:
α 3 ( 1 ) = β 2 ( 1 ) b 1 ( o 1 ) = 0.08374 ∗ 0.5 = 0.04187 α 3 ( 2 ) = β 2 ( 2 ) b 2 ( o 1 ) = 0.08878 ∗ 0.4 = 0.03551 α 3 ( 3 ) = β 2 ( 3 ) b 3 ( o 1 ) = 0.07548 ∗ 0.7 = 0.05284 \alpha_3(1) = \beta_2(1) b_1(o_1) = 0.08374 * 0.5 = 0.04187 \\ \alpha_3(2) = \beta_2(2) b_2(o_1) = 0.08878 * 0.4 = 0.03551 \\ \alpha_3(3) = \beta_2(3) b_3(o_1) = 0.07548 * 0.7 = 0.05284 α3(1)=β2(1)b1(o1)=0.083740.5=0.04187α3(2)=β2(2)b2(o1)=0.088780.4=0.03551α3(3)=β2(3)b3(o1)=0.075480.7=0.05284
終止:
P ( O ∣ λ ) = ∑ i = 1 3 α 3 ( i ) = 0.13022 P(O|\lambda) = \sum_{i=1}^3 \alpha_3(i) = 0.13022 P(Oλ)=i=13α3(i)=0.13022

後向演算法求解觀測序列的概率

狀態集合 Q = { 1 , 2 , 3 } Q=\{1,2,3\} Q={1,2,3},觀測集合 V = { 紅 , 白 } V=\{紅,白\} V={,}

初始概率分佈:
π ( 0 ) = [ 0.2 0.4 0.4 ] T \pi(0) =\left[ \begin{array}{cc}0.2 & 0.4 & 0.4\end{array} \right]^T π(0)=[0.20.40.4]T
狀態轉移概率矩陣:
A = [ 0.5 0.2 0.3 0.3 0.5 0.2 0.2 0.3 0.5 ] A =\left[ \begin{array}{cc}0.5 & 0.2 & 0.3 \\0.3 & 0.5 & 0.2 \\0.2 & 0.3 & 0.5\end{array} \right] A=0.50.30.20.20.50.30.30.20.5
觀測概率矩陣:
B = [ 0.5 0.5 0.4 0.6 0.7 0.3 ] B =\left[ \begin{array}{ccc}0.5 & 0.5 \\0.4 & 0.6 \\0.7 & 0.3\end{array} \right] B=0.50.40.70.50.60.3
T = 3 T=3 T=3 O = ( 紅 , 白 , 紅 ) O=(紅,白,紅) O=(,,),試用前向演算法計算 P ( O ∣ λ ) P(O|\lambda) P(Oλ)

概率分佈初值
π ( 3 ) = [ 1 1 1 ] T \pi(3) = \left[ \begin{array}{cc} 1 & 1 & 1 \end{array} \right]^T π(3)=[111]T
後向演算法計算:

第三個觀測值是紅,計算在初始概率分佈下,三個狀態觀測到紅的概率分別是:
α 3 ( 1 ) = π 1 b 1 ( o 1 ) = 1 ∗ 0.5 = 0.5 α 3 ( 2 ) = π 2 b 2 ( o 1 ) = 1 ∗ 0.4 = 0.4 α 3 ( 3 ) = π 3 b 3 ( o 1 ) = 1 ∗ 0.7 = 0.7 \alpha_3(1) =\pi_1 b_1(o_1) = 1 * 0.5 = 0.5 \\ \alpha_3(2) = \pi_2 b_2(o_1) = 1 * 0.4 = 0.4 \\ \alpha_3(3) = \pi_3 b_3(o_1) = 1 * 0.7 = 0.7 α3(1)=π1b1(o1)=10.5=0.5α3(2)=π2b2(o1)=10.4=0.4α3(3)=π3b3(o1)=10.7=0.7
在第三個觀測值為紅的前提下,前一個狀態是1的概率:
β 2 ( 1 ) = ∑ i = 1 3 α 3 ( i ) a 1 i = 0.5 ∗ 0.5 + 0.4 ∗ 0.2 + 0.7 ∗ 0.3 = 0.54 \beta_2(1) = \sum_{i=1}^3 \alpha_3(i) a_{1i} = 0.5 * 0.5 + 0.4 * 0.2 + 0.7 * 0.3 = 0.54 β2(1)=i=13α3(i)a1i=0.50.5+0.40.2+0.70.3=0.54
在第三個觀測值為紅的前提下,前一個狀態是2的概率:
β 2 ( 2 ) = ∑ i = 1 3 α 3 ( i ) a 2 i = 0.5 ∗ 0.3 + 0.4 ∗ 0.5 + 0.7 ∗ 0.2 = 0.49 \beta_2(2) = \sum_{i=1}^3 \alpha_3(i) a_{2i} = 0.5 * 0.3 + 0.4 * 0.5 + 0.7 * 0.2 = 0.49 β2(2)=i=13α3(i)a2i=0.50.3+0.40.5+0.70.2=0.49
在第三個觀測值為紅的前提下,前一個狀態是3的概率:
β 2 ( 3 ) = ∑ i = 1 3 α 3 ( i ) a 3 i = 0.5 ∗ 0.2 + 0.4 ∗ 0.3 + 0.7 ∗ 0.5 = 0.57 \beta_2(3) = \sum_{i=1}^3 \alpha_3(i) a_{3i} = 0.5 * 0.2 + 0.4 * 0.3 + 0.7 * 0.5 = 0.57 β2(3)=i=13α3(i)a3i=0.50.2+0.40.3+0.70.5=0.57
所以
π ( 2 ) = [ 0.54 0.49 0.57 ] T \pi(2) =\left[ \begin{array}{cc}0.54 & 0.49 & 0.57\end{array} \right]^T π(2)=[0.540.490.57]T
第二個觀測值為白,計算在上述概率分佈下,三個狀態觀測到白的概率分別是:
α 2 ( 1 ) = β 2 ( 1 ) b 1 ( o 2 ) = 0.54 ∗ 0.5 = 0.27 α 2 ( 2 ) = β 2 ( 2 ) b 2 ( o 2 ) = 0.49 ∗ 0.6 = 0.294 α 2 ( 3 ) = β 2 ( 3 ) b 3 ( o 2 ) = 0.57 ∗ 0.3 = 0.171 \alpha_2(1) = \beta_2(1) b_1(o_2) = 0.54 * 0.5 = 0.27 \\ \alpha_2(2) = \beta_2(2) b_2(o_2) = 0.49 * 0.6 = 0.294 \\ \alpha_2(3) = \beta_2(3) b_3(o_2) = 0.57 * 0.3 = 0.171 α2(1)=β2(1)b1(o2)=0.540.5=0.27α2(2)=β2(2)b2(o2)=0.490.6=0.294α2(3)=β2(3)b3(o2)=0.570.3=0.171
在第二、三個觀測值為白、紅的前提下,前一個狀態是1的概率:
β 1 ( 1 ) = ∑ i = 1 3 α 2 ( i ) a i 1 = 0.27 ∗ 0.5 + 0.294 ∗ 0.2 + 0.171 ∗ 0.3 = 0.2451 \beta_1(1) = \sum_{i=1}^3 \alpha_2(i) a_{i1} = 0.27 * 0.5 + 0.294 * 0.2 + 0.171 * 0.3 = 0.2451 β1(1)=i=13α2(i)ai1=0.270.5+0.2940.2+0.1710.3=0.2451
在第二、三個觀測值為白、紅的前提下,前一個狀態是2的概率:
β 1 ( 2 ) = ∑ i = 1 3 α 2 ( i ) a i 2 = 0.27 ∗ 0.3 + 0.294 ∗ 0.5 + 0.171 ∗ 0.2 = 0.2622 \beta_1(2) = \sum_{i=1}^3 \alpha_2(i) a_{i2} = 0.27 * 0.3 + 0.294 * 0.5 + 0.171 * 0.2 = 0.2622 β1(2)=i=13α2(i)ai2=0.270.3+0.2940.5+0.1710.2=0.2622
在第二、三個觀測值為白、紅的前提下,前一個狀態是3的概率:
β 1 ( 3 ) = ∑ i = 1 3 α 2 ( i ) a i 3 = 0.27 ∗ 0.2 + 0.294 ∗ 0.3 + 0.171 ∗ 0.5 = 0.2277 \beta_1(3) = \sum_{i=1}^3 \alpha_2(i) a_{i3} = 0.27 * 0.2 + 0.294 * 0.3 + 0.171 * 0.5 = 0.2277 β1(3)=i=13α2(i)ai3=0.270.2+0.2940.3+0.1710.5=0.2277
所以
π ( 1 ) = [ 0.2451 0.2622 0.2277 ] T \pi(1) =\left[ \begin{array}{cc} 0.2451 & 0.2622 & 0.2277 \end{array} \right]^T π(1)=[0.24510.26220.2277]T
第一個觀測值為紅,計算在上述概率分佈下,三個狀態觀測到紅的概率分別是:
α 1 ( 1 ) = β 1 ( 1 ) b 1 ( o 1 ) = 0.2451 ∗ 0.5 = 0.12255 α 1 ( 2 ) = β 1 ( 2 ) b 2 ( o 1 ) = 0.2622 ∗ 0.4 = 0.10488 α 1 ( 3 ) = β 1 ( 3 ) b 3 ( o 1 ) = 0.2277 ∗ 0.7 = 0.15939 \alpha_1(1) = \beta_1(1) b_1(o_1) = 0.2451 * 0.5 = 0.12255 \\ \alpha_1(2) = \beta_1(2) b_2(o_1) = 0.2622 * 0.4 = 0.10488 \\ \alpha_1(3) = \beta_1(3) b_3(o_1) = 0.2277 * 0.7 = 0.15939 α1(1)=β1(1)b1(o1)=0.24510.5=0.12255α1(2)=β1(2)b2(o1)=0.26220.4=0.10488α1(3)=β1(3)b3(o1)=0.22770.7=0.15939
在第一、二、三個觀測值為紅、白、紅的前提下,初始狀態是1的概率:
β 1 ( 1 ) = ∑ i = 1 3 α 2 ( i ) a i 1 = 0.27 ∗ 0.5 + 0.294 ∗ 0.2 + 0.171 ∗ 0.3 = 0.2451 \beta_1(1) = \sum_{i=1}^3 \alpha_2(i) a_{i1} = 0.27 * 0.5 + 0.294 * 0.2 + 0.171 * 0.3 = 0.2451 β1(1)=i=13α2(i)ai1=0.270.5+0.2940.2+0.1710.3=0.2451
在第一、二、三個觀測值為紅、白、紅的前提下,初始狀態是2的概率:
β 1 ( 2 ) = ∑ i = 1 3 α 2 ( i ) a i 2 = 0.27 ∗ 0.3 + 0.294 ∗ 0.5 + 0.171 ∗ 0.2 = 0.2622 \beta_1(2) = \sum_{i=1}^3 \alpha_2(i) a_{i2} = 0.27 * 0.3 + 0.294 * 0.5 + 0.171 * 0.2 = 0.2622 β1(2)=i=13α2(i)ai2=0.270.3+0.2940.5+0.1710.2=0.2622
在第一、二、三個觀測值為紅、白、紅的前提下,初始狀態是3的概率:
β 1 ( 3 ) = ∑ i = 1 3 α 2 ( i ) a i 3 = 0.27 ∗ 0.2 + 0.294 ∗ 0.3 + 0.171 ∗ 0.5 = 0.2277 \beta_1(3) = \sum_{i=1}^3 \alpha_2(i) a_{i3} = 0.27 * 0.2 + 0.294 * 0.3 + 0.171 * 0.5 = 0.2277 β1(3)=i=13α2(i)ai3=0.270.2+0.2940.3+0.1710.5=0.2277
所以
π ( 1 ) = [ 0.2451 0.2622 0.2277 ] T \pi(1) =\left[ \begin{array}{cc} 0.2451 & 0.2622 & 0.2277 \end{array} \right]^T π(1)=[0.24510.26220.2277]T

前向概率:給定隱馬爾可夫模型 λ \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 2 , ⋯   , o t , i t = q i ∣ λ ) \alpha_t(i) = P(o_1,o_2,\cdots,o_t,i_t=q_i|\lambda) αt(i)=P(o1,o2,,ot,it=qiλ)
λ \lambda λ在這裡表示的是引數,為了不與條件概率混淆,後面推導 α t ( i ) \alpha_t(i) αt(i)不會顯示錶示 λ \lambda λ,也就是會使用 α t ( i ) = P ( o 1 , o 2 , ⋯   , o t , i t = q i ) \alpha_t(i) = P(o_1,o_2,\cdots,o_t,i_t=q_i) αt(i)=P(o1,o2,,ot,it=qi)

觀測序列概率的前向演算法

輸入:隱馬爾可夫模型 λ \lambda λ,觀測序列 O O O

輸出:觀測序列概率 P ( O ∣ λ ) P(O|\lambda) P(Oλ)

(1)初值
α 1 ( i ) = P ( o 1 , i 1 = q i ) = P ( o 1 ∣ i 1 = q i ) P ( i 1 = q i ) = b i ( o 1 ) π i , i = 1 , 2 , ⋯   , N \alpha_1(i) = P(o_1,i_1=q_i) = P(o_1|i_1=q_i) P(i_1=q_i) = b_i(o_1) \pi_i, \quad i = 1,2,\cdots,N α1(i)=P(o1,i1=qi)=P(o1i1=qi)P(i1=qi)=bi(o1)πi,i=1,2,,N
(2)遞推
α t + 1 ( i ) = P ( o 1 , o 2 , ⋯   , o t + 1 , i t + 1 = q i ) = ∑ j = 1 N P ( o 1 , o 2 , ⋯   , o t , o t + 1 , i t = q j , i t + 1 = q i ) = ∑ j = 1 N P ( o t + 1 ∣ o 1 , o 2 , ⋯   , o t , i t = q j , i t + 1 = q i ) P ( o 1 , o 2 , ⋯   , o t , i t = q j , i t + 1 = q i ) = ∑ j = 1 N P ( o t + 1 ∣ i t + 1 = q i ) P ( o 1 , o 2 , ⋯   , o t , i t = q j , i t + 1 = q i ) = ∑ j = 1 N P ( o t + 1 ∣ i t + 1 = q i ) P ( i t + 1 = q i ∣ o 1 , o 2 , ⋯   , o t , i t = q j ) P ( o 1 , o 2 , ⋯   , o t , i t = q j ) = ∑ j = 1 N P ( o t + 1 ∣ i t + 1 = q i ) P ( i t + 1 = q i ∣ i t = q j ) P ( o 1 , o 2 , ⋯   , o t , i t = q j ) = [ ∑ j = 1 N P ( o 1 , o 2 , ⋯   , o t , i t = q j ) P ( i t + 1 = q i ∣ i t = q j ) ] P ( o t + 1 ∣ i t + 1 = q i ) = [ ∑ j = 1 N α t ( j ) a j i ] b i ( o t + 1 ) \begin{aligned} \alpha_{t+1}(i) &= P(o_1,o_2,\cdots,o_{t+1},i_{t+1}=q_i) \\ &= \sum_{j=1}^N P(o_1,o_2,\cdots,o_t,o_{t+1},i_t=q_j,i_{t+1} = q_i) \\ &= \sum_{j=1}^N P(o_{t+1}|o_1,o_2,\cdots,o_t,i_t=q_j,i_{t+1} = q_i) P(o_1,o_2,\cdots,o_t,i_t=q_j,i_{t+1} = q_i) \\ &= \sum_{j=1}^N P(o_{t+1}|i_{t+1}=q_i) P(o_1,o_2,\cdots,o_t,i_t=q_j,i_{t+1} = q_i) \\ &= \sum_{j=1}^N P(o_{t+1}|i_{t+1}=q_i) P(i_{t+1} = q_i|o_1,o_2,\cdots,o_t,i_t = q_j) P(o_1,o_2,\cdots,o_t,i_t=q_j) \\ &= \sum_{j=1}^N P(o_{t+1}|i_{t+1}=q_i) P(i_{t+1} = q_i|i_t = q_j) P(o_1,o_2,\cdots,o_t,i_t=q_j) \\ &= \left[ \sum_{j=1}^N P(o_1,o_2,\cdots,o_t,i_t=q_j) P(i_{t+1} = q_i|i_t = q_j) \right] P(o_{t+1}|i_{t+1}=q_i) \\ &= \left[ \sum_{j=1}^N \alpha_t(j) a_{ji} \right] b_i(o_{t+1}) \end{aligned} αt+1(i)=P(o1,o2,,ot+1,it+1=qi)=j=1NP(o1,o2,,ot,ot+1,it=qj,it+1=qi)=j=1NP(ot+1o1,o2,,ot,it=qj,it+1=qi)P(o1,o2,,ot,it=qj,it+1=qi)=j=1NP(ot+1it+1=qi)P(o1,o2,,ot,it=qj,it+1=qi)=j=1NP(ot+1it+1=qi)P(it+1=qio1,o2,,ot,it=qj)P(o1,o2,,ot,it=qj)=j=1NP(ot+1it+1=qi)P(it+1=qiit=qj)P(o1,o2,,ot,it=qj)=[j=1NP(o1,o2,,ot,it=qj)P(it+1=qiit=qj)]P(ot+1it+1=qi)=[j=1Nαt(j)aji]bi(ot+1)

其中, α t ( j ) = P ( o 1 , o 2 , ⋯   , o t , i t = q j ) \alpha_t(j) = P(o_1,o_2,\cdots,o_t,i_t=q_j) αt(j)=P(o1,o2,,ot,it=qj) a j i = P ( i t + 1 = q i ∣ i t = q j ) a_{ji} = P(i_{t+1} = q_i|i_t = q_j) aji=P(it+1=qiit=qj) b i ( o t + 1 ) = P ( o t + 1 ∣ i t + 1 = q i ) b_i(o_{t+1}) = P(o_{t+1}|i_{t+1}=q_i) bi(ot+1)=P(ot+1it+1=qi)

上面推導用到了隱馬爾可夫模型的兩個基本假設:

  • 齊次馬爾可夫性模型,即假設隱藏的馬爾可夫鏈在任意時刻 t t t的狀態只依賴於其前一時刻的狀態,與其他時刻的狀態及觀測無關,也與時刻 t t t無關。
    P ( i t ∣ i t − 1 , o t − 1 , ⋯   , i 1 , o 1 ) = P ( i t ∣ i t − 1 ) , t = 1 , 2 , ⋯   , T P(i_t|i_{t-1},o_{t-1},\cdots,i_1,o_1) = P(i_t|i_{t-1}), \quad t=1,2,\cdots,T P(itit1,ot1,,i1,o1)=P(itit1),t=1,2,,T

  • 觀測獨立性假設,即假設任意時刻的觀測值依賴於該時刻的馬爾可夫鏈的狀態,與其他觀測及狀態無關。
    P ( o t ∣ i T , o T , i T − 1 , o T − 1 , ⋯   , i t + 1 , o t + 1 , i t , i t − 1 , o t − 1 , ⋯   , i 1 , o 1 ) = P ( o t ∣ i t ) , t = 1 , 2 , ⋯   , T P(o_t|i_T,o_T,i_{T-1},o_{T-1},\cdots,i_{t+1},o_{t+1},i_t,i_{t-1},o_{t-1},\cdots,i_1,o_1) = P(o_t|i_t), \quad t=1,2,\cdots,T P(otiT,oT,iT1,oT1,,it+1,ot+1,it,it1,ot1,,i1,o1)=P(otit),t=1,2,,T

(3)終止
P ( O ∣ λ ) = P ( o 1 , o 2 , ⋯   , o T ∣ λ ) = ∑ i = 1 N P ( o 1 , o 2 , ⋯   , o T , i T = q i ∣ λ ) = ∑ i = 1 N α T ( i ) P(O|\lambda) = P(o_1,o_2,\cdots,o_T|\lambda) = \sum_{i=1}^N P(o_1,o_2,\cdots,o_T,i_T=q_i|\lambda) = \sum_{i=1}^N \alpha_T(i) P(Oλ)=P(o1,o2,,oTλ)=i=1NP(o1,o2,,oT,iT=qiλ)=i=1Nα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(i) = P(o_{t+1},o_{t+2},\cdots,o_T|i_t=q_i,\lambda) βt(i)=P(ot+1,ot+2,,oTit=qi,λ)
λ \lambda λ在這裡表示的是引數,為了不與條件概率混淆,後面推導 β t ( i ) \beta_t(i) βt(i)不會顯示錶示 λ \lambda λ,也就是會使用 β t ( i ) = P ( o t + 1 , o t + 2 , ⋯   , o T ∣ i t = q i ) \beta_t(i) = P(o_{t+1},o_{t+2},\cdots,o_T|i_t=q_i) βt(i)=P(ot+1,ot+2,,oTit=qi)

觀測序列概率的前向演算法

輸入:隱馬爾可夫模型 λ \lambda λ,觀測序列 O O O

輸出:觀測序列概率 P ( O ∣ λ ) P(O|\lambda) P(Oλ)

(1)初值
β T ( i ) = 1 , i = 1 , 2 , ⋯   , N \beta_T(i) = 1, \quad i = 1,2,\cdots,N βT(i)=1,i=1,2,,N
(2)遞推
β t ( i ) = P ( o t + 1 , o t + 2 , ⋯   , o T ∣ i t = q i ) = ∑ j = 1 N P ( o t + 1 , o t + 2 , ⋯   , o T , i t + 1 = q j ∣ i t = q i ) = ∑ j = 1 N P ( o t + 1 , o t + 2 , ⋯   , o T , i t + 1 = q j , i t = q i ) P ( i t = q i ) = ∑ j = 1 N P ( o t + 1 , o t + 2 , ⋯   , o T , i t + 1 = q j , i t = q i ) P ( o t + 2 , ⋯   , o T , i t + 1 = q j , i t = q i ) ⋅ P ( o t + 2 , ⋯   , o T , i t + 1 = q j , i t = q i ) P ( i t + 1 = q j , i t = q i ) ⋅ P ( i t + 1 = q j , i t = q i ) P ( i t = q i ) = ∑ j = 1 N P ( o t + 1 ∣ o t + 2 , ⋯   , o T , i t + 1 = q j , i t = q i ) P ( o t + 2 , ⋯   , o T ∣ i t + 1 = q j , i t = q i ) P ( i t + 1 = q j ∣ i t = q i ) = ∑ j = 1 N P ( o t + 1 ∣ i t + 1 = q j ) P ( o t + 2 , ⋯   , o T ∣ i t + 1 = q j ) P ( i t + 1 = q j ∣ i t = q i ) = ∑ j = 1 N P ( i t + 1 = q j ∣ i t = q i ) P ( o t + 1 ∣ i t + 1 = q j ) P ( o t + 2 , ⋯   , o T ∣ i t + 1 = q j ) = ∑ j = 1 N a i j b j ( o t + 1 ) β t + 1 ( j ) \begin{aligned} \beta_t(i) &= P(o_{t+1},o_{t+2},\cdots,o_T|i_t=q_i) \\ &= \sum_{j=1}^N P(o_{t+1},o_{t+2},\cdots,o_T,i_{t+1}=q_j|i_t=q_i) \\ &= \sum_{j=1}^N \frac{P(o_{t+1},o_{t+2},\cdots,o_T,i_{t+1}=q_j,i_t=q_i)}{P(i_t=q_i)} \\ &= \sum_{j=1}^N \frac{P(o_{t+1},o_{t+2},\cdots,o_T,i_{t+1}=q_j,i_t=q_i)}{P(o_{t+2},\cdots,o_T,i_{t+1}=q_j,i_t=q_i)} \cdot \frac{P(o_{t+2},\cdots,o_T,i_{t+1}=q_j,i_t=q_i)}{P(i_{t+1}=q_j,i_t=q_i)} \cdot \frac{P(i_{t+1}=q_j,i_t=q_i)}{P(i_t=q_i)} \\ &= \sum_{j=1}^N P(o_{t+1}|o_{t+2},\cdots,o_T,i_{t+1}=q_j,i_t=q_i) P(o_{t+2},\cdots,o_T|i_{t+1}=q_j,i_t=q_i) P(i_{t+1}=q_j|i_t=q_i) \\ &= \sum_{j=1}^N P(o_{t+1}|i_{t+1}=q_j) P(o_{t+2},\cdots,o_T|i_{t+1}=q_j) P(i_{t+1}=q_j|i_t=q_i) \\ &= \sum_{j=1}^N P(i_{t+1}=q_j|i_t=q_i) P(o_{t+1}|i_{t+1}=q_j) P(o_{t+2},\cdots,o_T|i_{t+1}=q_j) \\ &= \sum_{j=1}^N a_{ij} b_j(o_{t+1}) \beta_{t+1}(j) \end{aligned} βt(i)=P(ot+1,ot+2,,oTit=qi)=j=1NP(ot+1,ot+2,,oT,it+1=qjit=qi)=j=1NP(it=qi)P(ot+1,ot+2,,oT,it+1=qj,it=qi)=j=1NP(ot+2,,oT,it+1=qj,it=qi)P(ot+1,ot+2,,oT,it+1=qj,it=qi)P(it+1=qj,it=qi)P(ot+2,,oT,it+1=qj,it=qi)P(it=qi)P(it+1=qj,it=qi)=j=1NP(ot+1ot+2,,oT,it+1=qj,it=qi)P(ot+2,,oTit+1=qj,it=qi)P(it+1=qjit=qi)=j=1NP(ot+1it+1=qj)P(ot+2,,oTit+1=qj)P(it+1=qjit=qi)=j=1NP(it+1=qjit=qi)P(ot+1it+1=qj)P(ot+2,,oTit+1=qj)=j=1Naijbj(ot+1)βt+1(j)

其中, a i j = P ( i t + 1 = q j ∣ i t = q i ) a_{ij} = P(i_{t+1} = q_j|i_t = q_i) aij=P(it+1=qjit=qi) b j ( o t + 1 ) = P ( o t + 1 ∣ i t + 1 = q j ) b_j(o_{t+1}) = P(o_{t+1}|i_{t+1}=q_j) bj(ot+1)=P(ot+1it+1=qj) β t + 1 ( j ) = P ( o t + 2 , ⋯   , o T ∣ i t + 1 = q j ) \beta_{t+1}(j) = P(o_{t+2},\cdots,o_T|i_{t+1}=q_j) βt+1(j)=P(ot+2,,oTit+1=qj)

上面推導用到了隱馬爾可夫模型的兩個基本假設:

  • 齊次馬爾可夫性模型,即假設隱藏的馬爾可夫鏈在任意時刻 t t t的狀態只依賴於其前一時刻的狀態,與其他時刻的狀態及觀測無關,也與時刻 t t t無關。
    P ( i t ∣ i t − 1 , o t − 1 , ⋯   , i 1 , o 1 ) = P ( i t ∣ i t − 1 ) , t = 1 , 2 , ⋯   , T P(i_t|i_{t-1},o_{t-1},\cdots,i_1,o_1) = P(i_t|i_{t-1}), \quad t=1,2,\cdots,T P(itit1,ot1,,i1,o1)=P(itit1),t=1,2,,T

  • 觀測獨立性假設,即假設任意時刻的觀測值依賴於該時刻的馬爾可夫鏈的狀態,與其他觀測及狀態無關。
    P ( o t ∣ i T , o T , i T − 1 , o T − 1 , ⋯   , i t + 1 , o t + 1 , i t , i t − 1 , o t − 1 , ⋯   , i 1 , o 1 ) = P ( o t ∣ i t ) , t = 1 , 2 , ⋯   , T P(o_t|i_T,o_T,i_{T-1},o_{T-1},\cdots,i_{t+1},o_{t+1},i_t,i_{t-1},o_{t-1},\cdots,i_1,o_1) = P(o_t|i_t), \quad t=1,2,\cdots,T P(otiT,oT,iT1,oT1,,it+1,ot+1,it,it1,ot1,,i1,o1)=P(otit),t=1,2,,T

(3)終止
P ( O ∣ λ ) = P ( o 1 , o 2 , ⋯   , o T ∣ λ ) = ∑ i = 1 N P ( o 1 , o 2 , ⋯   , o T , i 1 = q i ) = ∑ i = 1 N P ( i 1 = q i ) ⋅ P ( o 1 , o 2 , ⋯   , o T , i 1 = q i ) P ( i 1 = q i ) = ∑ i = 1 N P ( i 1 = q i ) ⋅ P ( o 1 , o 2 , ⋯   , o T , i 1 = q i ) P ( o 2 , ⋯   , o T , i 1 = q i ) ⋅ P ( o 2 , ⋯   , o T , i 1 = q i ) P ( i 1 = q i ) = ∑ i = 1 N P ( i 1 = q i ) P ( o 1 ∣ o 2 , ⋯   , o T , i 1 = q i ) P ( o 2 , ⋯   , o T ∣ i 1 = q i ) = ∑ i = 1 N P ( i 1 = q i ) P ( o 1 ∣ i 1 = q i ) P ( o 2 , ⋯   , o T ∣ i 1 = q i ) = ∑ i = 1 N π i b i ( o 1 ) β 1 ( i ) \begin{aligned} P(O|\lambda) &= P(o_1,o_2,\cdots,o_T|\lambda) \\ &= \sum_{i=1}^N P(o_1,o_2,\cdots,o_T,i_1=q_i) \\ &= \sum_{i=1}^N P(i_1=q_i) \cdot \frac{P(o_1,o_2,\cdots,o_T,i_1=q_i)}{P(i_1=q_i)} \\ &= \sum_{i=1}^N P(i_1=q_i) \cdot \frac{P(o_1,o_2,\cdots,o_T,i_1=q_i)}{P(o_2,\cdots,o_T,i_1=q_i)} \cdot \frac{P(o_2,\cdots,o_T,i_1=q_i)}{P(i_1=q_i)} \\ &= \sum_{i=1}^N P(i_1=q_i) P(o_1|o_2,\cdots,o_T,i_1=q_i) P(o_2,\cdots,o_T|i_1=q_i) \\ &= \sum_{i=1}^N P(i_1=q_i) P(o_1|i_1=q_i) P(o_2,\cdots,o_T|i_1=q_i) \\ &= \sum_{i=1}^N \pi_i b_i(o_1) \beta_1(i) \end{aligned} P(Oλ)=P(o1,o2,,oTλ)=i=1NP(o1,o2,,oT,i1=qi)=i=1NP(i1=qi)P(i1=qi)P(o1,o2,,oT,i1=qi)=i=1NP(i1=qi)P(o2,,oT,i1=qi)P(o1,o2,,oT,i1=qi)P(i1=qi)P(o2,,oT,i1=qi)=i=1NP(i1=qi)P(o1o2,,oT,i1=qi)P(o2,,oTi1=qi)=i=1NP(i1=qi)P(o1i1=qi)P(o2,,oTi1=qi)=i=1Nπibi(o1)β1(i)

定義在時刻 T T T狀態為 i i i的所有單個路徑 ( i 1 , i 2 , ⋯   , i t ) (i_1,i_2,\cdots,i_t) (i1,i2,,it)中概率最大值為
δ t ( i ) = max ⁡ i 1 , i 2 , ⋯   , i t − 1 P ( i t = i , i t − 1 , ⋯   , i 1 , o t , ⋯   , o 1 ∣ λ ) , i = 1 , 2 , ⋯   , N \delta_t(i) = \max_{i_1,i_2,\cdots,i_{t-1}} P(i_t=i,i_{t-1},\cdots,i_1,o_t,\cdots,o_1|\lambda), \quad i=1,2,\cdots,N δt(i)=i1,i2,,it1maxP(it=i,it1,,i1,ot,,o1λ),i=1,2,,N
由定義可得變數 δ \delta δ的遞推公式:
δ t + 1 ( i ) = max ⁡ i 1 , i 2 , ⋯   , i t P ( i t + 1 = i , i t , ⋯   , i 1 , o t + 1 , ⋯   , o 1 ∣ λ ) = max ⁡ i ≤ j ≤ N [ δ t ( j ) a j i ] b i ( o t + 1 ) , i = 1 , 2 , ⋯   , N ; t = 1 , 2 , ⋯   , T − 1 \begin{aligned} \delta_{t+1}(i) &= \max_{i_1,i_2,\cdots,i_t} P(i_{t+1}=i,i_t,\cdots,i_1,o_{t+1},\cdots,o_1|\lambda) \\ &= \max_{i \le j \le N} \left[ \delta_t(j) a_{ji} \right] b_i(o_{t+1}), \quad i=1,2,\cdots,N;t=1,2,\cdots,T-1 \end{aligned} δt+1(i)=i1,i2,,itmaxP(it+1=i,it,,i1,ot+1,,o1λ)=ijNmax[δt(j)aji]bi(ot+1),i=1,2,,N;t=1,2,,T1
定義在時刻 t t t狀態為 i i i的所有單個路徑 ( i 1 , i 2 , ⋯   , i t − 1 , i ) (i_1,i_2,\cdots,i_{t-1},i) (i1,i2,,it1,i)中概率最大的路徑的第 t − 1 t-1 t1個結點為
ψ t ( i ) = arg ⁡ max ⁡ 1 ≤ j ≤ N [ δ t − 1 ( j ) a j i ] , i = 1 , 2 , ⋯   , N \psi_t(i) = \arg \max_{1 \le j \le N} [\delta_{t-1}(j) a_{ji}], \quad i=1,2,\cdots,N ψt(i)=arg1jNmax[δt1(j)aji],i=1,2,,N
下面介紹維特比演算法:

輸入:模型 λ = ( A , B , π ) \lambda=(A,B,\pi) λ=(A,B,π)和觀測 O = ( o 1 , o 2 , ⋯   , o T ) O=(o_1,o_2,\cdots,o_T) O=(o1,o2,,oT)

輸出:最優路徑 I ∗ = ( i 1 ∗ , i 2 ∗ , ⋯   , i T ∗ ) I^* = (i_1^*,i_2^*,\cdots,i_T^*) I=(i1,i2,,iT)

(1) 初始化
δ 1 ( i ) = π i b i ( o 1 ) , i = 1 , 2 , ⋯   , N ψ 1 ( i ) = 0 , i = 1 , 2 , ⋯   , N \delta_1(i) = \pi_i b_i(o_1), \quad i=1,2,\cdots,N \\ \psi_1(i) = 0, \quad i=1,2,\cdots,N δ1(i)=πibi(o1),i=1,2,,Nψ1(i)=0,i=1,2,,N
(2) 遞推

t = 2 , 3 , ⋯   , T t=2,3,\cdots,T t=2,3,,T
δ t ( i ) = max ⁡ 1 ≤ j ≤ N [ δ t − 1 ( j ) a j i ] b i ( o t ) , i = 1 , 2 , ⋯   , N δ t ( i ) = arg ⁡ max ⁡ 1 ≤ j ≤ N [ δ t − 1 ( j ) a j i ] , i = 1 , 2 , ⋯   , N \begin{aligned} \delta_t(i) = \max_{1 \le j \le N} \left[ \delta_{t-1}(j) a_{ji} \right] b_i(o_t),& \quad i=1,2,\cdots,N \\ \delta_t(i) = \arg \max_{1 \le j \le N} \left[ \delta_{t-1}(j) a_{ji} \right],& \quad i=1,2,\cdots,N \end{aligned} δt(i)=1jNmax[δt1(j)aji]bi(ot),δt(i)=arg1jNmax[δt1(j)aji],i=1,2,,Ni=1,2,,N
(3) 終止
P ∗ = max ⁡ 1 ≤ i ≤ N δ T ( i ) i T ∗ = arg ⁡ max ⁡ 1 ≤ i ≤ N [ δ T ( i ) ] \begin{aligned} P^* &= \max_{1 \le i \le N} \delta_T(i) \\ i_T^* &= \arg \max_{1 \le i \le N} \left[ \delta_T(i) \right] \end{aligned} PiT=1iNmaxδT(i)=arg1iNmax[δT(i)]
(4) 最優路徑回溯

t = T − 1 , T − 2 , ⋯   , 1 t=T-1,T-2,\cdots,1 t=T1,T2,,1
i t ∗ = ψ t + 1 ( i t + 1 ∗ ) i_t^* = \psi_{t+1}(i_{t+1}^*) it=ψt+1(it+1)
求得最優路徑 I ∗ = ( i 1 ∗ , i 2 ∗ , ⋯   , i T ∗ ) I^* = (i_1^*,i_2^*,\cdots,i_T^*) I=(i1,i2,,iT)

狀態集合 Q = { 1 , 2 , 3 } Q=\{1,2,3\} Q={1,2,3},觀測集合 V = { 紅 , 白 } V=\{紅,白\} V={,}

初始概率分佈:
π ( 0 ) = [ 0.2 0.4 0.4 ] T \pi(0) =\left[ \begin{array}{cc}0.2 & 0.4 & 0.4\end{array} \right]^T π(0)=[0.20.40.4]T
狀態轉移概率矩陣:
A = [ 0.5 0.2 0.3 0.3 0.5 0.2 0.2 0.3 0.5 ] A =\left[ \begin{array}{cc}0.5 & 0.2 & 0.3 \\0.3 & 0.5 & 0.2 \\0.2 & 0.3 & 0.5\end{array} \right] A=0.50.30.20.20.50.30.30.20.5
觀測概率矩陣:
B = [ 0.5 0.5 0.4 0.6 0.7 0.3 ] B =\left[ \begin{array}{ccc}0.5 & 0.5 \\0.4 & 0.6 \\0.7 & 0.3\end{array} \right] B=0.50.40.70.50.60.3
T = 3 T=3 T=3 O = ( 紅 , 白 , 紅 ) O=(紅,白,紅) O=(,,),試求最優狀態序列,即最優路徑 I ∗ = ( i 1 ∗ , i 2 ∗ , i 3 ∗ ) I^*=(i_1^*,i_2^*,i_3^*) I=(i1,i2,i3)

(1) 初始化

t = 1 t=1 t=1時,對每一個狀態 i i i i = 1 , 2 , 3 i=1,2,3 i=1,2,3,求狀態為 i i i觀測 o 1 o_1 o1為紅的概率,記此概率為 δ 1 ( i ) \delta_1(i) δ1(i),則
δ 1 ( i ) = π i b i ( o 1 ) = π i b i ( 紅 ) , i = 1 , 2 , 3 \delta_1(i) = \pi_i b_i(o_1) = \pi_i b_i(紅), \quad i=1,2,3 δ1(i)=πibi(o1)=πibi(),i=1,2,3
代入實際資料
δ 1 ( 1 ) = 0.2 ∗ 0.5 = 0.1 , δ 1 ( 2 ) = 0.4 ∗ 0.4 = 0.16 , δ 1 ( 3 ) = 0.4 ∗ 0.7 = 0.28 \delta_1(1)=0.2*0.5=0.1,\quad \delta_1(2)=0.4*0.4=0.16,\quad \delta_1(3)=0.4*0.7=0.28 δ1(1)=0.20.5=0.1,δ1(2)=0.40.4=0.16,δ1(3)=0.40.7=0.28
ψ 1 ( i ) = 0 , i = 1 , 2 , 3 \psi_1(i)=0, \quad i=1,2,3 ψ1(i)=0,i=1,2,3

(2) 在 t = 2 t=2 t=2時,對每個狀態 i i i i = 1 , 2 , 3 i=1,2,3 i=1,2,3,求在 t = 1 t=1 t=1時狀態為 j j j觀測為紅並在 t = 2 t=2 t=2時狀態為 i i i觀測 o 2 o_2 o2為白的路徑的最大概率,記此最大概率為 δ 2 ( i ) \delta_2(i) δ2(i),則
δ 2 ( i ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j i ] b i ( o 2 ) \delta_2(i) = \max_{1 \le j \le 3} \left[ \delta_1(j) a_{ji} \right] b_i(o_2) δ2(i)=1j3max[δ1(j)aji]bi(o2)
同時,對每個狀態 i i i i = 1 , 2 , 3 i=1,2,3 i=1,2,3,記錄概率最大路徑的前一個狀態 j j j
ψ 2 ( i ) = arg ⁡ max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j i ] \psi_2(i) = \arg \max_{1 \le j \le 3} \left[ \delta_1(j) a_{ji} \right] ψ2(i)=arg1j3max[δ1(j)aji]
計算:

δ 2 ( 1 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.1 ∗ 0.5 , 0.16 ∗ 0.3 , 0.28 ∗ 0.2 } ∗ 0.5 = 0.028 ψ 2 ( 1 ) = 3 δ 2 ( 2 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.1 ∗ 0.2 , 0.16 ∗ 0.5 , 0.28 ∗ 0.3 } ∗ 0.6 = 0.0504 ψ 2 ( 2 ) = 3 δ 2 ( 2 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.1 ∗ 0.3 , 0.16 ∗ 0.2 , 0.28 ∗ 0.5 } ∗ 0.3 = 0.042 ψ 2 ( 2 ) = 3 \begin{aligned} &\begin{aligned} \delta_2(1) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.1*0.5,0.16*0.3,0.28*0.2 \right\} * 0.5 \\ &= 0.028 \end{aligned} \\ &\psi_2(1) = 3 \\ &\begin{aligned} \delta_2(2) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.1*0.2,0.16*0.5,0.28*0.3 \right\} * 0.6 \\ &= 0.0504 \end{aligned} \\ &\psi_2(2) = 3 \\ &\begin{aligned} \delta_2(2) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.1*0.3,0.16*0.2,0.28*0.5 \right\} * 0.3 \\ &= 0.042 \end{aligned} \\ &\psi_2(2) = 3 \end{aligned} δ2(1)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.10.5,0.160.3,0.280.2}0.5=0.028ψ2(1)=3δ2(2)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.10.2,0.160.5,0.280.3}0.6=0.0504ψ2(2)=3δ2(2)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.10.3,0.160.2,0.280.5}0.3=0.042ψ2(2)=3

同樣,在 t = 3 t=3 t=3時,

δ 3 ( i ) = max ⁡ 1 ≤ j ≤ 3 [ δ 2 ( j ) a j i ] b i ( o 3 ) \delta_3(i) = \max_{1 \le j \le 3} \left[ \delta_2(j) a_{ji} \right] b_i(o_3) δ3(i)=1j3max[δ2(j)aji]bi(o3)

同時,對每個狀態 i i i i = 1 , 2 , 3 i=1,2,3 i=1,2,3,記錄概率最大路徑的前一個狀態 j j j

ψ 3 ( i ) = arg ⁡ max ⁡ 1 ≤ j ≤ 3 [ δ 2 ( j ) a j i ] \psi_3(i) = \arg \max_{1 \le j \le 3} \left[ \delta_2(j) a_{ji} \right] ψ3(i)=arg1j3max[δ2(j)aji]

計算:

δ 3 ( 1 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.028 ∗ 0.5 , 0.0504 ∗ 0.3 , 0.042 ∗ 0.2 } ∗ 0.5 = 0.00756 ψ 3 ( 1 ) = 2 δ 3 ( 2 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.028 ∗ 0.2 , 0.0504 ∗ 0.5 , 0.042 ∗ 0.3 } ∗ 0.4 = 0.01008 ψ 3 ( 2 ) = 2 δ 3 ( 2 ) = max ⁡ 1 ≤ j ≤ 3 [ δ 1 ( j ) a j 1 ] b 1 ( o 2 ) = max ⁡ j { 0.028 ∗ 0.3 , 0.0504 ∗ 0.2 , 0.042 ∗ 0.5 } ∗ 0.7 = 0.0147 ψ 3 ( 2 ) = 3 \begin{aligned} &\begin{aligned} \delta_3(1) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.028*0.5,0.0504*0.3,0.042*0.2 \right\} * 0.5 \\ &= 0.00756 \end{aligned} \\ &\psi_3(1) = 2 \\ &\begin{aligned} \delta_3(2) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.028*0.2,0.0504*0.5,0.042*0.3 \right\} * 0.4 \\ &= 0.01008 \end{aligned} \\ &\psi_3(2) = 2 \\ &\begin{aligned} \delta_3(2) &= \max_{1 \le j \le 3} \left[ \delta_1(j) a_{j1} \right] b_1(o_2) \\ &= \max_j \left\{ 0.028*0.3,0.0504*0.2,0.042*0.5 \right\} * 0.7 \\ &= 0.0147 \end{aligned} \\ &\psi_3(2) = 3 \end{aligned} δ3(1)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.0280.5,0.05040.3,0.0420.2}0.5=0.00756ψ3(1)=2δ3(2)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.0280.2,0.05040.5,0.0420.3}0.4=0.01008ψ3(2)=2δ3(2)=1j3max[δ1(j)aj1]b1(o2)=jmax{0.0280.3,0.05040.2,0.0420.5}0.7=0.0147ψ3(2)=3

(3) 以 P ∗ P^* P表示最優路徑的概率,則
P ∗ = max ⁡ 1 ≤ i ≤ 3 δ 3 ( i ) = 0.0147 P^* = \max_{1 \le i \le 3} \delta_3(i) = 0.0147 P=1i3maxδ3(i)=0.0147
最優路徑的終點是 i 3 ∗ i_3^* i3
i 3 ∗ = arg ⁡ max ⁡ i [ δ 3 ( i ) ] = 3 i_3^* = \arg \max_i[\delta_3(i)] = 3 i3=argimax[δ3(i)]=3

(4) 由最優路徑的終點 i 3 ∗ i_3^* i3,逆向找到 i 2 ∗ , i 1 ∗ i_2^*,i_1^* i2,i1

t = 2 t=2 t=2時, i 2 ∗ = ψ 3 ( i 3 ∗ ) = ψ 3 ( 3 ) = 3 i_2^* = \psi_3(i_3^*) = \psi_3(3) = 3 i2=ψ3(i3)=ψ3(3)=3

t = 1 t=1 t=1時, i 1 ∗ = ψ 2 ( i 2 ∗ ) = ψ 2 ( 3 ) = 3 i_1^* = \psi_2(i_2^*) = \psi_2(3) = 3 i1=ψ2(i2)=ψ2(3)=3

於是求得最有路徑,即最優狀態序列 I ∗ = ( i 1 ∗ , i 2 ∗ , i 3 ∗ ) = ( 3 , 3 , 3 ) I^*=(i_1^*,i_2^*,i_3^*)=(3,3,3) I=(i1,i2,i3)=(3,3,3)

如何簡單地理解維特比演算法(viterbi演算法)?https://www.zhihu.com/tardis/sogou/ans/489485567

連結:

https://www.zhihu.com/question/20962240/answer/6102575

https://blog.csdn.net/u012421852/article/details/80186816

https://blog.csdn.net/ppn029012/article/details/8923501

https://zhuanlan.zhihu.com/p/27907806

https://zhuanlan.zhihu.com/p/27056207

相關文章