# NAG FL Interfaceg05pjf (times_​mv_​varma)

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g05pjf ( mode, n, k, ip, phi, iq, var, r, lr, x, ldx,
 Integer, Intent (In) :: mode, n, k, ip, iq, ldvar, lr, ldx Integer, Intent (Inout) :: state(*), ifail Real (Kind=nag_wp), Intent (In) :: xmean(k), phi(k*k*ip), theta(k*k*iq), var(ldvar,k) Real (Kind=nag_wp), Intent (Inout) :: r(lr), x(ldx,n)
#include <nag.h>
 void g05pjf_ (const Integer *mode, const Integer *n, const Integer *k, const double xmean[], const Integer *ip, const double phi[], const Integer *iq, const double theta[], const double var[], const Integer *ldvar, double r[], const Integer *lr, Integer state[], double x[], const Integer *ldx, Integer *ifail)
The routine may be called by the names g05pjf or nagf_rand_times_mv_varma.

## 3Description

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 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 parameters. $\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 routine 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 (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 more efficiently using the information in a reference vector created during a previous call to g05pjf. See the description of the argument mode in Section 5 for details.
The routine 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 g05pjf 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 g05pjf 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 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 g05pjf.

## 4References

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

## 5Arguments

1: $\mathbf{mode}$Integer Input
On entry: a code for selecting the operation to be performed by the routine.
${\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 g05pjf.
${\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$, you must ensure that the reference vector r and the values of k, ip, iq, xmean, phi, theta, var and ldvar have not been changed between calls to g05pjf.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3: $\mathbf{k}$Integer Input
On entry: $k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
4: $\mathbf{xmean}\left({\mathbf{k}}\right)$Real (Kind=nag_wp) array Input
On entry: $\mu$, the vector of means of the multivariate time series.
5: $\mathbf{ip}$Integer Input
On entry: $p$, the number of autoregressive parameter matrices.
Constraint: ${\mathbf{ip}}\ge 0$.
6: $\mathbf{phi}\left({\mathbf{k}}×{\mathbf{k}}×{\mathbf{ip}}\right)$Real (Kind=nag_wp) array Input
On entry: must contain the elements of the ${\mathbf{ip}}×{\mathbf{k}}×{\mathbf{k}}$ autoregressive parameter matrices of the model, ${\varphi }_{1},{\varphi }_{2},\dots ,{\varphi }_{p}$. If phi is considered as a three-dimensional array, dimensioned as ${\mathbf{phi}}\left({\mathbf{k}},{\mathbf{k}},{\mathbf{ip}}\right)$, the $\left(i,j\right)$th element of ${\varphi }_{\mathit{l}}$ would be stored in ${\mathbf{phi}}\left(i,j,\mathit{l}\right)$; that is, ${\mathbf{phi}}\left(\left(\mathit{l}-1\right)×k×k+\left(j-1\right)×k+i\right)$ must be set equal to the $\left(i,j\right)$th element of ${\varphi }_{\mathit{l}}$, 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: $\mathbf{iq}$Integer Input
On entry: $q$, the number of moving average parameter matrices.
Constraint: ${\mathbf{iq}}\ge 0$.
8: $\mathbf{theta}\left({\mathbf{k}}×{\mathbf{k}}×{\mathbf{iq}}\right)$Real (Kind=nag_wp) array Input
On entry: must contain the elements of the ${\mathbf{iq}}×{\mathbf{k}}×{\mathbf{k}}$ moving average parameter matrices of the model, ${\theta }_{1},{\theta }_{2},\dots ,{\theta }_{q}$. If theta is considered as a three-dimensional array, dimensioned as theta(k,k,iq), the $\left(i,j\right)$th element of ${\theta }_{\mathit{l}}$ would be stored in ${\mathbf{theta}}\left(\mathit{i},\mathit{j},\mathit{l}\right)$; that is, ${\mathbf{theta}}\left(\left(\mathit{l}-1\right)×k×k+\left(\mathit{j}-1\right)×k+\mathit{i}\right)$ must be set equal to the $\left(\mathit{i},\mathit{j}\right)$th element of ${\theta }_{\mathit{l}}$, 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: $\mathbf{var}\left({\mathbf{ldvar}},{\mathbf{k}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{var}}\left(\mathit{i},\mathit{j}\right)$ must contain the ($\mathit{i},\mathit{j}$)th element of $\Sigma$, for $\mathit{i}=1,2,\dots ,{\mathbf{k}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{k}}$. Only the lower triangle is required.
Constraint: the elements of var must be such that $\Sigma$ is positive semidefinite.
10: $\mathbf{ldvar}$Integer Input
On entry: the first dimension of the array var as declared in the (sub)program from which g05pjf is called.
Constraint: ${\mathbf{ldvar}}\ge {\mathbf{k}}$.
11: $\mathbf{r}\left({\mathbf{lr}}\right)$Real (Kind=nag_wp) array Communication Array
On entry: if ${\mathbf{mode}}=1$ or $3$, the array r as output from the previous call to g05pjf 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 routine with ${\mathbf{mode}}=1$ or $3$. See Section 9.
12: $\mathbf{lr}$Integer Input
On entry: the dimension of the array r as declared in the (sub)program from which g05pjf is called.
Constraints:
• if ${\mathbf{k}}\ge 6$, ${\mathbf{lr}}\ge \left(5{\mathit{r}}^{2}+1\right)×{{\mathbf{k}}}^{2}+\left(4\mathit{r}+3\right)×{\mathbf{k}}+4$;
• if ${\mathbf{k}}<6$, ${\mathbf{lr}}\ge \left({\left({\mathbf{ip}}+{\mathbf{iq}}\right)}^{2}+1\right)×{{\mathbf{k}}}^{2}+\phantom{\rule{0ex}{0ex}}\left(4×\left({\mathbf{ip}}+{\mathbf{iq}}\right)+3\right)×{\mathbf{k}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left\{{\mathbf{k}}\mathit{r}\left({\mathbf{k}}\mathit{r}+2\right),{{\mathbf{k}}}^{2}{\left({\mathbf{ip}}+{\mathbf{iq}}\right)}^{2}+\mathit{l}\left(\mathit{l}+3\right)+{{\mathbf{k}}}^{2}\left({\mathbf{iq}}+1\right)\right\}+4$.
Where $\mathit{r}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ip}},{\mathbf{iq}}\right)$ and if ${\mathbf{ip}}=0$, $\mathit{l}={\mathbf{k}}\left({\mathbf{k}}+1\right)/2$, or if ${\mathbf{ip}}\ge 1$, $\mathit{l}={\mathbf{k}}\left({\mathbf{k}}+1\right)/2+\left({\mathbf{ip}}-1\right){{\mathbf{k}}}^{2}$.
See Section 9 for some examples of the required size of the array r.
13: $\mathbf{state}\left(*\right)$Integer array Communication 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.
14: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
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$.
15: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g05pjf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{k}}$.
16: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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$, $2$ or $3$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 1$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ip}}\ge 0$.
${\mathbf{ifail}}=6$
On entry, the AR parameters are outside the stationarity region.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{iq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iq}}\ge 0$.
${\mathbf{ifail}}=8$
On entry, the moving average parameter matrices are such that the model is non-invertible.
${\mathbf{ifail}}=9$
On entry, the covariance matrix var is not positive semidefinite to machine precision.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{ldvar}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldvar}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=11$
k is not the same as when r was set up in a previous call.
Previous value of ${\mathbf{k}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=12$
On entry, lr is not large enough, ${\mathbf{lr}}=〈\mathit{\text{value}}〉$: minimum length required $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=13$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=15$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=20$
An excessive number of iterations were required by the NAG routine used to evaluate the eigenvalues of the matrices used to test for stationarity or invertibility.
${\mathbf{ifail}}=21$
The reference vector cannot be computed because the AR parameters are too close to the boundary of the stationarity region.
${\mathbf{ifail}}=22$
An excessive number of iterations were required by the NAG routine used to evaluate the eigenvalues of the covariance matrix.
${\mathbf{ifail}}=23$
An excessive number of iterations were required by the NAG routine used to evaluate the eigenvalues stored in the reference vector.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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

## 8Parallelism and Performance

g05pjf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05pjf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
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.

Note that, in reference to ${\mathbf{ifail}}={\mathbf{8}}$, g05pjf 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, specified by the argument lr, 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 g13dxf 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$.

## 10Example

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 g05kff. Then, in the first call to g05pjf, ${\mathbf{mode}}=2$ in order to set up the reference vector before generating the first realization. In the subsequent call ${\mathbf{mode}}=3$ and a new recent history is generated and used to generate the second realization.

### 10.1Program Text

Program Text (g05pjfe.f90)

### 10.2Program Data

Program Data (g05pjfe.d)

### 10.3Program Results

Program Results (g05pjfe.r)