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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_multivar_normal (g05rz)

Purpose

nag_rand_multivar_normal (g05rz) sets up a reference vector and generates an array of pseudorandom numbers from a multivariate Normal distribution with mean vector a$a$ and covariance matrix C$C$.

Syntax

[r, state, x, ifail] = g05rz(mode, n, xmu, c, r, state, 'm', m, 'lr', lr)
[r, state, x, ifail] = nag_rand_multivar_normal(mode, n, xmu, c, r, state, 'm', m, 'lr', lr)

Description

When the covariance matrix is nonsingular (i.e., strictly positive definite), the distribution has probability density function
 f(x) = sqrt( ( |C − 1| )/( (2π)m ) ) exp( − (1/2)(x − a)TC − 1(x − a)) $f(x) = |C-1| (2π)m exp( - 12 (x-a)T C-1 (x-a) )$
where m$m$ is the number of dimensions, C$C$ is the covariance matrix, a$a$ is the vector of means and x$x$ is the vector of positions.
Covariance matrices are symmetric and positive semidefinite. Given such a matrix C$C$, there exists a lower triangular matrix L$L$ such that LLT = C$L{L}^{\mathrm{T}}=C$. L$L$ is not unique, if C$C$ is singular.
nag_rand_multivar_normal (g05rz) decomposes C$C$ to find such an L$L$. It then stores m$m$, a$a$ and L$L$ in the reference vector r$r$ which is used to generate a vector x$x$ of independent standard Normal pseudorandom numbers. It then returns the vector a + Lx$a+Lx$, which has the required multivariate Normal distribution.
It should be noted that this function will work with a singular covariance matrix C$C$, provided C$C$ is positive semidefinite, despite the fact that the above formula for the probability density function is not valid in that case. Wilkinson (1965) should be consulted if further information is required.
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_multivar_normal (g05rz).

References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley
Wilkinson J H (1965) The Algebraic Eigenvalue Problem Oxford University Press, Oxford

Parameters

Compulsory Input Parameters

1:     mode – int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode = 0${\mathbf{mode}}=0$
Set up reference vector only.
mode = 1${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to nag_rand_multivar_normal (g05rz).
mode = 2${\mathbf{mode}}=2$
Set up reference vector and generate variates.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$ or 2$2$.
2:     n – int64int32nag_int scalar
n$n$, the number of random variates required.
Constraint: n0${\mathbf{n}}\ge 0$.
3:     xmu(m) – double array
m, the dimension of the array, must satisfy the constraint m > 0${\mathbf{m}}>0$.
a$a$, the vector of means of the distribution.
4:     c(ldc,m) – double array
ldc, the first dimension of the array, must satisfy the constraint ldcm$\mathit{ldc}\ge {\mathbf{m}}$.
The covariance matrix of the distribution. Only the upper triangle need be set.
Constraint: C$C$ must be positive semidefinite to machine precision.
5:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint lrm × (m + 1) + 1${\mathbf{lr}}\ge {\mathbf{m}}×\left({\mathbf{m}}+1\right)+1$.
If mode = 1${\mathbf{mode}}=1$, the reference vector as set up by nag_rand_multivar_normal (g05rz) in a previous call with mode = 0${\mathbf{mode}}=0$ or 2$2$.
6:     state( : $:$) – 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:     m – int64int32nag_int scalar
Default: The dimension of the array xmu and the first dimension of the array c and the second dimension of the array c. (An error is raised if these dimensions are not equal.)
m$m$, the number of dimensions of the distribution.
Constraint: m > 0${\mathbf{m}}>0$.
2:     lr – int64int32nag_int scalar
Default: The dimension of the array r.
The dimension of the array r as declared in the (sub)program from which nag_rand_multivar_normal (g05rz) is called. If mode = 1${\mathbf{mode}}=1$, it must be the same as the value of lr specified in the prior call to nag_rand_multivar_normal (g05rz) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
Constraint: lrm × (m + 1) + 1${\mathbf{lr}}\ge {\mathbf{m}}×\left({\mathbf{m}}+1\right)+1$.

ldc ldx

Output Parameters

1:     r(lr) – double array
If mode = 0${\mathbf{mode}}=0$ or 2$2$, the reference vector that can be used in subsequent calls to nag_rand_multivar_normal (g05rz) with mode = 1${\mathbf{mode}}=1$.
2:     state( : $:$) – 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 updated information on the state of the generator.
3:     x(ldx,m) – double array
ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
The array of pseudorandom multivariate Normal vectors generated by the function, with x(i,j)${\mathbf{x}}\left(i,j\right)$ holding the j$j$th dimension for the i$i$th variate.
4:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
On entry, mode0${\mathbf{mode}}\ne 0$, 1$1$ or 2$2$.
ifail = 2${\mathbf{ifail}}=2$
On entry, n < 1${\mathbf{n}}<1$.
ifail = 3${\mathbf{ifail}}=3$
On entry, m < 1${\mathbf{m}}<1$.
ifail = 5${\mathbf{ifail}}=5$
The covariance matrix c is not positive semidefinite to machine precision.
ifail = 6${\mathbf{ifail}}=6$
On entry, ldc < m$\mathit{ldc}<{\mathbf{m}}$.
ifail = 7${\mathbf{ifail}}=7$
The reference vector r has been corrupted or m has changed since r was set up in a previous call to nag_rand_multivar_normal (g05rz) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 8${\mathbf{ifail}}=8$
On entry, lrm × (m + 1)${\mathbf{lr}}\le {\mathbf{m}}×\left({\mathbf{m}}+1\right)$.
ifail = 9${\mathbf{ifail}}=9$
 On entry, state vector was not initialized or has been corrupted.
ifail = 11${\mathbf{ifail}}=11$
On entry, ldx < n$\mathit{ldx}<{\mathbf{n}}$.

Not applicable.

Further Comments

The time taken by nag_rand_multivar_normal (g05rz) is of order nm3$n{m}^{3}$.
It is recommended that the diagonal elements of C$C$ should not differ too widely in order of magnitude. This may be achieved by scaling the variables if necessary. The actual matrix decomposed is C + E = LLT$C+E=L{L}^{\mathrm{T}}$, where E$E$ is a diagonal matrix with small positive diagonal elements. This ensures that, even when C$C$ is singular, or nearly singular, the Cholesky factor L$L$ corresponds to a positive definite covariance matrix that agrees with C$C$ within machine precision.

Example

```function nag_rand_multivar_normal_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

mode = int64(2);
n = int64(10);
xmu = [1; 2; -3; 0];
c = [1.69, 0.39, -1.86, 0.07;
0, 98.01, -7.07, -0.71;
0, 0, 11.56, 0.03;
0, 0, 0, 0.01];
r = zeros(31, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_multivar_normal(mode, n, xmu, c, r, state)
```
```

r =

4.5000
1.3000
0.3000
-1.4308
0.0538
0
9.8955
-0.6711
-0.0734
0
0
3.0104
0.0192
0
0
0
0.0367
1.0000
2.0000
-3.0000
0
0
0
0
0
0
0
0
0
0
0

state =

17
1234
1
0
24966
7893
22166
28418
17917
13895
19930
8
0
1234
1
1
1234

x =

1.4534  -14.1206   -3.7410    0.1184
-0.6191   -4.8000   -0.1473   -0.0304
1.8607    5.3206   -5.0753    0.0106
2.0861  -13.6996   -1.3451    0.1428
-0.6326    3.9729    0.5721   -0.0770
0.9754   -3.8162   -4.2978    0.0040
0.6174   -5.1573    2.5037    0.0772
2.0352   26.9359    2.2939   -0.0826
0.9941   14.7700   -1.0421   -0.0549
1.5780    2.8916   -2.1725   -0.0129

ifail =

0

```
```function g05rz_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

mode = int64(2);
n = int64(10);
xmu = [1; 2; -3; 0];
c = [1.69, 0.39, -1.86, 0.07;
0, 98.01, -7.07, -0.71;
0, 0, 11.56, 0.03;
0, 0, 0, 0.01];
r = zeros(31, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05rz(mode, n, xmu, c, r, state)
```
```

r =

4.5000
1.3000
0.3000
-1.4308
0.0538
0
9.8955
-0.6711
-0.0734
0
0
3.0104
0.0192
0
0
0
0.0367
1.0000
2.0000
-3.0000
0
0
0
0
0
0
0
0
0
0
0

state =

17
1234
1
0
24966
7893
22166
28418
17917
13895
19930
8
0
1234
1
1
1234

x =

1.4534  -14.1206   -3.7410    0.1184
-0.6191   -4.8000   -0.1473   -0.0304
1.8607    5.3206   -5.0753    0.0106
2.0861  -13.6996   -1.3451    0.1428
-0.6326    3.9729    0.5721   -0.0770
0.9754   -3.8162   -4.2978    0.0040
0.6174   -5.1573    2.5037    0.0772
2.0352   26.9359    2.2939   -0.0826
0.9941   14.7700   -1.0421   -0.0549
1.5780    2.8916   -2.1725   -0.0129

ifail =

0

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013