Commit f402d49f authored by Sören Henning's avatar Sören Henning

First working version of ARIMA forecaster

parent f8770671
......@@ -111,6 +111,12 @@
<artifactId>blueprints-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
<build>
......
package anomalydetection.forecast;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import anomalydetection.timeseries.EquidistantTimeSeries;
public class ARIMAForecaster extends AbstractRForecaster {
public ARIMAForecaster(final String host, final int port) {
super(host, port);
}
@Override
protected double performRForecast(final EquidistantTimeSeries timeSeries) throws REXPMismatchException, REngineException {
// Transfer time series to R
super.rConnection.assign("timeSeries", timeSeries.toValuesArray());
// Do ARIMA forecast on time Series
super.rConnection.eval("forecast <- forecast(auto.arima(ts(timeSeries)), h=1)");
// Retrieve result
double result = super.rConnection.eval("as.numeric(forecast$mean)").asDouble();
return result;
}
}
package anomalydetection.forecast;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import anomalydetection.timeseries.EquidistantTimeSeries;
public abstract class AbstractRForecaster implements Forecaster {
final RConnection rConnection;
public AbstractRForecaster(final String host, final int port) {
try {
this.rConnection = new RConnection(host, port);
this.rConnection.eval("library(forecast)");
} catch (RserveException e) {
// TODO Exception Handling
e.printStackTrace();
throw new IllegalStateException(e); // TODO
}
}
@Override
public final double forecast(final EquidistantTimeSeries timeSeries) {
// TODO Exception Handling
try {
performRForecast(timeSeries);
} catch (REXPMismatchException | REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Double.NaN; // TODO
}
protected abstract double performRForecast(final EquidistantTimeSeries timeSeries) throws REXPMismatchException, REngineException;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment