public static double correlation(ExampleSet exampleSet,Attribute firstAttribute,Attribute secondAttribute,boolean squared){
  double sumProd=0.0d;
  double sumFirst=0.0d;
  double sumSecond=0.0d;
  double sumFirstSquared=0.0d;
  double sumSecondSquared=0.0d;
  int counter=0;
  Iterator<Example> reader=exampleSet.iterator();
  while (reader.hasNext()) {
    Example example=reader.next();
    double first=example.getValue(firstAttribute);
    double second=example.getValue(secondAttribute);
    double prod=first * second;
    if (!Double.isNaN(prod)) {
      sumProd+=prod;
      sumFirst+=first;
      sumFirstSquared+=first * first;
      sumSecond+=second;
      sumSecondSquared+=second * second;
      counter++;
    }
  }
  double divisor=Math.sqrt((counter * sumFirstSquared - sumFirst * sumFirst) * (counter * sumSecondSquared - sumSecond * sumSecond));
  double r;
  if (divisor == 0) {
    r=Double.NaN;
  }
 else {
    r=(counter * sumProd - sumFirst * sumSecond) / divisor;
  }
  if (squared) {
    return r * r;
  }
 else {
    return r;
  }
}

public static double correlation ( ExampleSet exampleSet , Attribute firstAttribute , Attribute secondAttribute , boolean squared ) { double sumProd = 0.0d ; double sumFirst = 0.0d ; double sumSecond = 0.0d ; double sumFirstSquared = 0.0d ; double sumSecondSquared = 0.0d ; int counter = 0 ; Iterator < Example > reader = exampleSet . iterator ( ) ; while ( reader . hasNext ( ) ) { Example example = reader . next ( ) ; double first = example . getValue ( firstAttribute ) ; double second = example . getValue ( secondAttribute ) ; double prod = first * second ; if ( ! Double . isNaN ( prod ) ) { sumProd += prod ; sumFirst += first ; sumFirstSquared += first * first ; sumSecond += second ; sumSecondSquared += second * second ; counter ++ ; } } double divisor = Math . sqrt ( ( counter * sumFirstSquared - sumFirst * sumFirst ) * ( counter * sumSecondSquared - sumSecond * sumSecond ) ) ; double r ; if ( divisor == 0 ) { r = Double . NaN ; } else { r = ( counter * sumProd - sumFirst * sumSecond ) / divisor ; } if ( squared ) { return r * r ; } else { return r ; } }
this method calculates the correlation between two ( numerical ) attributes of an example set .
