Program: 144073
Project: 144073
Buggy source:
private void sampleZAndBurnWithCollapsedErrorRates(int iterationNumber){
  for (int p=0; p < numberOfDomains; p++) {
    for (int j=0; j < numberOfFunctions; j++) {
      disagreements[j][p]=0;
      for (int i=0; i < numberOfDataSamples[p]; i++)       if (functionOutputsArray[j][p][i] != labelsSamples[iterationNumber][p][i])       disagreements[j][p]++;
    }
  }
  for (int k=0; k < numberOfDomains * numberOfFunctions; k++) {
    sum_2[k]=0;
    for (int j=0; j < numberOfFunctions; j++) {
      for (int p=0; p < numberOfDomains; p++) {
        if (zSamples[iterationNumber][p][j] == k)         sum_2[k]+=disagreements[j][p];
      }
    }
  }
  for (int p=0; p < numberOfDomains; p++) {
    for (int j=0; j < numberOfFunctions; j++) {
      hdp.remove_items_table_assignment(p,j);
      int total_cnt=hdp.prob_table_assignment_for_item(p,j);
      double z_probabilities[]=new double[total_cnt];
      for (int i=0; i < total_cnt; i++) {
        z_probabilities[i]=Math.log(hdp.pdf[i].prob);
      }
      int previous_topic=zSamples[iterationNumber][p][j];
      sum_1[previous_topic]-=numberOfDataSamples[p];
      sum_2[previous_topic]-=disagreements[j][p];
      for (int i=0; i < total_cnt - 1; i++) {
        int k=hdp.pdf[i].topic;
        double alpha=alpha_e + sum_2[k] + disagreements[j][p];
        double beta=beta_e + sum_1[k] - sum_2[k] + numberOfDataSamples[p] - disagreements[j][p];
        z_probabilities[i]+=logBeta(alpha,beta) - logBeta(alpha_e + sum_2[k],beta_e + sum_1[k] - sum_2[k]);
      }
      z_probabilities[total_cnt - 1]+=logBeta(alpha_e + disagreements[j][p],beta_e + numberOfDataSamples[p] - disagreements[j][p]) - logBeta(alpha_e,beta_e);
      for (int i=1; i < total_cnt; i++) {
        z_probabilities[i]=MatrixUtilities.computeLogSumExp(z_probabilities[i - 1],z_probabilities[i]);
      }
      double uniform=Math.log(random.nextDouble()) + z_probabilities[total_cnt - 1];
      zSamples[iterationNumber][p][j]=hdp.pdf[total_cnt - 1].topic;
      for (int i=0; i < total_cnt - 1; i++) {
        if (z_probabilities[i] > uniform) {
          zSamples[iterationNumber][p][j]=hdp.pdf[i].topic;
          sum_1[hdp.pdf[i].topic]+=numberOfDataSamples[p];
          sum_2[hdp.pdf[i].topic]+=disagreements[j][p];
          hdp.add_items_table_assignment(p,j,hdp.pdf[i].table,hdp.pdf[i].topic);
          break;
        }
      }
      if (zSamples[iterationNumber][p][j] == hdp.pdf[total_cnt - 1].topic) {
        sum_1[hdp.pdf[total_cnt - 1].topic]+=numberOfDataSamples[p];
        sum_2[hdp.pdf[total_cnt - 1].topic]+=disagreements[j][p];
        hdp.add_items_table_assignment(p,j,hdp.pdf[total_cnt - 1].table,hdp.pdf[total_cnt - 1].topic);
      }
    }
  }
}

Fixed version (ground truth):
private void sampleZAndBurnWithCollapsedErrorRates(int iterationNumber){
  for (int p=0; p < numberOfDomains; p++) {
    for (int j=0; j < numberOfFunctions; j++) {
      hdp.remove_items_table_assignment(p,j);
      int total_cnt=hdp.prob_table_assignment_for_item(p,j);
      double z_probabilities[]=new double[total_cnt];
      for (int i=0; i < total_cnt; i++) {
        z_probabilities[i]=Math.log(hdp.pdf[i].prob);
      }
      int previous_topic=zSamples[iterationNumber][p][j];
      sum_1[previous_topic]-=numberOfDataSamples[p];
      sum_2[previous_topic]-=disagreements[j][p];
      for (int i=0; i < total_cnt - 1; i++) {
        int k=hdp.pdf[i].topic;
        double alpha=alpha_e + sum_2[k] + disagreements[j][p];
        double beta=beta_e + sum_1[k] - sum_2[k] + numberOfDataSamples[p] - disagreements[j][p];
        z_probabilities[i]+=logBeta(alpha,beta) - logBeta(alpha_e + sum_2[k],beta_e + sum_1[k] - sum_2[k]);
      }
      z_probabilities[total_cnt - 1]+=logBeta(alpha_e + disagreements[j][p],beta_e + numberOfDataSamples[p] - disagreements[j][p]) - logBeta(alpha_e,beta_e);
      for (int i=1; i < total_cnt; i++) {
        z_probabilities[i]=MatrixUtilities.computeLogSumExp(z_probabilities[i - 1],z_probabilities[i]);
      }
      double uniform=Math.log(random.nextDouble()) + z_probabilities[total_cnt - 1];
      zSamples[iterationNumber][p][j]=hdp.pdf[total_cnt - 1].topic;
      for (int i=0; i < total_cnt - 1; i++) {
        if (z_probabilities[i] > uniform) {
          zSamples[iterationNumber][p][j]=hdp.pdf[i].topic;
          sum_1[hdp.pdf[i].topic]+=numberOfDataSamples[p];
          sum_2[hdp.pdf[i].topic]+=disagreements[j][p];
          hdp.add_items_table_assignment(p,j,hdp.pdf[i].table,hdp.pdf[i].topic);
          break;
        }
      }
      if (zSamples[iterationNumber][p][j] == hdp.pdf[total_cnt - 1].topic) {
        sum_1[hdp.pdf[total_cnt - 1].topic]+=numberOfDataSamples[p];
        sum_2[hdp.pdf[total_cnt - 1].topic]+=disagreements[j][p];
        hdp.add_items_table_assignment(p,j,hdp.pdf[total_cnt - 1].table,hdp.pdf[total_cnt - 1].topic);
      }
    }
  }
}