Skip to content

ARIMA models

Jacob Rachiele edited this page Dec 2, 2017 · 16 revisions

Java Time Series is one of the few open source time series libraries that supports seasonal ARIMA modeling.

There are a minimum of three steps required to fit an ARIMA model, whether seasonal or non-seasonal.

  1. Build a time series object with your data.
  2. Specify the model order. This can be conveniently done using static factory constructors in the Arima class.
  3. Build an Arima object with the time series and the model order from the first two steps.

For an example, we'll use debit cards, a monthly series made available by Rob Hyndman, which can be accessed by using TestData.debitcards. If you have your own data stored in an array that you'd like to use instead, you can use the TimeSeries class constructors or the static convenience constructors in the Ts class . To convert a list of doubles to an array of primitives, use the static method DoubleFunctions.arrayFrom(Collection<? extends Number> data).

First, import the classes we discussed above.

import com.github.signaflo.timeseries.TestData;
import com.github.signaflo.timeseries.TimeSeries;
import com.github.signaflo.timeseries.forecast.Forecast;
import com.github.signaflo.timeseries.model.Model;
import com.github.signaflo.timeseries.model.arima.Arima;
import com.github.signaflo.timeseries.model.arima.ArimaOrder;

import static com.github.signaflo.data.visualization.Plots.plot

Next, we follow step one and create the series.

TimeSeries timeSeries = TestData.debitcards;

For step two, we can specify any model order that we think is reasonable. Let's use what is referred to as the airline model from Box & Jenkins.

ArimaOrder modelOrder = ArimaOrder.order(0, 1, 1, 0, 1, 1); // Note that intercept fitting will automatically be turned off

Finally, for step three, we again use a static factory constructor to create the Arima model.

Arima model = Arima.model(timeSeries, modelOrder);

Once we have our model, there are lots of useful methods we can call on it to extract information.

For example, in jshell or in a command line app, we can do the following.

System.out.println(model.aic()); // Get and display the model AIC
System.out.println(model.coefficients()); // Get and display the estimated coefficients
System.out.println(java.util.Arrays.toString(model.stdErrors()));
plot(model.predictionErrors());

To get a 12 step ahead forecast, we call the forecast method with some number of periods ahead.

Forecast forecast = model.forecast(12); // To specify the alpha significance level, add it as a second argument.

Once we have the forecast, we can view it as a table (plotting of forecast needs to be updated).

System.out.println(forecast);