g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_rngs_varma_time_series (g05pcc)

## 1  Purpose

nag_rngs_varma_time_series (g05pcc) generates a realization of a multivariate time series from a vector autoregressive moving average (VARMA) model. The realization may be continued or a new realization generated at subsequent calls to this function.

## 2  Specification

 #include #include
 void nag_rngs_varma_time_series (Nag_OrderType order, Integer mode, Integer k, const double xmean[], Integer p, const double phi[], Integer q, const double theta[], const double var[], Integer pdv, Integer n, double x[], Integer pdx, Integer igen, Integer iseed[], double r[], NagError *fail)

## 3  Description

Let the vector ${X}_{t}={\left({x}_{1t},{x}_{2t},\dots ,{x}_{kt}\right)}^{\mathrm{T}}$, denote a $k$ dimensional time series which is assumed to follow a vector autoregressive moving average (VARMA) 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}={\left({\epsilon }_{1t},{\epsilon }_{2t},\dots ,{\epsilon }_{kt}\right)}^{\mathrm{T}}$, is a vector of $k$ residual series assumed to be Normally distributed with zero mean and positive definite covariance matrix $\Sigma$. The components of ${\epsilon }_{t}$ are assumed to be uncorrelated at non-simultaneous lags. The ${\varphi }_{i}$'s and ${\theta }_{j}$'s are $k$ by $k$ matrices of arguments. $\left\{{\varphi }_{i}\right\}$, for $\mathit{i}=1,2,\dots ,p$, are called the autoregressive (AR) parameter matrices, and $\left\{{\theta }_{j}\right\}$, for $\mathit{j}=1,2,\dots ,q$, the moving average (MA) parameter matrices. The parameters in the model are thus the $p$ $k$ by $k$ $\varphi$-matrices, the $q$ $k$ by $k$ $\theta$-matrices, the mean vector $\mu$ and the residual error covariance matrix $\Sigma$. Let
 $Aϕ= ϕ1 I 0 . . . 0 ϕ2 0 I 0 . . 0 . . . . . . ϕp-1 0 . . . 0 I ϕp 0 . . . 0 0 pk×pk and Bθ= θ1 I 0 . . . 0 θ2 0 I 0 . . 0 . . . . . . θq- 1 0 . . . 0 I θq 0 . . . 0 0 qk×qk$
where $I$ denotes the $k$ by $k$ identity matrix.
The model (1) must be both stationary and invertible. The model is said to be stationary if the eigenvalues of $A\left(\varphi \right)$ lie inside the unit circle and invertible if the eigenvalues of $B\left(\theta \right)$ lie inside the unit circle.
For $k\ge 6$ the VARMA model (1) is recast into state space form and a realization of the state vector at time zero computed. For all other cases the function computes a realization of the pre-observed vectors ${X}_{0},{X}_{-1},\dots ,{X}_{1-p}$, ${\epsilon }_{0},{\epsilon }_{-1},\dots ,{\epsilon }_{1-q}$, from equation (1), see Shea (1988). This realization is then used to generate a sequence of successive time series observations. Note that special action is taken for pure MA models, that is for $p=0$.
At your request a new realization of the time series may be generated with less computation using only the information saved in a reference vector from a previous call to nag_rngs_varma_time_series (g05pcc). See the description of the argument mode in Section 5 for details.
The function returns a realization of ${X}_{1},{X}_{2},\dots ,{X}_{n}$. On a successful exit, the recent history is updated and saved in the array r so that nag_rngs_varma_time_series (g05pcc) 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.
Further computational details are given in Shea (1988). Note however that this function uses a spectral decomposition rather than a Cholesky factorization to generate the multivariate Normals. Although this method involves more multiplications than the Cholesky factorization method and is thus slightly slower it is more stable when faced with ill-conditioned covariance matrices. A method of assigning the AR and MA coefficient matrices so that the stationarity and invertibility conditions are satisfied is described in Barone (1987).
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_varma_time_series (g05pcc).

## 4  References

Barone P (1987) A method for generating independent realisations of a multivariate normal stationary and invertible ARMA$\left(p,q\right)$ process J. Time Ser. Anal. 8 125–130
Shea B L (1988) A note on the generation of independent realisations of a vector autoregressive moving average process J. Time Ser. Anal. 9 403–410

## 5  Arguments

1:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or Nag_ColMajor.
2:     modeIntegerInput
On entry: a code for selecting the operation to be performed by the function.
${\mathbf{mode}}=0$
Set up reference vector and compute a realization of the recent history.
${\mathbf{mode}}=1$
Generate terms in the time series using reference vector set up in a prior call to nag_rngs_varma_time_series (g05pcc).
${\mathbf{mode}}=2$
Combine the operations of ${\mathbf{mode}}=0$ and $1$.
${\mathbf{mode}}=3$
A new realization of the recent history is computed using information stored in the reference vector, and the following sequence of time series values are generated.
If ${\mathbf{mode}}=1$ or $3$, then you must ensure that the reference vector r and the values of k, p, q, xmean, phi, theta, var and pdv have not been changed between calls to nag_rngs_varma_time_series (g05pcc).
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
3:     kIntegerInput
On entry: $k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
4:     xmean[k]const doubleInput
On entry: $\mu$, the vector of means of the multivariate time series.
5:     pIntegerInput
On entry: $p$, the number of autoregressive parameter matrices.
Constraint: ${\mathbf{p}}\ge 0$.
6:     phi[${\mathbf{p}}×{\mathbf{k}}×{\mathbf{k}}$]const doubleInput
On entry: must contain the elements of the ${\mathbf{p}}×{\mathbf{k}}×{\mathbf{k}}$ autoregressive parameter matrices of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$. The $\left(i,j\right)$th element of ${\varphi }_{\mathit{l}}$ is stored in ${\mathbf{phi}}\left[\left(\mathit{l}-1\right)×k×k+\left(j-1\right)×k+i-1\right]$, for $\mathit{l}=1,2,\dots ,p$, $i=1,2,\dots ,k$ and $j=1,2,\dots ,k$.
Constraint: the elements of phi must satisfy the stationarity condition.
7:     qIntegerInput
On entry: $q$, the number of moving average parameter matrices.
Constraint: ${\mathbf{q}}\ge 0$.
8:     theta[${\mathbf{q}}×{\mathbf{k}}×{\mathbf{k}}$]const doubleInput
On entry: must contain the elements of the ${\mathbf{q}}×{\mathbf{k}}×{\mathbf{k}}$ moving average parameter matrices of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$. The $\left(i,j\right)$th element of ${\theta }_{\mathit{l}}$ is stored in ${\mathbf{theta}}\left[\left(\mathit{l}-1\right)×k×k+\left(\mathit{j}-1\right)×k+\mathit{i}-1\right]$, for $\mathit{l}=1,2,\dots ,q$, $\mathit{i}=1,2,\dots ,k$ and $\mathit{j}=1,2,\dots ,k$.
Constraint: the elements of theta must be within the invertibility region.
9:     var[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array var must be at least ${\mathbf{pdv}}×{\mathbf{k}}$.
Where ${\mathbf{VAR}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{var}}\left[\left(j-1\right)×{\mathbf{pdv}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{var}}\left[\left(i-1\right)×{\mathbf{pdv}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: ${\mathbf{VAR}}\left(i,j\right)$ must contain the ($i,j$)th element of $\Sigma$. Only the lower triangle is required.
Constraint: the elements of var must be such that $\Sigma$ is positive definite.
10:   pdvIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array var.
Constraint: ${\mathbf{pdv}}\ge {\mathbf{k}}$.
11:   nIntegerInput
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
12:   x[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array x must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdx}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{k}}×{\mathbf{pdx}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
Where ${\mathbf{X}}\left(i,t\right)$ appears in this document, it refers to the array element
• ${\mathbf{x}}\left[\left(t-1\right)×{\mathbf{pdx}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+t-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On exit: ${\mathbf{X}}\left(\mathit{i},\mathit{t}\right)$ will contain a realization of the $\mathit{i}$th component of ${X}_{\mathit{t}}$, for $\mathit{i}=1,2,\dots ,k$ and $\mathit{t}=1,2,\dots ,n$.
13:   pdxIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdx}}\ge {\mathbf{k}}$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdx}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
14:   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).
15:   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.
16:   r[$\mathit{dim}$]doubleCommunication Array
Note: the dimension, dim, of the array r must be at least
• $\left(5{\mathit{max_par}}^{2}+1\right){{\mathbf{k}}}^{2}+\left(4\mathit{max_par}+3\right){\mathbf{k}}+4$, when ${\mathbf{k}}\ge 6$;
• $\left({\left({\mathbf{p}}+{\mathbf{q}}\right)}^{2}+1\right){{\mathbf{k}}}^{2}+\left(4\left({\mathbf{p}}+{\mathbf{q}}\right)+3\right){\mathbf{k}}+\phantom{\rule{0ex}{0ex}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{\mathbf{k}}\mathit{max_par}\left({\mathbf{k}}\mathit{max_par}+2\right),{{\mathbf{k}}}^{2}{\left({\mathbf{p}}+{\mathbf{q}}\right)}^{2}+\mathit{l}\left(\mathit{l}+3\right)+{{\mathbf{k}}}^{2}\left({\mathbf{q}}+1\right)\right\}+4$, when ${\mathbf{k}}<6$.
Where $\mathit{max_par}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{p}},{\mathbf{q}}\right)$ and if ${\mathbf{p}}=0$, $\mathit{l}={\mathbf{k}}\left({\mathbf{k}}+1\right)/2$, or if ${\mathbf{p}}\ge 1$, $\mathit{l}={\mathbf{k}}\left({\mathbf{k}}+1\right)/2+\left({\mathbf{p}}-1\right){{\mathbf{k}}}^{2}$.
See Section 8 for some examples of the required size of the array r
On entry: if ${\mathbf{mode}}=1$ or $3$, the array r as output from the previous call to nag_rngs_varma_time_series (g05pcc) must be input without any change.
If ${\mathbf{mode}}=0$ or $2$, the contents of r need not be set.
On exit: information required for any subsequent calls to the function with ${\mathbf{mode}}=1$ or $3$. See Section 8.
17:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_CLOSE_TO_STATIONARITY
The reference vector cannot be computed because the AR parameters are too close to the boundary of the stationarity region.
NE_INT
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 1$.
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
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_INT_2
On entry, ${\mathbf{pdv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdv}}\ge {\mathbf{k}}$.
On entry, ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{k}}$.
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_INVERTIBILITY
On entry, the MA parameter matrices are outside the invertibility region.
NE_OUTSIDE_STATIONARITY
On entry, the AR parameter matrices are outside the stationarity region.
NE_POS_DEF
On entry, the covariance matrix var is not positive definite.
NE_TOO_MANY_ITER
An excessive number of iterations were required by the NAG function used to evaluate the eigenvalues of the covariance matrix.
An excessive number of iterations were required by the NAG function used to evaluate the eigenvalues of the matrices used to test for stationarity or invertibility.
An excessive number of iterations were required by the NAG function used to evaluate the eigenvalues to be stored in the reference vector.

## 7  Accuracy

The accuracy is limited by the matrix computations performed, and this is dependent on the condition of the argument and covariance matrices.

Note that, in reference to NE_INVERTIBILITY, nag_rngs_varma_time_series (g05pcc) will permit moving average parameters on the boundary of the invertibility region.
The elements of r contain amongst other information details of the spectral decompositions which are used to generate future multivariate Normals. Note that these eigenvectors may not be unique on different machines. For example the eigenvectors corresponding to multiple eigenvalues may be permuted. Although an effort is made to ensure that the eigenvectors have the same sign on all machines, differences in the signs may theoretically still occur.
The following table gives some examples of the required size of the array r, for $k=1,2$ or $3$, and for various values of $p$ and $q$.
 $q$ 0 1 2 3 13 20 31 46 0 36 56 92 144 85 124 199 310 19 30 45 64 1 52 88 140 208 115 190 301 448 p 35 50 69 92 2 136 188 256 340 397 508 655 838 57 76 99 126 3 268 336 420 520 877 1024 1207 1426
Note that nag_tsa_arma_roots (g13dxc) may be used to check whether a VARMA model is stationary and invertible.
The time taken depends on the values of $p$, $q$ and especially $n$ and $k$.

## 9  Example

This program generates two realizations, each of length $48$, from the bivariate AR(1) model
 $Xt-μ=ϕ1Xt-1-μ+εt$
with
 $ϕ1= 0.80 0.07 0.00 0.58 ,$
 $μ= 5.00 9.00 ,$
and
 $Σ= 2.97 0 0.64 5.38 .$
The pseudorandom number generator is initialized by a call to nag_rngs_init_repeatable (g05kbc). Then, in the first call to nag_rngs_varma_time_series (g05pcc), mode is set to $2$ in order to set up the reference vector before generating the first realization. In the subsequent call mode is set to $3$ and a new recent history is generated and used to generate the second realization.

### 9.1  Program Text

Program Text (g05pcce.c)

### 9.2  Program Data

Program Data (g05pcce.d)

### 9.3  Program Results

Program Results (g05pcce.r)