Hidden Markov Model (HMM) 詳細推導及思路分析

往期文章連接目錄

Before reading this post, make sure you are familiar with the EM Algorithm and decent among of knowledge of convex optimization. If not, please check out my previous posthtml

Let’s get started!dom

Conditional independence

A A and B B are conditionally independent given C C if and only if, given knowledge that C C occurs, knowledge of whether A A occurs provides no information on the likelihood of B B occurring, and knowledge of whether B B occurs provides no information on the likelihood of A A occurring.ide

Formally, if we denote conditional independence of A A and B B given C C by ( A  ⁣ ⁣ ⁣ B ) C (A\perp \!\!\!\perp B)\mid C , then by definition, we havesvg

( A  ⁣ ⁣ ⁣ B ) C       P ( A , B C ) = P ( A C ) P ( B C ) (A\perp \!\!\!\perp B)\mid C\quad \iff \quad P(A, B\mid C)= P(A\mid C) \cdot P(B\mid C) post

Given the knowledge that C C occurs, to show the knowledge of whether B B occurs provides no information on the likelihood of A A occurring, we have學習

P ( A B , C ) = P ( A , B , C ) P ( B , C ) = P ( A , B C ) P ( C ) P ( B , C ) = P ( A C ) P ( B C ) P ( C ) P ( B C ) P ( C ) = P ( A C ) \begin{aligned} P(A | B ,C) &=\frac{P(A , B , C)}{P(B , C)} \\ &=\frac{P(A , B | C) \cdot P(C)}{P(B , C)} \\ &=\frac{P(A | C) \cdot P(B | C) \cdot P(C)}{P(B | C) \cdot P(C)} \\ &=P(A | C) \end{aligned} ui

Two classical cases where X X and Z Z are conditionally independent

Case 1 :this

From the above directed graph, we have P ( X , Y , Z ) = P ( X ) P ( Y X ) P ( Z Y ) P(X,Y,Z) = P(X)\cdot P(Y|X)\cdot P(Z|Y) . Hence we have

P ( Z X , Y ) = P ( X , Y , Z ) P ( X , Y ) = P ( X ) P ( Y X ) P ( Z Y ) P ( X ) P ( Y X ) = P ( Z Y ) \begin{aligned} P(Z|X,Y) &= \frac{P(X,Y,Z)}{P(X,Y)}\\ &= \frac{P(X)\cdot P(Y|X)\cdot P(Z|Y)}{P(X)\cdot P(Y|X)}\\ &= P(Z|Y) \end{aligned}

Therefore, X X and Z Z are conditionally independent.

Case 2 :

From the above directed graph, we have P ( X , Y , Z ) = P ( Y ) P ( X Y ) P ( Z Y ) P(X,Y,Z) = P(Y)\cdot P(X|Y) \cdot P(Z|Y) . Hence we have

P ( Z X , Y ) = P ( X , Y , Z ) P ( X , Y ) = P ( Y ) P ( X Y ) P ( Z Y ) P ( Y ) P ( X Y ) = P ( Z Y ) \begin{aligned} P(Z|X,Y) &= \frac{P(X,Y,Z)}{P(X,Y)}\\ &= \frac{P(Y)\cdot P(X|Y) \cdot P(Z|Y)}{P(Y)\cdot P(X|Y)}\\ &= P(Z|Y) \end{aligned}

Therefore, X X and Z Z are conditionally independent.

Settings of the Hidden Markov Model (HMM)

The HMM is based on augmenting the Markov chain. A Markov chain is a model that tells us something about the probabilities of sequences of random variables, states, each of which can take on values from some set. A Markov chain makes a very strong assumption that if we want to predict the future in the sequence, all that matters is the current state.

To put it formally, suppose we have a sequence of state variables z 1 , z 2 , . . . , z n z_1, z_2, ..., z_n . Then the Markov assumption is

p ( z n z 1 z 2 . . . z n 1 ) = p ( z n z n 1 ) p(z_n | z_1z_2...z_{n-1}) = p(z_n | z_{n-1})

A Markov chain is useful when we need to compute a probability for a sequence of observable events. However, in many cases the events we are interested in are hidden. For example we don’t normally observe part-of-speech (POS) tags in a text. Rather, we see words, and must infer the tags from the word sequence. We call the tags hidden because they are not observed.

A hidden Markov model (HMM) allows us to talk about both observed events (like words that we see in the input) and hidden events (like part-of-speech tags) that we think of as causal factors in our probabilistic model. An HMM is specified by the following components:

  • A sequence of hidden states z z , where z k z_k takes values from all possible hidden states Z = { 1 , 2 , . . , m } Z = \{1,2,..,m\} .

  • A sequence of observations x x , where x = ( x 1 , x 2 , . . . , x n ) x = (x_1, x_2, ..., x_n) . Each one is drawn from a vocabulary V V .

  • A transition probability matrix A A , where A A is an m × m m \times m matrix. A i j A_{ij} represents the probability of moving from state i i to state j j : A i j = p ( z t + 1 = j z t = i ) A_{ij} = p(z_{t+1}=j| z_t=i) , and j = 1 m A i j = 1 \sum_{j=1}^{m} A_{ij} = 1 for all i i .

  • An emission probability matrix B B , where B B is an m × V m \times |V| matrix. B i j B_{ij} represents the probability of an observation x j x_j being generated from a state i i : B i j = P ( x t = V j z t = i ) B_{ij} = P(x_t = V_j|z_t = i)

  • An initial probability distribution π \pi over states, where π = ( π 1 , π 2 , . . . , π m ) \pi = (\pi_1, \pi_2, ..., \pi_m) . π i \pi_i is the probability that the Markov chain will start in state i i . i = 1 m π i = 1 \sum_{i=1}^{m} \pi_i = 1 .

Given a sequence x x and the corresponding hidden states z z (like one in the picture above), we have

P ( x , z θ ) = p ( z 1 ) [ p ( z 2 z 1 ) p ( z 3 z 2 ) . . . ( z n z n 1 ) ] [ p ( x 1 z 1 ) p ( x 2 z 2 ) . . . p ( x n z n ) ] (0) P(x, z|\theta) = p(z_1) \cdot [p(z_2|z_1)\cdot p(z_3|z_2)\cdot ... \cdotp(z_n|z_{n-1})] \cdot [p(x_1|z_1)\cdot p(x_2|z_2)\cdot ... \cdot p(x_n|z_n)] \tag 0

We get p ( z 1 ) p(z_1) from π \pi , p ( z k + 1 z k ) p(z_{k+1}|z_k) from A A , and p ( x k z k ) p(x_k|z_k) from B B .

Useful probabilities p ( z k x ) p(z_k | x) and p ( z k + 1 , z k x ) p(z_{k+1}, z_k | x)

p ( z k x ) p(z_k | x) and p ( z k + 1 , z k x ) p(z_{k+1}, z_k | x) are useful probabilities and we are going to use them later.

Intuition: Once we have a sequence x x , we might be interested in find the probability of any hidden state z k z_k , i.e., find probabilities p ( z k = 1 x ) , p ( z k = 2 x ) , . . . , p ( z k = m x ) p(z_k =1| x), p(z_k =2| x), ..., p(z_k =m| x) . we have the following

p ( z k x ) = p ( z k , x ) p ( x ) ( 1 ) p ( z k , x ) ( 2 ) \begin{aligned} p(z_k | x) &= \frac{p(z_k, x)}{p(x)} & & (1)\\ &\propto p(z_k, x) & & (2)\\ \end{aligned}

Note that from ( 1 ) (1) to (2), since p ( x ) p(x) doesn’t change for all values of z k z_k , p ( z k x ) p(z_k | x) is proportional to p ( z k , x ) p(z_k, x) .

p ( z k = i , x ) = p ( z k = i , x 1 : k , x k + 1 : n ) = p ( z k = i , x 1 : k ) p ( x k + 1 : n z k = i , x 1 : k ) ( 3 ) = p ( z k = i , x 1 : k ) p ( x k + 1 : n z k = i ) ( 4.1 ) = α k ( z k = i ) β k ( z k = i ) ( 4.11 ) \begin{aligned} p(z_k=i, x) &= p(z_k=i, x_{1:k}, x_{k+1:n}) \\ &= p(z_k=i, x_{1:k}) \cdot p(x_{k+1:n}|z_k=i, x_{1:k}) & & (3)\\ &= p(z_k=i, x_{1:k}) \cdot p(x_{k+1:n}|z_k=i) & & (4.1) \\ &= \alpha_k(z_k=i) \cdot \beta_k(z_k=i) &&(4.11)\\ \end{aligned}

From the above graph, we see that the second term ( 3 ) (3) is the 2nd classical cases. So x k + 1 : n x_{k+1:n} and x 1 : k x_{1:k} are conditionally independent. This is why we can go from ( 3 ) (3) to ( 4.1 ) (4.1) . We are going to use the Forward Algorithm to compute p ( z k , x 1 : k ) p(z_k, x_{1:k}) , and Backward Algorithm to compute p ( x k + 1 : n z k ) p(x_{k+1:n}|z_k) later.

We denote p ( z k , x 1 : k ) p(z_k, x_{1:k}) by α k ( z k ) \alpha_k(z_k) and p ( x k + 1 : n z k ) p(x_{k+1:n}|z_k) by β k ( z k ) \beta_k(z_k) .

After we know how to calculate these two terms separately, we can calculate p ( z k x ) p(z_k | x) easily by introducing a normalization term. That is,

p ( z k = i x ) = p ( z k = i , x ) j = 1 m p ( z k = j , x ) = α k ( z k = i ) β k ( z k = i ) j = 1 m α k ( z k = j ) β k ( z k = j ) ( 4.2 ) \begin{aligned} p(z_k = i | x) &= \frac{p(z_k = i, x)}{\sum_{j=1}^m p(z_k = j, x)} \\ &= \frac{\alpha_k(z_k=i)\beta_k(z_k=i)}{\sum_{j=1}^{m} \alpha_k(z_k=j)\beta_k(z_k=j)} && (4.2) \end{aligned}

where j m p ( z k = j , x ) \sum_j^m p(z_k = j, x) is the normalization term which makes p ( z k = 1 , x ) p(z_k = 1, x) take values between 0 0 and 1 1 for all z k z_k .

Similarly, we are also interested in finding p ( z k + 1 , z k x ) p(z_{k+1}, z_k | x) , where

p ( z k + 1 , z k x ) p ( z k + 1 , z k , x ) p(z_{k+1}, z_k | x) \propto p(z_{k+1}, z_k, x)

By using the property of conditional independence, we have

p ( z k + 1 = j , z k = i , x ) = p ( z k = i , z k + 1 = j , x 1 : k , x k + 1 , x k + 2 : n ) = p ( z k = i , x 1 : k ) p ( x k + 2 : n ) z k + 1 = j ) p ( z k + 1 = j z k = i ) p ( x k + 1 z k + 1 = j ) ( 4.3 ) = α k ( z k = i ) β k + 1 ( z k + 1 = j ) p ( z k + 1 = j z k = i ) p ( x k + 1 z k + 1 = j ) ( 4.4 ) \begin{aligned} p(z_{k+1}=j, z_k=i, x) &= p(z_k=i, z_{k+1}=j, x_{1:k}, x_{k+1}, x_{k+2:n}) \\ &= p(z_k=i, x_{1:k}) \cdot p(x_{k+2:n)|z_{k+1}=j}) \cdot p(z_{k+1}=j|z_{k}=i) \cdot p(x_{k+1}| z_{k+1}=j) && (4.3)\\ &= \alpha_k(z_k=i) \cdot \beta_{k+1}(z_{k+1}=j) \cdot p(z_{k+1}=j|z_{k}=i) \cdot p(x_{k+1}| z_{k+1}=j) && (4.4) \\ \end{aligned}

Note that we can find the third and the forth term from the transition probability matrix and the emission probability matrix. Again, we can calculate p ( z k + 1 , z k x ) p(z_{k+1}, z_k | x) simply by introducing a normalization term. That is,

p ( z k + 1 = s , z k = r x ) = p ( z k + 1 = s , z k = r , x ) i = 1 m j = 1 m p ( z k + 1 = j , z k = i , x ) = α k ( z k = r ) β k + 1 ( z k + 1 = s ) p ( z k + 1 = s z k = r ) p ( x k + 1 z k + 1 = s ) i = 1 m α k ( z k = i ) β k + 1 ( z k + 1 = j ) p ( z k + 1 = j z k = i ) p ( x k + 1 z k + 1 = j ) ( 4.42 ) \begin{aligned} p(z_{k+1}=s, z_k=r | x) &= \frac{p(z_{k+1}=s, z_k=r, x)}{\sum_{i=1}^{m} \sum_{j=1}^{m} p(z_{k+1}=j, z_k=i, x)} \\ &= \frac{\alpha_k(z_k=r) \cdot \beta_{k+1}(z_{k+1}=s) \cdot p(z_{k+1}=s|z_{k}=r) \cdot p(x_{k+1}| z_{k+1}=s)}{\sum_{i=1}^{m} \alpha_k(z_k=i) \cdot \beta_{k+1}(z_{k+1}=j) \cdot p(z_{k+1}=j|z_{k}=i) \cdot p(x_{k+1}| z_{k+1}=j)} && (4.42) \end{aligned}

Remark

  • We denote

    γ k ( i ) = p ( z k = i x ) = p ( z k = i , x ) p ( x ) (4.43) \gamma_k(i) = p(z_k = i | x) = \frac{p(z_k=i, x)}{p(x)} \tag{4.43}

  • We denote

    ξ k ( i , j ) = p ( z k + 1 = j , z k = i x ) = p ( z k + 1 = j , z k = i , x ) p ( x ) (4.44) \xi_k(i,j) = p(z_{k+1}=j, z_k=i | x) = \frac{p(z_{k+1}=j, z_k=i, x)}{p(x)} \tag{4.44}

Three fundamental problems of HMM

Problem 1 (Likelihood): Given an observation sequence x x and parameters θ = ( A , B , π ) \theta = (A, B, \pi) , determine the likelihood p ( x θ ) p(x|\theta) .

Problem 2 (Learning): Given an observation sequence x x , learn the parameters θ = ( A , B , π ) \theta = (A, B, \pi) .

Problem 3 (Inference): Given an observation sequence x x and parameters θ = ( A , B , π ) \theta = (A, B, \pi) , discover the best hidden state sequence z z .

Problem 1 (Likelihood)

Goal: Given an observation sequence x x and parameters θ = ( A , B , π ) \theta = (A, B, \pi) , determine the likelihood p ( x θ ) p(x|\theta) .

Naive Way:

From ( 0 ) (0) , we have already know how to compute P ( x , z θ ) P(x, z|\theta) , so we can compute p ( x θ ) p(x|\theta) by summing all possible sequence z z :

p ( x θ ) = z P ( x , z θ ) p ( z θ ) p(x|\theta) = \sum_z P(x, z|\theta) \cdot p(z|\theta)

This method is not applicable since there are m n m^n ways of combinations of sequence z z . So we introduce the following two algorithm: Forward Algorithm and Backward Algorithm.

Forward Algorithm

  • Goal: Compute p ( z k , x 1 : k ) p(z_k, x_{1:k}) , given θ = ( A , B , π ) \theta = (A, B, \pi) .

From the picture above, it’s natural to compute p ( z k , x 1 : k ) p(z_k, x_{1:k}) by dynamic programming (DP). That is, to calculate it in terms of p ( z k 1 , x 1 : k 1 ) p(z_{k-1}, x_{1:k-1}) :

p ( z k , x 1 : k ) = z k 1 p ( z k , z k 1 , x 1 : k 1 , x k ) ( 5 ) = z k 1 p ( z k 1 , x 1 : k 1 ) p ( z k , x k z k 1 , x 1 : k 1 ) ( 6 ) = z k 1 p ( z k 1 , x 1 : k 1 ) p ( z k z k 1 , x 1 : k 1 ) p ( x k z k , z k 1 , x 1 : k 1 ) ( 7 ) = z k 1 p ( z k 1 , x 1 : k 1 ) p ( z k z k 1 ) p ( x k z k ) ( 8 ) \begin{aligned} p(z_k , x_{1:k}) &= \sum_{z_{k-1}} p(z_k, z_{k-1}, x_{1:k-1}, x_k) & & (5)\\ &= \sum_{z_{k-1}} p(z_{k-1}, x_{1:k-1}) \cdot p(z_k, x_k|z_{k-1}, x_{1:k-1}) & & (6)\\ &= \sum_{z_{k-1}} p(z_{k-1}, x_{1:k-1}) \cdot p(z_k|z_{k-1}, x_{1:k-1}) \cdot p(x_k|z_k, z_{k-1}, x_{1:k-1}) & & (7)\\ &= \sum_{z_{k-1}} p(z_{k-1}, x_{1:k-1}) \cdot p(z_k|z_{k-1}) \cdot p(x_k|z_k) & & (8)\\ \end{aligned}

Ramark:

  • From ( 6 ) (6) to ( 7 ) (7) , we use the fact that p ( b , c a ) = p ( b a ) p ( c a , b ) p(b,c|a) = p(b|a) \cdot p(c|a,b) .

  • From ( 7 ) (7) to ( 8 ) (8) , we use the conditional independence, which is visualized in the picture above.

  • We denote p ( z k , x 1 : k ) p(z_k , x_{1:k}) by α k ( z k ) \alpha_k(z_k) , so

    α k ( z k ) = p ( z k , x 1 : k ) = z k 1 α k 1 ( z k 1 ) p ( z k z k 1 ) p ( x k z k ) (9) \alpha_k(z_k) = p(z_k , x_{1:k}) = \sum_{z_{k-1}} \alpha_{k-1}(z_{k-1}) \cdot p(z_k|z_{k-1}) \cdot p(x_k|z_k) \tag 9

  • In equation ( 9 ) (9) , the term p ( z k z k 1 ) p(z_k|z_{k-1}) is the transition probability from state z k 1 z_{k-1} to state z k z_{k} ; the term p ( x k z k ) p(x_k|z_k) is the emission probability of observing x k x_k given state z k z_k .

  • α 1 ( z 1 = q ) = p ( z 1 = q , x 1 ) = π q p ( x 1 z 1 = q ) \alpha_1(z_1=q) = p(z_1=q, x_1) = \pi_q \cdot p(x_1 | z_1 = q) , where p ( x 1 z 1 = q ) p(x_1 | z_1 = q) is an emmission probability.

Knowing how to compute p ( z k , x 1 : k ) p(z_k , x_{1:k}) recurssively, we have

p ( x θ ) = p ( x 1 : n θ ) = z n p ( z n , x 1 : n ) = z n α n ( z n ) = q = 1 m α n ( z n = q ) p(x|\theta) = p(x_{1:n}|\theta) = \sum_{z_n} p(z_n, x_{1:n}) = \sum_{z_n} \alpha_n(z_n) = \sum_{q=1}^{m} \alpha_n(z_n=q)

Backward Algorithm

  • Goal: Compute p ( x k + 1 : n z k ) p(x_{k+1:n} | z_k) , given θ = ( A , B , π ) \theta = (A, B, \pi) .

Again, we are going to use DP to compute p ( x k + 1 : n z k ) p(x_{k+1:n} | z_k) in terms of p ( x k + 2 : n z k + 1 ) p(x_{k+2:n} | z_{k+1}) :

p ( x k + 1 : n z k ) = z k + 1 p ( x k + 1 , x k + 2 : n , z k + 1 z k ) = z k + 1 p ( x k + 2 : n , z k + 1 z k ) p ( x k + 1 z k , x k + 2 : n , z k + 1 ) = z k + 1 p ( z k + 1 z k ) p ( x k + 2 : n z k + 1 , z k ) p ( x k + 1 z k , x k + 2 : n , z k + 1 ) ( 10 ) = z k + 1 p ( x k + 2 : n z k + 1 ) p ( z k + 1 z k ) p ( x k + 1 z k + 1 ) ( 11 ) \begin{aligned} p(x_{k+1:n} | z_k) &= \sum_{z_{k+1}} p(x_{k+1}, x_{k+2:n}, z_{k+1} | z_k) \\ &= \sum_{z_{k+1}} p(x_{k+2:n}, z_{k+1}| z_k) \cdot p(x_{k+1}| z_k, x_{k+2:n}, z_{k+1}) \\ &= \sum_{z_{k+1}} p(z_{k+1}|z_k) \cdot p(x_{k+2:n}|z_{k+1}, z_k) \cdot p(x_{k+1} | z_k, x_{k+2:n}, z_{k+1}) && (10)\\ &= \sum_{z_{k+1}} p(x_{k+2:n}|z_{k+1}) \cdot p(z_{k+1}|z_k) \cdot p(x_{k+1}|z_{k+1}) && (11)\\ \end{aligned}

Ramark:

  • From ( 10 ) (10) to ( 11 ) (11) , we use the conditional independece similar to the one in forward algorithm.

  • We denote p ( x k + 1 : n z k ) p(x_{k+1:n} | z_k) by β k ( z k ) \beta_k(z_k) , so

    β k ( z k ) = p ( x k + 1 : n z k ) = z k + 1 p ( x k + 2 : n z k + 1 ) p ( z k + 1 z k ) p ( x k + 1 z k + 1 ) (12) \beta_k(z_k) = p(x_{k+1:n} | z_k) = \sum_{z_{k+1}} p(x_{k+2:n}|z_{k+1}) \cdot p(z_{k+1}|z_k) \cdot p(x_{k+1}|z_{k+1}) \tag {12}

  • In equation ( 12 ) (12) , the term p ( z k + 1 z k ) p(z_{k+1}|z_k) is the transition probability from state z k z_{k} to state z k + 1 z_{k+1} ; the term p ( x k + 1 z k + 1 ) p(x_{k+1}|z_{k+1}) is the emission probability of observing x k + 1 x_{k+1} given state z k + 1 z_{k+1} .

  • β n ( z n ) = 1 \beta_n(z_n) = 1 .

Knowing how to compute p ( x k + 1 : n z k ) p(x_{k+1:n} | z_k) recursively, we have

p ( x θ ) = z 1 p ( x , z 1 ) = z 1 p ( x z 1 ) p ( z 1 ) = z 1 p ( x 1 , x 1 + 1 : n z 1 ) p ( z 1 ) = z 1 p ( x 1 z 1 ) p ( x 1 + 1 : n z 1 , x 1 ) p ( z 1 ) ( 13 ) = z 1 p ( x 1 z 1 ) p ( x 1 + 1 : n z 1 ) p ( z 1 ) ( 14 ) = z 1 p ( x 1 z 1 ) β 1 ( z 1 ) p ( z 1 ) = q = 1 m β 1 ( z 1 = q ) p ( x 1 z 1 = q ) π q \begin{aligned} p(x|\theta) &= \sum_{z_1} p(x, z_1) = \sum_{z_1} p(x | z_1) \cdot p(z_1) \\ &= \sum_{z_1} p(x_1, x_{1+1:n} | z_1) \cdot p(z_1) \\ &= \sum_{z_1} p(x_1 | z_1) \cdot p(x_{1+1:n}|z_1, x_1) \cdot p(z_1) &&(13)\\ &= \sum_{z_1} p(x_1 | z_1) \cdot p(x_{1+1:n}|z_1) \cdot p(z_1) &&(14)\\ &= \sum_{z_1} p(x_1 | z_1) \cdot \beta_1(z_1) \cdot p(z_1) \\ &= \sum_{q=1}^m \beta_1(z_1=q) \cdot p(x_1 | z_1=q) \cdot \pi_q \end{aligned}

From ( 13 ) (13) to ( 14 ) (14) , we use the conditional independence. To make it clean, I didn’t include θ \theta in the above derivation, but keep in mind x x is conditioned on θ \theta .

Problem 2 (Learning)

Goal: Given an observation sequence x x , learn the parameters θ = ( A , B , π ) \theta = (A, B, \pi) .

Given that the hidden states are unknown, it’s natural to use the EM Algorithm to solve parameters. Remind that the EM Algorithm consists of two steps:

  • An expectation (E) step, which creates a function Q ( θ , θ i ) Q(\theta, \theta_i) for the expectation of the log-likelihood log p ( x , z θ ) \log p(x,z|\theta) evaluated using the current conditional distribution of z z given x x and the current estimate of the parameters θ i \theta_i , where

Q ( θ , θ i ) = E z P ( z x , θ i ) [ log p ( x , z θ ) ] = z P ( z x , θ i ) log p ( x , z θ ) \begin{aligned} Q(\theta, \theta_i) &= E_{z \sim P(z|x,\theta_i)}[\log p(x,z|\theta)] \\ &= \sum_z P(z|x,\theta_i) \cdot \log p(x,z|\theta) \\ \end{aligned}

  • A maximization (M) step, which computes parameters maximizing the expected log-likelihood Q ( θ , θ i ) Q(\theta, \theta_i) found on the E E step and then update parameters to θ i + 1 \theta_{i+1} .

We fist initialize parameters θ 0 = ( A 0 , B 0 , π 0 ) \theta_0 = (A_0, B_0, \pi_0)

E Step:

We are going to construct Q ( θ , θ i ) Q(\theta, \theta_i) .

Q ( θ , θ i ) = E z P ( z x , θ i ) [ log p ( x , z θ ) ] = z P ( z x , θ i ) log p ( x , z θ ) = log p ( x , z θ ) p ( x , z θ i ) p ( x θ i ) ( 15 ) log p ( x , z θ ) p ( x , z θ i ) ( 16 ) \begin{aligned} Q(\theta, \theta_i) &= E_{z \sim P(z|x,\theta_i)}[\log p(x,z|\theta)] \\ &= \sum_z P(z|x,\theta_i) \cdot \log p(x,z|\theta) \\ &= \log p(x,z|\theta) \cdot \frac{p(x,z|\theta_i)}{p(x|\theta_i)} &&(15)\\ &\propto \log p(x,z|\theta) \cdot p(x,z|\theta_i) &&(16)\\ \end{aligned}

Since we know x x and θ i \theta_i , p ( x θ i ) p(x|\theta_i) is a constant and therefore we can write from ( 15 ) (15) to ( 16 ) (16) . In the earlier section 「Settings of the Hidden Markov Model」 of the post, we deduce that

P ( x , z θ ) = p ( z 1 ) [ p ( z 2 z 1 ) p ( z 3 z 2 ) . . . ( z n z n 1 ) ] [ p ( x 1 z 1 ) p ( x 2 z 2 ) . . . p ( x n z n ) ] P(x, z|\theta) = p(z_1) \cdot [p(z_2|z_1)\cdot p(z_3|z_2)\cdot ... \cdotp(z_n|z_{n-1})] \cdot [p(x_1|z_1)\cdot p(x_2|z_2)\cdot ... \cdot p(x_n|z_n)]

So we can formulate Q ( θ , θ i ) Q(\theta, \theta_i) as

Q ( θ , θ i ) = z ( log π z i + t = 1 n 1 log p ( z t + 1 z t ) + t = 1 n log p ( x n z n ) ) p ( x , z θ i ) = z log π z i p ( x , z θ i ) + z t = 1 n 1 log p ( z t + 1 z t ) p ( x , z θ i ) + z t = 1 n log p ( x t z t ) p ( x , z θ i ) \begin{aligned} Q(\theta, \theta_i) &= \sum_z \left( \log \pi_{z_i} + \sum_{t=1}^{n-1} \log p(z_{t+1}|z_t) + \sum_{t=1}^{n} \log p(x_n|z_n)\right) \cdot p(x,z|\theta_i) \\ &= \sum_z \log \pi_{z_i} \cdot p(x,z|\theta_i) + \sum_z \sum_{t=1}^{n-1} \log p(z_{t+1}|z_t) \cdot p(x,z|\theta_i) + \sum_z \sum_{t=1}^{n} \log p(x_t|z_t) \cdot p(x,z|\theta_i) \\ \end{aligned}

M Step:

We are going to maximize Q ( θ , θ i ) Q(\theta, \theta_i) and update θ i + 1 \theta_{i+1} .

Note that we write Q ( θ , θ i ) Q(\theta, \theta_i) as the sum of three terms. The first therm is related to π \pi , the second term is related to A A , and the third term is related to B B . Therefore we can maximize each term separately.

We can write the first term as

z log π z i p ( x , z θ i ) = j = 1 m log π j p ( x , z 1 = j θ i ) \sum_z \log \pi_{z_i} \cdot p(x,z|\theta_i) = \sum_{j=1}^m \log \pi_j \cdot p(x, z_1 = j|\theta_i)

under the constraint j = 1 m π j = 1 \sum_{j=1}^m \pi_j = 1 . Clearly, this is a convex optimization problem:

The Lagrangian L L associated with the problem is

L ( π , v ) = j = 1 m log π j p ( x , z 1 = j θ i ) + v ( j = 1 m π j 1 ) L(\pi, v) = \sum_{j=1}^m \log \pi_j \cdot p(x, z_1 = j|\theta_i) + v \cdot (\sum_{j=1}^m \pi_j - 1)

Note that any pair of primal and dual optimal points must satisfy the KKT conditions. So we use one KKT property that the gradient must vanish at the optimal point to find π \pi . This might not be the optimal π \pi since 「any pair of primal and dual optimal points must satisfy the KKT conditions」 doesn’t imply that a point satisfying the KKT conditions is the optimal.

L π j = p ( x , z 1 = j θ i ) 1 π j + v = 0 p ( x , z 1 = j θ i ) + v π j = 0 π j = p ( x , z 1 = j θ i ) v ( 17 ) \begin{aligned} \frac{\partial L}{\partial \pi_j} = p(x, z_1=j|\theta_i) \cdot \frac{1}{\pi_j} + v & = 0 \\ p(x, z_1=j|\theta_i) + v \cdot \pi_j & = 0 \\ \pi_j & = \frac{-p(x, z_1=j|\theta_i)}{v} & & (17)\\ \end{aligned}

By setting L π j = 0 \frac{\partial L}{\partial \pi_j} = 0 for all j j , we have

j = 1 m p ( x , z 1 = j θ i ) + v j = 1 m π j = 0 p ( x θ i ) + v = 0 v = p ( x θ i ) ( 18 ) \begin{aligned} \sum_{j=1}^m p(x, z_1=j|\theta_i) + v \cdot \sum_{j=1}^m \pi_j &= 0 \\ p(x|\theta_i) + v &= 0\\ v &= - p(x|\theta_i) && (18)\\ \end{aligned}

By plugging ( 18 ) (18) into ( 17 ) (17) , we have

π j = p ( x , z 1 = j θ i ) v = p ( x , z 1 = j θ i ) p ( x θ i ) = γ 1 ( j ) \pi_j = \frac{-p(x, z_1=j|\theta_i)}{v} = \frac{p(x, z_1=j|\theta_i)}{p(x|\theta_i)} = \gamma_1(j) .

In the similar way, we can write the second term as

z t = 1 n 1 log p ( z t + 1 z t ) p ( x , z θ i ) = j = 1 m k = 1 m t = 1 n 1 log p ( z t + 1 = k z t = j ) p ( x , z t = j , z t + 1 = k θ i ) z t = 1 n log p ( x t z t ) p ( x , z θ i ) = j = 1 m t = 1 n log p ( x t z t = j ) p ( x , z t = j θ i ) \sum_z \sum_{t=1}^{n-1} \log p(z_{t+1}|z_t) \cdot p(x,z|\theta_i) = \sum_{j=1}^m \sum_{k=1}^m \sum_{t=1}^{n-1} \log p(z_{t+1}=k|z_t=j) \cdot p(x,z_t=j, z_{t+1}=k|\theta_i) \\ \sum_z \sum_{t=1}^{n} \log p(x_t|z_t) \cdot p(x,z|\theta_i) = \sum_{j=1}^m \sum_{t=1}^n \log p(x_t|z_t=j) \cdot p(x,z_t=j|\theta_i)

with seperate constraints

k = 1 m p ( z t + 1 = k z t = j ) = 1 , j = 1 m p ( x t z t = j ) = 1 \sum_{k=1}^m p(z_{t+1}=k|z_t=j) = 1, \\ \sum_{j=1}^m p(x_t|z_t=j) = 1

We can solve for optimal parameters similar to solving for π \pi . After we set the gradient of corresponding Lagrangian to 0 0 , we have

A j k = p ( z t + 1 = k z t = j ) = t = 1 n 1 p ( x , z t = j , z t + 1 = k θ i ) t = 1 n 1 p ( x , z t = j θ i ) = t = 1 n 1 p ( x , z t = j , z t + 1 = k θ i ) / p ( x θ i ) t = 1 n 1 p ( x , z t = j θ i ) / p ( x θ i ) = t = 1 n 1 ξ t ( j k ) t = 1 n 1 γ t ( j ) ( 19 ) B j k = p ( x t = V k z t = j ) = t = 1 n 1 p ( x , z t = j θ i ) I ( x t = V k ) t = 1 n 1 p ( x , z t = j θ i ) = t = 1 n 1 p ( x , z t = j θ i ) / p ( x θ i ) I ( x t = V k ) t = 1 n 1 p ( x , z t = j θ i ) / p ( x θ i ) = t = 1 n 1 γ t ( j ) I ( x t = V k ) t = 1 n 1 γ t ( j ) ( 20 ) \begin{aligned} A_{jk} &= p(z_{t+1}=k|z_t=j) \\ &= \frac{\sum_{t=1}^{n-1} p(x, z_t=j, z_{t+1}=k|\theta_i)}{\sum_{t=1}^{n-1} p(x, z_t=j|\theta_i)} \\ &= \frac{\sum_{t=1}^{n-1} p(x, z_t=j, z_{t+1}=k|\theta_i)/ p(x|\theta_i)}{\sum_{t=1}^{n-1} p(x, z_t=j|\theta_i)/p(x|\theta_i)} \\ &= \frac{\sum_{t=1}^{n-1} \xi_t(jk)}{\sum_{t=1}^{n-1} \gamma_t(j)} &&(19)\\\\ B_{jk} &= p(x_t= V_k|z_t=j) \\ &= \frac{\sum_{t=1}^{n-1} p(x, z_t=j|\theta_i)\cdot I(x_t= V_k)}{\sum_{t=1}^{n-1} p(x, z_t=j | \theta_i)} \\ &= \frac{\sum_{t=1}^{n-1} p(x, z_t=j|\theta_i)/ p(x|\theta_i) \cdot I(x_t= V_k)}{\sum_{t=1}^{n-1} p(x, z_t=j | \theta_i)/ p(x|\theta_i)} \\ &= \frac{\sum_{t=1}^{n-1} \gamma_t(j) \cdot I(x_t=V_k)}{\sum_{t=1}^{n-1} \gamma_t(j)} &&(20)\\ \end{aligned}

Remark: I ( x t = V k ) I(x_t= V_k) is an indicator function. If x t = V k x_t= V_k , then I ( x t = V k ) = 1 I(x_t= V_k) = 1 , and 0 0 otherwise. We get the results ( 19 ) (19) and ( 20 ) (20) from ( 4.43 ) (4.43) and ( 4.44 ) (4.44) .

We also call this algorithm Baum-Welch algorithm.

Problem 3 (Inference)

Goal: Given an observation sequence x x and parameters θ = ( A , B , π ) \theta = (A, B, \pi) , discover the best hidden state sequence z z .

Method 1:Brute force.

This is not applicable. Every hidden state has m m choices and the sequence has length n n . So there are m n m^n possible combinations.

Method 2: Use Forward/ Backward Algorithm.
Given a sequence x x , we know how to compute p ( z k x ) p(z_k | x) <

相關文章
相關標籤/搜索