Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_rand_times_mv_varma (g05pj)

## Purpose

nag_rand_times_mv_varma (g05pj) 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 nag_rand_times_mv_varma (g05pj).

## Syntax

[r, state, x, ifail] = g05pj(mode, n, xmean, ip, phi, iq, theta, var, r, state, 'k', k, 'lr', lr)
[r, state, x, ifail] = nag_rand_times_mv_varma(mode, n, xmean, ip, phi, iq, theta, var, r, state, 'k', k, 'lr', lr)

## 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 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 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 (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 nag_rand_times_mv_varma (g05pj). See the description of the argument mode in Arguments 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_rand_times_mv_varma (g05pj) 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 Arguments for details.
Further computational details are given in Shea (1988). Note, however, that nag_rand_times_mv_varma (g05pj) 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_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_times_mv_varma (g05pj).

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{mode}$int64int32nag_int scalar
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_rand_times_mv_varma (g05pj).
${\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, ip, iq, xmean, phi, theta, var and ldvar have not been changed between calls to nag_rand_times_mv_varma (g05pj).
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
2:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of observations to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     $\mathrm{xmean}\left({\mathbf{k}}\right)$ – double array
$\mu$, the vector of means of the multivariate time series.
4:     $\mathrm{ip}$int64int32nag_int scalar
$p$, the number of autoregressive parameter matrices.
Constraint: ${\mathbf{ip}}\ge 0$.
5:     $\mathrm{phi}\left({\mathbf{k}}×{\mathbf{k}}×{\mathbf{ip}}\right)$ – double array
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)$, then 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.
6:     $\mathrm{iq}$int64int32nag_int scalar
$q$, the number of moving average parameter matrices.
Constraint: ${\mathbf{iq}}\ge 0$.
7:     $\mathrm{theta}\left({\mathbf{k}}×{\mathbf{k}}×{\mathbf{iq}}\right)$ – double array
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), then 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.
8:     $\mathrm{var}\left(\mathit{ldvar},{\mathbf{k}}\right)$ – double array
ldvar, the first dimension of the array, must satisfy the constraint $\mathit{ldvar}\ge {\mathbf{k}}$.
${\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.
9:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
If ${\mathbf{mode}}=1$ or $3$, the array r as output from the previous call to nag_rand_times_mv_varma (g05pj) must be input without any change.
If ${\mathbf{mode}}=0$ or $2$, the contents of r need not be set.
10:   $\mathrm{state}\left(:\right)$int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

### Optional Input Parameters

1:     $\mathrm{k}$int64int32nag_int scalar
Default: the dimension of the array xmean and the first dimension of the array var and the second dimension of the array var. (An error is raised if these dimensions are not equal.)
$k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
2:     $\mathrm{lr}$int64int32nag_int scalar
Default: the dimension of the array r.
The dimension of the array r.
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 Further Comments for some examples of the required size of the array r.

### Output Parameters

1:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
Information required for any subsequent calls to the function with ${\mathbf{mode}}=1$ or $3$. See Further Comments.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{n}}\right)$ – double array
${\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$.
4:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=1$
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{k}}\ge 1$.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{ip}}\ge 0$.
${\mathbf{ifail}}=6$
On entry, the AR parameters are outside the stationarity region.
${\mathbf{ifail}}=7$
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$
Constraint: $\mathit{ldvar}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=11$
k is not the same as when r was set up in a previous call.
${\mathbf{ifail}}=12$
On entry, lr is not large enough, ${\mathbf{lr}}=_$: minimum length required .
${\mathbf{ifail}}=13$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=15$
Constraint: $\mathit{ldx}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=20$
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.
${\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 function used to evaluate the eigenvalues of the covariance matrix.
W  ${\mathbf{ifail}}=23$
An excessive number of iterations were required by the NAG function used to evaluate the eigenvalues stored in the reference vector.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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 ${\mathbf{ifail}}={\mathbf{8}}$, nag_rand_times_mv_varma (g05pj) 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 nag_tsa_uni_arma_roots (g13dx) 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$.

## 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_rand_init_repeat (g05kf). Then, in the first call to nag_rand_times_mv_varma (g05pj), ${\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.
```function g05pj_example

fprintf('g05pj example results\n\n');

% Initialize the generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

n     = int64(48);
xmean = [5; 9];
ip    = int64(1);
iq    = int64(0);
phi   = [0.8; 0; 0.07; 0.58];
theta = [];
var   = [2.97, 0; 0.64, 5.38];
r     = zeros(600, 1);

% Generate the first realisation
% Use mode = 2 to set up R and generate values
mode = int64(2);
[r, state, x, ifail] = g05pj( ...
mode, n, xmean, ip, phi, iq, ...
theta, var, r, state);
% Display the results
fprintf(' Realisation Number 1\n\n');
for i=1:2
fprintf(' Series number %d\n', i);
fprintf(' ---------------\n');
for j=1:6
fprintf(' %8.3f', x(i, (j-1)*8+1:j*8));
fprintf('\n');
end
fprintf('\n');
end
fprintf('\n');

% Generate a second realisation
% Use mode = 3 to reset the series and generate values
mode = int64(3);
[r, state, x, ifail] = g05pj( ...
mode, n, xmean, ip, phi, iq, ...
theta, var, r, state);
% Display the results
fprintf(' Realisation Number 2\n\n');
for i=1:2
fprintf(' Series number %d\n', i);
fprintf(' ---------------\n');
for j=1:6
fprintf(' %8.3f', x(i, (j-1)*8+1:j*8));
fprintf('\n');
end
fprintf('\n');
end
fprintf('\n');

```
```g05pj example results

Realisation Number 1

Series number 1
---------------
4.833    2.813    3.224    3.825    1.023    1.415    2.184    3.005
5.547    4.832    4.705    5.484    9.407   10.335    8.495    7.478
6.373    6.692    6.698    6.976    6.200    4.458    2.520    3.517
3.054    5.439    5.699    7.136    5.750    8.497    9.563   11.604
9.020   10.063    7.976    5.927    4.992    4.222    3.982    7.107
3.554    7.045    7.025    4.106    5.106    5.954    8.026    7.212

Series number 2
---------------
8.458    9.140   10.866   10.975    9.245    5.054    5.023   12.486
10.534   10.590   11.376    8.793   14.445   13.237   11.030    8.405
7.187    8.291    5.920    9.390   10.055    6.222    7.751   10.604
12.441   10.664   10.960    8.022   10.073   12.870   12.665   14.064
11.867   12.894   10.546   12.754    8.594    9.042   12.029   12.557
9.746    5.487    5.500    8.629    9.723    8.632    6.383   12.484

Realisation Number 2

Series number 1
---------------
5.396    4.811    2.685    5.824    2.449    3.563    5.663    6.209
3.130    4.308    4.333    4.903    1.770    1.278    1.340   -0.527
1.745    3.211    4.478    5.170    5.365    4.852    6.080    6.464
2.765    2.148    6.641    7.224   10.316    7.102    5.604    3.934
4.839    3.698    5.210    5.384    7.652    7.315    7.332    7.561
7.537    7.788    6.868    7.575    6.108    6.188    8.132   10.310

Series number 2
---------------
11.345   10.070   13.654   12.409   11.329   13.054   12.465    9.867
10.263   13.394   10.553   10.331    7.814    8.747   10.025   11.167
10.626    9.366    9.607    9.662   10.492   10.766   11.512   10.813
10.799    8.780    9.221   14.245   11.575   10.620    8.282    5.447
9.935    9.386   11.627   10.066   11.394    7.951    7.907   12.616
15.246    9.962   13.216   11.350   11.227    6.021    6.968   12.428

```