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_rand_times_arma (g05ph)

## Purpose

nag_rand_times_arma (g05ph) generates a realisation of a univariate time series from an autoregressive moving average (ARMA) model. The realisation may be continued or a new realisation generated at subsequent calls to nag_rand_times_arma (g05ph).

## Syntax

[r, state, var, x, ifail] = g05ph(mode, n, xmean, phi, theta, avar, r, state, 'ip', ip, 'iq', iq)
[r, state, var, x, ifail] = nag_rand_times_arma(mode, n, xmean, phi, theta, avar, r, state, 'ip', ip, 'iq', iq)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: ip and iq are now optional, inferred from the size of phi and theta respectively
.

## Description

Let the vector xt${x}_{t}$, denote a time series which is assumed to follow an autoregressive moving average (ARMA) model of the form:
 xt − μ = φ1(xt − 1 − μ) + φ2(xt − 2 − μ) + ⋯ + φp(xt − p − μ) + εt − θ1εt − 1 − θ2εt − 2 − ⋯ − θqεt − q
$xt-μ= ϕ1(xt-1-μ)+ϕ2(xt-2-μ)+⋯+ϕp(xt-p-μ)+ εt-θ1εt-1-θ2εt-2-⋯-θqεt-q$
where εt${\epsilon }_{t}$, is a residual series of independent random perturbations assumed to be Normally distributed with zero mean and variance σ2${\sigma }^{2}$. The parameters {φi}$\left\{{\varphi }_{i}\right\}$, for i = 1,2,,p$\mathit{i}=1,2,\dots ,p$, are called the autoregressive (AR) parameters, and {θj}$\left\{{\theta }_{j}\right\}$, for j = 1,2,,q$\mathit{j}=1,2,\dots ,q$, the moving average (MA) parameters. The parameters in the model are thus the p$p$ φ$\varphi$ values, the q$q$ θ$\theta$ values, the mean μ$\mu$ and the residual variance σ2${\sigma }^{2}$.
nag_rand_times_arma (g05ph) sets up a reference vector containing initial values corresponding to a stationary position using the method described in Tunnicliffe–Wilson (1979). The function can then return a realisation of x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$. On a successful exit, the recent history is updated and saved in the reference vector r so that nag_rand_times_arma (g05ph) may be called again to generate a realisation of xn + 1,xn + 2,${x}_{n+1},{x}_{n+2},\dots$, etc. See the description of the parameter mode in Section [Parameters] for details.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_times_arma (g05ph).

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley
Tunnicliffe–Wilson G (1979) Some efficient computational procedures for high order ARMA models J. Statist. Comput. Simulation 8 301–309

## Parameters

### Compulsory Input Parameters

1:     mode – int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode = 0${\mathbf{mode}}=0$
Set up reference vector only.
mode = 1${\mathbf{mode}}=1$
Generate terms in the time series using reference vector set up in a prior call to nag_rand_times_arma (g05ph).
mode = 2${\mathbf{mode}}=2$
Set up reference vector and generate terms in the time series.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$ or 2$2$.
2:     n – int64int32nag_int scalar
n$n$, the number of observations to be generated.
Constraint: n0${\mathbf{n}}\ge 0$.
3:     xmean – double scalar
The mean of the time series.
4:     phi(ip) – double array
ip, the dimension of the array, must satisfy the constraint ip0${\mathbf{ip}}\ge 0$.
The autoregressive coefficients of the model, φ1,φ2,,φp${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
5:     theta(iq) – double array
iq, the dimension of the array, must satisfy the constraint iq0${\mathbf{iq}}\ge 0$.
The moving average coefficients of the model, θ1,θ2,,θq${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
6:     avar – double scalar
σ2${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: avar0.0${\mathbf{avar}}\ge 0.0$.
7:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint lrip + iq + 6 + max (ip,iq + 1)$\mathit{lr}\ge {\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_times_arma (g05ph).
8:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

### Optional Input Parameters

1:     ip – int64int32nag_int scalar
Default: The dimension of the array phi.
p$p$, the number of autoregressive coefficients supplied.
Constraint: ip0${\mathbf{ip}}\ge 0$.
2:     iq – int64int32nag_int scalar
Default: The dimension of the array theta.
q$q$, the number of moving average coefficients supplied.
Constraint: iq0${\mathbf{iq}}\ge 0$.

lr

### Output Parameters

1:     r(lr) – double array
lrip + iq + 6 + max (ip,iq + 1)$\mathit{lr}\ge {\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
The reference vector.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
3:     var – double scalar
The proportion of the variance of a term in the series that is due to the moving-average (error) terms in the model. The smaller this is, the nearer is the model to non-stationarity.
4:     x(n) – double array
Contains the next n$n$ observations from the time series.
5:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
On entry, mode0${\mathbf{mode}}\ne 0$, 1$1$ or 2$2$.
ifail = 2${\mathbf{ifail}}=2$
On entry, n < 0${\mathbf{n}}<0$.
ifail = 4${\mathbf{ifail}}=4$
On entry, ip < 0${\mathbf{ip}}<0$.
ifail = 5${\mathbf{ifail}}=5$
phi does not define a stationary autoregressive process.
ifail = 6${\mathbf{ifail}}=6$
On entry, iq < 0${\mathbf{iq}}<0$.
ifail = 8${\mathbf{ifail}}=8$
On entry, avar < 0.0${\mathbf{avar}}<0.0$.
ifail = 9${\mathbf{ifail}}=9$
Either r has been corrupted or the value of ip or iq is not the same as when r was set up in a previous call to nag_rand_times_arma (g05ph) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 10${\mathbf{ifail}}=10$
On entry, lr < ip + iq + 6 + max (ip,iq + 1)$\mathit{lr}<{\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
ifail = 11${\mathbf{ifail}}=11$
 On entry, state vector was not initialized or has been corrupted.

## Accuracy

Any errors in the reference vector's initial values should be very much smaller than the error term; see Tunnicliffe–Wilson (1979).

The time taken by nag_rand_times_arma (g05ph) is essentially of order (ip)2${\left({\mathbf{ip}}\right)}^{2}$.
Note:  The reference vector, r, contains a copy of the recent history of the series. If attempting to re-initialize the series by calling nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg) a call to nag_rand_times_arma (g05ph) with mode = 0${\mathbf{mode}}=0$ must also be made. In the repeatable case the calls to nag_rand_times_arma (g05ph) should be performed in the same order (at the same point(s) in simulation) every time nag_rand_init_repeat (g05kf) is used. When the generator state is saved and restored using the parameter state, the time series reference vector must be saved and restored as well.
The ARMA model for a time series can also be written as:
 (xn − E) = A1 (xn − 1 − E) + ⋯ + ANA (xn − NA − E) + B1 an + ⋯ + BNB an − NB + 1 $(xn-E) = A1 (xn-1-E) + ⋯ + ANA (xn-NA-E) + B1 an + ⋯ + BNB an-NB+1$
where
• xn${x}_{n}$ is the observed value of the time series at time n$n$,
• NA$\mathit{NA}$ is the number of autoregressive parameters, Ai${A}_{i}$,
• NB$\mathit{NB}$ is the number of moving average parameters, Bi${B}_{i}$,
• E$E$ is the mean of the time series,
and
• at${a}_{t}$ is a series of independent random Standard Normal perturbations.
This is the form used in nag_rand_times_arma (g05ph). This is related to the form given in Section [Description] by:
• B12 = σ2${B}_{1}^{2}={\sigma }^{2}$,
• Bi + 1 = θiσ = θiB1,  i = 1,2,,q${B}_{i+1}=-{\theta }_{i}\sigma =-{\theta }_{i}{B}_{1}\text{, }i=1,2,\dots ,q$,
• NB = q + 1$\mathit{NB}=q+1$,
• E = μ$E=\mu$,
• Ai = φi,  i = 1,2,,p${A}_{i}={\varphi }_{i}\text{, }i=1,2,\dots ,p$,
• NA = p$\mathit{NA}=p$.

## Example

```function nag_rand_times_arma_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% Set the ARMA model parameters
xmean = 0;
phi = [0.4; 0.2];
avar = 1;

mode = int64(2);
n = int64(10);
theta = [0];
r = zeros(2*numel(phi)+numel(theta)+6, 1);

% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
% Set up the reference vector and generate the N realisations
if ifail == 0
[r, state, var, x, ifail] = ...
nag_rand_times_arma(mode, n, xmean, phi, theta, avar, r, state);
x
end
```
```

x =

-1.7103
-0.4042
-0.1845
-1.5004
-1.1946
-1.8184
-1.0895
1.6408
1.3555
1.1908

```
```function g05ph_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% Set the ARMA model parameters
xmean = 0;
phi = [0.4; 0.2];
avar = 1;

mode = int64(2);
n = int64(10);
theta = [0];
r = zeros(2*numel(phi)+numel(theta)+6, 1);

% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
% Set up the reference vector and generate the N realisations
if ifail == 0
[r, state, var, x, ifail] = ...
g05ph(mode, n, xmean, phi, theta, avar, r, state);
x
end
```
```

x =

-1.7103
-0.4042
-0.1845
-1.5004
-1.1946
-1.8184
-1.0895
1.6408
1.3555
1.1908

```