Program: 16605
Project: 16605
Buggy source:
/** 
 * Evaluate the goodness of fit of a given tree to the original distance matrix. The returned value is the coefficient of variation, i.e. the square root of the LS error normalized by the mean. <p> This measure can also give an estimate of the quality of the distance matrix, because a bad fit may mean that the distance is non-additive.
 * @param tree Phylogenetic Distance Tree to evaluate
 * @param matrix Distance Matrix with the original distances
 * @return the square root of the average tree LS error normalized by theaverage tree distance (coefficient of variation, CV).
 */
public static double evaluate(Phylogeny tree,DistanceMatrix matrix){
  int numSequences=matrix.getSize();
  List<PhylogenyNode> externalNodes=tree.getExternalNodes();
  HashMap<String,PhylogenyNode> externalNodesHashMap=new HashMap<String,PhylogenyNode>();
  Set<PhylogenyNode> path=new HashSet<PhylogenyNode>();
  for (  PhylogenyNode node : externalNodes) {
    externalNodesHashMap.put(node.getName(),node);
  }
  int count=0;
  double averageMatrixDistance=0.0;
  double averageTreeDistance=0.0;
  double averageTreeErrorDistance=0.0;
  for (int row=0; row < numSequences - 1; row++) {
    String nodeName1=matrix.getIdentifier(row);
    PhylogenyNode node1=externalNodesHashMap.get(nodeName1);
    markPathToRoot(node1,path);
    for (int col=row + 1; col < numSequences; col++) {
      count++;
      String nodeName2=matrix.getIdentifier(col);
      PhylogenyNode node2=externalNodesHashMap.get(nodeName2);
      double distance=matrix.getValue(col,row);
      averageMatrixDistance=averageMatrixDistance + distance;
      PhylogenyNode commonParent=findCommonParent(node2,path);
      if (commonParent != null) {
        double treeDistance=getNodeDistance(commonParent,node1) + getNodeDistance(commonParent,node2);
        averageTreeDistance+=treeDistance;
        averageTreeErrorDistance+=(distance - treeDistance) * (distance - treeDistance);
        logger.info("String_Node_Str",nodeName1,nodeName2,distance,treeDistance,Math.abs(distance - treeDistance));
      }
 else {
        logger.warn("String_Node_Str",node1,node2);
      }
    }
    path.clear();
  }
  averageMatrixDistance/=count;
  averageTreeDistance/=count;
  averageTreeErrorDistance/=count;
  logger.info("String_Node_Str",averageMatrixDistance);
  logger.info("String_Node_Str",averageTreeDistance);
  logger.info("String_Node_Str",averageTreeErrorDistance);
  return Math.sqrt((averageTreeErrorDistance)) / (averageMatrixDistance);
}

Fixed version (ground truth):
/** 
 * Evaluate the goodness of fit of a given tree to the original distance matrix. The returned value is the coefficient of variation, i.e. the square root of the LS error normalized by the mean. <p> This measure can also give an estimate of the quality of the distance matrix, because a bad fit may mean that the distance is non-additive.
 * @param tree Phylogenetic Distance Tree to evaluate
 * @param matrix Distance Matrix with the original distances
 * @return the square root of the average tree LS error normalized by theaverage tree distance (coefficient of variation, CV).
 */
public static double evaluate(Phylogeny tree,DistanceMatrix matrix){
  int numSequences=matrix.getSize();
  List<PhylogenyNode> externalNodes=tree.getExternalNodes();
  HashMap<String,PhylogenyNode> externalNodesHashMap=new HashMap<String,PhylogenyNode>();
  Set<PhylogenyNode> path=new HashSet<PhylogenyNode>();
  for (  PhylogenyNode node : externalNodes) {
    externalNodesHashMap.put(node.getName(),node);
  }
  int count=0;
  double averageMatrixDistance=0.0;
  double averageTreeDistance=0.0;
  double averageTreeErrorDistance=0.0;
  for (int row=0; row < numSequences - 1; row++) {
    String nodeName1=matrix.getIdentifier(row);
    PhylogenyNode node1=externalNodesHashMap.get(nodeName1);
    markPathToRoot(node1,path);
    for (int col=row + 1; col < numSequences; col++) {
      count++;
      String nodeName2=matrix.getIdentifier(col);
      PhylogenyNode node2=externalNodesHashMap.get(nodeName2);
      double distance=matrix.getValue(col,row);
      averageMatrixDistance=averageMatrixDistance + distance;
      PhylogenyNode commonParent=findCommonParent(node2,path);
      if (commonParent != null) {
        double treeDistance=getNodeDistance(commonParent,node1) + getNodeDistance(commonParent,node2);
        averageTreeDistance+=treeDistance;
        averageTreeErrorDistance+=(distance - treeDistance) * (distance - treeDistance);
        logger.info("String_Node_Str",nodeName1,nodeName2,distance,treeDistance,Math.abs(distance - treeDistance));
      }
 else {
        logger.warn("String_Node_Str",node1,node2);
      }
    }
    path.clear();
  }
  averageMatrixDistance/=count;
  averageTreeDistance/=count;
  averageTreeErrorDistance/=count;
  logger.info("String_Node_Str",averageMatrixDistance);
  logger.info("String_Node_Str",averageTreeDistance);
  logger.info("String_Node_Str",averageTreeErrorDistance);
  return Math.sqrt(averageTreeErrorDistance) / averageMatrixDistance;
}