SampleCovarianceCalculator implements Function<double[], Double> { @Override public Double evaluate(final double[]... x) { Validate.notNull(x, "x"); Validate.isTrue(x.length > 1); final double[] x1 = x[0]; final double[] x2 = x[1]; Validate.isTrue(x1.length > 1); final int n = x1.length; Validate.isTrue(x2.length == n); final double mean1 = MEAN_CALCULATOR.evaluate(x1); final double mean2 = MEAN_CALCULATOR.evaluate(x2); double sum = 0; for (int i = 0; i < n; i++) { sum += (x1[i] - mean1) * (x2[i] - mean2); } return sum / (n - 1); }  @Override Double evaluate(final double[]... x);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTS() { CALCULATOR.evaluate((double[][]) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOneTS() { CALCULATOR.evaluate(X0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOneShortTS() { CALCULATOR.evaluate(new double[] {1}, X1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongLengthTS() { CALCULATOR.evaluate(new double[] {1, 2, 3, 4, 5, 6, 7, 8}, X1); }
@Test public void test() { assertEquals(CALCULATOR.evaluate(X0, X0) / VAR0, 1, EPS); assertEquals(CALCULATOR.evaluate(X0, X1), COV1, EPS); }