Program: 32978
Project: 32978
Buggy source:
/** 
 * TODO: better version.
 * @param parameterIndex
 * @return
 */
private double calGradient(int parameterIndex){
  double count=0;
  if (parameterIndex < numWeightsForFeatures) {
    int classIndex=cacheToClass[parameterIndex];
    int featureIndex=cacheToFeature[parameterIndex];
    for (int i=0; i < dataSet.getNumDataPoints(); i++) {
      double featureValue=(featureIndex == -1) ? 1.0 : dataSet.getRow(i).get(featureIndex);
      double fValue=0.0;
      if (dataSet.getMultiLabels()[i].matchClass(classIndex)) {
        fValue=featureValue;
      }
      double sumValue=0.0;
      double[] probs=this.probabilityMatrix.getProbabilitiesForData(i);
      for (int num=0; num < probs.length; num++) {
        if (supportedCombinations.get(num).matchClass(classIndex)) {
          sumValue+=probs[num] * featureValue;
        }
      }
      count+=(sumValue - fValue);
    }
    if (featureIndex != -1) {
      count+=cmlcrf.getWeights().getWeightForIndex(parameterIndex) / gaussianPriorVariance;
    }
  }
 else {
    int start=parameterIndex - numWeightsForFeatures;
    int l1=cacheToL1[start];
    int l2=cacheToL2[start];
    int featureCase=start % 4;
    for (int i=0; i < dataSet.getNumDataPoints(); i++) {
      MultiLabel label=dataSet.getMultiLabels()[i];
      double fValue=0.0;
switch (featureCase) {
case 0:
        if (!label.matchClass(l1) && !label.matchClass(l2))         fValue=1.0;
      break;
case 1:
    if (label.matchClass(l1) && !label.matchClass(l2))     fValue=1.0;
  break;
case 2:
if (!label.matchClass(l1) && label.matchClass(l2)) fValue=1.0;
break;
case 3:
if (label.matchClass(l1) && label.matchClass(l2)) fValue=1.0;
break;
default :
throw new RuntimeException("String_Node_Str" + featureCase + "String_Node_Str");
}
double sumValue=0.0;
double[] probs=this.probabilityMatrix.getProbabilitiesForData(i);
for (int num=0; num < probs.length; num++) {
MultiLabel label1=supportedCombinations.get(num);
switch (featureCase) {
case 0:
if (!label1.matchClass(l1) && !label1.matchClass(l2)) sumValue+=probs[num];
break;
case 1:
if (label1.matchClass(l1) && !label1.matchClass(l2)) sumValue+=probs[num];
break;
case 2:
if (!label1.matchClass(l1) && label1.matchClass(l2)) sumValue+=probs[num];
break;
case 3:
if (label1.matchClass(l1) && label1.matchClass(l2)) sumValue+=probs[num];
break;
default :
throw new RuntimeException("String_Node_Str" + featureCase + "String_Node_Str");
}
}
count+=(sumValue - fValue);
}
count+=cmlcrf.getWeights().getWeightForIndex(parameterIndex) / gaussianPriorVariance;
}
return count;
}

Fixed version (ground truth):
/** 
 * TODO: better version.
 * @param parameterIndex
 * @return
 */
private double calGradient(int parameterIndex){
  double count=0;
  if (parameterIndex < numWeightsForFeatures) {
    int classIndex=parameterToClass[parameterIndex];
    int featureIndex=parameterToFeature[parameterIndex];
    for (int i=0; i < dataSet.getNumDataPoints(); i++) {
      double featureValue=(featureIndex == -1) ? 1.0 : dataSet.getRow(i).get(featureIndex);
      double fValue=0.0;
      if (dataSet.getMultiLabels()[i].matchClass(classIndex)) {
        fValue=featureValue;
      }
      double sumValue=0.0;
      double[] probs=this.probabilityMatrix.getProbabilitiesForData(i);
      for (int num=0; num < probs.length; num++) {
        if (supportedCombinations.get(num).matchClass(classIndex)) {
          sumValue+=probs[num] * featureValue;
        }
      }
      count+=(sumValue - fValue);
    }
    if (featureIndex != -1) {
      count+=cmlcrf.getWeights().getWeightForIndex(parameterIndex) / gaussianPriorVariance;
    }
  }
 else {
    int start=parameterIndex - numWeightsForFeatures;
    int l1=parameterToL1[start];
    int l2=parameterToL2[start];
    int featureCase=start % 4;
    for (int i=0; i < dataSet.getNumDataPoints(); i++) {
      MultiLabel label=dataSet.getMultiLabels()[i];
      double fValue=0.0;
switch (featureCase) {
case 0:
        if (!label.matchClass(l1) && !label.matchClass(l2))         fValue=1.0;
      break;
case 1:
    if (label.matchClass(l1) && !label.matchClass(l2))     fValue=1.0;
  break;
case 2:
if (!label.matchClass(l1) && label.matchClass(l2)) fValue=1.0;
break;
case 3:
if (label.matchClass(l1) && label.matchClass(l2)) fValue=1.0;
break;
default :
throw new RuntimeException("String_Node_Str" + featureCase + "String_Node_Str");
}
double sumValue=0.0;
double[] probs=this.probabilityMatrix.getProbabilitiesForData(i);
for (int num=0; num < probs.length; num++) {
MultiLabel label1=supportedCombinations.get(num);
switch (featureCase) {
case 0:
if (!label1.matchClass(l1) && !label1.matchClass(l2)) sumValue+=probs[num];
break;
case 1:
if (label1.matchClass(l1) && !label1.matchClass(l2)) sumValue+=probs[num];
break;
case 2:
if (!label1.matchClass(l1) && label1.matchClass(l2)) sumValue+=probs[num];
break;
case 3:
if (label1.matchClass(l1) && label1.matchClass(l2)) sumValue+=probs[num];
break;
default :
throw new RuntimeException("String_Node_Str" + featureCase + "String_Node_Str");
}
}
count+=(sumValue - fValue);
}
count+=cmlcrf.getWeights().getWeightForIndex(parameterIndex) / gaussianPriorVariance;
}
return count;
}