Program: 72492
Project: 72492
Buggy source:
/** 
 * Alpha-beta recursion
 * @param y input sequence
 * @param prior prior guess vectors
 * @param A transition probability matrix
 * @return An array of assigned labels to observations
 * @throws IllegalActionException
 */
protected final int[] classifyHMM(double[][] y,double[] prior,double[][] A) throws IllegalActionException {
  int nStates=_nStates;
  double[][] alphas=new double[y.length][nStates];
  double[][] gamma=new double[y.length][nStates];
  double[] alphaNormalizers=new double[y.length];
  double alphaSum=0;
  for (int t=0; t < y.length; t++) {
    alphaSum=0;
    for (int i=0; i < nStates; i++) {
      alphas[t][i]=0;
      if (t == 0) {
        alphas[t][i]=prior[i] * emissionProbability(y[t],i);
      }
 else {
        for (int qt=0; qt < nStates; qt++) {
          alphas[t][i]+=A[qt][i] * emissionProbability(y[t],i) * alphas[t - 1][qt];
        }
      }
      alphaSum+=alphas[t][i];
    }
    for (int i=0; i < nStates; i++) {
      alphas[t][i]/=alphaSum;
      alphaNormalizers[t]=alphaSum;
    }
  }
  for (int t=y.length - 1; t >= 0; t--) {
    for (int qt=0; qt < nStates; qt++) {
      if (t == y.length - 1) {
        gamma[t][qt]=alphas[t][qt];
      }
 else {
        gamma[t][qt]=0;
        for (int qtp=0; qtp < nStates; qtp++) {
          double alphasum=0;
          for (int j=0; j < nStates; j++) {
            alphasum+=alphas[t][j] * A[j][qtp];
          }
          gamma[t][qt]+=(alphas[t][qt] * A[qt][qtp] * gamma[t + 1][qtp]) / alphasum;
        }
      }
    }
  }
  int[] clusterAssignments=new int[y.length];
  for (int t=0; t < y.length; t++) {
    int maxState=0;
    for (int j=1; j < nStates; j++) {
      if (gamma[t][j] > gamma[t][maxState]) {
        maxState=j;
      }
    }
    clusterAssignments[t]=maxState;
  }
  double logLikelihood=0.0;
  for (int t=0; t < _observations.length - 1; t++) {
    logLikelihood+=emissionProbability(y[t],clusterAssignments[t]);
    logLikelihood+=A[clusterAssignments[t]][clusterAssignments[t + 1]];
  }
  logLikelihood+=emissionProbability(y[_observations.length - 1],clusterAssignments[_observations.length - 1]);
  _likelihood=logLikelihood;
  return clusterAssignments;
}

Fixed version (ground truth):
/** 
 * Alpha-beta recursion
 * @param y input sequence
 * @param prior prior guess vectors
 * @param A transition probability matrix
 * @return An array of assigned labels to observations
 * @exception IllegalActionException
 */
protected final int[] classifyHMM(double[][] y,double[] prior,double[][] A) throws IllegalActionException {
  int nStates=_nStates;
  double[][] alphas=new double[y.length][nStates];
  double[][] gamma=new double[y.length][nStates];
  double[] alphaNormalizers=new double[y.length];
  double alphaSum=0;
  for (int t=0; t < y.length; t++) {
    alphaSum=0;
    for (int i=0; i < nStates; i++) {
      alphas[t][i]=0;
      if (t == 0) {
        alphas[t][i]=prior[i] * emissionProbability(y[t],i);
      }
 else {
        for (int qt=0; qt < nStates; qt++) {
          alphas[t][i]+=A[qt][i] * emissionProbability(y[t],i) * alphas[t - 1][qt];
        }
      }
      alphaSum+=alphas[t][i];
    }
    for (int i=0; i < nStates; i++) {
      alphas[t][i]/=alphaSum;
      alphaNormalizers[t]=alphaSum;
    }
  }
  for (int t=y.length - 1; t >= 0; t--) {
    for (int qt=0; qt < nStates; qt++) {
      if (t == y.length - 1) {
        gamma[t][qt]=alphas[t][qt];
      }
 else {
        gamma[t][qt]=0;
        for (int qtp=0; qtp < nStates; qtp++) {
          double alphasum=0;
          for (int j=0; j < nStates; j++) {
            alphasum+=alphas[t][j] * A[j][qtp];
          }
          gamma[t][qt]+=(alphas[t][qt] * A[qt][qtp] * gamma[t + 1][qtp]) / alphasum;
        }
      }
    }
  }
  int[] clusterAssignments=new int[y.length];
  for (int t=0; t < y.length; t++) {
    int maxState=0;
    for (int j=1; j < nStates; j++) {
      if (gamma[t][j] > gamma[t][maxState]) {
        maxState=j;
      }
    }
    clusterAssignments[t]=maxState;
  }
  double logLikelihood=0.0;
  for (int t=0; t < _observations.length - 1; t++) {
    logLikelihood+=emissionProbability(y[t],clusterAssignments[t]);
    logLikelihood+=A[clusterAssignments[t]][clusterAssignments[t + 1]];
  }
  logLikelihood+=emissionProbability(y[_observations.length - 1],clusterAssignments[_observations.length - 1]);
  _likelihood=logLikelihood;
  return clusterAssignments;
}