--forward Algorithm for learning small memory in hidden Markov model

Source: Internet
Author: User

The first is the Python implementation of the forward algorithm:

#-*-coding:utf-8-*-__author__='Zhanghe'defforward (n,m,a,b,p,observed): P= 0.0#number of States observedLEN =Len (observed)#Intermediate probability len*mQ = [([0]*n) forIinchrange (LEN)]#The first observed state, the initial probability of the state multiplied by the condition probability of the hidden state to the observed state.      forJinchRange (N): Q[0][j]= p[j]*B[j][observation.index (observed[0])#after the first state, first from each state of the previous day, the probability of transfer to the current state summation, and then multiply the hidden state to observe the condition probability of the state.      forIinchRange (1, LEN): forJinchRange (N): Sum= 0.0 forKinchRange (N): Sum+ = q[i-1][k]*A[k][j] Q[i][j]= Sum *B[j][observation.index (Observed[i]) forIinchRange (N): P+ = Q[len-1][i]returnP#3 hidden layer states: Sun Cloud RainHidden =[]hidden.append ('Sun') Hidden.append ('Cloud') Hidden.append ('Rain') N=len (hidden)#4 Observation Layer states: dry dryish damp soggyobservation =[]observation.append ('Dry') Observation.append ('Damp') Observation.append ('Soggy') M=Len (observation)#Initial state matrix (1*n first day is the probability of Sun,cloud,rain)P = (0.3,0.3,0.4)#State Transition matrix A (probability of transition between n*n hidden layer states)A= (0.2,0.3,0.5), (0.1,0.5,0.4), (0.6,0.1,0.3))#confusion Matrix B (probability of the observed layer state corresponding to the N*m hidden layer State)B= (0.1,0.5,0.4), (0.2,0.4,0.4), (0.3,0.6,0.1))#assuming that a set of sequences is observed as observed, the output HMM model (N,M,A,B,P) produces the probability of observing the observed of the sequenceobserved = ['Dry']Printforward (n,m,a,b,p,observed) observed= ['Damp']Printforward (n,m,a,b,p,observed) observed= ['Dry','Damp']Printforward (n,m,a,b,p,observed) observed= ['Dry','Damp','Soggy']PrintForward (n,m,a,b,p,observed)

Output Result:

0.21
0.51
0.1074
0.030162

The first two results are the same as those calculated by hand;

The latter two results are not calculated manually, but you step through the code during the debugger, running the same process as the manual calculation process.

--forward Algorithm for learning small memory in hidden Markov model

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.