# NAG Library Routine Document

## 1Purpose

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.

## 2Specification

Fortran Interface
 Subroutine g13asf ( n, v, mr, m, par, npar, r, rcm, chi, idf, iw, liw, work,
 Integer, Intent (In) :: n, mr(7), m, npar, ishow, ldrcm, liw, lwork Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: idf, iw(liw) Real (Kind=nag_wp), Intent (In) :: v(n), par(npar) Real (Kind=nag_wp), Intent (Inout) :: rcm(ldrcm,m) Real (Kind=nag_wp), Intent (Out) :: r(m), chi, siglev, work(lwork)
#include <nagmk26.h>
 void g13asf_ (const Integer *n, const double v[], const Integer mr[], const Integer *m, const double par[], const Integer *npar, const Integer *ishow, double r[], double rcm[], const Integer *ldrcm, double *chi, Integer *idf, double *siglev, Integer iw[], const Integer *liw, double work[], const Integer *lwork, Integer *ifail)

## 3Description

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 9.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.

## 4References

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

## 5Arguments

1:     $\mathbf{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:     $\mathbf{v}\left({\mathbf{n}}\right)$ – 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:     $\mathbf{mr}\left(7\right)$ – 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:     $\mathbf{m}$ – IntegerInput
On entry: the value of $m$, the number of residual autocorrelations to be computed. See Section 9.2 for advice on the value of m.
Constraint: ${\mathbf{npar}}<{\mathbf{m}}<{\mathbf{n}}$.
5:     $\mathbf{par}\left({\mathbf{npar}}\right)$ – 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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{r}\left({\mathbf{m}}\right)$ – 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:     $\mathbf{rcm}\left({\mathbf{ldrcm}},{\mathbf{m}}\right)$ – 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:   $\mathbf{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:   $\mathbf{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:   $\mathbf{idf}$ – IntegerOutput
On exit: the number of degrees of freedom of chi.
13:   $\mathbf{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:   $\mathbf{iw}\left({\mathbf{liw}}\right)$ – Integer arrayWorkspace
15:   $\mathbf{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:   $\mathbf{work}\left({\mathbf{lwork}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
17:   $\mathbf{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:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . 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  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 arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value  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).
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{ldrcm}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldrcm}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{liw}}=〈\mathit{\text{value}}〉$ and the minimum size $\text{required}=〈\mathit{\text{value}}〉$
Constraint: ${\mathbf{liw}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,q,P,Q\right)$.
On entry, ${\mathbf{lwork}}=〈\mathit{\text{value}}〉$ and the minimum size $\text{required}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lwork}}\ge {\mathbf{npar}}×\left(m+{\mathbf{npar}}+1\right)+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,q,P,Q\right)×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(s,1\right)+m$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}<{\mathbf{n}}$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{npar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}>{\mathbf{npar}}$.
On entry, ${\mathbf{mr}}\left(1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(1\right)\ge 0$.
On entry, ${\mathbf{mr}}\left(3\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(3\right)\ge 0$.
On entry, ${\mathbf{mr}}\left(4\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(4\right)\ge 0$.
On entry, ${\mathbf{mr}}\left(4\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{mr}}\left(6\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(4\right)=0$ and ${\mathbf{mr}}\left(6\right)=0$.
On entry, ${\mathbf{mr}}\left(6\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(6\right)\ge 0$.
On entry, ${\mathbf{mr}}\left(7\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mr}}\left(7\right)\ge 0$.
On entry, ${\mathbf{npar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{npar}}\ne 0$ and ${\mathbf{npar}}=p+q+P+Q$.
${\mathbf{ifail}}=2$
On entry, the non-seasonal autoregressive operator is nonstationary. To proceed you must supply different parameter estimates in the array par.
On entry, the non-seasonal moving average operator is non-invertible. To proceed you must supply different parameter estimates in the array par.
On entry, the seasonal autoregressive operator is nonstationary. To proceed you must supply different parameter estimates in the array par.
On entry, the seasonal moving average operator is non-invertible. To proceed you must supply different parameter estimates in the array par.
${\mathbf{ifail}}=3$
On entry, the elements of v are nearly identical giving 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$
Excessive iterations needed to find zeros of determinental polynomials.
${\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 are set to $1/\sqrt{n}$. All other output quantities will be correct.
${\mathbf{ifail}}=6$
The matrix rcm could not be computed because one of its diagonal elements was found to be non-positive. In this case, the off-diagonal elements of rcm are returned as zero and the diagonal elements set to $1/\sqrt{n}$.
${\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 computations are believed to be stable.

## 8Parallelism and Performance

g13asf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13asf 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.

### 9.1Timing

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

### 9.2Choice 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$.

### 9.3Approximate 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.

### 9.4Alternative 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.

## 10Example

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

### 10.1Program Text

Program Text (g13asfe.f90)

### 10.2Program Data

Program Data (g13asfe.d)

### 10.3Program Results

Program Results (g13asfe.r)