g13as is a diagnostic checking method suitable for use after fitting a Box–Jenkins ARMA model to a univariate time series using (G13AEF not in this release) g13af. The residual autocorrelation function is returned along with an estimate of its asymptotic standard errors and correlations. Also, g13as calculates the Box–Ljung portmanteau statistic and its significance level for testing model adequacy.

# Syntax

C#
```public static void g13as(
int n,
double[] v,
int[] mr,
int m,
double[] par,
int ishow,
double[] r,
double[,] rcm,
out double chi,
out int idf,
out double siglev,
out int ifail
)```
Visual Basic
```Public Shared Sub g13as ( _
n As Integer, _
v As Double(), _
mr As Integer(), _
m As Integer, _
par As Double(), _
ishow As Integer, _
r As Double(), _
rcm As Double(,), _
<OutAttribute> ByRef chi As Double, _
<OutAttribute> ByRef idf As Integer, _
<OutAttribute> ByRef siglev As Double, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g13as(
int n,
array<double>^ v,
array<int>^ mr,
int m,
array<double>^ par,
int ishow,
array<double>^ r,
array<double,2>^ rcm,
[OutAttribute] double% chi,
[OutAttribute] int% idf,
[OutAttribute] double% siglev,
[OutAttribute] int% ifail
)```
F#
```static member g13as :
n : int *
v : float[] *
mr : int[] *
m : int *
par : float[] *
ishow : int *
r : float[] *
rcm : float[,] *
chi : float byref *
idf : int byref *
siglev : float byref *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of observations in the residual series.
If g13as is used following a call to (G13AEF not in this release), then n must be the value ${\mathbf{icount}}\left[1\right]$ returned by (G13AEF not in this release).
If g13as is used following a call to g13af, then n must be the value nres returned by g13af.
Constraint: ${\mathbf{n}}\ge 3$.
v
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: ${\mathbf{v}}\left[\mathit{t}-1\right]$ must contain an estimate of ${\epsilon }_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$.
If g13as is used following a call to (G13AEF not in this release) then the actual argument v must be ${\mathbf{exr}}\left[{\mathbf{icount}}\left[0\right]\right]$ as returned by (G13AEF not in this release).
If g13as is used following a call to g13af then the actual argument v must be res as returned by g13af.
Constraint: ${\mathbf{v}}$ must contain at least two distinct elements.
mr
Type: array<System..::..Int32>[]()[][]
An array of size [$7$]
On entry: the orders vector ($p$, $d$, $q$, $P$, $D$, $Q$, $s$) as supplied to (G13AEF not in this release) g13af.
Constraints:
• $p,q,P,Q,s\ge 0$;
• $p+q+P+Q>0$;
• if $s=0$, then $P=0$ and $Q=0$.
m
Type: System..::..Int32
On entry: the value of $m$, the number of residual autocorrelations to be computed. See [Choice of $m$] for advice on the value of m.
Constraint: $\mathbf{_npar}<{\mathbf{m}}<{\mathbf{n}}$.
par
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
Note: dim1 must satisfy the constraint: $\mathbf{_npar}={\mathbf{mr}}\left[0\right]+{\mathbf{mr}}\left[2\right]+{\mathbf{mr}}\left[3\right]+{\mathbf{mr}}\left[5\right]$
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.
ishow
Type: System..::..Int32
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.
r
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: an estimate of the residual autocorrelation coefficient at lag $\mathit{l}$, for $\mathit{l}=1,2,\dots ,m$. If ${\mathbf{ifail}}={3}$ on exit then all elements of r are set to zero.
rcm
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On exit: the estimated standard errors and correlations of the elements in the array r. The correlation between ${\mathbf{r}}\left[i-1\right]$ and ${\mathbf{r}}\left[j-1\right]$ is returned as ${\mathbf{rcm}}\left[i-1,j-1\right]$ except that if $i=j$ then ${\mathbf{rcm}}\left[i-1,j-1\right]$ contains the standard error of ${\mathbf{r}}\left[i-1\right]$. If on exit, ${\mathbf{ifail}}\ge {5}$, then all off-diagonal elements of rcm are set to zero and all diagonal elements are set to $1/\sqrt{n}$.
chi
Type: System..::..Double%
On exit: the value of the portmanteau statistic, ${Q}_{\left(m\right)}$. If ${\mathbf{ifail}}={3}$ on exit then chi is returned as zero.
idf
Type: System..::..Int32%
On exit: the number of degrees of freedom of chi.
siglev
Type: System..::..Double%
On exit: the significance level of chi based on idf degrees of freedom. If ${\mathbf{ifail}}={3}$ on exit, siglev is returned as one.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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 [Choice of $m$].) 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 g13as.)
Note:  for additive models with fixed parameter values (i.e., fitted by g13dd) (G13DSF not in this release) should be used instead of g13as.

# References

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

# Error Indicators and Warnings

Note: g13as may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (NPAR, LDRCM) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{mr}}\left[0\right]<0$, or ${\mathbf{mr}}\left[2\right]<0$, or ${\mathbf{mr}}\left[3\right]<0$, or ${\mathbf{mr}}\left[5\right]<0$, or ${\mathbf{mr}}\left[6\right]<0$, or ${\mathbf{mr}}\left[6\right]=0$ and either ${\mathbf{mr}}\left[3\right]>0$ or ${\mathbf{mr}}\left[5\right]>0$, or ${\mathbf{mr}}\left[0\right]={\mathbf{mr}}\left[2\right]={\mathbf{mr}}\left[3\right]={\mathbf{mr}}\left[5\right]=0$, or ${\mathbf{m}}\ge {\mathbf{n}}$, or ${\mathbf{n}}<3$, 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}$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The computations are believed to be stable.

None.

# Timing

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

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

# Approximate Standard Errors

When ${\mathbf{ifail}}={5}$ or ${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.

# Alternative Applications

g13as may be used for diagnostic checking of suitable univariate ARMA models, as described in [Description], fitted by g13be or g13dd. Great care must be taken in obtaining the input values for g13as from the output values from g13be or g13dd.

# Example

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

Example program (C#): g13ase.cs

Example program data: g13ase.d

Example program results: g13ase.r