What is hidden Markov model? Hidden Markov models are extended based on Markov chains and can be used to solve three types of problems: Estimation, decoding, and learning. Among them, the famous Viterbi is used in the decoding problem.AlgorithmThis algorithm can be used for part-of-speech tagging in Chinese word segmentation algorithms. If you are not clear about the concept of this algorithm, please refer to the relevant documentation.
The Viterbi algorithm is simple only for algorithms, blocking most developers from knowledge about probability statistics. Today, we mainly provide a description of the Viterbi Algorithm Based on C language for beginners. Some of them can be optimized, but this instance is sufficient to demonstrate the Viterbi algorithm. The naming rules are not standardized. Please forgive me.
//Three statuses# DefineSdim 3EnumS {ONE, TWO, Thr };//Two outputs# DefineVdim 2EnumV {h, t };
The S type is used to describe the state in HMM (Hidden Markov Model). There are three States: one, two, and Thr (which can be understood as three coins ); the V type is used to describe the possible output results H and V in each State (each coin has two output results: H (Front) and T (back )).
// Status transition matrix Double A [sdim] [sdim]; // Probability of output v in State S Double B [vdim] [sdim]; Void Init () {A [one] [one] = 0.9 ; A [one] [two] = 0.05 ; // The probability of switching from one state to two state is 0.05. A [one] [thr] = 0.05 ; A [two] [one] = 0.45 ; A [two] [two] = 0.1 ; A [two] [thr] = 0.45 ; A [thr] [one] = 0.45 ; A [thr] [two] = 0.45 ; A [thr] [thr] = 0.1 ; B [H] [one] = 0.5 ;// In state one, the probability of output H is 0.5. B [H] [two] = 0.75 ; B [H] [thr] = 0.25 ; B [T] [one] = 0.5 ; B [T] [two] = 0.25 ; B [T] [thr] = 0.75 ;}
Matrix A describes the probability of switching from one state to another. B matrix is used to describe the probability of output results in State S. The init function initializes the two matrices to complete hmm initialization.
// CT: current stage; s: Target status. // Obtains the maximum probability of transition from the current phase to the Target S state. // Returns the state of the maximum probability value and the maximum probability value. Double Getvalue ( Double ** Ppvalues, S, Int CT, S * PRE ){ Double * Pvalues = ( Double *) Calloc (sdim, Sizeof ( Double )); For ( Int I = 0 ; I <sdim; I ++ ) {Pvalues [I] = Ppvalues [cT] [I] * A [I] [s];} Double Value = pvalues [ 0 ]; Int N = 0 ; For ( Int I = 1 ; I <sdim; I ++){ If (Pvalues [I]> Value) {Value = Pvalues [I]; n = I ;}} free (pvalues ); * Pre = (S) N; Return Value;} s * Viterbi (V * Seq, size_t Len ){ // Used to save the state transition path (LEN column, sdim row) S ** GMM = (s **) malloc (LEN * Sizeof (S *)); For ( Int I = 0 ; I <Len; I ++ ) {GMM [I] = (S *) calloc (sdim, Sizeof (S ));} // Used to save the maximum probability value of each stage (LEN column, sdim row) Double ** Ppvalues = ( Double **) Calloc (Len, Sizeof ( Double *)); For ( Int I = 0 ; I <Len; I ++ ) {Ppvalues [I] = ( Double *) Calloc (sdim, Sizeof ( Double ));} // T is used to describe the stage For ( Int T = 0 ; T <Len; t ++) {Printf ( " % D \ n " , T ); If (T = 0 ){ For ( Int I = 0 ; I <sdim; I ++ ) {Ppvalues [T] [I] = B [seq [T] [I]/ 3 ; // In stage 1, the probability of each State is 0th (a prior probability) }} Else { For ( Int I = 0 ; I <sdim; I ++ ) {S ARG; Double Value = getvalue (ppvalues, (s) I, T- 1 ,& Arg); GMM [T] [I] =ARG; ppvalues [T] [I] = Value * B [seq [T] [I] ;}} // Obtains the final maximum probability value and the obtained State. Double Value = ppvalues [Len- 1 ] [ 0 ]; Int N = 0 ; For ( Int I = 1 ; I <sdim; I ++ ){ If (Ppvalues [Len- 1 ] [I]> Value) {Value = Ppvalues [Len- 1 ] [I]; n = I ;}} printf ( " \ N % F \ t % d \ n " , Value, n); s * Res = (S *) calloc (Len, Sizeof (S )); For ( Int I = 1 ; I <Len; I ++ ) {Res [I - 1 ] = GMM [I] [N];} res [Len - 1 ] = (S) N; For ( Int I = 0 ; I <Len; I ++ ) {Free (bpsv[ I]); free (ppvalues [I]);} Free (bpsv); free (ppvalues ); Return Res ;}
AboveCodeIs the key code of the Viterbi algorithm. Viterbi function input observation sequence, the output is most likely to output the state conversion sequence of the observation sequence. The following is the test code.
// Given the output result sequence, obtain the State Transfer sequence most likely to generate the sequence Int Main ( Void ) {V O [ 6 ]; // Output result Sequence O [ 0 ] = H; O [ 1 ] =H; O [ 2 ] = T; O [ 3 ] = H; O [ 4 ] = T; O [ 5 ] = T; Init (); Int Len = 3 ; S * Res = Viterbi (O, Len); printf ( " \ Shortterbi result: " ); For ( Int I = 0 ; I <Len; I ++ ) {Printf ( " % D " , Res [I]);} Free (RES ); Return 0 ;}
Development Environment: G ++ 4.6.3.
(End)