# NAG CL Interfaceg13asc (uni_​arima_​resid)

Settings help

CL Name Style:

## 1Purpose

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

## 2Specification

 #include
 void g13asc (Nag_ArimaOrder *arimav, Integer n, const double v[], Integer m, const double par[], Integer narma, double r[], double rc[], Integer tdrc, double *chi, Integer *df, double *siglev, NagError *fail)
The function may be called by the names: g13asc, nag_tsa_uni_arima_resid or nag_tsa_resid_corr.

## 3Description

Consider the univariate multiplicative autoregressive-moving average model
 $ϕ (B) Φ ( B s ) ( W t -μ) = θ (B) Θ ( B s ) ε t$ (1)
where ${W}_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$, denotes a time series and ${\epsilon }_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$, is a residual series assumed to be Normally distributed with zero mean and variance ${\sigma }^{2}\left(>0\right)$. 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 - ϕ 1 B - ϕ 2 B 2 - ⋯ - ϕ p B p$
is the non-seasonal autoregressive (AR) operator;
 $θ (B) = 1 - θ 1 B - θ 2 B 2 - ⋯ - θ q B q$
is the non-seasonal moving average (MA) operator;
 $Φ ( B s ) = 1 - Φ 1 B s - Φ 2 B 2s - ⋯ - Φ P B Ps$
is the seasonal AR operator; and
 $Θ ( B s ) = 1 - Θ 1 B s - Θ 2 B 2s - ⋯ - Θ Q B Qs$
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 + 1 n ( ε ^ t-l - ε ¯) ( ε ^ t - ε ¯) ∑ t=1 n ( ε ^ t - ε ¯) 2 , l = 1 , 2 , …$
where ${\stackrel{^}{\epsilon }}_{t}$ denotes an estimate of the $t$th residual, ${\epsilon }_{t}$, and $\overline{\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)):
 $Q (m) = n (n+2) ∑ l=1 m r ^ l 2 / (n-l)$
where $m$ denotes the number of residual autocorrelations computed. (Advice on the choice of $m$ is given in Section 9.) 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:
 $Var( r ^) = [ I m -X (XTX) −1 XT] / 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 g13asc.)
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{arimav}$Nag_ArimaOrder *
Pointer to structure of type Nag_ArimaOrder with the following members:
pInteger
dIntegerInput
qIntegerInput
bigpIntegerInput
bigdIntegerInput
bigqIntegerInput
sIntegerInput
On entry: these seven members of arimav must specify the orders vector $\left(p,d,q,P,D,Q,s\right)$, respectively, of the ARIMA model for the output noise component.
$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)$ arguments.
$d$, $D$ and $s$ refer, respectively, to the order of non-seasonal differencing, the order of seasonal differencing and the seasonal period.
Constraints:
• $\mathbf{arimav}\mathbf{\to }\mathbf{p}$, $\mathbf{arimav}\mathbf{\to }\mathbf{q}$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}$, $\mathbf{arimav}\mathbf{\to }\mathbf{s}\ge 0$,
• $\mathbf{arimav}\mathbf{\to }\mathbf{p}+\mathbf{arimav}\mathbf{\to }\mathbf{q}+\mathbf{arimav}\mathbf{\to }\mathbf{bigp}+\mathbf{arimav}\mathbf{\to }\mathbf{bigq}>0$,
• if $\mathbf{arimav}\mathbf{\to }\mathbf{s}=0$, then $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}=0$ and $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}=0$.
2: $\mathbf{n}$Integer Input
On entry: the number of observations in the residual series, $n$.
Constraint: ${\mathbf{n}}\ge 3$.
3: $\mathbf{v}\left[{\mathbf{n}}\right]$const double Input
On entry: ${\mathbf{v}}\left[\mathit{t}-1\right]$ must contain an estimate of ${\epsilon }_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$.
Constraint: v must contain at least two distinct elements.
4: $\mathbf{m}$Integer Input
On entry: the value of $m$, the number of residual autocorrelations to be computed. See Section 9 for advice on the value of m.
Constraint: ${\mathbf{narma}}<{\mathbf{m}}<{\mathbf{n}}$.
5: $\mathbf{par}\left[{\mathbf{narma}}\right]$const double Input
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{narma}$Integer Input
On entry: the number of ARMA arguments, $\varphi$, $\theta$, $\Phi$ and $\Theta$ arguments, i.e., ${\mathbf{narma}}=p+q+P+Q$.
Constraint: ${\mathbf{narma}}=\mathbf{arimav}\mathbf{\to }\mathbf{p}+\mathbf{arimav}\mathbf{\to }\mathbf{q}+\mathbf{arimav}\mathbf{\to }\mathbf{bigp}+\mathbf{arimav}\mathbf{\to }\mathbf{bigq}$.
7: $\mathbf{r}\left[{\mathbf{m}}\right]$double Output
On exit: an estimate of the residual autocorrelation coefficient at lag $\mathit{l}$, for $\mathit{l}=1,2,\dots ,m$. If ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE_G13AS_ZERO_VAR}}$ on exit then all elements of r are set to zero.
8: $\mathbf{rc}\left[{\mathbf{m}}×{\mathbf{tdrc}}\right]$double Output
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{rc}}\left[\left(i-1\right)×{\mathbf{tdrc}}+j-1\right]$ except that if $i=j$ then ${\mathbf{rc}}\left[\left(i-1\right)×{\mathbf{tdrc}}+j-1\right]$ contains the standard error of ${\mathbf{r}}\left[i-1\right]$. If on exit, ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE_G13AS_FACT}}$ or NE_G13AS_DIAG, then all off-diagonal elements of rc are set to zero and all diagonal elements are set to $1/\sqrt{n}$.
9: $\mathbf{tdrc}$Integer Input
On entry: the stride separating matrix column elements in the array rc.
Constraint: ${\mathbf{tdrc}}\ge {\mathbf{m}}$.
10: $\mathbf{chi}$double * Output
On exit: the value of the portmanteau statistic, ${Q}_{\left(m\right)}$. If ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE_G13AS_ZERO_VAR}}$ on exit then chi is returned as zero.
11: $\mathbf{df}$Integer * Output
On exit: the number of degrees of freedom of chi.
12: $\mathbf{siglev}$double * Output
On exit: the significance level of chi based on df degrees of freedom. If ${\mathbf{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE_G13AS_ZERO_VAR}}$ on exit then siglev is returned as one.
13: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdrc}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdrc}}\ge {\mathbf{m}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_ARIMA_INPUT
On entry, $\mathbf{arimav}\mathbf{\to }\mathbf{p}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{d}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{q}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigd}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}=⟨\mathit{\text{value}}⟩$ and $\mathbf{arimav}\mathbf{\to }\mathbf{s}=⟨\mathit{\text{value}}⟩$.
Constraints on the members of arimav are:
• $\mathbf{arimav}\mathbf{\to }\mathbf{p}$, $\mathbf{arimav}\mathbf{\to }\mathbf{q}$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}$, $\mathbf{arimav}\mathbf{\to }\mathbf{s}\ge 0$, $\mathbf{arimav}\mathbf{\to }\mathbf{p}+\mathbf{arimav}\mathbf{\to }\mathbf{q}+\mathbf{arimav}\mathbf{\to }\mathbf{bigp}+\mathbf{arimav}\mathbf{\to }\mathbf{bigq}>0$, if $\mathbf{arimav}\mathbf{\to }\mathbf{s}=0$, then $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}=0$ and $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}=0$.
NE_G13AS_AR
On entry, the autoregressive (or moving average) arguments are extremely close to or outside the stationarity (or invertibility) region. To proceed, you must supply different parameter estimates in the array par.
NE_G13AS_DIAG
This is an unlikely exit. At least one of the diagonal elements of rc was found to be either negative or zero. In this case all off-diagonal elements of rc are returned as zero and all diagonal elements of rc set to $1/\sqrt{\left(n\right)}$.
NE_G13AS_FACT
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 rc are returned as zero and the diagonal elements set to $1/\sqrt{\left(n\right)}$. All other output quantities will be correct.
NE_G13AS_ITER
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 arguments are undefined.
NE_G13AS_ZERO_VAR
On entry, the residuals are practically identical giving zero (or near zero) variance. In this case chi is set to zero, siglev to one and all the elements of r set to zero.
NE_INPUT_NARMA
On entry, $\mathbf{arimav}\mathbf{\to }\mathbf{p}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{q}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigp}=⟨\mathit{\text{value}}⟩$, $\mathbf{arimav}\mathbf{\to }\mathbf{bigq}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{narma}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{narma}}=\mathbf{arimav}\mathbf{\to }\mathbf{p}+\mathbf{arimav}\mathbf{\to }\mathbf{q}+\mathbf{arimav}\mathbf{\to }\mathbf{bigp}+\mathbf{arimav}\mathbf{\to }\mathbf{bigq}$.
NE_INT_3
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{narma}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{narma}}<{\mathbf{m}}<{\mathbf{n}}$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 3$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g13asc is not threaded in any implementation.

### 9.1Timing

The time taken by g13asc 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:
 $W t - μ = ∑ j=1 ∞ π j ( W t-j -μ) + ε t$
or as an infinite order moving average process:
 $W t - μ = ∑ 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{fail}}\mathbf{.}\mathbf{code}={\mathbf{NE_G13AS_FACT}}\text{​ or ​}{\mathbf{NE_G13AS_DIAG}}$ all the standard errors in rc are set to $1/\sqrt{n}$. This is the asymptotic standard error of ${\stackrel{^}{r}}_{l}$ when all the autoregressive and moving average arguments are assumed to be known rather than estimated.

## 10Example

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

### 10.1Program Text

Program Text (g13asce.c)

### 10.2Program Data

Program Data (g13asce.d)

### 10.3Program Results

Program Results (g13asce.r)