public static double weightedCoefficient(double[] x,double[] y,double[] weights){
  final int xdim=x.length;
  final int ydim=y.length;
  if (xdim != ydim) {
    throw new IllegalArgumentException("Invalid arguments: arrays differ in length.");
  }
  if (xdim != weights.length) {
    throw new IllegalArgumentException("Dimensionality doesn't agree to weights.");
  }
  double sumXX=0., sumYY=0., sumXY=0., sumWe=weights[0];
  double meanX=x[0], meanY=y[0];
  for (int i=1; i < xdim; ++i) {
    final double xv=x[i], yv=y[i], w=weights[i];
    final double deltaX=xv - meanX;
    final double deltaY=yv - meanY;
    sumWe+=w;
    meanX+=deltaX * w / sumWe;
    meanY+=deltaY * w / sumWe;
    final double neltaX=xv - meanX;
    final double neltaY=yv - meanY;
    sumXX+=w * deltaX * neltaX;
    sumYY+=w * deltaY * neltaY;
    sumXY+=w * deltaX * neltaY;
  }
  if (!(sumXX > 0. && sumYY > 0.)) {
    return (sumXX == sumYY) ? 1. : 0.;
  }
  return sumXY / Math.sqrt(sumXX * sumYY);
}

public static double weightedCoefficient ( double [ ] x , double [ ] y , double [ ] weights ) { final int xdim = x . length ; final int ydim = y . length ; if ( xdim != ydim ) { throw new IllegalArgumentException ( "Invalid arguments: arrays differ in length." ) ; } if ( xdim != weights . length ) { throw new IllegalArgumentException ( "Dimensionality doesn't agree to weights." ) ; } double sumXX = 0. , sumYY = 0. , sumXY = 0. , sumWe = weights [ 0 ] ; double meanX = x [ 0 ] , meanY = y [ 0 ] ; for ( int i = 1 ; i < xdim ; ++ i ) { final double xv = x [ i ] , yv = y [ i ] , w = weights [ i ] ; final double deltaX = xv - meanX ; final double deltaY = yv - meanY ; sumWe += w ; meanX += deltaX * w / sumWe ; meanY += deltaY * w / sumWe ; final double neltaX = xv - meanX ; final double neltaY = yv - meanY ; sumXX += w * deltaX * neltaX ; sumYY += w * deltaY * neltaY ; sumXY += w * deltaX * neltaY ; } if ( ! ( sumXX > 0. && sumYY > 0. ) ) { return ( sumXX == sumYY ) ? 1. : 0. ; } return sumXY / Math . sqrt ( sumXX * sumYY ) ; }
compute the pearson product - moment correlation coefficient for two featurevectors .
