code: public static double pearsonCorrelation(double[] x, double[] y) {
  		double result;
  		double sum_sq_x = 0, sum_sq_y = 0;
      double mean_x = x[0], mean_y = y[0];
  		double sum_coproduct = 0;
  		for(int i=2; i<x.length+1;++i) {
  			double w = (i - 1)*1.0/i;
  			double delta_x = x[i-1] - mean_x;
  			double delta_y = y[i-1] - mean_y;
  			sum_sq_x += delta_x * delta_x*w;
  			sum_sq_y += delta_y * delta_y*w;
  			sum_coproduct += delta_x * delta_y*w;
  			mean_x += delta_x / i;
  			mean_y += delta_y / i;
  		}
  		double pop_sd_x = Math.sqrt(sum_sq_x/x.length);
  		double pop_sd_y = Math.sqrt(sum_sq_y/y.length);
  		double cov_x_y = sum_coproduct / x.length;
      double denom = pop_sd_x*pop_sd_y;
      if(denom == 0.0)
        return 0.0;
      result = cov_x_y/denom;
  		return result;
  	}
docs: Direct computation of Pearson product-moment correlation coefficient.
  Note that if x and y are involved in several computations of
  pearsonCorrelation, it is perhaps more advisable to first standardize
  x and y, then compute innerProduct(x,y)/(x.length-1).