PearsonsCorrelation { public double correlation(final double[] xArray, final double[] yArray) { SimpleRegression regression = new SimpleRegression(); if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } else if (xArray.length < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2); } else { for(int i=0; i<xArray.length; i++) { regression.addData(xArray[i], yArray[i]); } return regression.getR(); } } PearsonsCorrelation();  PearsonsCorrelation(double[][] data);  PearsonsCorrelation(RealMatrix matrix);  PearsonsCorrelation(Covariance covariance);  PearsonsCorrelation(RealMatrix covarianceMatrix, int numberOfObservations); RealMatrix getCorrelationMatrix(); RealMatrix getCorrelationStandardErrors(); RealMatrix getCorrelationPValues(); RealMatrix computeCorrelationMatrix(RealMatrix matrix); RealMatrix computeCorrelationMatrix(double[][] data); double correlation(final double[] xArray, final double[] yArray); RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix);  }
@Test public void testConstant() { double[] noVariance = new double[] {1, 1, 1, 1}; double[] values = new double[] {1, 2, 3, 4}; Assert.assertTrue(Double.isNaN(new PearsonsCorrelation().correlation(noVariance, values))); Assert.assertTrue(Double.isNaN(new PearsonsCorrelation().correlation(values, noVariance))); }
@Test public void testInsufficientData() { double[] one = new double[] {1}; double[] two = new double[] {2}; try { new PearsonsCorrelation().correlation(one, two); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { } RealMatrix matrix = new BlockRealMatrix(new double[][] {{0},{1}}); try { new PearsonsCorrelation(matrix); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { } }