@Test public void testCorrelation2() { Number[] data1 = new Number[20]; data1[0] = (double) 68; data1[1] = (double) 71; data1[2] = (double) 62; data1[3] = (double) 75; data1[4] = (double) 58; data1[5] = (double) 60; data1[6] = (double) 67; data1[7] = (double) 68; data1[8] = (double) 71; data1[9] = (double) 69; data1[10] = (double) 68; data1[11] = (double) 67; data1[12] = (double) 63; data1[13] = (double) 62; data1[14] = (double) 60; data1[15] = (double) 63; data1[16] = (double) 65; data1[17] = (double) 67; data1[18] = (double) 63; data1[19] = (double) 61; Number[] data2 = new Number[20]; data2[0] = 4.1; data2[1] = 4.6; data2[2] = 3.8; data2[3] = 4.4; data2[4] = 3.2; data2[5] = 3.1; data2[6] = 3.8; data2[7] = 4.1; data2[8] = 4.3; data2[9] = 3.7; data2[10] = 3.5; data2[11] = 3.2; data2[12] = 3.7; data2[13] = 3.3; data2[14] = 3.4; data2[15] = 4.0; data2[16] = 4.1; data2[17] = 3.8; data2[18] = 3.4; data2[19] = 3.6; double r = Statistics.getCorrelation(data1, data2); assertEquals(0.7306356862792885, r, 0.000000000001); }
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);  }