# NAG Library Routine Document

## 1Purpose

g13adf calculates preliminary estimates of the parameters of an autoregressive integrated moving average (ARIMA) model from the autocorrelation function of the appropriately differenced times series.

## 2Specification

Fortran Interface
 Subroutine g13adf ( mr, r, nk, xv, npar, wa, nwa, par, rv, isf,
 Integer, Intent (In) :: mr(7), nk, npar, nwa Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: isf(4) Real (Kind=nag_wp), Intent (In) :: r(nk), xv Real (Kind=nag_wp), Intent (Out) :: wa(nwa), par(npar), rv
#include nagmk26.h
 void g13adf_ (const Integer mr[], const double r[], const Integer *nk, const double *xv, const Integer *npar, double wa[], const Integer *nwa, double par[], double *rv, Integer isf[], Integer *ifail)

## 3Description

Preliminary estimates of the $p$ non-seasonal autoregressive parameters ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$ and the $q$ non-seasonal moving average parameters ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$ may be obtained from the sample autocorrelations relating to lags $1$ to $p+q$, i.e., ${r}_{1},\dots ,{r}_{p+q}$, of the differenced ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{t}$, where ${x}_{t}$ is assumed to follow a (possibly) seasonal ARIMA model (see Section 3 in g13aef for the specification of an ARIMA model).
Taking ${r}_{0}=1$ and ${r}_{-k}={r}_{k}$, the ${\varphi }_{i}$, for $\mathit{i}=1,2,\dots ,p$ are the solutions to the equations
 $r q+i-1 ϕ1+ r q+i-2 ϕ2 +⋯+ r q+i-p ϕp = rq+i , i=1,2,…,p.$
The ${\theta }_{j}$, for $\mathit{j}=1,2,\dots ,q$, are obtained from the solutions to the equations
 $cj=τ0τj+τ1τj+1+⋯+τq+jτq, j=0,1,…,q$
(Cramer Wold-factorization), by setting
 $θj=-τjτ0 ,$
where ${c}_{j}$ are the ‘covariances’ modified in a two stage process by the autoregressive parameters.
Stage 1:
 $dj=rj-ϕ1rj-1-⋯-ϕprj-p, j=0,1,…,q; dj=0, j=q+1,q+2,…,p+q.$
Stage 2:
 $cj=dj-ϕ1dj+ 1-ϕ2dj+ 2-⋯-ϕpdj+p, j= 0,1,…,q.$
The $P$ seasonal autoregressive parameters ${\Phi }_{1},{\Phi }_{2},\dots ,{\Phi }_{P}$ and the $Q$ seasonal moving average parameters ${\Theta }_{1},{\Theta }_{2},\dots ,{\Theta }_{Q}$ are estimated in the same way as the non-seasonal parameters, but each ${r}_{j}$ is replaced in the calculation by ${r}_{s×j}$, where $s$ is the seasonal period.
An estimate of the residual variance is obtained by successively reducing the sample variance, first for non-seasonal, and then for seasonal, parameter estimates. If moving average parameters are estimated, the variance is reduced by a multiplying factor of ${\tau }_{0}^{2}$, but otherwise by ${c}_{0}$.
Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day

## 5Arguments

1:     $\mathbf{mr}\left(7\right)$ – Integer arrayInput
On entry: the orders vector $\left(p,d,q,P,D,Q,s\right)$ of the ARIMA model whose parameters are to be estimated. $p$, $q$, $P$ and $Q$ refer respectively to the number of autoregressive $\left(\varphi \right)$, moving average $\left(\theta \right)$, seasonal autoregressive $\left(\Phi \right)$ and seasonal moving average $\left(\Theta \right)$ parameters. $d$, $D$ and $s$ refer respectively to the order of non-seasonal differencing, the order of seasonal differencing and the seasonal period.
Constraints:
• $p,d,q,P,D,Q,s\ge 0$;
• $p+q+P+Q>0$;
• $s\ne 1$;
• if $s=0$, $P+D+Q=0$;
• if $s>1$, $P+D+Q>0$.
2:     $\mathbf{r}\left({\mathbf{nk}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the autocorrelations (starting at lag $1$), which must have been calculated after the time series has been appropriately differenced.
Constraint: $-1.0\le {\mathbf{r}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{nk}}$.
3:     $\mathbf{nk}$ – IntegerInput
On entry: the maximum lag of the autocorrelations in array r.
Constraint: ${\mathbf{nk}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p+q,s×\left(P+Q\right)\right)$.
4:     $\mathbf{xv}$ – Real (Kind=nag_wp)Input
On entry: the series sample variance, calculated after appropriate differencing has been applied to the series.
Constraint: ${\mathbf{xv}}>0.0$.
5:     $\mathbf{npar}$ – IntegerInput
On entry: the exact number of parameters specified in the model by array mr.
Constraint: ${\mathbf{npar}}=p+q+P+Q$.
6:     $\mathbf{wa}\left({\mathbf{nwa}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
7:     $\mathbf{nwa}$ – IntegerInput
On entry: the amount of workspace available.
Constraint: if ${\mathbf{mr}}=\left(p,d,q,P,D,Q,s\right)$ and ${p}^{\prime }=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,P\right)$ and ${q}^{\prime }=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(q,Q\right)$, ${\mathbf{nwa}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({{p}^{\prime }}^{2}+{p}^{\prime },4\left({q}^{\prime }+1\right)\right)$.
8:     $\mathbf{par}\left({\mathbf{npar}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the first npar elements of par contain the preliminary estimates of the ARIMA model parameters, in standard order.
9:     $\mathbf{rv}$ – Real (Kind=nag_wp)Output
On exit: an estimate of the residual variance of the preliminarily estimated model.
10:   $\mathbf{isf}\left(4\right)$ – Integer arrayOutput
On exit: contains success/failure indicators, one for each of the four types of parameter (autoregressive, moving average, seasonal autoregressive, seasonal moving average).
The indicator has the interpretation:
 $\phantom{-}0$ No parameter of this type is in the model. $\phantom{-}1$ Parameters of this type appear in the model and satisfactory preliminary estimates of this type were obtained. $-1$ Parameters of this type appear in the model but satisfactory preliminary estimates of this type were not obtainable. The estimates of this type of parameter were set to $0.0$ in array par.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, the orders vector mr is invalid. One of the constraints in Section 5 has been violated.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{nk}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p+q,s×\left(P+Q\right)\right)$. There are not enough autocorrelations to enable the required model to be estimated.
${\mathbf{ifail}}=3$
 On entry, at least one element of r lies outside the range $\left[-1.0,1.0\right]$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{xv}}\le 0.0$.
${\mathbf{ifail}}=5$
 On entry, ${\mathbf{npar}}\ne p+q+P+Q$.
${\mathbf{ifail}}=6$
 On entry, the workspace array wa is too small. See Section 5 for the minimum size formula.
${\mathbf{ifail}}=7$
Satisfactory parameter estimates could not be obtained for all parameter types in the model. Inspect array isf for indicators of the parameter type(s) which could not be estimated.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The performance of the algorithm is conditioned by the roots of the autoregressive and moving average operators. If these are not close to unity in modulus, the errors, $e$, should satisfy $e<100\epsilon$ where $\epsilon$ is machine precision.

## 8Parallelism and Performance

g13adf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by g13adf is approximately proportional to $\left({p}^{3}+{q}^{2}+{P}^{3}+{Q}^{2}\right)\text{.}$

## 10Example

This example reads the sample autocorrelations to lag $40$ and the sample variance of the lagged and doubly differenced series of airline passenger totals (Box and Jenkins example series G (see Box and Jenkins (1976))). Preliminary estimates of the parameters of the $\left(0,1,1,0,1,1,12\right)$ model are obtained by a call to g13adf.