Program: 143964
Project: 143964
Buggy source:
private Results evaluate(Integrator.ErrorRates estimatedErrorRates,Integrator.Data<Integrator.Data.PredictedInstance> integratedData,String methodName){
  List<Integrator.ErrorRates.Instance> estimatedErrorRatesList=estimatedErrorRates.stream().sorted((i1,i2) -> Integer.compare(i1.functionId(),i2.functionId())).sorted((i1,i2) -> i1.label().name().compareTo(i2.label().name())).collect(Collectors.toList());
  Map<Label,Map<Integer,Double>> errorRatesMap=new HashMap<>();
  Map<Label,Map<Integer,Double>> sampleErrorRatesMap=new HashMap<>();
  double mseError=0.0;
  double madError=0.0;
  double mseErrorWeighted=0.0;
  double madErrorWeighted=0.0;
  for (  Integrator.ErrorRates.Instance instance : estimatedErrorRatesList) {
    if (!errorRatesMap.containsKey(instance.label())) {
      errorRatesMap.put(instance.label(),new HashMap<>());
      sampleErrorRatesMap.put(instance.label(),new HashMap<>());
    }
    errorRatesMap.get(instance.label()).put(instance.functionId(),instance.value());
    Optional<Integrator.ErrorRates.Instance> sampleErrorRateResultInstance=sampleErrorRates.stream().filter(i -> i.label().equals(instance.label()) && i.functionId() == instance.functionId()).findFirst();
    double sampleErrorRate=0.0;
    if (sampleErrorRateResultInstance.isPresent())     sampleErrorRate=sampleErrorRateResultInstance.get().value();
    sampleErrorRatesMap.get(instance.label()).put(instance.functionId(),sampleErrorRate);
    double seError=(instance.value() - sampleErrorRate) * (instance.value() - sampleErrorRate);
    double adError=Math.abs(instance.value() - sampleErrorRate);
    mseError+=seError;
    madError+=adError;
    mseErrorWeighted+=seError / numberOfInstances.get(instance.label());
    madErrorWeighted+=adError / numberOfInstances.get(instance.label());
  }
  mseError/=estimatedErrorRatesList.size();
  madError/=estimatedErrorRatesList.size();
  mseErrorWeighted/=labels.size();
  madErrorWeighted/=labels.size();
  Map<Integer,Map<Label,Boolean>> estimatedLabelsMap=new HashMap<>();
  Map<Integer,Map<Label,Boolean>> trueLabelsMap=new HashMap<>();
  double[] madErrorRank={0.0};
  double[] madErrorRankWeighted={0.0};
  double[] aucLabel={0.0};
  double[] madLabel={0.0};
  double[] madHardLabel={0.0};
  double[] aucLabelWeighted={0.0};
  double[] madLabelWeighted={0.0};
  double[] madHardLabelWeighted={0.0};
  integratedData.stream().map(Integrator.Data.PredictedInstance::label).distinct().forEach(label -> {
    List<Integer> rankedByErrorRate=CollectionUtilities.sortByValue(errorRatesMap.get(label)).entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
    List<Integer> rankedBySampleErrorRate=CollectionUtilities.sortByValue(sampleErrorRatesMap.get(label)).entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
    for (int i=0; i < rankedByErrorRate.size(); i++) {
      double adErrorRank=Math.abs(rankedByErrorRate.get(i) - rankedBySampleErrorRate.get(i));
      madErrorRank[0]+=adErrorRank;
      madErrorRankWeighted[0]+=adErrorRank * numberOfInstances.get(label);
    }
    double[] adLabel={0};
    double[] adHardLabel={0};
    int[] numberOfSamples={0};
    Map<Integer,Boolean> observedInstances=new HashMap<>();
    observedData.stream().filter(i -> i.label().equals(label)).forEach(instance -> observedInstances.put(instance.id(),instance.value()));
    List<Boolean> observedLabels=new ArrayList<>();
    List<Double> predictions=new ArrayList<>();
    integratedData.stream().filter(i -> i.label().equals(label) && observedInstances.containsKey(i.id())).sorted((i1,i2) -> i1.value() - i2.value() == 0.0 ? 0 : i1.value() - i2.value() > 0.0 ? -1 : 1).forEach(i -> {
      observedLabels.add(observedInstances.get(i.id()));
      predictions.add(i.value());
    }
);
    double areaUnderTheCurve=PrecisionRecall.areaUnderTheCurve(observedLabels,predictions);
    aucLabel[0]+=areaUnderTheCurve;
    aucLabelWeighted[0]+=areaUnderTheCurve / numberOfInstances.get(label);
    integratedData.stream().filter(i -> i.label().equals(label) && observedInstances.containsKey(i.id())).forEach(instance -> {
      adLabel[0]+=Math.abs(instance.value() - (observedInstances.get(instance.id()) ? 1.0 : 0.0));
      if ((instance.value() >= 0.5) != observedInstances.get(instance.id()))       adHardLabel[0]++;
      numberOfSamples[0]++;
      if (!estimatedLabelsMap.containsKey(instance.id()))       estimatedLabelsMap.put(instance.id(),new HashMap<>());
      estimatedLabelsMap.get(instance.id()).put(label,instance.value() >= 0.5);
      if (!trueLabelsMap.containsKey(instance.id()))       trueLabelsMap.put(instance.id(),new HashMap<>());
      trueLabelsMap.get(instance.id()).put(label,observedInstances.get(instance.id()));
    }
);
    double accuracyLabel=adLabel[0] / (double)numberOfSamples[0];
    double accuracyHardLabel=adHardLabel[0] / (double)numberOfSamples[0];
    madLabel[0]+=accuracyLabel;
    madHardLabel[0]+=accuracyHardLabel;
    madLabelWeighted[0]+=accuracyLabel / numberOfInstances.get(label);
    madHardLabelWeighted[0]+=accuracyHardLabel / numberOfInstances.get(label);
  }
);
  madErrorRank[0]/=labels.size();
  madErrorRankWeighted[0]/=numberOfInstances.values().stream().mapToInt(n -> n).sum();
  aucLabel[0]/=labels.size();
  madLabel[0]/=labels.size();
  madHardLabel[0]/=labels.size();
  aucLabelWeighted[0]/=labels.size();
  madLabelWeighted[0]/=labels.size();
  madHardLabelWeighted[0]/=labels.size();
  return new Results(methodName,errorRatesMap,sampleErrorRatesMap,madErrorRank[0],mseError,madError,madErrorRankWeighted[0],mseErrorWeighted,madErrorWeighted,aucLabel[0],madLabel[0],madHardLabel[0],aucLabelWeighted[0],madLabelWeighted[0],madHardLabelWeighted[0]);
}

Fixed version (ground truth):
private Results evaluate(Integrator.ErrorRates estimatedErrorRates,Integrator.Data<Integrator.Data.PredictedInstance> integratedData,String methodName){
  List<Integrator.ErrorRates.Instance> estimatedErrorRatesList=estimatedErrorRates.stream().sorted((i1,i2) -> Integer.compare(i1.functionId(),i2.functionId())).sorted((i1,i2) -> i1.label().name().compareTo(i2.label().name())).collect(Collectors.toList());
  Map<Label,Map<Integer,Double>> errorRatesMap=new HashMap<>();
  Map<Label,Map<Integer,Double>> sampleErrorRatesMap=new HashMap<>();
  double mseError=0.0;
  double madError=0.0;
  double mseErrorWeighted=0.0;
  double madErrorWeighted=0.0;
  for (  Integrator.ErrorRates.Instance instance : estimatedErrorRatesList) {
    if (!errorRatesMap.containsKey(instance.label())) {
      errorRatesMap.put(instance.label(),new HashMap<>());
      sampleErrorRatesMap.put(instance.label(),new HashMap<>());
    }
    errorRatesMap.get(instance.label()).put(instance.functionId(),instance.value());
    Optional<Integrator.ErrorRates.Instance> sampleErrorRateResultInstance=sampleErrorRates.stream().filter(i -> i.label().equals(instance.label()) && i.functionId() == instance.functionId()).findFirst();
    double sampleErrorRate=0.0;
    if (sampleErrorRateResultInstance.isPresent())     sampleErrorRate=sampleErrorRateResultInstance.get().value();
    sampleErrorRatesMap.get(instance.label()).put(instance.functionId(),sampleErrorRate);
    double seError=(instance.value() - sampleErrorRate) * (instance.value() - sampleErrorRate);
    double adError=Math.abs(instance.value() - sampleErrorRate);
    mseError+=seError;
    madError+=adError;
    mseErrorWeighted+=seError / numberOfInstances.get(instance.label());
    madErrorWeighted+=adError / numberOfInstances.get(instance.label());
  }
  mseError/=estimatedErrorRatesList.size();
  madError/=estimatedErrorRatesList.size();
  mseErrorWeighted/=labels.size();
  madErrorWeighted/=labels.size();
  Map<Integer,Map<Label,Boolean>> estimatedLabelsMap=new HashMap<>();
  Map<Integer,Map<Label,Boolean>> trueLabelsMap=new HashMap<>();
  double[] madErrorRank={0.0};
  double[] madErrorRankWeighted={0.0};
  double[] aucLabel={0.0};
  double[] madLabel={0.0};
  double[] madHardLabel={0.0};
  double[] aucLabelWeighted={0.0};
  double[] madLabelWeighted={0.0};
  double[] madHardLabelWeighted={0.0};
  integratedData.stream().map(Integrator.Data.PredictedInstance::label).distinct().forEach(label -> {
    List<Integer> rankedByErrorRate=CollectionUtilities.sortByValue(errorRatesMap.get(label)).entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
    List<Integer> rankedBySampleErrorRate=CollectionUtilities.sortByValue(sampleErrorRatesMap.get(label)).entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
    int[] ranks=new int[rankedByErrorRate.size()];
    int[] sampleRanks=new int[rankedByErrorRate.size()];
    for (int i=0; i < rankedByErrorRate.size(); i++) {
      ranks[rankedByErrorRate.get(i)]=i;
      sampleRanks[rankedBySampleErrorRate.get(i)]=i;
    }
    double tempMadErrorRank=0.0;
    double tempMadErrorRankWeighted=0.0;
    for (int i=0; i < rankedByErrorRate.size(); i++) {
      double adErrorRank=Math.abs(ranks[i] - sampleRanks[i]);
      tempMadErrorRank+=adErrorRank;
      tempMadErrorRankWeighted+=adErrorRank * numberOfInstances.get(label);
    }
    madErrorRank[0]+=tempMadErrorRank / rankedByErrorRate.size();
    madErrorRankWeighted[0]+=tempMadErrorRankWeighted / rankedByErrorRate.size();
    double[] adLabel={0};
    double[] adHardLabel={0};
    int[] numberOfSamples={0};
    Map<Integer,Boolean> observedInstances=new HashMap<>();
    observedData.stream().filter(i -> i.label().equals(label)).forEach(instance -> observedInstances.put(instance.id(),instance.value()));
    List<Boolean> observedLabels=new ArrayList<>();
    List<Double> predictions=new ArrayList<>();
    integratedData.stream().filter(i -> i.label().equals(label) && observedInstances.containsKey(i.id())).sorted((i1,i2) -> i1.value() - i2.value() == 0.0 ? 0 : i1.value() - i2.value() > 0.0 ? -1 : 1).forEach(i -> {
      observedLabels.add(observedInstances.get(i.id()));
      predictions.add(i.value());
    }
);
    double areaUnderTheCurve=PrecisionRecall.areaUnderTheCurve(observedLabels,predictions);
    aucLabel[0]+=areaUnderTheCurve;
    aucLabelWeighted[0]+=areaUnderTheCurve / numberOfInstances.get(label);
    integratedData.stream().filter(i -> i.label().equals(label) && observedInstances.containsKey(i.id())).forEach(instance -> {
      adLabel[0]+=Math.abs(instance.value() - (observedInstances.get(instance.id()) ? 1.0 : 0.0));
      if ((instance.value() >= 0.5) != observedInstances.get(instance.id()))       adHardLabel[0]++;
      numberOfSamples[0]++;
      if (!estimatedLabelsMap.containsKey(instance.id()))       estimatedLabelsMap.put(instance.id(),new HashMap<>());
      estimatedLabelsMap.get(instance.id()).put(label,instance.value() >= 0.5);
      if (!trueLabelsMap.containsKey(instance.id()))       trueLabelsMap.put(instance.id(),new HashMap<>());
      trueLabelsMap.get(instance.id()).put(label,observedInstances.get(instance.id()));
    }
);
    double accuracyLabel=adLabel[0] / (double)numberOfSamples[0];
    double accuracyHardLabel=adHardLabel[0] / (double)numberOfSamples[0];
    madLabel[0]+=accuracyLabel;
    madHardLabel[0]+=accuracyHardLabel;
    madLabelWeighted[0]+=accuracyLabel / numberOfInstances.get(label);
    madHardLabelWeighted[0]+=accuracyHardLabel / numberOfInstances.get(label);
  }
);
  madErrorRank[0]/=labels.size();
  madErrorRankWeighted[0]/=numberOfInstances.values().stream().mapToInt(n -> n).sum();
  aucLabel[0]/=labels.size();
  madLabel[0]/=labels.size();
  madHardLabel[0]/=labels.size();
  aucLabelWeighted[0]/=labels.size();
  madLabelWeighted[0]/=labels.size();
  madHardLabelWeighted[0]/=labels.size();
  return new Results(methodName,errorRatesMap,sampleErrorRatesMap,madErrorRank[0],mseError,madError,madErrorRankWeighted[0],mseErrorWeighted,madErrorWeighted,aucLabel[0],madLabel[0],madHardLabel[0],aucLabelWeighted[0],madLabelWeighted[0],madHardLabelWeighted[0]);
}