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