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 realization of a univariate time series from an autoregressive moving average (ARMA) model. The realization may be continued or a new realization 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:
 At Mark 23: ip and iq were made optional, inferred from the size of phi and theta respectively

## Description

Let the vector ${x}_{t}$, denote a time series which is assumed to follow an autoregressive moving average (ARMA) model of the form:
 $xt-μ= ϕ1xt-1-μ+ϕ2xt-2-μ+⋯+ϕpxt-p-μ+ εt-θ1εt-1-θ2εt-2-⋯-θqεt-q$
where ${\epsilon }_{t}$, is a residual series of independent random perturbations assumed to be Normally distributed with zero mean and variance ${\sigma }^{2}$. The parameters $\left\{{\varphi }_{i}\right\}$, for $\mathit{i}=1,2,\dots ,p$, are called the autoregressive (AR) parameters, and $\left\{{\theta }_{j}\right\}$, for $\mathit{j}=1,2,\dots ,q$, the moving average (MA) parameters. The parameters in the model are thus the $p$ $\varphi$ values, the $q$ $\theta$ values, the mean $\mu$ and the residual variance ${\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 realization of ${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 realization of ${x}_{n+1},{x}_{n+2},\dots$, etc. See the description of the argument mode in Arguments 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:     $\mathrm{mode}$int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate terms in the time series using reference vector set up in a prior call to nag_rand_times_arma (g05ph).
${\mathbf{mode}}=2$
Set up reference vector and generate terms in the time series.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
2:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     $\mathrm{xmean}$ – double scalar
The mean of the time series.
4:     $\mathrm{phi}\left({\mathbf{ip}}\right)$ – double array
The autoregressive coefficients of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
5:     $\mathrm{theta}\left({\mathbf{iq}}\right)$ – double array
The moving average coefficients of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
6:     $\mathrm{avar}$ – double scalar
${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: ${\mathbf{avar}}\ge 0.0$.
7:     $\mathrm{r}\left(\mathit{lr}\right)$ – double array
lr, the dimension of the array, must satisfy the constraint $\mathit{lr}\ge {\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
If ${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_times_arma (g05ph).
8:     $\mathrm{state}\left(:\right)$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:     $\mathrm{ip}$int64int32nag_int scalar
Default: the dimension of the array phi.
$p$, the number of autoregressive coefficients supplied.
Constraint: ${\mathbf{ip}}\ge 0$.
2:     $\mathrm{iq}$int64int32nag_int scalar
Default: the dimension of the array theta.
$q$, the number of moving average coefficients supplied.
Constraint: ${\mathbf{iq}}\ge 0$.

### Output Parameters

1:     $\mathrm{r}\left(\mathit{lr}\right)$ – double array
$\mathit{lr}={\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
The reference vector.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{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:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
Contains the next $n$ observations from the time series.
5:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{ip}}\ge 0$.
${\mathbf{ifail}}=5$
On entry, the AR parameters are outside the stationarity region.
${\mathbf{ifail}}=6$
Constraint: ${\mathbf{iq}}\ge 0$.
${\mathbf{ifail}}=8$
Constraint: ${\mathbf{avar}}\ge 0.0$.
${\mathbf{ifail}}=9$
ip or iq is not the same as when r was set up in a previous call.
Reference vector r has been corrupted or not initialized correctly.
${\mathbf{ifail}}=10$
On entry, lr is not large enough, $\mathit{lr}=_$: minimum length required .
${\mathbf{ifail}}=11$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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 ${\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 ${\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 argument 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$
where
• ${x}_{n}$ is the observed value of the time series at time $n$,
• $\mathit{NA}$ is the number of autoregressive parameters, ${A}_{i}$,
• $\mathit{NB}$ is the number of moving average parameters, ${B}_{i}$,
• $E$ is the mean of the time series,
and
• ${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 Description by:
• ${B}_{1}^{2}={\sigma }^{2}$,
• ${B}_{i+1}=-{\theta }_{i}\sigma =-{\theta }_{i}{B}_{1}\text{, }i=1,2,\dots ,q$,
• $\mathit{NB}=q+1$,
• $E=\mu$,
• ${A}_{i}={\varphi }_{i}\text{, }i=1,2,\dots ,p$,
• $\mathit{NA}=p$.

## Example

This example generates values for an autoregressive model given by
 $xt=0.4xt-1+0.2xt-2+εt$
where ${\epsilon }_{t}$ is a series of independent random Normal perturbations with variance $1.0$. The random number generators are initialized by nag_rand_init_repeat (g05kf) and then nag_rand_times_arma (g05ph) is called to initialize a reference vector and generate a sample of ten observations.
```function g05ph_example

fprintf('g05ph example results\n\n');

% Initialize the generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

% 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);

% Set up the reference vector and generate the N realisations
[r, state, var, x, ifail] = ...
g05ph( ...
mode, n, xmean, phi, theta, avar, r, state);

disp('Realizations:');
disp(x);

```
```g05ph example results

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

```