Program: 20196
Project: 20196
Buggy source:
/** 
 * Label features using heuristic described in  "Learning from Labeled Features using Generalized Expectation Criteria" Gregory Druck, Gideon Mann, Andrew McCallum.
 * @param list InstanceList used to compute statistics for labeling features.
 * @param features List of features to label.
 * @return Labeled features, HashMap mapping feature indices to list of labels.
 */
public static HashMap<Integer,ArrayList<Integer>> labelFeatures(InstanceList list,ArrayList<Integer> features){
  HashMap<Integer,ArrayList<Integer>> labeledFeatures=new HashMap<Integer,ArrayList<Integer>>();
  double[][] featureLabelCounts=getFeatureLabelCounts(list);
  int numLabels=list.getTargetAlphabet().size();
  int minRank=100 * numLabels;
  InfoGain infogain=new InfoGain(list);
  double sum=0;
  for (int rank=0; rank < minRank; rank++) {
    sum+=infogain.getValueAtRank(rank);
  }
  double mean=sum / minRank;
  for (int i=0; i < features.size(); i++) {
    int fi=features.get(i);
    if (infogain.value(fi) < mean) {
      continue;
    }
    double[] prob=featureLabelCounts[fi];
    MatrixOps.plusEquals(prob,1e-8);
    MatrixOps.timesEquals(prob,1. / MatrixOps.sum(prob));
    int[] sortedIndices=getMaxIndices(prob);
    ArrayList<Integer> labels=new ArrayList<Integer>();
    if (numLabels > 2) {
      boolean discard=false;
      double threshold=prob[sortedIndices[0]] / 2;
      for (int li=0; li < numLabels; li++) {
        if (prob[li] > threshold) {
          labels.add(li);
        }
        if (labels.size() > (numLabels / 2)) {
          discard=true;
          break;
        }
      }
      if (discard) {
        continue;
      }
    }
 else {
      labels.add(sortedIndices[0]);
    }
    labeledFeatures.put(fi,labels);
  }
  return labeledFeatures;
}

Fixed version (ground truth):
/** 
 * Label features using heuristic described in  "Learning from Labeled Features using Generalized Expectation Criteria" Gregory Druck, Gideon Mann, Andrew McCallum.
 * @param list InstanceList used to compute statistics for labeling features.
 * @param features List of features to label.
 * @return Labeled features, HashMap mapping feature indices to list of labels.
 */
public static HashMap<Integer,ArrayList<Integer>> labelFeatures(InstanceList list,ArrayList<Integer> features){
  HashMap<Integer,ArrayList<Integer>> labeledFeatures=new HashMap<Integer,ArrayList<Integer>>();
  double[][] featureLabelCounts=getFeatureLabelCounts(list);
  int numLabels=list.getTargetAlphabet().size();
  int minRank=100 * numLabels;
  InfoGain infogain=new InfoGain(list);
  double sum=0;
  for (int rank=0; rank < minRank; rank++) {
    sum+=infogain.getValueAtRank(rank);
  }
  double mean=sum / minRank;
  for (int i=0; i < features.size(); i++) {
    int fi=features.get(i);
    if (infogain.value(fi) < mean) {
      System.err.println("String_Node_Str" + list.getDataAlphabet().lookupObject(fi));
      continue;
    }
    double[] prob=featureLabelCounts[fi];
    MatrixOps.plusEquals(prob,1e-8);
    MatrixOps.timesEquals(prob,1. / MatrixOps.sum(prob));
    int[] sortedIndices=getMaxIndices(prob);
    ArrayList<Integer> labels=new ArrayList<Integer>();
    if (numLabels > 2) {
      boolean discard=false;
      double threshold=prob[sortedIndices[0]] / 2;
      for (int li=0; li < numLabels; li++) {
        if (prob[li] > threshold) {
          labels.add(li);
        }
        if (labels.size() > (numLabels / 2)) {
          discard=true;
          break;
        }
      }
      if (discard) {
        continue;
      }
    }
 else {
      labels.add(sortedIndices[0]);
    }
    labeledFeatures.put(fi,labels);
  }
  return labeledFeatures;
}