Program: 20209
Project: 20209
Buggy source:
public double modelLogLikelihood(){
  double logLikelihood=0.0;
  int nonZeroTopics;
  int[] topicCounts=new int[numTopics];
  double[] topicLogGammas=new double[numTopics];
  int[] docTopics;
  for (int topic=0; topic < numTopics; topic++) {
    topicLogGammas[topic]=Dirichlet.logGammaStirling(alpha[topic]);
  }
  for (int doc=0; doc < data.size(); doc++) {
    LabelSequence topicSequence=(LabelSequence)data.get(doc).topicSequence;
    docTopics=topicSequence.getFeatures();
    for (int token=0; token < docTopics.length; token++) {
      topicCounts[docTopics[token]]++;
    }
    for (int topic=0; topic < numTopics; topic++) {
      if (topicCounts[topic] > 0) {
        logLikelihood+=(Dirichlet.logGammaStirling(alpha[topic] + topicCounts[topic]) - topicLogGammas[topic]);
      }
    }
    logLikelihood-=Dirichlet.logGammaStirling(alphaSum + docTopics.length);
    Arrays.fill(topicCounts,0);
  }
  logLikelihood+=data.size() * Dirichlet.logGammaStirling(alphaSum);
  int nonZeroTypeTopics=0;
  for (int type=0; type < numTypes; type++) {
    for (int topic=0; topic < numTopics; topic++) {
      if (topicCounts[topic] > 0) {
        nonZeroTypeTopics++;
        logLikelihood+=Dirichlet.logGammaStirling(beta + typeTopicCounts[type].get(topic));
      }
    }
  }
  for (int topic=0; topic < numTopics; topic++) {
    logLikelihood-=Dirichlet.logGammaStirling((beta * numTopics) + tokensPerTopic[topic]);
  }
  logLikelihood+=(Dirichlet.logGammaStirling(beta * numTopics)) - (Dirichlet.logGammaStirling(beta) * nonZeroTypeTopics);
  return logLikelihood;
}

Fixed version (ground truth):
public double modelLogLikelihood(){
  double logLikelihood=0.0;
  int nonZeroTopics;
  int[] topicCounts=new int[numTopics];
  double[] topicLogGammas=new double[numTopics];
  int[] docTopics;
  for (int topic=0; topic < numTopics; topic++) {
    topicLogGammas[topic]=Dirichlet.logGammaStirling(alpha[topic]);
  }
  for (int doc=0; doc < data.size(); doc++) {
    LabelSequence topicSequence=(LabelSequence)data.get(doc).topicSequence;
    docTopics=topicSequence.getFeatures();
    for (int token=0; token < docTopics.length; token++) {
      topicCounts[docTopics[token]]++;
    }
    for (int topic=0; topic < numTopics; topic++) {
      if (topicCounts[topic] > 0) {
        logLikelihood+=(Dirichlet.logGammaStirling(alpha[topic] + topicCounts[topic]) - topicLogGammas[topic]);
      }
    }
    logLikelihood-=Dirichlet.logGammaStirling(alphaSum + docTopics.length);
    Arrays.fill(topicCounts,0);
  }
  logLikelihood+=data.size() * Dirichlet.logGammaStirling(alphaSum);
  int nonZeroTypeTopics=0;
  for (int type=0; type < numTypes; type++) {
    int[] usedTopics=typeTopicCounts[type].keys();
    for (    int topic : usedTopics) {
      int count=typeTopicCounts[type].get(topic);
      if (count > 0) {
        nonZeroTypeTopics++;
        logLikelihood+=Dirichlet.logGammaStirling(beta + count);
      }
    }
  }
  for (int topic=0; topic < numTopics; topic++) {
    logLikelihood-=Dirichlet.logGammaStirling((beta * numTopics) + tokensPerTopic[topic]);
  }
  logLikelihood+=(Dirichlet.logGammaStirling(beta * numTopics)) - (Dirichlet.logGammaStirling(beta) * nonZeroTypeTopics);
  return logLikelihood;
}