CovarianceMatrixCalculator implements Function<DoubleTimeSeries<?>, DoubleMatrix2D> { @Override public DoubleMatrix2D evaluate(final DoubleTimeSeries<?>... x) { ArgumentChecker.notEmpty(x, "x"); final int n = x.length; final double[][] covariance = new double[n][n]; DoubleTimeSeries<?> ts; for (int i = 0; i < n; i++) { ts = x[i]; covariance[i][i] = _calculator.evaluate(ts, ts); for (int j = 0; j < i; j++) { covariance[i][j] = _calculator.evaluate(ts, x[j]); covariance[j][i] = covariance[i][j]; } } return new DoubleMatrix2D(covariance); } CovarianceMatrixCalculator(final CovarianceCalculator calculator); @Override DoubleMatrix2D evaluate(final DoubleTimeSeries<?>... x);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTSArray() { CALCULATOR.evaluate((DoubleTimeSeries<?>[]) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyTSArray() { CALCULATOR.evaluate(new DoubleTimeSeries<?>[0]); }
@Test public void test() { final DoubleMatrix2D matrix = CALCULATOR.evaluate(TS1, TS2); assertEquals(matrix.getNumberOfRows(), 2); assertEquals(matrix.getNumberOfColumns(), 2); assertEquals(matrix.getEntry(0, 0), 4. / 3, EPS); assertEquals(matrix.getEntry(1, 0), -4. / 3, EPS); assertEquals(matrix.getEntry(0, 1), -4. / 3, EPS); assertEquals(matrix.getEntry(1, 1), 4. / 3, EPS); }