Program: 20192
Project: 20192
Buggy source:
public double getValue(){
  if (!cacheStale) {
    return cachedValue;
  }
  if (objWeight == 0) {
    return 0.0;
  }
  Arrays.fill(cachedGradient,0);
  int numRefDist=constraints.size();
  int numFeatures=trainingList.getDataAlphabet().size() + 1;
  int numLabels=trainingList.getTargetAlphabet().size();
  double scalingFactor=objWeight;
  if (mapping == null) {
    setMapping();
  }
  double[][] modelExpectations=new double[numRefDist][numLabels];
  double[][] ratio=new double[numRefDist][numLabels];
  double[] featureCounts=new double[numRefDist];
  double[][] scores=new double[trainingList.size()][numLabels];
  for (int ii=0; ii < trainingList.size(); ii++) {
    Instance instance=trainingList.get(ii);
    double instanceWeight=trainingList.getInstanceWeight(instance);
    if (instance.getTarget() != null) {
      continue;
    }
    FeatureVector fv=(FeatureVector)instance.getData();
    classifier.getClassificationScoresWithTemperature(instance,temperature,scores[ii]);
    for (int loc=0; loc < fv.numLocations(); loc++) {
      int featureIndex=fv.indexAtLocation(loc);
      if (constraints.containsKey(featureIndex)) {
        int cIndex=mapping.get(featureIndex);
        double val;
        if (!useValues) {
          val=1.;
        }
 else {
          val=fv.valueAtLocation(loc);
        }
        featureCounts[cIndex]+=val;
        for (int l=0; l < numLabels; l++) {
          modelExpectations[cIndex][l]+=scores[ii][l] * val * instanceWeight;
        }
      }
    }
    if (constraints.containsKey(defaultFeatureIndex)) {
      int cIndex=mapping.get(defaultFeatureIndex);
      featureCounts[cIndex]+=1;
      for (int l=0; l < numLabels; l++) {
        modelExpectations[cIndex][l]+=scores[ii][l] * instanceWeight;
      }
    }
  }
  double value=0;
  for (  int featureIndex : constraints.keySet()) {
    int cIndex=mapping.get(featureIndex);
    if (featureCounts[cIndex] > 0) {
      for (int label=0; label < numLabels; label++) {
        double cProb=constraints.get(featureIndex)[label];
        modelExpectations[cIndex][label]/=featureCounts[cIndex];
        ratio[cIndex][label]=cProb / modelExpectations[cIndex][label];
        value+=scalingFactor * cProb * Math.log(modelExpectations[cIndex][label]);
        value-=scalingFactor * cProb * Math.log(cProb);
      }
      assert(Maths.almostEquals(MatrixOps.sum(modelExpectations[cIndex]),1));
    }
  }
  for (int ii=0; ii < trainingList.size(); ii++) {
    Instance instance=trainingList.get(ii);
    if (instance.getTarget() != null) {
      continue;
    }
    double instanceWeight=trainingList.getInstanceWeight(instance);
    FeatureVector fv=(FeatureVector)instance.getData();
    for (int loc=0; loc < fv.numLocations() + 1; loc++) {
      int featureIndex;
      if (loc == fv.numLocations()) {
        featureIndex=defaultFeatureIndex;
      }
 else {
        featureIndex=fv.indexAtLocation(loc);
      }
      if (constraints.containsKey(featureIndex)) {
        int cIndex=mapping.get(featureIndex);
        if (featureCounts[cIndex] == 0) {
          continue;
        }
        double val;
        if ((featureIndex == defaultFeatureIndex) || (!useValues)) {
          val=1;
        }
 else {
          val=fv.valueAtLocation(loc);
        }
        double instanceExpectation=0;
        for (int label=0; label < numLabels; label++) {
          instanceExpectation+=ratio[cIndex][label] * scores[ii][label];
        }
        for (int label=0; label < numLabels; label++) {
          if (scores[ii][label] == 0)           continue;
          assert(!Double.isInfinite(scores[ii][label]));
          double weight=scalingFactor * instanceWeight * temperature* (val / featureCounts[cIndex])* scores[ii][label]* (ratio[cIndex][label] - instanceExpectation);
          MatrixOps.rowPlusEquals(cachedGradient,numFeatures,label,fv,weight);
          cachedGradient[numFeatures * label + defaultFeatureIndex]+=weight;
        }
      }
    }
  }
  cachedValue=value;
  cacheStale=false;
  double reg=getRegularization();
  progressLogger.info("String_Node_Str" + value + "String_Node_Str"+ reg+ "String_Node_Str"+ cachedValue);
  return value;
}

Fixed version (ground truth):
public double getValue(){
  if (!cacheStale) {
    return cachedValue;
  }
  if (objWeight == 0) {
    return 0.0;
  }
  Arrays.fill(cachedGradient,0);
  int numRefDist=constraints.size();
  int numFeatures=trainingList.getDataAlphabet().size() + 1;
  int numLabels=trainingList.getTargetAlphabet().size();
  double scalingFactor=objWeight;
  if (mapping == null) {
    setMapping();
  }
  double[][] modelExpectations=new double[numRefDist][numLabels];
  double[][] ratio=new double[numRefDist][numLabels];
  double[] featureCounts=new double[numRefDist];
  double[][] scores=new double[trainingList.size()][numLabels];
  for (int ii=0; ii < trainingList.size(); ii++) {
    Instance instance=trainingList.get(ii);
    double instanceWeight=trainingList.getInstanceWeight(instance);
    if (instance.getTarget() != null) {
      continue;
    }
    FeatureVector fv=(FeatureVector)instance.getData();
    classifier.getClassificationScoresWithTemperature(instance,temperature,scores[ii]);
    for (int loc=0; loc < fv.numLocations(); loc++) {
      int featureIndex=fv.indexAtLocation(loc);
      if (constraints.containsKey(featureIndex)) {
        int cIndex=mapping.get(featureIndex);
        double val;
        if (!useValues) {
          val=1.;
        }
 else {
          val=fv.valueAtLocation(loc);
        }
        featureCounts[cIndex]+=val;
        for (int l=0; l < numLabels; l++) {
          modelExpectations[cIndex][l]+=scores[ii][l] * val * instanceWeight;
        }
      }
    }
    if (constraints.containsKey(defaultFeatureIndex)) {
      int cIndex=mapping.get(defaultFeatureIndex);
      featureCounts[cIndex]+=1;
      for (int l=0; l < numLabels; l++) {
        modelExpectations[cIndex][l]+=scores[ii][l] * instanceWeight;
      }
    }
  }
  double value=0;
  for (  int featureIndex : constraints.keySet()) {
    int cIndex=mapping.get(featureIndex);
    if (featureCounts[cIndex] > 0) {
      for (int label=0; label < numLabels; label++) {
        double cProb=constraints.get(featureIndex)[label];
        modelExpectations[cIndex][label]/=featureCounts[cIndex];
        ratio[cIndex][label]=cProb / modelExpectations[cIndex][label];
        value+=scalingFactor * cProb * Math.log(modelExpectations[cIndex][label]);
        if (cProb > 0) {
          value-=scalingFactor * cProb * Math.log(cProb);
        }
      }
      assert(Maths.almostEquals(MatrixOps.sum(modelExpectations[cIndex]),1));
    }
  }
  for (int ii=0; ii < trainingList.size(); ii++) {
    Instance instance=trainingList.get(ii);
    if (instance.getTarget() != null) {
      continue;
    }
    double instanceWeight=trainingList.getInstanceWeight(instance);
    FeatureVector fv=(FeatureVector)instance.getData();
    for (int loc=0; loc < fv.numLocations() + 1; loc++) {
      int featureIndex;
      if (loc == fv.numLocations()) {
        featureIndex=defaultFeatureIndex;
      }
 else {
        featureIndex=fv.indexAtLocation(loc);
      }
      if (constraints.containsKey(featureIndex)) {
        int cIndex=mapping.get(featureIndex);
        if (featureCounts[cIndex] == 0) {
          continue;
        }
        double val;
        if ((featureIndex == defaultFeatureIndex) || (!useValues)) {
          val=1;
        }
 else {
          val=fv.valueAtLocation(loc);
        }
        double instanceExpectation=0;
        for (int label=0; label < numLabels; label++) {
          instanceExpectation+=ratio[cIndex][label] * scores[ii][label];
        }
        for (int label=0; label < numLabels; label++) {
          if (scores[ii][label] == 0)           continue;
          assert(!Double.isInfinite(scores[ii][label]));
          double weight=scalingFactor * instanceWeight * temperature* (val / featureCounts[cIndex])* scores[ii][label]* (ratio[cIndex][label] - instanceExpectation);
          MatrixOps.rowPlusEquals(cachedGradient,numFeatures,label,fv,weight);
          cachedGradient[numFeatures * label + defaultFeatureIndex]+=weight;
        }
      }
    }
  }
  cachedValue=value;
  cacheStale=false;
  double reg=getRegularization();
  progressLogger.info("String_Node_Str" + value + "String_Node_Str"+ reg+ "String_Node_Str"+ cachedValue);
  return value;
}