SpikeDoubleTimeSeriesFilter extends TimeSeriesFilter { @Override public FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts) { ArgumentChecker.notNull(ts, "ts"); if (ts.isEmpty()) { s_logger.info("Time series was empty"); return new FilteredTimeSeries(EMPTY_SERIES, EMPTY_SERIES); } final int n = ts.size(); final int[] filteredDates = new int[n]; final double[] filteredData = new double[n]; final int[] rejectedDates = new int[n]; final double[] rejectedData = new double[n]; LocalDateDoubleEntryIterator it = ts.iterator(); int firstDate = it.nextTimeFast(); double firstValue = it.currentValue(); int secondDate = 0; double secondValue = 0; int i = 0, j = 0; while (it.hasNext()) { secondDate = it.nextTimeFast(); secondValue = it.currentValue(); if (Math.abs(firstValue / secondValue - 1) < _maxPercentageMove) { filteredDates[i] = firstDate; filteredData[i++] = firstValue; } else { rejectedDates[j] = firstDate; rejectedData[j++] = firstValue; } firstDate = secondDate; firstValue = secondValue; } if (Math.abs(secondValue / firstValue - 1) < _maxPercentageMove) { filteredDates[i] = secondDate; filteredData[i++] = secondValue; } else { rejectedDates[j] = secondDate; rejectedData[j++] = secondValue; } return getFilteredSeries(filteredDates, filteredData, i, rejectedDates, rejectedData, j); } SpikeDoubleTimeSeriesFilter(final double maxPercentageMove); void setMaxPercentageMove(final double maxPercentageMove); @Override FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTS() { FILTER.evaluate((LocalDateDoubleTimeSeries) null); }
@Test public void testEmptyTS() { final FilteredTimeSeries filtered = FILTER.evaluate(EMPTY_SERIES); assertEquals(filtered.getFilteredTS(), EMPTY_SERIES); assertEquals(filtered.getRejectedTS(), EMPTY_SERIES); }
@Test public void testInitialSpike() { final double[] data = Arrays.copyOf(DATA, N); data[0] = 100.; final LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of(DATES, data); final LocalDateDoubleTimeSeries rejected = FILTER.evaluate(ts).getRejectedTS(); assertEquals(rejected.size(), 1); assertEquals(rejected.getTimeAtIndex(0), ts.getTimeAtIndex(0)); assertEquals(rejected.getValueAtIndex(0), ts.getValueAtIndex(0)); }
@Test public void testSpike() { final double[] data = Arrays.copyOf(DATA, N); data[10] = 100.; LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of(DATES, data); FilteredTimeSeries filtered = FILTER.evaluate(ts); assertSeries(ts, filtered, 10); data[10] = -100.; ts = ImmutableLocalDateDoubleTimeSeries.of(DATES, data); filtered = FILTER.evaluate(ts); assertSeries(ts, filtered, 10); }