g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_rngs_arma_time_series (g05pac)

## 1  Purpose

nag_rngs_arma_time_series (g05pac) 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_rngs_arma_time_series (g05pac).

## 2  Specification

 #include #include
 void nag_rngs_arma_time_series (Integer mode, double xmean, Integer p, const double phi[], Integer q, const double theta[], double avar, double *var, Integer n, double x[], Integer igen, Integer iseed[], double r[], 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$ (1)
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_rngs_arma_time_series (g05pac) 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_rngs_arma_time_series (g05pac) 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_rngs_init_repeatable (g05kbc) (for a repeatable sequence if computed sequentially) or nag_rngs_init_nonrepeatable (g05kcc) (for a non-repeatable sequence) must be called prior to the first call to nag_rngs_arma_time_series (g05pac).

## 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:     modeIntegerInput
On entry: 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_rngs_arma_time_series (g05pac).
${\mathbf{mode}}=2$
Set up reference vector and generate terms in the time series.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
2:     xmeandoubleInput
On entry: the mean of the time series.
3:     pIntegerInput
On entry: $p$, the number of autoregressive coefficients supplied.
Constraint: ${\mathbf{p}}\ge 0$.
4:     phi[p]const doubleInput
On entry: the autoregressive coefficients of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
5:     qIntegerInput
On entry: $q$, the number of moving average coefficients supplied.
Constraint: ${\mathbf{q}}\ge 0$.
6:     theta[q]const doubleInput
On entry: the moving average coefficients of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
7:     avardoubleInput
On entry: ${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: ${\mathbf{avar}}\ge 0.0$.
8:     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.
9:     nIntegerInput
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
10:   x[n]doubleOutput
On exit: contains the next $n$ observations from the time series.
11:   igenIntegerInput
On entry: must contain the identification number for the generator to be used to return a pseudorandom number and should remain unchanged following initialization by a prior call to nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc).
12:   iseed[$4$]IntegerCommunication Array
On entry: contains values which define the current state of the selected generator.
On exit: contains updated values defining the new state of the selected generator.
13:   r[$\mathit{dim}$]doubleCommunication Array
Note: the dimension, dim, of the array r must be at least $\left({\mathbf{p}}+{\mathbf{q}}+5+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{p}},{\mathbf{q}}+1\right)\right)$.
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rngs_arma_time_series (g05pac).
On exit: the reference vector.
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{mode}}=〈\mathit{\text{value}}〉$.
Constraint: $0\le {\mathbf{mode}}\le 2$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
On entry, ${\mathbf{p}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{p}}\ge 0$.
On entry, ${\mathbf{q}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{q}}\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_REAL
On entry, ${\mathbf{avar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{avar}}\ge 0.0$.
NE_STATIONARY_AR
phi does not define a stationary autoregressive process.

## 7  Accuracy

The errors in the initialization process should be very much smaller than the error term; see Tunnicliffe–Wilson (1979).

The time taken by nag_rngs_arma_time_series (g05pac) is essentially of order ${\left({\mathbf{p}}\right)}^{2}$.
Note:  nag_rngs_init_repeatable (g05kbc) and nag_rngs_init_nonrepeatable (g05kcc) must be used with care if this function is used as well. The reference vector, as mentioned before, contains a copy of the recent history of the series. This will not be altered properly by calls to any of the above functions. A call to nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc) should be followed by a call to nag_rngs_arma_time_series (g05pac) with ${\mathbf{mode}}=0$ to re-initialize the time series reference vector in use. To maintain repeatability with nag_rngs_init_repeatable (g05kbc), the calls to nag_rngs_arma_time_series (g05pac) should be performed in the same order and at the same point or points in the simulation every time nag_rngs_init_repeatable (g05kbc) is used. When the generator state is saved and restored using the arguments igen and iseed, 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 arguments, ${A}_{i}$,
• $\mathit{NB}$ is the number of moving average arguments, ${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=c$,
• ${A}_{i}={\varphi }_{i}\text{, }i=1,2,\dots ,p$,
• $\mathit{NA}=p$.

## 9  Example

This example calls nag_rngs_arma_time_series (g05pac) to set up the reference vector for an autoregressive model after initialization by nag_rngs_init_repeatable (g05kbc). The model is 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$. nag_rngs_arma_time_series (g05pac) is then called to generate a sample of ten observations, which are printed.

### 9.1  Program Text

Program Text (g05pace.c)

None.

### 9.3  Program Results

Program Results (g05pace.r)