g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_rand_arma (g05phc)

## 1  Purpose

nag_rand_arma (g05phc) 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_arma (g05phc).

## 2  Specification

 #include #include
 void nag_rand_arma (Nag_ModeRNG mode, Integer n, double xmean, Integer ip, const double phi[], Integer iq, const double theta[], double avar, double r[], Integer lr, Integer state[], double *var, double x[], NagError *fail)

## 3  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_arma (g05phc) 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_arma (g05phc) 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 Section 5 for details.
One of the initialization functions nag_rand_init_repeatable (g05kfc) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeatable (g05kgc) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_arma (g05phc).

## 4  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

## 5  Arguments

1:     modeNag_ModeRNGInput
On entry: a code for selecting the operation to be performed by the function.
${\mathbf{mode}}=\mathrm{Nag_InitializeReference}$
Set up reference vector only.
${\mathbf{mode}}=\mathrm{Nag_GenerateFromReference}$
Generate terms in the time series using reference vector set up in a prior call to nag_rand_arma (g05phc).
${\mathbf{mode}}=\mathrm{Nag_InitializeAndGenerate}$
Set up reference vector and generate terms in the time series.
Constraint: ${\mathbf{mode}}=\mathrm{Nag_InitializeReference}$, $\mathrm{Nag_GenerateFromReference}$ or $\mathrm{Nag_InitializeAndGenerate}$.
2:     nIntegerInput
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     xmeandoubleInput
On entry: the mean of the time series.
4:     ipIntegerInput
On entry: $p$, the number of autoregressive coefficients supplied.
Constraint: ${\mathbf{ip}}\ge 0$.
5:     phi[ip]const doubleInput
On entry: the autoregressive coefficients of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
6:     iqIntegerInput
On entry: $q$, the number of moving average coefficients supplied.
Constraint: ${\mathbf{iq}}\ge 0$.
7:     theta[iq]const doubleInput
On entry: the moving average coefficients of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
8:     avardoubleInput
On entry: ${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: ${\mathbf{avar}}\ge 0.0$.
9:     r[lr]doubleCommunication Array
On entry: if ${\mathbf{mode}}=\mathrm{Nag_GenerateFromReference}$, the reference vector from the previous call to nag_rand_arma (g05phc).
On exit: the reference vector.
10:   lrIntegerInput
On entry: the dimension of the array r.
Constraint: ${\mathbf{lr}}\ge {\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
11:   state[$\mathit{dim}$]IntegerCommunication Array
Note: the dimension, $\mathit{dim}$, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
12:   vardouble *Output
On exit: 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.
13:   x[n]doubleOutput
On exit: contains the next $n$ observations from the time series.
14:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT
On entry, ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ip}}\ge 0$.
On entry, ${\mathbf{iq}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{iq}}\ge 0$.
On entry, lr is not large enough, ${\mathbf{lr}}=⟨\mathit{\text{value}}⟩$: minimum length required $\text{}=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
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.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_PREV_CALL
ip or iq is not the same as when r was set up in a previous call.
Previous value of ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$.
Previous value of ${\mathbf{iq}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{iq}}=⟨\mathit{\text{value}}⟩$.
NE_REAL
On entry, ${\mathbf{avar}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{avar}}\ge 0.0$.
NE_REF_VEC
Reference vector r has been corrupted or not initialized correctly.
NE_STATIONARY_AR
On entry, the AR parameters are outside the stationarity region.

## 7  Accuracy

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

## 8  Parallelism and Performance

Not applicable.

The time taken by nag_rand_arma (g05phc) 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_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc) a call to nag_rand_arma (g05phc) with ${\mathbf{mode}}=\mathrm{Nag_InitializeReference}$ must also be made. In the repeatable case the calls to nag_rand_arma (g05phc) should be performed in the same order (at the same point(s) in simulation) every time nag_rand_init_repeatable (g05kfc) 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 related to the form given in Section 3 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$.

## 10  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_repeatable (g05kfc) and then nag_rand_arma (g05phc) is called to initialize a reference vector and generate a sample of ten observations.

### 10.1  Program Text

Program Text (g05phce.c)

None.

### 10.3  Program Results

Program Results (g05phce.r)