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

# Syntax

C#
```public static void g05ph(
int mode,
int n,
double xmean,
int ip,
double[] phi,
int iq,
double[] theta,
double avar,
double[] r,
G05..::..G05State g05state,
out double var,
double[] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05ph ( _
mode As Integer, _
n As Integer, _
xmean As Double, _
ip As Integer, _
phi As Double(), _
iq As Integer, _
theta As Double(), _
avar As Double, _
r As Double(), _
g05state As G05..::..G05State, _
<OutAttribute> ByRef var As Double, _
x As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05ph(
int mode,
int n,
double xmean,
int ip,
array<double>^ phi,
int iq,
array<double>^ theta,
double avar,
array<double>^ r,
G05..::..G05State^ g05state,
[OutAttribute] double% var,
array<double>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05ph :
mode : int *
n : int *
xmean : float *
ip : int *
phi : float[] *
iq : int *
theta : float[] *
avar : float *
r : float[] *
g05state : G05..::..G05State *
var : float byref *
x : float[] *
ifail : int byref -> unit
```

#### Parameters

mode
Type: System..::..Int32
On entry: a code for selecting the operation to be performed by the method.
${\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 g05ph.
${\mathbf{mode}}=2$
Set up reference vector and generate terms in the time series.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
n
Type: System..::..Int32
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
xmean
Type: System..::..Double
On entry: the mean of the time series.
ip
Type: System..::..Int32
On entry: $p$, the number of autoregressive coefficients supplied.
Constraint: ${\mathbf{ip}}\ge 0$.
phi
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On entry: the autoregressive coefficients of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$.
iq
Type: System..::..Int32
On entry: $q$, the number of moving average coefficients supplied.
Constraint: ${\mathbf{iq}}\ge 0$.
theta
Type: array<System..::..Double>[]()[][]
An array of size [iq]
On entry: the moving average coefficients of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$.
avar
Type: System..::..Double
On entry: ${\sigma }^{2}$, the variance of the Normal perturbations.
Constraint: ${\mathbf{avar}}\ge 0.0$.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05ph.
On exit: the reference vector.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
var
Type: System..::..Double%
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.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: contains the next $n$ observations from the time series.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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}$.
g05ph sets up a reference vector containing initial values corresponding to a stationary position using the method described in Tunnicliffe–Wilson (1979). The method 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 g05ph 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 [Parameters] for details.
One of the initialization methods (G05KFF not in this release) (for a repeatable sequence if computed sequentially) or (G05KGF not in this release) (for a non-repeatable sequence) must be called prior to the first call to 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}\ne 0$, $1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ip}}<0$.
${\mathbf{ifail}}=5$
phi does not define a stationary autoregressive process.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{iq}}<0$.
${\mathbf{ifail}}=8$
On entry, ${\mathbf{avar}}<0.0$.
${\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 g05ph with ${\mathbf{mode}}=0$ or $2$.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{lr}}<{\mathbf{ip}}+{\mathbf{iq}}+6+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}+1\right)$.
${\mathbf{ifail}}=11$
 On entry, state vector was not initialized or has been corrupted.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

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

# Parallelism and Performance

None.

The time taken by g05ph is essentially of order ${\left({\mathbf{ip}}\right)}^{2}$.
The ARMA model for a time series can also be written as:
 $xn-E=A1xn-1-E+⋯+ANAxn-NA-E+B1an+⋯+BNBan-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 [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$.