/** * * @param historyTimeseries * timeseries * * @param modelFunc * modelFunction * @param forecastFunc * forecastfunction * @param strategy * FC strategy */ public AbstractRForecaster(final ITimeSeries<Double> historyTimeseries, final String modelFunc, final String forecastFunc, final ForecastMethod strategy) { super(historyTimeseries); this.modelFunc = modelFunc; this.forecastFunc = forecastFunc; this.strategy = strategy; if (!forecastPackageAvailable) { this.logForecastModuleNotAvailableOrLoaded(); } }
protected IForecastResult createNaNForecast(final ITimeSeries<Double> timeseries, final int numForecastSteps) { final ITimeSeries<Double> tsForecast = this.prepareForecastTS(); final ITimeSeries<Double> tsLower = this.prepareForecastTS(); final ITimeSeries<Double> tsUpper = this.prepareForecastTS(); final Double fcQuality = Double.NaN; final Double[] nanArray = new Double[numForecastSteps]; Arrays.fill(nanArray, Double.NaN); tsForecast.appendAll(nanArray); tsLower.appendAll(nanArray); tsUpper.appendAll(nanArray); return new ForecastResult(tsForecast, this.getTsOriginal(), this.getConfidenceLevel(), fcQuality, tsLower, tsUpper, this.strategy); }
@Override public int getConfidenceLevel() { if (this.supportsConfidence()) { return this.confidenceLevel; } else { if (!this.warningAlreadyLogged) { LOGGER.warn("Confidence level not supported. Falling back to 0.0."); this.warningAlreadyLogged = true; } return 0; } } }
/** * Initiation of the test, setting up the test time series. * * @param timeUnit * Used time unit */ private void initForecastWithTimeUnit(final TimeUnit tu) { this.ts = new TimeSeries<Double>(this.startTime, TimeUnit.MILLISECONDS, this.deltaTime, tu); this.steps = 1; this.mean = Double.valueOf(2.0); this.ts.append(this.mean - 2); this.ts.append(this.mean - 1); this.ts.append(this.mean); this.ts.append(this.mean + 1); this.ts.append(this.mean + 2); this.forecaster = new MeanForecaster(this.ts, MeanForecasterTest.CONFIDENCE_LEVEL); this.forecast = this.forecaster.forecast(this.steps); this.forecastSeries = this.forecast.getForecast(); this.upperSeries = this.forecast.getUpper(); this.lowerSeries = this.forecast.getLower(); }
/** * @param numForecastSteps * amount of to calculate FC steps * @return ForecastResult */ @Override public final IForecastResult forecast(final int numForecastSteps) { final ITimeSeries<Double> history = this.getTsOriginal(); IForecastResult forecast = null; if (this.satisfiesInputTSRequirements(history)) { try { forecast = this.forecastWithR(numForecastSteps); } catch (final InvalidREvaluationResultException exception) { LOGGER.warn("Exception occured when forecast with R", exception); } } if (forecast == null) { LOGGER.warn("Null result for forecast. Falling back to Double.NaN result."); forecast = this.createNaNForecast(history, numForecastSteps); } return forecast; }
@Override public AnomalyScore calculateAnomalyScore(final IForecastResult forecast, final ITimeSeriesPoint<Double> current) { if (forecast.getForecast().getPoints().size() == 0) { return null; } final Double nextpredicted = forecast.getForecast().getPoints().get(0).getValue(); if (null == nextpredicted) { return null; } double measuredValue = 0.0; measuredValue = current.getValue(); double difference = nextpredicted - measuredValue; final double sum = nextpredicted + measuredValue; difference = Math.abs(difference / sum); return new AnomalyScore(difference); } }
/** * Test if the correct calculation is done. In this case mean is bigger than the lower bounds. */ @Test public void testLowerCalculationOneStep() { Assert.assertEquals(this.steps, this.lowerSeries.size()); final ITimeSeriesPoint<Double> stepFC = this.forecast.getLower().getPoints().get(0); Assert.assertTrue(this.mean > stepFC.getValue()); }
/** * Test if the correct calculation is done. In this case mean is smaller than the upper bounds. */ @Test public void testUpperCalculationOneStep() { Assert.assertEquals(this.steps, this.upperSeries.size()); final ITimeSeriesPoint<Double> stepFC = this.forecast.getUpper().getPoints().get(0); Assert.assertTrue(this.mean < stepFC.getValue()); } }
/** * * @return TS */ protected ITimeSeries<T> prepareForecastTS() { final ITimeSeries<T> history = this.getTsOriginal(); final long startTime = history.getStartTime(); final TimeSeries<T> tsFC = new TimeSeries<T>(startTime, history.getTimeSeriesTimeUnit(), history.getDeltaTime(), history.getDeltaTimeUnit()); return tsFC; }
/** * Checks whether the input seems to be plausible. * * @return true if plausible, else false */ @Override public boolean isPlausible() { if ((this.meanAbsoluteScaledError == 0) || Double.isNaN(this.meanAbsoluteScaledError)) { return false; } final double maximumObserved = ForecastResult.calcMaximum(this.tsOriginal); final List<Double> values = this.tsForecast.getValues(); for (final Double value : values) { if ((value > (maximumObserved * 2)) || (value < 0)) { return false; } } return true; }
protected IForecastResult createNaNForecast(final ITimeSeries<Double> timeseries, final int numForecastSteps) { final ITimeSeries<Double> tsForecast = this.prepareForecastTS(); final ITimeSeries<Double> tsLower = this.prepareForecastTS(); final ITimeSeries<Double> tsUpper = this.prepareForecastTS(); final Double fcQuality = Double.NaN; final Double[] nanArray = new Double[numForecastSteps]; Arrays.fill(nanArray, Double.NaN); tsForecast.appendAll(nanArray); tsLower.appendAll(nanArray); tsUpper.appendAll(nanArray); return new ForecastResult(tsForecast, this.getTsOriginal(), this.getConfidenceLevel(), fcQuality, tsLower, tsUpper, this.strategy); }
/** * @param numForecastSteps * amount of to calculate FC steps * @return ForecastResult */ @Override public final IForecastResult forecast(final int numForecastSteps) { final ITimeSeries<Double> history = this.getTsOriginal(); IForecastResult forecast = null; if (this.satisfiesInputTSRequirements(history)) { try { forecast = this.forecastWithR(numForecastSteps); } catch (final InvalidREvaluationResultException exception) { LOG.warn("Exception occured when forecast with R", exception); } } if (forecast == null) { LOG.warn("Null result for forecast. Falling back to Double.NaN result."); forecast = this.createNaNForecast(history, numForecastSteps); } return forecast; }
@Override public AnomalyScore calculateAnomalyScore(final IForecastResult forecast, final ITimeSeriesPoint<Double> current) { if (forecast.getForecast().getPoints().size() == 0) { return null; } final Double nextpredicted = forecast.getForecast().getPoints().get(0).getValue(); if (null == nextpredicted) { return null; } double measuredValue = 0.0; measuredValue = current.getValue(); double difference = nextpredicted - measuredValue; final double sum = nextpredicted + measuredValue; difference = Math.abs(difference / sum); return new AnomalyScore(difference); } }
/** * * @param historyTimeseries * timeseries * @param modelFunc * modelFunction * @param forecastFunc * forecastfunction * @param confidenceLevel * value of confedenclevel * * @param strategy * FC strategy */ public AbstractRForecaster(final ITimeSeries<Double> historyTimeseries, final String modelFunc, final String forecastFunc, final int confidenceLevel, final ForecastMethod strategy) { super(historyTimeseries, confidenceLevel); this.modelFunc = modelFunc; this.forecastFunc = forecastFunc; this.strategy = strategy; if (!forecastPackageAvailable) { this.logForecastModuleNotAvailableOrLoaded(); } }
@Override public int getConfidenceLevel() { if (this.supportsConfidence()) { return this.confidenceLevel; } else { if (!this.warningAlreadyLogged) { LOG.warn("Confidence level not supported. Falling back to 0.0."); this.warningAlreadyLogged = true; } return 0; } } }
/** * * @return TS */ protected ITimeSeries<T> prepareForecastTS() { final ITimeSeries<T> history = this.getTsOriginal(); final long startTime = history.getStartTime(); final TimeSeries<T> tsFC = new TimeSeries<>(startTime, history.getTimeSeriesTimeUnit(), history.getDeltaTime(), history.getDeltaTimeUnit()); return tsFC; }
/** * Checks whether the input seems to be plausible. * * @return true if plausible, else false */ @Override public boolean isPlausible() { if ((this.meanAbsoluteScaledError == 0) || Double.isNaN(this.meanAbsoluteScaledError)) { return false; } final double maximumObserved = ForecastResult.calcMaximum(this.tsOriginal); final List<Double> values = this.tsForecast.getValues(); for (final Double value : values) { if ((value > (maximumObserved * 2)) || (value < 0)) { return false; } } return true; }
/** * One step of a mean calculation. */ @Test public void testMeanCalculationOneStep() { Assert.assertEquals(this.steps, this.forecastSeries.size()); final ITimeSeriesPoint<Double> stepFC = this.forecast.getForecast().getPoints().get(0); Assert.assertEquals(this.mean, stepFC.getValue()); }
/** * * @param historyTimeseries * timeseries * * @param modelFunc * modelFunction * @param forecastFunc * forecastfunction * @param strategy * FC strategy */ public AbstractRForecaster(final ITimeSeries<Double> historyTimeseries, final String modelFunc, final String forecastFunc, final ForecastMethod strategy) { super(historyTimeseries); this.modelFunc = modelFunc; this.forecastFunc = forecastFunc; this.strategy = strategy; if (!forecastPackageAvailable) { this.logForecastModuleNotAvailableOrLoaded(); } }
/** * * @param historyTimeseries * timeseries * @param modelFunc * modelFunction * @param forecastFunc * forecastfunction * @param confidenceLevel * value of confedenclevel * * @param strategy * FC strategy */ public AbstractRForecaster(final ITimeSeries<Double> historyTimeseries, final String modelFunc, final String forecastFunc, final int confidenceLevel, final ForecastMethod strategy) { super(historyTimeseries, confidenceLevel); this.modelFunc = modelFunc; this.forecastFunc = forecastFunc; this.strategy = strategy; if (!forecastPackageAvailable) { this.logForecastModuleNotAvailableOrLoaded(); } }