# ARIMA Models

Auto-Regressive Integrated Moving Average (ARIMA) models are used to model time series data and produce forecasts for future values.

The type of ARIMA model is usually specified as ARIMA(p,d,q). p is the autoregressive (AR)order, or the number of autoregressive components. q is the moving average (MA) order, or the number of moving average components in the model. d is the degree of differencing. When d is zero, the model is called an ARMA(p,q) model. When in addition p or q is zero, the model is called an MA(q) model or AR(p) model, respectively.

ARIMA models are implemented by the ArimaModel class.

## Creating ARIMA Models

The ArimaModel class has two constructors. The first takes three arguments and is used to construct an ARMA(p,q) model. The first argument is a Vector<T> that contains the time series data. The second and third arguments are the autoregressive and moving average order, respectively. The following creates an ARMA(2,1), or equivalently, an ARIMA(2,0,1) model for a time series vector y.

C#
``var model1 = new ArimaModel(y, 2, 1);``

The second constructor takes four arguments and is used to construct ARIMA(p,d,q) models. The first argument is once again the Vector<T> that contains the time series data. The remaining arguments are the autoregressive order, degree of differencing, and the moving average order. For example, to create an ARIMA(1,1,1) model, we can write:

C#
``var model2 = new ArimaModel(y, 1, 1, 1);``

For an ARMA(p,q) model, the mean is usually not zero, and it is estimated by default. When a series is differenced enough times, the mean of the differenced series will be close to zero. For ARIMA models with nonzero degree of differencing, the mean is not estimated by default. In each case, the default behavior can be overridden by setting the EstimateMean property.

## Computing the Model

The Compute method estimates the parameters of the model by minimizing the exact maximum likelihood function.

The parameters of the model can be retrieved through the Parameters collection. The first p parameters are the coefficients of the autoregressive components. The next q parameters are the coefficients of the moving average components. If the mean was estimated also, it is returned as the last parameter. The autoregressive and moving average coefficients can also be retrieved separately through the AutoRegressiveParameters and MovingAverageParameters properties.

The members of these collections are of type Parameter<T>, and can be used to obtain a wide range of information about the computed values, including the standard error, significance tests and confidence intervals.

C#
``````model1.Fit();
Console.WriteLine("AR(0): {0}", model1.AutoRegressiveParameters[0].Value);
Console.WriteLine("MA(0): {0}", model1.MovingAverageParameters[0].Value);
Console.WriteLine("Mean: {0}", model1.Parameters[3].Value);
Console.WriteLine(model1.ParameterValues);``````

## Verifying the Quality of the Model

Because an ARIMA model is computed by directly maximizing the likelihood function, it does not have the same range of diagnostic values available for linear regression models. Still, a number of standard measures are available.

The LogLikelihood method returns the logarithm of the likelihood of the computed model. The GetAkaikeInformationCriterion method returns the Akaike Information Criterion (AIC) value. This is commonly used to compare different models. The GetBayesianInformationCriterion method returns the Bayesian Information Criterion (BIC) value, which is sometimes used instead of the AIC.

C#
``````var ll = model1.LogLikelihood;
var aic = model1.GetAkaikeInformationCriterion();
var bic = model1.GetBayesianInformationCriterion();``````

Another way to test the model is by looking at the residuals. If the model is adequate, the residuals should no longer be correlated. This can be tested using the Ljung-Box test. This test is implemented by the LjungBoxTest class. The ARIMA model object provides a convenience method, GetLjungBoxTest, that returns the Ljung-Box test with the correct degrees of freedom. The method takes one argument: the number of auto-correlations to include in the test.

C#
``````var lb = model1.GetLjungBoxTest(5);
Console.WriteLine("Ljung-Box χ2: {0:F2}", lb.Statistic);
Console.WriteLine("Ljung-Box p: {0:F4}", lb.PValue);``````

## Forecasting

Once the model has been computed, the Forecast method can then be used to forecast new values. This method has three overloads.

Without arguments, the method returns the one step ahead forecast based on the computed model. With a single argument, it computes a point forecast the specified number of steps ahead. It returns a Vector<T> that contains the point forecast for the specified number of periods.

C#
``var forecast = model2.Forecast(4);``