G05PHF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG05PHF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G05PHF 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 G05PHF.

## 2  Specification

 SUBROUTINE G05PHF ( MODE, N, XMEAN, IP, PHI, IQ, THETA, AVAR, R, LR, STATE, VAR, X, IFAIL)
 INTEGER MODE, N, IP, IQ, LR, STATE(*), IFAIL REAL (KIND=nag_wp) XMEAN, PHI(IP), THETA(IQ), AVAR, R(LR), VAR, X(N)

## 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}$.
G05PHF sets up a reference vector containing initial values corresponding to a stationary position using the method described in Tunnicliffe–Wilson (1979). The routine 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 G05PHF may be called again to generate a realization of ${x}_{n+1},{x}_{n+2},\dots$, etc. See the description of the parameter MODE in Section 5 for details.
One of the initialization routines G05KFF (for a repeatable sequence if computed sequentially) or G05KGF (for a non-repeatable sequence) must be called prior to the first call to G05PHF.

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

1:     $\mathrm{MODE}$ – INTEGERInput
On entry: a code for selecting the operation to be performed by the routine.
${\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 G05PHF.
${\mathbf{MODE}}=2$
Set up reference vector and generate terms in the time series.
Constraint: ${\mathbf{MODE}}=0$, $1$ or $2$.
2:     $\mathrm{N}$ – INTEGERInput
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{N}}\ge 0$.
3:     $\mathrm{XMEAN}$ – REAL (KIND=nag_wp)Input
On entry: the mean of the time series.
4:     $\mathrm{IP}$ – INTEGERInput
On entry: $p$, the number of autoregressive coefficients supplied.
Constraint: ${\mathbf{IP}}\ge 0$.
5:     $\mathrm{PHI}\left({\mathbf{IP}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: the autoregressive coefficients of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
6:     $\mathrm{IQ}$ – INTEGERInput
On entry: $q$, the number of moving average coefficients supplied.
Constraint: ${\mathbf{IQ}}\ge 0$.
7:     $\mathrm{THETA}\left({\mathbf{IQ}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: the moving average coefficients of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
8:     $\mathrm{AVAR}$ – REAL (KIND=nag_wp)Input
On entry: ${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: ${\mathbf{AVAR}}\ge 0.0$.
9:     $\mathrm{R}\left({\mathbf{LR}}\right)$ – REAL (KIND=nag_wp) arrayCommunication Array
On entry: if ${\mathbf{MODE}}=1$, the reference vector from the previous call to G05PHF.
On exit: the reference vector.
10:   $\mathrm{LR}$ – INTEGERInput
On entry: the dimension of the array R as declared in the (sub)program from which G05PHF is called.
Constraint: ${\mathbf{LR}}\ge {\mathbf{IP}}+{\mathbf{IQ}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{IP}},{\mathbf{IQ}}+1\right)$.
11:   $\mathrm{STATE}\left(*\right)$ – INTEGER arrayCommunication Array
Note: the actual argument supplied must be the array STATE supplied to the initialization routines G05KFF or G05KGF.
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:   $\mathrm{VAR}$ – REAL (KIND=nag_wp)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:   $\mathrm{X}\left({\mathbf{N}}\right)$ – REAL (KIND=nag_wp) arrayOutput
On exit: contains the next $n$ observations from the time series.
14:   $\mathrm{IFAIL}$ – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, ${\mathbf{MODE}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{MODE}}=0$, $1$ or $2$.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{N}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{N}}\ge 0$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{IP}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{IP}}\ge 0$.
${\mathbf{IFAIL}}=5$
On entry, the AR parameters are outside the stationarity region.
${\mathbf{IFAIL}}=6$
On entry, ${\mathbf{IQ}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{IQ}}\ge 0$.
${\mathbf{IFAIL}}=8$
On entry, ${\mathbf{AVAR}}=〈\mathit{\text{value}}〉$.
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.
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}}〉$.
Reference vector R has been corrupted or not initialized correctly.
${\mathbf{IFAIL}}=10$
On entry, LR is not large enough, ${\mathbf{LR}}=〈\mathit{\text{value}}〉$: minimum length required $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{IFAIL}}=11$
On entry, STATE vector has been corrupted or not initialized.
${\mathbf{IFAIL}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

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

G05PHF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

## 9  Further Comments

The time taken by G05PHF 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 G05KFF or G05KGF a call to G05PHF with ${\mathbf{MODE}}=0$ must also be made. In the repeatable case the calls to G05PHF should be performed in the same order (at the same point(s) in simulation) every time G05KFF 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$
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 G05PHF. 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 G05KFF and then G05PHF is called to initialize a reference vector and generate a sample of ten observations.

### 10.1  Program Text

Program Text (g05phfe.f90)

### 10.2  Program Data

Program Data (g05phfe.d)

### 10.3  Program Results

Program Results (g05phfe.r)

G05PHF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual