@Test public void testCorrelation1() { Number[] data1 = new Number[3]; data1[0] = (double) 1; data1[1] = (double) 2; data1[2] = (double) 3; Number[] data2 = new Number[3]; data2[0] = (double) 1; data2[1] = (double) 2; data2[2] = (double) 3; double r = Statistics.getCorrelation(data1, data2); assertEquals(1.0, r, 0.00000001); }
public static double getCorrelation(Number[] data1, Number[] data2) { if (data1 == null) { throw new IllegalArgumentException("Null 'data1' argument."); } if (data2 == null) { throw new IllegalArgumentException("Null 'data2' argument."); } if (data1.length != data2.length) { throw new IllegalArgumentException( "'data1' and 'data2' arrays must have same length." ); } int n = data1.length; double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumY2 = 0.0; double sumXY = 0.0; for (int i = 0; i < n; i++) { double x = 0.0; if (data1[i] != null) { x = data1[i].doubleValue(); } double y = 0.0; if (data2[i] != null) { y = data2[i].doubleValue(); } sumX = sumX + x; sumY = sumY + y; sumXY = sumXY + (x * y); sumX2 = sumX2 + (x * x); sumY2 = sumY2 + (y * y); } return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY), 0.5); }
Statistics { public static double getCorrelation(Number[] data1, Number[] data2) { if (data1 == null) { throw new IllegalArgumentException("Null 'data1' argument."); } if (data2 == null) { throw new IllegalArgumentException("Null 'data2' argument."); } if (data1.length != data2.length) { throw new IllegalArgumentException( "'data1' and 'data2' arrays must have same length." ); } int n = data1.length; double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumY2 = 0.0; double sumXY = 0.0; for (int i = 0; i < n; i++) { double x = 0.0; if (data1[i] != null) { x = data1[i].doubleValue(); } double y = 0.0; if (data2[i] != null) { y = data2[i].doubleValue(); } sumX = sumX + x; sumY = sumY + y; sumXY = sumXY + (x * y); sumX2 = sumX2 + (x * x); sumY2 = sumY2 + (y * y); } return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY), 0.5); } }
Statistics { public static double getCorrelation(Number[] data1, Number[] data2) { if (data1 == null) { throw new IllegalArgumentException("Null 'data1' argument."); } if (data2 == null) { throw new IllegalArgumentException("Null 'data2' argument."); } if (data1.length != data2.length) { throw new IllegalArgumentException( "'data1' and 'data2' arrays must have same length." ); } int n = data1.length; double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumY2 = 0.0; double sumXY = 0.0; for (int i = 0; i < n; i++) { double x = 0.0; if (data1[i] != null) { x = data1[i].doubleValue(); } double y = 0.0; if (data2[i] != null) { y = data2[i].doubleValue(); } sumX = sumX + x; sumY = sumY + y; sumXY = sumXY + (x * y); sumX2 = sumX2 + (x * x); sumY2 = sumY2 + (y * y); } return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY), 0.5); }  }
Statistics { public static double getCorrelation(Number[] data1, Number[] data2) { if (data1 == null) { throw new IllegalArgumentException("Null 'data1' argument."); } if (data2 == null) { throw new IllegalArgumentException("Null 'data2' argument."); } if (data1.length != data2.length) { throw new IllegalArgumentException( "'data1' and 'data2' arrays must have same length." ); } int n = data1.length; double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumY2 = 0.0; double sumXY = 0.0; for (int i = 0; i < n; i++) { double x = 0.0; if (data1[i] != null) { x = data1[i].doubleValue(); } double y = 0.0; if (data2[i] != null) { y = data2[i].doubleValue(); } sumX = sumX + x; sumY = sumY + y; sumXY = sumXY + (x * y); sumX2 = sumX2 + (x * x); sumY2 = sumY2 + (y * y); } return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY), 0.5); }  static double calculateMean(Number[] values); static double calculateMean(Number[] values,
            boolean includeNullAndNaN); static double calculateMean(Collection<Number> values); static double calculateMean(Collection<Number> values,
            boolean includeNullAndNaN); static double calculateMedian(List<Number> values); static double calculateMedian(List<Number> values,
            boolean copyAndSort); static double calculateMedian(List<Number> values, int start, int end); static double calculateMedian(List<Number> values,
            int start, int end, boolean copyAndSort); static double getStdDev(Number[] data); static double[] getLinearFit(Number[] xData, Number[] yData); static double getSlope(Number[] xData, Number[] yData); static double getCorrelation(Number[] data1, Number[] data2); static double[][] getMovingAverage(Number[] xData,
                                              Number[] yData,
                                              int period); }
Statistics { public static double getCorrelation(Number[] data1, Number[] data2) { if (data1 == null) { throw new IllegalArgumentException("Null 'data1' argument."); } if (data2 == null) { throw new IllegalArgumentException("Null 'data2' argument."); } if (data1.length != data2.length) { throw new IllegalArgumentException( "'data1' and 'data2' arrays must have same length." ); } int n = data1.length; double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumY2 = 0.0; double sumXY = 0.0; for (int i = 0; i < n; i++) { double x = 0.0; if (data1[i] != null) { x = data1[i].doubleValue(); } double y = 0.0; if (data2[i] != null) { y = data2[i].doubleValue(); } sumX = sumX + x; sumY = sumY + y; sumXY = sumXY + (x * y); sumX2 = sumX2 + (x * x); sumY2 = sumY2 + (y * y); } return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY), 0.5); }  static double calculateMean(Number[] values); static double calculateMean(Number[] values,
            boolean includeNullAndNaN); static double calculateMean(Collection<Number> values); static double calculateMean(Collection<Number> values,
            boolean includeNullAndNaN); static double calculateMedian(List<Number> values); static double calculateMedian(List<Number> values,
            boolean copyAndSort); static double calculateMedian(List<Number> values, int start, int end); static double calculateMedian(List<Number> values,
            int start, int end, boolean copyAndSort); static double getStdDev(Number[] data); static double[] getLinearFit(Number[] xData, Number[] yData); static double getSlope(Number[] xData, Number[] yData); static double getCorrelation(Number[] data1, Number[] data2); static double[][] getMovingAverage(Number[] xData,
                                              Number[] yData,
                                              int period);  }