Program: 57236
Project: 57236
Buggy source:
/** 
 * <p> Calculates the average and the standard deviation for the values of one dimension or record in the data table. Whether the average is calculated for the column or row is determined by the type of the   {@link VirtualArray}. </p> <p> The objectID has to be of the "opposing" type, i.e., if the virtualArray is of type   {@link VirtualArray}, the id has to be a dimension id. </p> <p> The std-dev is calculated in the same loop as the average, according to <a href="http://www.strchr.com/standard_deviation_in_one_pass">this blog.</a>. The problems of this method discussed there doesn not apply here since we use only values between 0 and 1.} </p>
 * @param virtualArray
 * @param table
 * @param objectID
 * @return
 */
public static Average calculateAverage(VirtualArray virtualArray,Table table,IDType objectIDType,Integer objectID){
  Average averageDimension=new Average();
  double sumOfValues=0;
  double sqrSum=0;
  int nrValidValues=0;
  ATableBasedDataDomain dataDomain=table.getDataDomain();
  IDType virtualArrayIDType=virtualArray.getIdType();
  IDType resolvedVAIDType=dataDomain.getPrimaryIDType(virtualArrayIDType);
  IDType resolvedObjectIDType=dataDomain.getPrimaryIDType(objectIDType);
  if (!resolvedVAIDType.equals(virtualArrayIDType)) {
    PerspectiveInitializationData data=new PerspectiveInitializationData();
    data.setData(virtualArray);
    Perspective tempPerspective=new Perspective(dataDomain,virtualArrayIDType);
    tempPerspective.init(data);
    virtualArray=dataDomain.convertForeignPerspective(tempPerspective).getVirtualArray();
  }
  Collection<Integer> ids;
  if (!resolvedObjectIDType.equals(objectIDType)) {
    ids=IDMappingManagerRegistry.get().getIDMappingManager(objectIDType).getID(objectIDType,resolvedObjectIDType,objectID);
  }
 else {
    ids=new ArrayList<Integer>(1);
    ids.add(objectID);
  }
  if (ids == null)   return null;
  for (  Integer virtualArrayID : virtualArray) {
    Float value;
    for (    Integer id : ids) {
      value=table.getDataDomain().getNormalizedValue(resolvedObjectIDType,id,resolvedVAIDType,virtualArrayID);
      if (value != null && !value.isNaN()) {
        sumOfValues+=value;
        sqrSum+=Math.pow(value,2);
        nrValidValues++;
      }
    }
  }
  averageDimension.arithmeticMean=sumOfValues / nrValidValues;
  averageDimension.standardDeviation=Math.sqrt(sqrSum / nrValidValues - (Math.pow(averageDimension.arithmeticMean,2)));
  return averageDimension;
}

Fixed version (ground truth):
/** 
 * <p> Calculates the average and the standard deviation for the values of one dimension or record in the data table. Whether the average is calculated for the column or row is determined by the type of the   {@link VirtualArray}. </p> <p> The objectID has to be of the "opposing" type, i.e., if the virtualArray is of type   {@link VirtualArray}, the id has to be a dimension id. </p> <p> The std-dev is calculated in the same loop as the average, according to <a href="http://www.strchr.com/standard_deviation_in_one_pass">this blog.</a>. The problems of this method discussed there doesn not apply here since we use only values between 0 and 1.} </p>
 * @param virtualArray
 * @param table
 * @param objectID
 * @return
 */
public static Average calculateAverage(VirtualArray virtualArray,Table table,IDType objectIDType,Integer objectID){
  Average averageDimension=new Average();
  double sumOfValues=0;
  double sqrSum=0;
  int nrValidValues=0;
  ATableBasedDataDomain dataDomain=table.getDataDomain();
  IDType virtualArrayIDType=virtualArray.getIdType();
  IDType resolvedVAIDType=dataDomain.getPrimaryIDType(virtualArrayIDType);
  IDType resolvedObjectIDType=dataDomain.getPrimaryIDType(objectIDType);
  if (!resolvedVAIDType.equals(virtualArrayIDType)) {
    PerspectiveInitializationData data=new PerspectiveInitializationData();
    data.setData(virtualArray);
    Perspective tempPerspective=new Perspective(dataDomain,virtualArrayIDType);
    tempPerspective.init(data);
    virtualArray=dataDomain.convertForeignPerspective(tempPerspective).getVirtualArray();
  }
  Collection<Integer> ids;
  if (!resolvedObjectIDType.equals(objectIDType)) {
    ids=IDMappingManagerRegistry.get().getIDMappingManager(objectIDType).getIDAsSet(objectIDType,resolvedObjectIDType,objectID);
  }
 else {
    ids=new ArrayList<Integer>(1);
    ids.add(objectID);
  }
  if (ids == null)   return null;
  for (  Integer virtualArrayID : virtualArray) {
    Float value;
    for (    Integer id : ids) {
      value=table.getDataDomain().getNormalizedValue(resolvedObjectIDType,id,resolvedVAIDType,virtualArrayID);
      if (value != null && !value.isNaN()) {
        sumOfValues+=value;
        sqrSum+=Math.pow(value,2);
        nrValidValues++;
      }
    }
  }
  averageDimension.arithmeticMean=sumOfValues / nrValidValues;
  averageDimension.standardDeviation=Math.sqrt(sqrSum / nrValidValues - (Math.pow(averageDimension.arithmeticMean,2)));
  return averageDimension;
}