[ML-13-2] Hidden Markov Model HMM-forward and backward algorithm

 

[ML-13-1 ] Hidden Markov Model HMM

[ML-13-2 ] Hidden Markov Model HMM- forward and backward algorithm

[ML-13-3 ] Hidden Markov Model HMM--Baum-Welch (Baum- Welch)

[ML-13-4 ] Hidden Markov Model HMM-- Viterbi (Viterbi) Algorithm for Prediction Problems

table of Contents

  1. introduction
  2. Direct calculation
  3. Forward algorithm
  4. Forward algorithm example
  5. Backward algorithm
  6. Calculation of common probability of HMM

I. Introduction

In [ML-13-1] Hidden Markov Model HMM, we talked about the basic knowledge of HMM model and the three basic problems of HMM. In this article, we focus on the solution of the first basic problem of HMM. That is, given the model λ = (A, B, π) and the observed sequence Q = {q1, q2, ..., qT}, calculate the probability P (Q | λ) of the observed sequence Q under the model λ

Forward probability-backward probability actually refers to the information converted from the probability value of state si corresponding to time t in an observation sequence.

In the following figure (implied by q and observation by y), the former is the forward probability (all 1-t observation states and the implicit state at time t), and the latter is the backward probability

2. Direct calculation

According to the probability formula, enumerate all possible state sequences with length T I = {i1, i2, ..., iT}, find each state sequence I and observation sequence Q = {q1, q2, ..., qT} Joint probability P (Q, I; λ), and then sum all possible state sequences to get the final probability P (Q; λ)

    The forward-backward algorithm is to help us solve this problem at a lower time complexity.

Three, forward algorithm

The forward-backward algorithm is a general term for the forward algorithm and the backward algorithm. Both algorithms can be used to find the probability of the HMM observation sequence. Let's first look at how the forward algorithm solves this problem.

  The forward algorithm is essentially an algorithm for dynamic programming, that is, we have to find the formula for recursion of the local state, so that step by step from the optimal solution of the subproblem to the optimal solution of the entire problem.

Definition: Given λ, the probability that part of the observation sequence at time t is q1, q2, ..., qt and the hidden state is si is the forward probability. Remember:

The derivation process is as follows, and you can also understand it yourself: the i state at time t + 1 may be derived from each state after transformation

It can be seen from the recursive formula that the time complexity of our algorithm is O (TN ^ 2), which is several orders of magnitude less than the time complexity of the brute force solution O (TN ^ T).

Four, forward algorithm examples

4.1 Examples

Suppose there are three boxes, numbered 1,2,3; each box is equipped with small balls of black and white colors, the proportion of balls is as follows:

According to the following rules, the ball with the replacement is extracted to obtain the observation sequence of the ball color:

  1. Select a box according to the probability of π, randomly extract a small ball from the box, record the color, and put it back into the box;
  2. Select a new box according to a certain conditional probability and repeat the operation;
  3. Obtained the observation sequence finally: "White black and white white black
  • State set: S = {Box 1, Box 2, Box 3}
  • Observation set: O = {white, black}
  • State sequence and observation sequence length T = 5
  • Initial probability distribution π
  • State transition probability matrix A
  • Observation probability matrix B

Given the parameters π, A, and B, what is the probability that the observed sequence is "white, black, white, white and black" ?? Note that in this process, the observer can only see the color sequence of the ball, but not the ball It was taken from which box.

4.2 Calculation process

Get the final result:

Five, backward algorithm

  Familiar with the forward algorithm to find the probability of HMM observation sequence, now look at how to use the backward algorithm to find the probability of HMM observation sequence.

The backward algorithm is very similar to the forward algorithm. They are both dynamic programming. The only difference is that the selected local state is different. The backward algorithm uses "backward probability". So how is the backward probability defined?

Definition: Given λ, it is defined that under the premise that the state at time t is si, the part of the observation sequence from t + 1 to T is qt + 1, qt + 2, ..., the probability of qT is the backward probability. Remember:

Derivation:

According to this formula, the probabilities in the previous section can also be calculated, and the results obtained are the same. This article will not give examples.

6. Calculation of common probabilities of HMM

 Using the forward and backward probabilities, we can calculate the probability formula for a single state and two states in the HMM.

6.1 Probability of a single state

Given the model λ and the observation sequence Q, the probability of being in state si at time t is written as:

The significance of a single state probability is mainly used to judge the most likely state at each moment, so that a state sequence can be obtained as the final prediction result.

Using the definition of forward probability and backward probability, we can know:

From the above two expressions:

6.2 Joint probability of two states

Given the model λ and the observation sequence Q, the probability of being in state si at time t and being in state sj at time t + 1 is written as:

6.3 The above two summation can be obtained:

Appendix 1: Forward probability

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

# --encoding:utf-8 --

"" " An algorithm implemented in class to calculate the forward probability in the HMM model ( forward algorithm )" ""

import numpy as np

def calc_alpha(pi, A, B, Q, alpha):

"""

Calculate the forward probability alpha and save the result in the alpha matrix (T * n)

:param pi: 1*n

: stop A: n * n

:param B: n*m

: param Q: 1 * T => T array

:param alpha: T*n

:return: alpha

"""

# 1. Get related variable information

n = np.shape(A)[0]

T = np.shape(Q)[0]

# 2 Update the forward probability value at time t = 1

for i in range(n):

alpha[0][i] = pi[i] * B[i][Q[0]]

# 3. 更新t=2... T时刻对应的前向概率值

tmp = np.zeros(n)

for t in range(1, T):

# 迭代计算t时刻对应的前向概率值

for i in range(n):

# 计算时刻t状态为i的前向概率

# a. 计算上一个时刻t-1累计到当前状态i的概率值

for j in range(n):

tmp[j] = alpha[t - 1][j] * A[j][i]

# b. 更新时刻t对应状态i的前向概率值

alpha[t][i] = np.sum(tmp) * B[i][Q[t]]

# 4. 返回结果

return alpha

if __name__ == '__main__':

# 测试

pi = np.array([0.2, 0.5, 0.3])

A = np.array([

[0.5, 0.4, 0.1],

[0.2, 0.2, 0.6],

[0.2, 0.5, 0.3]

])

B = np.array([

[0.4, 0.6],

[0.8, 0.2],

[0.5, 0.5]

])

# 白,黑,白,白,黑

Q = [0, 1, 0, 0, 1]

alpha = np.zeros((len(Q), len(A)))

# 开始计算

calc_alpha(pi, A, B, Q, alpha)

# 输出最终结果

print(alpha)

# 计算最终概率值:

p = 0

for i in alpha[-1]:

p += i

print(Q, end="->出现的概率为:")

print(p)

结果:

[[0.08 0.4 0.15 ]

[0.09 0.0374 0.1465 ]

[0.032712 0.093384 0.037695 ]

[0.01702872 0.04048728 0.03530505]

[0.0142037 0.00651229 0.01829338]]

[0, 1, 0, 0, 1]->出现的概率为:0.03900936690000001

附件一:手写练习

Guess you like

Origin www.cnblogs.com/yifanrensheng/p/12684727.html