MedianAbsoluteDeviationDoubleTimeSeriesFilter 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 double median = _medianCalculator.evaluate(ts); final double mad = _madCalculator.evaluate(ts); 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]; final LocalDateDoubleEntryIterator it = ts.iterator(); int i = 0, j = 0; while (it.hasNext()) { int date = it.nextTimeFast(); double value = it.currentValue(); if (Math.abs(value - median) > _standardDeviations * mad / .6745) { rejectedDates[j] = date; rejectedData[j++] = value; } else { filteredDates[i] = date; filteredData[i++] = value; } } return getFilteredSeries(filteredDates, filteredData, i, rejectedDates, rejectedData, j); } MedianAbsoluteDeviationDoubleTimeSeriesFilter(final double standardDeviations); void setStandardDeviations(final double standardDeviations); @Override FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNull() { 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 testMasked() { final LocalDateDoubleTimeSeries subSeries = TS.subSeries(DATES[0], DATES[11]); final FilteredTimeSeries result = FILTER.evaluate(ImmutableLocalDateDoubleTimeSeries.of(subSeries)); assertTimeSeries(result, 9); }
@Test public void test() { assertTimeSeries(FILTER.evaluate(TS), 498); }