}
}
}
// Try to discover the model, given the symbols
HMM hmm=new HMM(atrans,b,symbols);
hmm.forwardbackward();
// Inspect results
int jmax=0,ncorrect=0;
double test;
for (i=0;i<N;i++) {
test=0;
for (j=0;j<M;j++) {
if (hmm.pstate[i][j] > test) {
test=hmm.pstate[i][j];
jmax=j;
}
}
// System.out.printf(state[i] << " %f\n", jmax);
if (jmax == state[i]) ncorrect++;
}
System.out.printf("Fraction correct: %f\n", (double)(ncorrect)/N);
localflag = (double)(ncorrect)/N < 0.75;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** HMM: Actual state was not the top probability more than 25% of the time");
}
// Inspect reconstructed transition matrix
int jpen=0;
ncorrect=0;
for (i=0;i<N;i++) {
test=0.0;
for (j=0;j<M;j++) {
if (hmm.pstate[i][j] > test) {
test=hmm.pstate[i][j];
jmax=j;
}
}
// Find second largest
test=0.0;
for (j=0;j<M;j++) {
if (j != jmax) {
if (hmm.pstate[i][j] > test) {
test=hmm.pstate[i][j];
jpen=j;
}
}
}
// System.out.printf(state[i] << " %f\n", jmax);
if (jmax == state[i] || jpen == state[i]) ncorrect++;
}
System.out.printf("Fraction correct: %f\n", (double)(ncorrect)/N);
double beforeBW=(double)(ncorrect)/N;
localflag = (double)(ncorrect)/N < 0.95;
globalflag = globalflag || localflag;
if (localflag) {
fail("*** HMM: Actual state was not in top 2 probabilities more than 5% of the time");
}
// System.out.println("Log-likelihood: %f\n", hmm.loglikelihood());
// Test Baum-Welch reestimation
for (i=0;i<100;i++) {
hmm.baumwelch();
hmm.forwardbackward();
// System.out.println("Log-likelihood: %f\n", hmm.loglikelihood());
}
// Inspect reconstructed transition matrix
ncorrect=0;