Application of Hidden Markov model and Viterbi algorithm

Source: Internet
Author: User

Jostree Reprint Please specify the source http://www.cnblogs.com/jostree/p/4335810.html

An example:

Wei Xiaobao use dice to play, he has two kinds of dice a normal dice, there is a kind of uneven dice, to carry out thousands.

He had a 2/5 chance of being a player when he started the game.

For normal dice A, the probability of each point appearing is 1/6.

For non-uniform dice b,5,6 Two probability of occurrence is 3/10, the remaining is 1/10.

The random law of the thousand is as follows:

We observed the result of throwing: ob={1,3,4,5,5,6,6,3,2,6}

Please tell me when Wei Xiaobao is out.

We can model this $x_i$ represents the type of dice $i$ to throw, $y _i$ represents the number of points $i$ thrown, $\lambda$ represents each probability parameter.

Then the probability of $t$ using the $i$ dice $\delta_t (i) $ equals

\begin{equation} \delta_t (i) =\max_{x_1,\dots,x_{t-1}}p (X_1,\DOTS,X_{T-1},X_T=I,Y_1,\DOTS,Y_T|\LAMBDA) \end{ Equation}

In fact $\delta_{t+1} (i) $ can be torn down by $\delta_t (i) $:

\begin{eqnarray} \delta_{t+1} (i) &=& \max_{x_1,\dots,x_{t}}p (x_1,\dots,x_{t},x_{t+1}=i,y_1,\dots,y_{t+1}| \LAMBDA) \ \ &=& \max_j \delta_t (j) \alpha_{ji}\beta_i (Y_{t+1}) \end{eqnarray}

where $\alpha_{ji}$ represents the probability of transferring from the $j$ dice to the $i$ dice.

$\beta_i (Y_{t+1}) $ indicates the probability of casting a point $y_{t+1}$ using the I-dice.

This allows the use of dynamic programming algorithms for successive recursive computations.

The results are as follows:

T $y _t$ $\delta_t (A) $ $\psi_t (A) $ $\delta_t (B) $ $\psi_t (B) $
1 1 0.1 A 0.04 A
2 3 0.0133333 A 0.0036 B
3 4 0.00177778 A 0.000324 B
4 5 0.000237037 A 0.000106667 A
5 5 3.16049e-05 A 2.88e-05 B
6 6 4.21399e-06 A 7.776e-06 B
7 6 5.61866e-07 A 2.09952e-06 B
8 3 7.49154e-08 A 1.88957e-07 B
9 2 9.98872e-09 A 1.70061e-08 B
10 6 1.33183e-09 A 4.59165e-09 B


Thus from $\psi_t (A) $ and $\psi_t (B) $ select $\delta_t () $ larger dice, thus predicting the type of dice sequence for: aaaaabbbbb

The code looks like this:

1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4#include <string>5#include <iostream>6 using namespacestd;7 Doubleinitp[2] = {0.6,0.4};//the initial probability of the dice A, b8 Doubletransfermatrix[2][2] = {{0.8,0.2}, {0.1,0.9}};//probability of transfer between dice9 Doubleemissionp[2][6]={{1/6.0,1/6.0,1/6.0,1/6.0,1/6.0,1/6.0},//the probability of firing the dice aTen{0.1,0.1,0.1,0.1,0.3,0.3}};//probability of firing of dice B One Doubledp[Ten][2];//Dp[i][j] Step i, the maximum probability of using the J Dice A Doubledps[Ten][2];//Dps[i][j] step I, when using the J dice, get the maximum probability when using the dice type, 0->a, 1->b - intob[Ten] = {1,3,4,5,5,6,6,3,2,6};//observed points - BOOLdicearray[Ten];//predicting dice use sequences the voidViterbi () - { -Memset (DP,0,sizeof(DP)); -memset (DpS,0,sizeof(DpS)); +memset (Dicearray,0,sizeof(Dicearray)); -dp[0][0] = initp[0]* emissionp[0][ob[0]-1]; +dp[0][1] = initp[1]* emissionp[1][ob[0]-1]; A      for(inti =1; I <Ten; i++)//number of throws at     { -          for(intj =0; J <2; J + +)//Current status -         { -              for(intK =0; K <2; k++)//Previous State -             { -                 DoubleTEMPP = dp[i-1][K] * TRANSFERMATRIX[K][J] * emissionp[j][ob[i]-1] ; in                 if(Dp[i][j] <tempp) -                 { toDP[I][J] =tempp; +DPS[I][J] =K; -                 } the             } *         } $         if(dp[i][0] < dp[i][1] )Panax Notoginseng         { -Dicearray[i] = dps[i][1]; the         } +         Else A         { theDicearray[i] = dps[i][0]; +         } -     } $ } $ intMainintargcChar*argv[]) - { - Viterbi (); thecout<<"probabilities and dice in each state of each step:"<<Endl; -      for(inti =0; I <Ten; i++ )Wuyi     { the          for(intj =0; J <2; J + + ) -         { Wucout<<dp[i][j]<<" "<<dpS[i][j]<<"    "; -         } Aboutcout<<Endl; $     } -cout<<"predict the type of dice, 0->a, 1->b:"<<Endl; -      for(inti =0; I <Ten; i++ ) -     { Acout<<dicearray[i]<<" "; +     } thecout<<Endl; -}
View Code

Hidden Markov model and application of Viterbi algorithm

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.