Viterbi algorithm (VITERBI)-Walkthrough (Brute Force + code implementation)

Source: Internet
Author: User

1. Introduction

Viterbi algorithm is a general-purpose dynamic programming algorithm for shortest path, and it can be used for many other problems, such as text mining and word segmentation theory. Since it is a dynamic programming algorithm, it is necessary to find the appropriate local state, as well as the recursive formula of the local state. In Hmm, the Viterbi algorithm defines two local states for recursion.

The first local state is the maximum probability in the time I hide state for I all possible state transfer path i1,i2.......it

The second local state is recursively obtained by the first local state.

2. Detailed algorithm

(1) Starting from the point S, for the first state X1 each node, it may be assumed that there are N1, calculate S to their distance d (s,x1i), where x1i represents a node of any State 1. Because there is only one step, these distances are s to their respective shortest distances.

t " > i "> (2) For all nodes of the second state X2, the shortest distance from s to them is calculated. For the feature node X2i, the path from S to it can be x1i by any node in the N1 of State 1 , and the corresponding path length is d (s,x2i) = d (s,x1i) + D (x1i,x2i).

    since I have N1 possibility, we need to calculate each one to find the minimum value. That is: D (s,x2i) min =d (s,x1i) + {d (x1i,x2i)}1→n1 This requires N1 multiplication calculation for each node of the second state. Assuming that the state has N2 nodes, the distance of these nodes of S is counted once and there is an O (N1 n2) calculation.

(3) Next, similar to the above method from the second state to go to the third State, go to the last state, the entire grid to get the shortest path from beginning to end.

(4) Assuming that the most nodes in the implied Markov chain have a D node, that is, the width of the entire grid is D, then any step of the complexity is not more than O (D2), because the grid length is N, so the complexity of the entire Viterbi algorithm is O (N· D2).

3. Example Display

(1) There are only three types of weather (sunny,cloudy,rainy), seaweed humidity has four classes {dry,dryish, damp,soggy}, and seaweed humidity and weather have a certain relationship.

(2) Hidden state: Sunny, Cloudy, Rainy;

(3) Observing the status sequence: {Dry, damp, soggy}

(4) Initial state sequence:

  

(5) State transition matrix:

  

(6) Emission matrix:

  

Suppose the 3-day continuous observation of seaweed humidity is (dry,damp,soggy), the most probable weather conditions for the three days.

  1. Violent cracking:

Condition: The next day is only related to the weather on the first day, and the weather on the third day is only related to the weather of the next day.

So the first day of humidity for the dry condition is:

P (Dry|day1-sunny) =0.63*0.6

P (Dry|day1-cloudy) =0.17*0.25

P (Dry|day1-rainy) =0.2*0.05

P (Dry|day1-suny) is the most probable, so the maximum probability for the first day is: Sunny

The next day the humidity is damp:

    P (damp|day2-Sunny) =max{p (Dry|day1-sunny) *0.5,p (dry|day1-cloudy) *0.25,p (dry|day1-rainy) *0.25}*0.15

P (damp|day2-Cloudy) =max{p (Dry|day1-sunny) *0.375,p (dry|day1-cloudy) *0.125,p (dry|day1-rainy) *0.625}* 0.25

P (damp|day2-Rainy) =max{p (Dry|day1-sunny) *0.125,p (dry|day1-cloudy) *0.625,p (dry|day1-rainy) *0.35}*0.35

P (Damp|day2-cloud) is the highest probability, so the next day maximum probability is: Cloud

The third day of humidity is soggy:

P (Soggy|day3-sunny) =max{p (Damp|day2-sunny) *0.5,p (damp|day2-cloudy) *0.25,p (damp|day2-rainy) *0.25}*0.05

P (Soggy|day3-cloudy) =max{p (Damp|day2-sunny) *0.375,p (damp|day2-cloudy) *0.125,p (damp|day2-rainy) *0.625}*0.25

P (Soggy|day3-rainy) =max{p (Damp|day2-sunny) *0.125,p (damp|day2-cloudy) *0.625,p (damp|day2-rainy) *0.375}*0.5

P (Soggy|day3-rainy) is the most probable, so the maximum probability for the third day is: Rainy

So the maximum probability of a 3-day weather is {sunny,cloud,rainy}

Analysis:

Day one: The maximum probability of humidity being dry in each weather is the probable weather;

The next day: corresponding to the first day of the various weather conversion sunny, and the probability of conversion of the largest case, take the weather humidity of damp probability, cloudy and rainy the same, and then take these three kinds of weather probability the biggest weather

third day: corresponding to the next day of the various weather conversion sunny (note here the next day for the first day, the maximum probability has been obtained) and the probability of conversion maximum, take the weather humidity of damp probability, cloudy and rainy the same, and then take these three kinds of weather probability the biggest weather

   2, Python code implementation:

#-*-coding:utf-8-*-ImportNumPy as NPdefViterbi (trainsition_probability,emission_probability,pi,obs_seq):#Convert to matrix for calculationtrainsition_probability=Np.array (trainsition_probability) emission_probability=Np.array (emission_probability) Pi=Np.array (PI)#defines the matrix to returnF =Np.zeros (Row, Col)#Initial Statef[:,0]=pi*Np.transpose (emission_probability[:,obs_seq[0]) forTinchRange (1, Col): List_max=[]         forNinchRange (Row): list_x=list (Np.array (f[:,t-1]) *Np.transpose (Trainsition_probability[:,n]))#get the maximum probabilitylist_p=[]             forIinchList_x:list_p.append (i*10000) List_max.append (Max (list_p)/10000) f[:, T]= Np.array (List_max) *Np.transpose (emission_probability[:,obs_seq[t])returnFif __name__=='__main__':    #Hidden Stateinvisible=['Sunny','Cloud','Rainy']    #Initial Statepi=[0.63,0.17,0.20]    #Transfer Matrixtrainsition_probability=[[0.5,0.375,0.125],                             [0.25,0.125,0.625],                             [0.25,0.375,0.375]]    #Emission Matrixemission_probability=[[0.6,0.2,0.15,0.05],                          [0.25,0.25,0.25,0.25],                          [0.05,0.10,0.35,0.5]]    #Last Display statusobs_seq=[0,2,3]    #finally returns a matrix result of a row*colRow =Np.array (trainsition_probability). shape[0] Col=Len (obs_seq) F=Viterbi (trainsition_probability, emission_probability, Pi, obs_seq)PrintF

Results:

[[0.378 0.02835 0.00070875]
[0.0425 0.0354375 0.00265781]
[0.01 0.0165375 0.01107422]]

Each column represents the probability of dry,damp,soggy, each row represents sunny,cloud,rainy, so you can see that the maximum probability of the weather is {sunny,cloud,rainy}.

Viterbi algorithm (VITERBI)-Walkthrough (Brute Force + code implementation)

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.