TimeSeriesDifferenceOperator extends Function1D<DateDoubleTimeSeries<?>, DateDoubleTimeSeries<?>> { @Override public DateDoubleTimeSeries<?> evaluate(final DateDoubleTimeSeries<?> ts) { Validate.notNull(ts, "time series"); Validate.isTrue(ts.size() > _lag, "time series length must be > _lag"); final int[] times = ts.timesArrayFast(); final double[] values = ts.valuesArrayFast(); final int n = times.length; final int[] differenceTimes = new int[n - _lag]; final double[] differenceValues = new double[n - _lag]; for (int i = _lag; i < n; i++) { differenceTimes[i - _lag] = times[i]; differenceValues[i - _lag] = values[i] - values[i - _lag]; } return ImmutableLocalDateDoubleTimeSeries.of(differenceTimes, differenceValues); } TimeSeriesDifferenceOperator();  TimeSeriesDifferenceOperator(int lag); @Override DateDoubleTimeSeries<?> evaluate(final DateDoubleTimeSeries<?> ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullTsException() { OP_DIF_1.evaluate((LocalDateDoubleTimeSeries) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void tooShortTimeSeriesException() { LocalDateDoubleTimeSeries tooShortTs = ImmutableLocalDateDoubleTimeSeries.of( new LocalDate[] {LocalDate.of(2014, 1, 2)}, new Double[] {1.0}); OP_DIF_1.evaluate(tooShortTs); }
@Test public void difference1() { DateDoubleTimeSeries<?> tsDiff1 = OP_DIF_1.evaluate(TS_1); assertEquals(NB_DATA_1 - 1, tsDiff1.size()); for (int i = 1; i < NB_DATA_1; i++) { LocalDate dateTs = TS_1.getTimeAtIndex(i); LocalDate dateDiff = (LocalDate) tsDiff1.getTimeAtIndex(i - 1); assertEquals(dateTs, dateDiff); double diffComputed = tsDiff1.getValueAtIndex(i - 1); double diffExpected = TS_1.getValueAtIndex(i) - TS_1.getValueAtIndex(i - 1); assertEquals(diffExpected, diffComputed, TOLERANCE_DIFF); } }
@Test public void difference2() { DateDoubleTimeSeries<?> tsDiff = OP_DIF_2.evaluate(TS_1); assertEquals(NB_DATA_1 - 2, tsDiff.size()); for (int i = 2; i < NB_DATA_1; i++) { LocalDate dateTs = TS_1.getTimeAtIndex(i); LocalDate dateDiff = (LocalDate) tsDiff.getTimeAtIndex(i - 2); assertEquals(dateTs, dateDiff); double diffComputed = tsDiff.getValueAtIndex(i - 2); double diffExpected = TS_1.getValueAtIndex(i) - TS_1.getValueAtIndex(i - 2); assertEquals(diffExpected, diffComputed, TOLERANCE_DIFF); } }