HMM traditional back-direction algorithm, has been implemented, for reference only.
PackageJxutcm.edu.cn.hmm.model;
ImportJxutcm.edu.cn.hmm.bean.HMMHelper;
ImportJxutcm.edu.cn.util.TCMMath;
/**
* Back-direction algorithm
Purpose
* 1, first calculate the back variable matrix
* 2, then use the back-to-variable matrix to calculate the probability of an observation sequence
* @authorAool
*/
PublicclassBackwardextendshmm{
Publicint[] O;//observation Sequence Observe//such as yellow red blue yellow green These index positions in enum Color {Red,yellow,blue,green}
Public Double[] beta;//matrix of the back variable
/**
* Flag Indicates whether A and B are natural logarithms (LnX) true:a and B are naturally logarithmic after false:a and B are not naturally logarithmic
*/
PublicBackward (Double[] A,Double[] B,Double[] PI,int[] O,BooleanFlag) {
Super(A, B, PI, flag);
This. O=o;
}
PublicBackward (Hmm hmm,int[] O) {
Super(hmm);
This. O=o;
}
/**
* "Calculate the back-to-variable matrix"
* Under the condition of time t, the hidden State is s_i (I hidden state, a total of n hidden states), the HMM output observation sequence O (t+1) ... The probability of O (T)
* beta[t [i] = beta_t (i) = log (P (O (t+1) ... O (T) | q_t=s_i,λ))
*/
PublicvoidCalculatebackmatrix () {
intT = O.length;
Beta =NewDoubleT [N];//probability of the occurrence of multiple states occurring at each moment (per line)
//1, initialization--the T-moment, the second hidden state output observation sequence of the back variable is set to 1 that is, log (1) =0
for(inti = 0; i < N; i++) {
beta[T-1] [i] = 0;//= log (1)//should be hmm.loga[k][0]
}
//2. Inductive calculation--b_t (i)
for(intt = T-1-1; T >= 0; t--) {//at T-moment, starting from T-2-the subscript starts at 0--t-1 indicates the final moment
for(inti = 0; i < N; i++) {//In the hidden state of the first I
Doublesum = double.negative_infinity;//= log (0)
for(intj = 0; J < N; J + +) {//Cumulative probability to J--b[t][i] = b_t (i) =∑aij * BJ (o_t+1) *b_t+1 (j) where B_t+1 (j) =b[t+1][j], the summation symbol above is n, the following is the beginning of j=1
//sum + = a[I [j] * b[j] [O (t+1)] * beta[t+1] [j]
sum = Tcmmath.logplus (sum, loga[I [j] + logb[j] [o[T+1]] + beta[t + 1][j]);
}
//beta[t [i] = "t moment all hidden state I" arrives "t+1 moment hidden State J" and "T+1 moment shows O (t+1)" of the Back variable probability
//beta[T] [i] =∑ (a[I [j] * b[j] [O (t+1)] * beta[t+1] [j]) summation symbol denotes 1<=j <=n
beta[t [i] = sum;//in "t time, I hidden state" under the output of the observation sequence ot+1 ... The probability of the occurrence of OT (local of the known observation sequence)
}
}
}
/**
* "Calculate the probability of an observation sequence"-if the back-to-variable matrix is first computed-returns the natural logarithm
* P (O |μ) =∑pi_i*b_i*beta_1 (i) (sum upper bound n, summation lower bound i=1)--the probability that all hidden states are accumulated in the t=1 moment is the observed sequence
* Calculate t=0 time, in the No. 0 State of the output observation sequence O0 ... The probability of the occurrence of OT (local of the observed sequence)
*/
PublicDoubleLogprob () {
Doublesum = double.negative_infinity;//= log (0)
for(inti = 0; i < N; i++) {
sum = Tcmmath.logplus (sum, logpi[i] + logb[I [o[0]] + beta[0 [i]);
}
returnSum
}
/**
* Print back to variable matrix
*/
PublicvoidPrint () {
for(intj = 0; J < N; J + +) {
for(inti = 0; i < beta.length; i++) {
System.out.print (Hmmhelper.fmtlog (beta[I [j]));
}
System.out.println ();
}
}
}
HMM traditional back-direction algorithm