HMM Forward Backward algorithm

Source: Internet
Author: User
Tags emit

Recently studied NLP quite interested, but because of relatively lazy, so had to find someone else on the Internet better blog, back up, but also convenient to find their own later (in fact, generally will not look back, hey-_-!!)

The code has been re-written again, so it doesn't post the original code.

1. Forward algorithm (excerpt from http://www.cnblogs.com/kaituorensheng/archive/2012/12/01/2797230.html)

The evaluation problem of the implicit Markov type is that, in the known o=o1o2 of an observation sequence ... OT, and Model μ= (a,b,π}, the probability of observing the sequence O, i.e. P (o|μ}

If you exhaust all the state combinations, i.e. s1s1 ... S1, S1S1 ... S2, S1S1 ... S3, ..., s3s3 ... S3. In this case, T1 time has n states, T2 time has n states ... tt moment has n states, so there are n*n*...*n= NT kinds of combinations, time complexity is O (NT), when the calculation, there will be "exponential explosion", when T is very large, can not calculate this value. In order to solve this problem, Baum proposes a forward algorithm.

Induction Process

First, the forward variable αt (i) is introduced: at time t, the HMM output sequence is o1o2 ... OT, the probability of being in the state Si at the moment of T .

When t=1, the output sequence is O1, at this time the probability of calculation is P (o1|μ): Suppose there are three states (such as) 1, 2, 3, the output sequence is O1, there are three possible one is the status of 1 issued, the second is issued from the state 2, three is issued from the State 3. In addition, from the state 1 to emit the observed value O1 probability is B1 (O1), from the state 2 to emit the observed value O1 the probability of B2 (O1), from the state 3 to emit the observed value O1 probability of B3 (O1). So we can figure out

P (o1|μ) =π1*b1 (O1) +π2*b2 (O1) +π3*b3 (O1) = α1 (1) + α1 (2) + α1 (3)

When t=2, the output sequence is O1o2, at this time the probability of calculation is P (o1o2|μ): Suppose there are three states (such as) 1, 2, 3, the output sequence is O1, there are three possible one is the status of 1 issued, the second is issued from the state 2, three is issued from the State 3. In addition, from the state 1 to emit the observed value O2 probability is B1 (O2), from the state 2 to emit the observed value O2 the probability of B2 (O2), from the state 3 to emit the observed value O2 probability of B3 (O2).

If the observation value from the State 1 O2, may be from the first moment of the 1, 2 or 3 of the state loaded, if the transition from the state 1, the probability is α1 (1)*a11*b1 (O2), if the transition from the state 2, the probability is α1 (2)*a21*b1 ( O2), if converted from State 3, the probability is α1 (3)*a31*b1 (O2), so

P (o1o2,q2=s1|μ) = α1 (1)*a11*b1 (O2) + α1 (2)*a21*b1 (O2) + α1 (3)*a31*b1 (O2) =α2 (1)

similarly: P (o1o2,q2=s1|μ) =α1 (1) *a12*b2 (O2) +α1 (2) *a22*b2 (O2) +α1 (3) *a32*b2 (O2) =α2 (2)

P (o1o2,q2=s1|μ) =α1 (1) *a13*b1 (O2) +α1 (2) *a23*b3 (O2) +α1 (3) *a33*b3 (O2) =α2 (3)

So: P (o1o2|μ) =p (o1o2,q2=s1|μ) + P (o1o2,q2=s1|μ) + P (o1o2,q2=s1|μ)

=α2 (1) +α2 (2) +α2 (3)

etc...

Forward Algorithm

Step1 initialization: α1 (i) =πi*bi (O1), 1≤i≤n

Step2 inductive calculation:

Step3 End:

P (o|μ) =

Complexity of Time

The forward variable that calculates a state at a certain point needs to look at the N states of the previous moment, when the time complexity is O (n), there are N states at each moment, the time complexity is N*o (N) =o (N2), and the t moment, so the time complexity is T*o (N2) =o (N2T).

Program Illustration

The Forward algorithm calculates P (o| M):

Step1:α1 (1) =π1*b1 (red) =0.2*0.5=0.1α1 (2) =π2*b2 (red) ==0.4*0.4= 0.16α1 (3) =π3*b3 (red) ==0.4*0.7=0.21

Step2:α2 (1) =α1 (1) *a11*b1 (white) +α1 (2) *a21*b1 (white) +α1 (3) *a31*b1 (white)

...

Step3:p (o| M) =α3 (1) +α3 (2) +α3 (3)

2. Post-algorithm (excerpt from http://www.cnblogs.com/kaituorensheng/archive/2012/12/03/2800489.html)

For hmm evaluation problem, the forward variable can be calculated by using the forward algorithm from the front to the back, and the back-to-forward variable can be calculated from the back to the front by use of the dynamic programming.

We introduce the back variable βt (i): Given the model μ= (a,b,π), and at the time the T state is Si , the output sequence is the probability of ot+1ot+2...ot, i.e.

βt (i) =p (ot+1ot+2...ot|qt=si,μ)

Induction process

Suppose there are still 3 states

when t=t, by definition: time t State qt output for ot+1 ... the probability that the output from the t+1 is not present (because the T moment is the terminating termination state), that is, after T is empty, is an inevitable event, so Βt ( i) =1,1≤1≤n

When T=t-1,

βt-1 (i) =p (ot|qt-1=si,μ) = AI1*B1 (OT) *βt (1) + AI2*B2 (OT) *βt (2) + ai3*b3 (OT) *βt (3)

......

When T=1,

β1 (1) =p (O2o3 ... ot|q2=s1,μ) = A11*B1 (O2) *β2 (1) + A12*B2 (O2) *β2 (2) + A13*B3 (O2) *β2 (3)

β1 (2) =p (O2o3 ... ot|q2=s1,μ) = A21*B1 (O2) *β2 (1) + A22*B2 (O2) *β2 (2) + A23*B3 (O2) *β2 (3)

β1 (3) =p (O2o3 ... ot|q2=s1,μ) = A31*B1 (O2) *β2 (1) + A32*B2 (O2) *β2 (2) + A33*B3 (O2) *β2 (3)

P (O1o2 ... ot|μ) =

=

=

The back algorithm

Step1 initialization: βt (i) = 1, 1≤1≤n

Step2 inductive calculation:

1≤t≤t-1, 1≤i≤n

Step3 the end and:

P (o|μ) =

Complexity of Time

To calculate a time in a state of the back variable needs to look at the next moment of N state, at this time the complexity of O (n), each moment has N states, at this time the complexity of N*o (N) =o (N2), and the T-moment, so the time complexity of T*o (N2) =o (N2T).

Program illustration

The back algorithm

Calculate P (o| M):

Step1:β4 (1) = 1β4 (2) = 1β4 (3) = 1

Step2:β3 (1) =β4 (1) *a11*b1 (white) +β4 (2) *a12*b2 (white) +β4 (3) *a13*b3 (white)

...

Step3:p (o| M) =π1*β1 (1) *b1 (O1) +π2*β1 (2) *b2 (O1) +π3*β1 (3) *b3 (O1)

3. Forward-back algorithm (excerpt from http://www.cnblogs.com/kaituorensheng/archive/2012/12/05/2803182.html)

Re-review:

Forward variable αt (i): At the moment T, under the condition of the known model μ= (a,b,π), the state is in SI, the output sequence is O102 ... Ot, forward variable is αt (i)

Back variable βt (i): at moment T, the output sequence is ot+1ot+2...ot and the back variable is βt (i) under the condition that the known model μ= (a,b,π) and the state is in SI

Formula derivation:

P (o,qt=si|μ) = P (O1o2 ... OT, qt=si|μ)

=p (O1o2 ... Ot, qt=si,ot+1ot+2...ot|μ)

=p (O1o2 ... Ot, qt=si|μ) * P (ot+1ot+2...ot| O1o2 ... Ot, qt=si,μ)

=p (O1o2 ... Ot, qt=si|μ) * P (ot+1ot+2...ot|qt=si,μ)

=αt (i) *βt (i)

P (o|μ) =

Case Analysis:

Analysis:

P (q4=s3| O,M) = P (Q4=S3, o| M)/P (o| M

= P (o,q4=s3| M)/P (o| M

=α4 (3) *β4 (3)/

HMM Forward Backward Algorithm (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.