G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13ASF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G13ASF is a diagnostic checking routine suitable for use after fitting a Box–Jenkins ARMA model to a univariate time series using G13AEF or G13AFF. The residual autocorrelation function is returned along with an estimate of its asymptotic standard errors and correlations. Also, G13ASF calculates the Box–Ljung portmanteau statistic and its significance level for testing model adequacy.

## 2  Specification

 SUBROUTINE G13ASF ( N, V, MR, M, PAR, NPAR, ISHOW, R, RCM, LDRCM, CHI, IDF, SIGLEV, IW, LIW, WORK, LWORK, IFAIL)
 INTEGER N, MR(7), M, NPAR, ISHOW, LDRCM, IDF, IW(LIW), LIW, LWORK, IFAIL REAL (KIND=nag_wp) V(N), PAR(NPAR), R(M), RCM(LDRCM,M), CHI, SIGLEV, WORK(LWORK)

## 3  Description

Consider the univariate multiplicative autoregressive-moving average model
 $ϕBΦBsWt-μ=θBΘBsεt$ (1)
where ${W}_{t}$, for $\mathit{t}=1,2,\dots ,n$, denotes a time series and ${\epsilon }_{t}$, for $\mathit{t}=1,2,\dots ,n$, is a residual series assumed to be normally distributed with zero mean and variance ${\sigma }^{2}$ ($\text{}>0$). The ${\epsilon }_{t}$'s are also assumed to be uncorrelated. Here $\mu$ is the overall mean term, $s$ is the seasonal period and $B$ is the backward shift operator such that ${B}^{r}{W}_{t}={W}_{t-r}$. The polynomials in (1) are defined as follows:
 $ϕB=1-ϕ1B-ϕ2B2-⋯-ϕpBp$
is the non-seasonal autoregressive (AR) operator;
 $θ B= 1-θ1B-θ2B2-⋯-θqBq$
is the non-seasonal moving average (MA) operator;
 $ΦBs=1-Φ1Bs-Φ2B2s-⋯-ΦPBPs$
is the seasonal AR operator; and
 $Θ Bs= 1-Θ1Bs-Θ2B2s-⋯-ΘQBQs$
is the seasonal MA operator. The model (1) is assumed to be stationary, that is the zeros of $\varphi \left(B\right)$ and $\Phi \left({B}^{s}\right)$ are assumed to lie outside the unit circle. The model (1) is also assumed to be invertible, that is the zeros of $\theta \left(B\right)$ and $\Theta \left({B}^{s}\right)$ are assumed to lie outside the unit circle. When both $\Phi \left({B}^{s}\right)$ and $\Theta \left({B}^{s}\right)$ are absent from the model, that is when $P=Q=0$, then the model is said to be non-seasonal.
The estimated residual autocorrelation coefficient at lag $l$, ${\stackrel{^}{r}}_{l}$, is computed as:
 $r^l=∑t=l+1nε^t-l-ε-ε^t-ε- ∑t=1n ε^t-ε- 2 , l=1,2,…$
where ${\stackrel{^}{\epsilon }}_{t}$ denotes an estimate of the $t$th residual, ${\epsilon }_{t}$, and $\stackrel{-}{\epsilon }=\sum _{t=1}^{n}{\stackrel{^}{\epsilon }}_{t}/n$. A portmanteau statistic, ${Q}_{\left(m\right)}$, is calculated from the formula (see Box and Ljung (1978)):
 $Qm=nn+2∑l=1mr^l2/n-l$
where $m$ denotes the number of residual autocorrelations computed. (Advice on the choice of $m$ is given in Section 8.2.) Under the hypothesis of model adequacy, ${Q}_{\left(m\right)}$ has an asymptotic ${\chi }^{2}$-distribution on $m-p-q-P-Q$ degrees of freedom. Let ${\stackrel{^}{r}}^{\mathrm{T}}=\left({\stackrel{^}{r}}_{1},{\stackrel{^}{r}}_{2},\dots ,{\stackrel{^}{r}}_{m}\right)$ then the variance-covariance matrix of $\stackrel{^}{r}$ is given by:
 $Varr^=Im-XXTX-1XT/n.$
The construction of the matrix $X$ is discussed in McLeod (1978). (Note that the mean, $\mu$, and the residual variance, ${\sigma }^{2}$, play no part in calculating $\mathrm{Var}\left(\stackrel{^}{r}\right)$ and therefore are not required as input to G13ASF.)
Note:  for additive models with fixed parameter values (i.e., fitted by G13DDF) G13DSF should be used instead of G13ASF.
Box G E P and Ljung G M (1978) On a measure of lack of fit in time series models Biometrika 65 297–303
McLeod A I (1978) On the distribution of the residual autocorrelations in Box–Jenkins models J. Roy. Statist. Soc. Ser. B 40 296–302

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations in the residual series.
If G13ASF is used following a call to G13AEF, then N must be the value ${\mathbf{ICOUNT}}\left(2\right)$ returned by G13AEF.
If G13ASF is used following a call to G13AFF, then N must be the value NRES returned by G13AFF.
Constraint: ${\mathbf{N}}\ge 3$.
2:     V(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{V}}\left(\mathit{t}\right)$ must contain an estimate of ${\epsilon }_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$.
If G13ASF is used following a call to G13AEF then the actual argument V must be ${\mathbf{EXR}}\left({\mathbf{ICOUNT}}\left(1\right)+1\right)$ as returned by G13AEF.
If G13ASF is used following a call to G13AFF then the actual argument V must be RES as returned by G13AFF.
Constraint: ${\mathbf{V}}$ must contain at least two distinct elements.
3:     MR($7$) – INTEGER arrayInput
On entry: the orders vector ($p$, $d$, $q$, $P$, $D$, $Q$, $s$) as supplied to G13AEF or G13AFF.
Constraints:
• $p,q,P,Q,s\ge 0$;
• $p+q+P+Q>0$;
• if $s=0$, then $P=0$ and $Q=0$.
4:     M – INTEGERInput
On entry: the value of $m$, the number of residual autocorrelations to be computed. See Section 8.2 for advice on the value of M.
Constraint: ${\mathbf{NPAR}}<{\mathbf{M}}<{\mathbf{N}}$.
5:     PAR(NPAR) – REAL (KIND=nag_wp) arrayInput
On entry: the parameter estimates in the order ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$, ${\Phi }_{1},{\Phi }_{2},\dots ,{\Phi }_{P}$, ${\Theta }_{1},{\Theta }_{2},\dots ,{\Theta }_{Q}$ only.
Constraint: the elements in PAR must satisfy the stationarity and invertibility conditions.
6:     NPAR – INTEGERInput
On entry: the total number of $\varphi$, $\theta$, $\Phi$ and $\Theta$ parameters, i.e., ${\mathbf{NPAR}}=p+q+P+Q$.
Constraint: ${\mathbf{NPAR}}={\mathbf{MR}}\left(1\right)+{\mathbf{MR}}\left(3\right)+{\mathbf{MR}}\left(4\right)+{\mathbf{MR}}\left(6\right)$.
7:     ISHOW – INTEGERInput
On entry: must be nonzero if the residual autocorrelations, their standard errors and the portmanteau statistics are to be printed and zero otherwise.
These quantities are available also as output variables in R, RCM, CHI, IDF and SIGLEV.
8:     R(M) – REAL (KIND=nag_wp) arrayOutput
On exit: an estimate of the residual autocorrelation coefficient at lag $\mathit{l}$, for $\mathit{l}=1,2,\dots ,m$. If ${\mathbf{IFAIL}}={\mathbf{3}}$ on exit then all elements of R are set to zero.
9:     RCM(LDRCM,M) – REAL (KIND=nag_wp) arrayOutput
On exit: the estimated standard errors and correlations of the elements in the array R. The correlation between ${\mathbf{R}}\left(i\right)$ and ${\mathbf{R}}\left(j\right)$ is returned as ${\mathbf{RCM}}\left(i,j\right)$ except that if $i=j$ then ${\mathbf{RCM}}\left(i,j\right)$ contains the standard error of ${\mathbf{R}}\left(i\right)$. If on exit, ${\mathbf{IFAIL}}\ge {\mathbf{5}}$, then all off-diagonal elements of RCM are set to zero and all diagonal elements are set to $1/\sqrt{n}$.
10:   LDRCM – INTEGERInput
On entry: the first dimension of the array RCM as declared in the (sub)program from which G13ASF is called.
Constraint: ${\mathbf{LDRCM}}\ge {\mathbf{M}}$.
11:   CHI – REAL (KIND=nag_wp)Output
On exit: the value of the portmanteau statistic, ${Q}_{\left(m\right)}$. If ${\mathbf{IFAIL}}={\mathbf{3}}$ on exit then CHI is returned as zero.
12:   IDF – INTEGEROutput
On exit: the number of degrees of freedom of CHI.
13:   SIGLEV – REAL (KIND=nag_wp)Output
On exit: the significance level of CHI based on IDF degrees of freedom. If ${\mathbf{IFAIL}}={\mathbf{3}}$ on exit, SIGLEV is returned as one.
14:   IW(LIW) – INTEGER arrayWorkspace
15:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which G13ASF is called.
Constraint: ${\mathbf{LIW}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{MR}}\left(1\right),{\mathbf{MR}}\left(3\right),{\mathbf{MR}}\left(4\right),{\mathbf{MR}}\left(6\right)\right)$.
16:   WORK(LWORK) – REAL (KIND=nag_wp) arrayWorkspace
17:   LWORK – INTEGERInput
On entry: the dimension of the array WORK as declared in the (sub)program from which G13ASF is called.
Constraint: ${\mathbf{LWORK}}\ge {\mathbf{NPAR}}×\left({\mathbf{M}}+{\mathbf{NPAR}}+1\right)+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{MR}}\left(1\right),{\mathbf{MR}}\left(3\right),{\mathbf{MR}}\left(4\right),{\mathbf{MR}}\left(6\right)\right)×\phantom{\rule{0ex}{0ex}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{MR}}\left(7\right),1\right)+{\mathbf{M}}$.
18:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: G13ASF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{MR}}\left(1\right)<0$, or ${\mathbf{MR}}\left(3\right)<0$, or ${\mathbf{MR}}\left(4\right)<0$, or ${\mathbf{MR}}\left(6\right)<0$, or ${\mathbf{MR}}\left(7\right)<0$, or ${\mathbf{MR}}\left(7\right)=0$ and either ${\mathbf{MR}}\left(4\right)>0$ or ${\mathbf{MR}}\left(6\right)>0$, or ${\mathbf{MR}}\left(1\right)={\mathbf{MR}}\left(3\right)={\mathbf{MR}}\left(4\right)={\mathbf{MR}}\left(6\right)=0$, or ${\mathbf{M}}\le {\mathbf{NPAR}}$, or ${\mathbf{M}}\ge {\mathbf{N}}$, or ${\mathbf{N}}<3$, or ${\mathbf{NPAR}}\ne {\mathbf{MR}}\left(1\right)+{\mathbf{MR}}\left(3\right)+{\mathbf{MR}}\left(4\right)+{\mathbf{MR}}\left(6\right)$, or ${\mathbf{LDRCM}}<{\mathbf{M}}$, or LIW is too small, or LWORK is too small.
${\mathbf{IFAIL}}=2$
On entry, the autoregressive (or moving average) parameters are extremely close to or outside the stationarity (or invertibility) region. To proceed, you must supply different parameter estimates in the array PAR.
${\mathbf{IFAIL}}=3$
On entry, the residuals are practically identical giving zero (or near zero) variance. In this case CHI is set to zero and SIGLEV to one and all the elements of R are set to zero.
${\mathbf{IFAIL}}=4$
This is an unlikely exit brought about by an excessive number of iterations being needed to evaluate the zeros of the AR or MA polynomials. All output parameters are undefined.
${\mathbf{IFAIL}}=5$
On entry, one or more of the AR operators has a factor in common with one or more of the MA operators. To proceed, this common factor must be deleted from the model. In this case, the off-diagonal elements of RCM are returned as zero and the diagonal elements set to $1/\sqrt{n}$. All other output quantities will be correct.
${\mathbf{IFAIL}}=6$
This is an unlikely exit. At least one of the diagonal elements of RCM was found to be either negative or zero. In this case all off-diagonal elements of RCM are returned as zero and all diagonal elements of RCM set to $1/\sqrt{n}$.

## 7  Accuracy

The computations are believed to be stable.

### 8.1  Timing

The time taken by G13ASF depends upon the number of residual autocorrelations to be computed, $m$.

### 8.2  Choice of $m$

The number of residual autocorrelations to be computed, $m$ should be chosen to ensure that when the ARMA model (1) is written as either an infinite order autoregressive process:
 $Wt-μ=∑j=1∞πjWt-j-μ+εt$
or as an infinite order moving average process:
 $Wt-μ=∑j= 1∞ψjεt-j+εt$
then the two sequences $\left\{{\pi }_{1},{\pi }_{2},\dots \right\}$ and $\left\{{\psi }_{1},{\psi }_{2},\dots \right\}$ are such that ${\pi }_{j}$ and ${\psi }_{j}$ are approximately zero for $j>m$. An overestimate of $m$ is therefore preferable to an under-estimate of $m$. In many instances the choice $m=10$ will suffice. In practice, to be on the safe side, you should try setting $m=20$.

### 8.3  Approximate Standard Errors

When ${\mathbf{IFAIL}}={\mathbf{5}}$ or ${\mathbf{6}}$ all the standard errors in RCM are set to $1/\sqrt{n}$. This is the asymptotic standard error of ${\stackrel{^}{r}}_{l}$ when all the autoregressive and moving average parameters are assumed to be known rather than estimated.

### 8.4  Alternative Applications

G13ASF may be used for diagnostic checking of suitable univariate ARMA models, as described in Section 3, fitted by G13BEF or G13DDF. Great care must be taken in obtaining the input values for G13ASF from the output values from G13BEF or G13DDF.

## 9  Example

This example fits an ARIMA$\left(1,1,2\right)$ model to a series of $30$ observations. $10$ residual autocorrelations are computed.

### 9.1  Program Text

Program Text (g13asfe.f90)

### 9.2  Program Data

Program Data (g13asfe.d)

### 9.3  Program Results

Program Results (g13asfe.r)