code: public static double spearmanRankCorrelationCoefficient(double[] a, 
                                                              double[] b) {
          check(a, b);
          int N = a.length;
          int NcubedMinusN = (N * N * N) - N;
  
          // Convert a and b into rankings.  The last value of this array is the
          // correction factor for computing the correlation based on the number
          // of ties.  (Eq. 9.6; p. 239).  Note that the ranks are not integers
          // beacused tied values are assigned the average of their ranks (e.g.,
          // a tie at positions 1 and 2 would be be assigned a rank of 1.5).
          double[] rankedA = rank(a);
          double[] rankedB = rank(b);
      
          double sumDiffs = 0;
          for (int i = 0; i < rankedA.length - 1; ++i) {
              double diff = rankedA[i] - rankedB[i];
              sumDiffs += diff * diff;
          }
  
          double aCorrectionFactor = rankedA[rankedA.length - 1];
          double bCorrectionFactor = rankedB[rankedB.length - 1];
  
          double tiesSum = aCorrectionFactor + bCorrectionFactor;
                  
          // Compute Spearman's rho using Eq. 9.7 (p. 239)
          return (NcubedMinusN - (6 * sumDiffs) - ((tiesSum) / 2d))
              / Math.sqrt((NcubedMinusN * NcubedMinusN)
                          - (tiesSum * NcubedMinusN)
                          + (aCorrectionFactor * bCorrectionFactor));
      }
docs: Computes the Spearman rank correlation coefficient for the two arrays.
  
  <p> This implementation properly accounts for ties according to the
  procedure specified in <i>Nonparametric Statistics for The Behavioral
  Sciences</i> by Sidney Siegel and N. John Castellan Jr. Second
  Ed. (1988).
  
  @throws IllegalArgumentException when the length of the two vectors are
  not the same.