Program: 144091
Project: 144091
Buggy source:
private void sampleZ(int iterationNumber){
  for (int p=0; p < numberOfDomains; p++) {
    dp.remove_topic_assignment(zSamples[iterationNumber][p]);
    int total_cnt=dp.prob_topics();
    double z_probabilities[]=new double[total_cnt];
    for (int i=0; i < total_cnt; i++) {
      z_probabilities[i]=Math.log(dp.pdf[i].prob);
    }
    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 i=0; i < total_cnt - 1; i++) {
        int k=dp.pdf[i].topic;
        z_probabilities[i]+=disagreements[j][p] * Math.log(errorRateSamples[iterationNumber + 1][k][j]);
        z_probabilities[i]+=(numberOfDataSamples[p] - disagreements[j][p]) * Math.log(1 - errorRateSamples[iterationNumber + 1][k][j]);
      }
      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 + 1][p]=dp.pdf[total_cnt - 1].topic;
    for (int i=0; i < total_cnt - 1; i++) {
      if (z_probabilities[i] > uniform) {
        zSamples[iterationNumber + 1][p]=dp.pdf[i].topic;
        dp.add_topic_assingment(dp.pdf[i].topic);
        break;
      }
    }
    if (zSamples[iterationNumber + 1][p] == dp.pdf[total_cnt - 1].topic) {
      dp.add_topic_assingment(dp.pdf[total_cnt - 1].topic);
      int numberOfErrorRatesBelowChance=0;
      for (int j=0; j < numberOfFunctions; j++) {
        errorRateSamples[iterationNumber + 1][dp.pdf[total_cnt - 1].topic][j]=randomDataGenerator.nextBeta(alpha_e + disagreements[j][p],beta_e + numberOfDataSamples[p] - disagreements[j][p]);
        if (errorRateSamples[iterationNumber + 1][dp.pdf[total_cnt - 1].topic][j] < 0.5)         numberOfErrorRatesBelowChance+=1;
      }
      if (numberOfErrorRatesBelowChance < numberOfFunctions / 2.0)       for (int j=0; j < numberOfFunctions; j++)       errorRateSamples[iterationNumber + 1][dp.pdf[total_cnt - 1].topic][j]=1 - errorRateSamples[iterationNumber + 1][dp.pdf[total_cnt - 1].topic][j];
    }
  }
}

Fixed version (ground truth):
private void sampleZ(int iterationNumber){
  for (int p=0; p < numberOfDomains; p++) {
    dp.remove_topic_assignment(zSamples[iterationNumber][p]);
    int total_cnt=dp.prob_topics();
    double z_probabilities[]=new double[total_cnt];
    for (int i=0; i < total_cnt; i++) {
      z_probabilities[i]=Math.log(dp.pdf[i].prob);
    }
    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 i=0; i < total_cnt - 1; i++) {
        int k=dp.pdf[i].topic;
        z_probabilities[i]+=disagreements[j][p] * Math.log(errorRateSamples[iterationNumber + 1][k][j]);
        z_probabilities[i]+=(numberOfDataSamples[p] - disagreements[j][p]) * Math.log(1 - errorRateSamples[iterationNumber + 1][k][j]);
      }
      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 + 1][p]=dp.pdf[total_cnt - 1].topic;
    for (int i=0; i < total_cnt - 1; i++) {
      if (z_probabilities[i] > uniform) {
        zSamples[iterationNumber + 1][p]=dp.pdf[i].topic;
        dp.add_topic_assingment(dp.pdf[i].topic);
        break;
      }
    }
    if (zSamples[iterationNumber + 1][p] == dp.pdf[total_cnt - 1].topic) {
      dp.add_topic_assingment(dp.pdf[total_cnt - 1].topic);
    }
  }
}