/**
* "Calculate the Viterbi matrix"
* delta[T [k] = V_k (i) =log (max (P (pi in state k have sym I | path pi)))
*/
PublicvoidCalculateviterbimatrix () {
intT = O.length;
Delta =NewDoubleT [N];
PSI =NewintT [N];
//1, initialize, calculate the initial moment (the 1th moment of intuition) the local probability of all States
for(inti = 0; i < N; i++) {
Delta[0][i] = logpi[i] + logb[i] [o[0]];
}
//2, induction, recursive calculation
for(intt = 1; T < T; t++) {
for(inti = 0; i < N; i++) {
intMaxprodindex = 0;
DoubleMaxprod = delta[t-1][Maxprodindex] + loga[maxprodindex [i];
for(intj = 1; J < N; J + +) {
DoubleProd = delta[t-1][j] + loga[J [i];
if(Prod > Maxprod) {
Maxprodindex = j;
Maxprod = prod;
}
}
delta[T] [i] = Maxprod + logb[I [o[T]];
psi[t [i] = Maxprodindex;
}
}
//3, (hidden state sequence) backtracking to find the best path--from T-1 moment to initial moment--finding the best path to accumulate maximum probability
Path=Newint[T];//define the best path
DoubleMaxprod = delta[T-1 [0];
path[t-1]=0;
for(inti = 1; i < N; i++) {
if(delta[T-1] [i] > Maxprod) {
path[T-1] = i;
Maxprod = delta[T-1 [i];
}
}
//4. Path backtracking-Finding the best path
for(intT= T-2; t>=0; t--) {
path[T]= psi[t+1] [path[t+1]];
}
}
/**
* Print Best hidden path
* @sincehmm must give the state and Syms parameters
*/
PublicvoidPrintpath () {
if(state==NULL){
ThrowNewIllegalArgumentException ("Hidden state name is not set");
}
intT=o.length;
for(inti=0; i< T; i++) {
System.out.print (state[path[i]] + "");
}
System.out.println ();
}
The Viterbi algorithm Java implementation of the Hidden Markov model, the detailed principle please self-view 52NLP