Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_tsa_uni_arima_resid (g13as)

## Purpose

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

## Syntax

[r, rcm, chi, idf, siglev, ifail] = g13as(v, mr, m, par, ishow, 'n', n, 'npar', npar)
[r, rcm, chi, idf, siglev, ifail] = nag_tsa_uni_arima_resid(v, mr, m, par, ishow, 'n', n, 'npar', npar)

## Description

Consider the univariate multiplicative autoregressive-moving average model
 φ(B)Φ(Bs)(Wt − μ) = θ(B)Θ(Bs)εt $ϕ(B)Φ(Bs)(Wt-μ)=θ(B)Θ(Bs)εt$ (1)
where Wt${W}_{t}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$, denotes a time series and εt${\epsilon }_{t}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$, is a residual series assumed to be normally distributed with zero mean and variance σ2${\sigma }^{2}$ ( > 0$\text{}>0$). The εt${\epsilon }_{t}$'s are also assumed to be uncorrelated. Here μ$\mu$ is the overall mean term, s$s$ is the seasonal period and B$B$ is the backward shift operator such that BrWt = Wtr${B}^{r}{W}_{t}={W}_{t-r}$. The polynomials in (1) are defined as follows:
 φ(B) = 1 − φ1B − φ2B2 − ⋯ − φpBp $ϕ(B)=1-ϕ1B-ϕ2B2-⋯-ϕpBp$
is the non-seasonal autoregressive (AR) operator;
 θ (B) = 1 − θ1B − θ2B2 − ⋯ − θqBq $θ (B)= 1-θ1B-θ2B2-⋯-θqBq$
is the non-seasonal moving average (MA) operator;
 Φ(Bs) = 1 − Φ1Bs − Φ2B2s − ⋯ − ΦPBPs $Φ(Bs)=1-Φ1Bs-Φ2B2s-⋯-ΦPBPs$
is the seasonal AR operator; and
 Θ (Bs) = 1 − Θ1Bs − Θ2B2s − ⋯ − ΘQBQs $Θ (Bs)= 1-Θ1Bs-Θ2B2s-⋯-ΘQBQs$
is the seasonal MA operator. The model (1) is assumed to be stationary, that is the zeros of φ(B)$\varphi \left(B\right)$ and Φ(Bs)$\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 θ(B)$\theta \left(B\right)$ and Θ(Bs)$\Theta \left({B}^{s}\right)$ are assumed to lie outside the unit circle. When both Φ(Bs)$\Phi \left({B}^{s}\right)$ and Θ(Bs)$\Theta \left({B}^{s}\right)$ are absent from the model, that is when P = Q = 0$P=Q=0$, then the model is said to be non-seasonal.
The estimated residual autocorrelation coefficient at lag l$l$, l${\stackrel{^}{r}}_{l}$, is computed as:
 r̂l = ( ∑ t = l + 1n(ε̂t − l − ε)(ε̂t − ε))/( ∑ t = 1n(ε̂t − ε)2),  l = 1,2, … $r^l=∑t=l+1n(ε^t-l-ε-)(ε^t-ε-) ∑t=1n (ε^t-ε-) 2 , l=1,2,…$
where ε̂t${\stackrel{^}{\epsilon }}_{t}$ denotes an estimate of the t$t$th residual, εt${\epsilon }_{t}$, and ε = t = 1nε̂t / n$\stackrel{-}{\epsilon }=\sum _{t=1}^{n}{\stackrel{^}{\epsilon }}_{t}/n$. A portmanteau statistic, Q(m)${Q}_{\left(m\right)}$, is calculated from the formula (see Box and Ljung (1978)):
 m Q(m) = n(n + 2) ∑ r̂l2 / (n − l) l = 1
$Q(m)=n(n+2)∑l=1mr^l2/(n-l)$
where m$m$ denotes the number of residual autocorrelations computed. (Advice on the choice of m$m$ is given in Section [Choice of ].) Under the hypothesis of model adequacy, Q(m)${Q}_{\left(m\right)}$ has an asymptotic χ2${\chi }^{2}$-distribution on mpqPQ$m-p-q-P-Q$ degrees of freedom. Let T = (1,2,,m)${\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̂) = [Im − X(XTX) − 1XT] / n. $Var(r^)=[Im-X(XTX)-1XT]/n.$
The construction of the matrix X$X$ is discussed in McLeod (1978). (Note that the mean, μ$\mu$, and the residual variance, σ2${\sigma }^{2}$, play no part in calculating Var()$\mathrm{Var}\left(\stackrel{^}{r}\right)$ and therefore are not required as input to nag_tsa_uni_arima_resid (g13as).)
Note:  for additive models with fixed parameter values (i.e., fitted by nag_tsa_multi_varma_estimate (g13dd)) nag_tsa_multi_varma_diag (g13ds) should be used instead of nag_tsa_uni_arima_resid (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

## Parameters

### Compulsory Input Parameters

1:     v(n) – double array
n, the dimension of the array, must satisfy the constraint n3${\mathbf{n}}\ge 3$.
v(t)${\mathbf{v}}\left(\mathit{t}\right)$ must contain an estimate of εt${\epsilon }_{\mathit{t}}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$.
If nag_tsa_uni_arima_resid (g13as) is used following a call to nag_tsa_uni_arima_estim (g13ae) then the actual argument v must be exr(icount(1) + 1)${\mathbf{exr}}\left({\mathbf{icount}}\left(1\right)+1\right)$ as returned by nag_tsa_uni_arima_estim (g13ae).
If nag_tsa_uni_arima_resid (g13as) is used following a call to nag_tsa_uni_arima_estim_easy (g13af) then the actual argument v must be res as returned by nag_tsa_uni_arima_estim_easy (g13af).
Constraint: v${\mathbf{v}}$ must contain at least two distinct elements.
2:     mr(7$7$) – int64int32nag_int array
The orders vector (p$p$, d$d$, q$q$, P$P$, D$D$, Q$Q$, s$s$) as supplied to nag_tsa_uni_arima_estim (g13ae) or nag_tsa_uni_arima_estim_easy (g13af).
Constraints:
• p,q,P,Q,s0$p,q,P,Q,s\ge 0$;
• p + q + P + Q > 0$p+q+P+Q>0$;
• if s = 0$s=0$, then P = 0$P=0$ and Q = 0$Q=0$.
3:     m – int64int32nag_int scalar
The value of m$m$, the number of residual autocorrelations to be computed. See Section [Choice of ] for advice on the value of m.
Constraint: npar < m < n${\mathbf{npar}}<{\mathbf{m}}<{\mathbf{n}}$.
4:     par(npar) – double array
npar, the dimension of the array, must satisfy the constraint npar = mr(1) + mr(3) + mr(4) + mr(6)${\mathbf{npar}}={\mathbf{mr}}\left(1\right)+{\mathbf{mr}}\left(3\right)+{\mathbf{mr}}\left(4\right)+{\mathbf{mr}}\left(6\right)$.
The parameter estimates in the order φ1,φ2,,φp${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$, θ1,θ2,,θq${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$, Φ1,Φ2,,ΦP${\Phi }_{1},{\Phi }_{2},\dots ,{\Phi }_{P}$, Θ1,Θ2,,ΘQ${\Theta }_{1},{\Theta }_{2},\dots ,{\Theta }_{Q}$ only.
Constraint: the elements in par must satisfy the stationarity and invertibility conditions.
5:     ishow – int64int32nag_int scalar
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.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array v.
n$n$, the number of observations in the residual series.
If nag_tsa_uni_arima_resid (g13as) is used following a call to nag_tsa_uni_arima_estim (g13ae), then n must be the value icount(2)${\mathbf{icount}}\left(2\right)$ returned by nag_tsa_uni_arima_estim (g13ae).
If nag_tsa_uni_arima_resid (g13as) is used following a call to nag_tsa_uni_arima_estim_easy (g13af), then n must be the value nres returned by nag_tsa_uni_arima_estim_easy (g13af).
Constraint: n3${\mathbf{n}}\ge 3$.
2:     npar – int64int32nag_int scalar
Default: The dimension of the array par.
The total number of φ$\varphi$, θ$\theta$, Φ$\Phi$ and Θ$\Theta$ parameters, i.e., npar = p + q + P + Q${\mathbf{npar}}=p+q+P+Q$.
Constraint: npar = mr(1) + mr(3) + mr(4) + mr(6)${\mathbf{npar}}={\mathbf{mr}}\left(1\right)+{\mathbf{mr}}\left(3\right)+{\mathbf{mr}}\left(4\right)+{\mathbf{mr}}\left(6\right)$.

### Input Parameters Omitted from the MATLAB Interface

ldrcm iw liw work lwork

### Output Parameters

1:     r(m) – double array
An estimate of the residual autocorrelation coefficient at lag l$\mathit{l}$, for l = 1,2,,m$\mathit{l}=1,2,\dots ,m$. If ${\mathbf{ifail}}={\mathbf{3}}$ on exit then all elements of r are set to zero.
2:     rcm(ldrcm,m) – double array
ldrcmm$\mathit{ldrcm}\ge {\mathbf{m}}$.
The estimated standard errors and correlations of the elements in the array r. The correlation between r(i)${\mathbf{r}}\left(i\right)$ and r(j)${\mathbf{r}}\left(j\right)$ is returned as rcm(i,j)${\mathbf{rcm}}\left(i,j\right)$ except that if i = j$i=j$ then rcm(i,j)${\mathbf{rcm}}\left(i,j\right)$ contains the standard error of r(i)${\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)$1/\sqrt{n}$.
3:     chi – double scalar
The value of the portmanteau statistic, Q(m)${Q}_{\left(m\right)}$. If ${\mathbf{ifail}}={\mathbf{3}}$ on exit then chi is returned as zero.
4:     idf – int64int32nag_int scalar
The number of degrees of freedom of chi.
5:     siglev – double scalar
The significance level of chi based on idf degrees of freedom. If ${\mathbf{ifail}}={\mathbf{3}}$ on exit, siglev is returned as one.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Note: nag_tsa_uni_arima_resid (g13as) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, mr(1) < 0${\mathbf{mr}}\left(1\right)<0$, or mr(3) < 0${\mathbf{mr}}\left(3\right)<0$, or mr(4) < 0${\mathbf{mr}}\left(4\right)<0$, or mr(6) < 0${\mathbf{mr}}\left(6\right)<0$, or mr(7) < 0${\mathbf{mr}}\left(7\right)<0$, or mr(7) = 0${\mathbf{mr}}\left(7\right)=0$ and either mr(4) > 0${\mathbf{mr}}\left(4\right)>0$ or mr(6) > 0${\mathbf{mr}}\left(6\right)>0$, or mr(1) = mr(3) = mr(4) = mr(6) = 0${\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 m ≥ n${\mathbf{m}}\ge {\mathbf{n}}$, or n < 3${\mathbf{n}}<3$, or npar ≠ mr(1) + mr(3) + mr(4) + mr(6)${\mathbf{npar}}\ne {\mathbf{mr}}\left(1\right)+{\mathbf{mr}}\left(3\right)+{\mathbf{mr}}\left(4\right)+{\mathbf{mr}}\left(6\right)$, or ldrcm < m$\mathit{ldrcm}<{\mathbf{m}}$, or liw is too small, or lwork is too small.
ifail = 2${\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.
W ifail = 3${\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.
ifail = 4${\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.
ifail = 5${\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)$1/\sqrt{n}$. All other output quantities will be correct.
ifail = 6${\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)$1/\sqrt{n}$.

## Accuracy

The computations are believed to be stable.

### Timing

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

### Choice of m

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

### Approximate Standard Errors

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

### Alternative Applications

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

## Example

```function nag_tsa_uni_arima_resid_example
v = [19.62746933753217;
-5.309287748702281;
9.798293298952524;
15.24123763096101;
-9.169280254619032;
16.11067630246432;
15.39291433217872;
-5.450004312837414;
-27.6205284052683;
-18.13061645026666;
5.720197603730954;
-13.08812421913039;
-22.71514540998739;
-14.92555865550874;
4.693009266513346;
33.54056385487841;
19.71375888111006;
-27.33603163811242;
32.12310786592178;
-11.76808232386082;
1.152380086683287;
-1.775570325352465;
23.68209327485152;
-10.62375411730706;
13.96192179999122;
-5.272654385984258;
-28.78678842222793;
-20.65731709475202;
-2.255525918186007];
mr = [int64(1);1;2;0;0;0;0];
m = int64(10);
par = [-0.05429075588805302;
-0.5547824600332715;
-0.6734171925737445];
ishow = int64(1);
[r, rcm, chi, idf, siglev, ifail] = nag_tsa_uni_arima_resid(v, mr, m, par, ishow)
```
```
RESIDUAL AUTOCORRELATION FUNCTION
---------------------------------

LAG  K      1      2      3      4      5      6      7
R(K)      0.020 -0.040 -0.019  0.068 -0.143 -0.046 -0.205
ST.ERROR  0.007  0.125  0.128  0.150  0.168  0.168  0.178
---------------------------------------------------------
LAG  K      8      9     10
R(K)     -0.108 -0.001 -0.058
ST.ERROR  0.179  0.181  0.183
---------------------------------------------------------

BOX - LJUNG PORTMANTEAU STATISTIC =      3.465
SIGNIFICANCE LEVEL =      0.839
(BASED ON   7 DEGREES OF FREEDOM)

VALUE OF IFAIL PARAMETER ON EXIT FROM G13ASF =   0

r =

0.0199
-0.0401
-0.0191
0.0683
-0.1427
-0.0456
-0.2048
-0.1082
-0.0007
-0.0581

rcm =

Columns 1 through 9

0.0067    0.9986    0.2512    0.5337   -0.4515   -0.0672    0.3230   -0.1352   -0.1394
0.9986    0.1252    0.3019    0.5451   -0.4251   -0.0920    0.3193   -0.1175   -0.1466
0.2512    0.3019    0.1277    0.3989    0.3692   -0.4447   -0.0019    0.2821   -0.1538
0.5337    0.5451    0.3989    0.1503    0.0587    0.2843   -0.1867   -0.0770    0.1658
-0.4515   -0.4251    0.3692    0.0587    0.1683    0.0854    0.0939   -0.1056   -0.0041
-0.0672   -0.0920   -0.4447    0.2843    0.0854    0.1682    0.0602    0.1049   -0.0975
0.3230    0.3193   -0.0019   -0.1867    0.0939    0.0602    0.1777    0.0123    0.0538
-0.1352   -0.1175    0.2821   -0.0770   -0.1056    0.1049    0.0123    0.1793    0.0320
-0.1394   -0.1466   -0.1538    0.1658   -0.0041   -0.0975    0.0538    0.0320    0.1809
0.1652    0.1576   -0.1015   -0.0401    0.0717   -0.0157   -0.0375    0.0305    0.0080

Column 10

0.1652
0.1576
-0.1015
-0.0401
0.0717
-0.0157
-0.0375
0.0305
0.0080
0.1835

chi =

3.4654

idf =

7

siglev =

0.8389

ifail =

0

```
```function g13as_example
v = [19.62746933753217;
-5.309287748702281;
9.798293298952524;
15.24123763096101;
-9.169280254619032;
16.11067630246432;
15.39291433217872;
-5.450004312837414;
-27.6205284052683;
-18.13061645026666;
5.720197603730954;
-13.08812421913039;
-22.71514540998739;
-14.92555865550874;
4.693009266513346;
33.54056385487841;
19.71375888111006;
-27.33603163811242;
32.12310786592178;
-11.76808232386082;
1.152380086683287;
-1.775570325352465;
23.68209327485152;
-10.62375411730706;
13.96192179999122;
-5.272654385984258;
-28.78678842222793;
-20.65731709475202;
-2.255525918186007];
mr = [int64(1);1;2;0;0;0;0];
m = int64(10);
par = [-0.05429075588805302;
-0.5547824600332715;
-0.6734171925737445];
ishow = int64(1);
[r, rcm, chi, idf, siglev, ifail] = g13as(v, mr, m, par, ishow)
```
```
RESIDUAL AUTOCORRELATION FUNCTION
---------------------------------

LAG  K      1      2      3      4      5      6      7
R(K)      0.020 -0.040 -0.019  0.068 -0.143 -0.046 -0.205
ST.ERROR  0.007  0.125  0.128  0.150  0.168  0.168  0.178
---------------------------------------------------------
LAG  K      8      9     10
R(K)     -0.108 -0.001 -0.058
ST.ERROR  0.179  0.181  0.183
---------------------------------------------------------

BOX - LJUNG PORTMANTEAU STATISTIC =      3.465
SIGNIFICANCE LEVEL =      0.839
(BASED ON   7 DEGREES OF FREEDOM)

VALUE OF IFAIL PARAMETER ON EXIT FROM G13ASF =   0

r =

0.0199
-0.0401
-0.0191
0.0683
-0.1427
-0.0456
-0.2048
-0.1082
-0.0007
-0.0581

rcm =

Columns 1 through 9

0.0067    0.9986    0.2512    0.5337   -0.4515   -0.0672    0.3230   -0.1352   -0.1394
0.9986    0.1252    0.3019    0.5451   -0.4251   -0.0920    0.3193   -0.1175   -0.1466
0.2512    0.3019    0.1277    0.3989    0.3692   -0.4447   -0.0019    0.2821   -0.1538
0.5337    0.5451    0.3989    0.1503    0.0587    0.2843   -0.1867   -0.0770    0.1658
-0.4515   -0.4251    0.3692    0.0587    0.1683    0.0854    0.0939   -0.1056   -0.0041
-0.0672   -0.0920   -0.4447    0.2843    0.0854    0.1682    0.0602    0.1049   -0.0975
0.3230    0.3193   -0.0019   -0.1867    0.0939    0.0602    0.1777    0.0123    0.0538
-0.1352   -0.1175    0.2821   -0.0770   -0.1056    0.1049    0.0123    0.1793    0.0320
-0.1394   -0.1466   -0.1538    0.1658   -0.0041   -0.0975    0.0538    0.0320    0.1809
0.1652    0.1576   -0.1015   -0.0401    0.0717   -0.0157   -0.0375    0.0305    0.0080

Column 10

0.1652
0.1576
-0.1015
-0.0401
0.0717
-0.0157
-0.0375
0.0305
0.0080
0.1835

chi =

3.4654

idf =

7

siglev =

0.8389

ifail =

0

```