The Viterbi algorithm Java implementation of the Hidden Markov model, the detailed principle please self-view 52NLP

Source: Internet
Author: User


/**
* "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

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.