PDF version (NAG web site
, 64-bit version, 64-bit version)
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 and covariance matrix .
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
where
is the number of dimensions,
is the covariance matrix,
is the vector of means and
is the vector of positions.
Covariance matrices are symmetric and positive semidefinite. Given such a matrix , there exists a lower triangular matrix such that . is not unique, if is singular.
nag_rand_multivar_normal (g05rz) decomposes to find such an . It then stores , and in the reference vector which is used to generate a vector of independent standard Normal pseudorandom numbers. It then returns the vector , which has the required multivariate Normal distribution.
It should be noted that this function will work with a singular covariance matrix
, provided
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:
– int64int32nag_int scalar
-
A code for selecting the operation to be performed by the function.
- Set up reference vector only.
- or
- Generate variates using reference vector set up in a prior call to nag_rand_multivar_normal (g05rz).
- or
- Set up reference vector and generate variates.
The variates are stored differently in
x for
or
compared with
or
.
Constraint:
, , , or .
- 2:
– int64int32nag_int scalar
-
, the number of random variates required.
Constraint:
.
- 3:
– double array
-
, the vector of means of the distribution.
- 4:
– double array
-
ldc, the first dimension of the array, must satisfy the constraint
.
The covariance matrix of the distribution. Only the upper triangle need be set.
Constraint:
must be positive semidefinite to
machine precision.
- 5:
– double array
- 6:
– 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:
– 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.)
, the number of dimensions of the distribution.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
r.
The dimension of the array
r. if
or
, it must be the same as the value of
lr specified in the prior call to
nag_rand_multivar_normal (g05rz) with
,
or
.
Constraint:
.
Output Parameters
- 1:
– double array
- 2:
– int64int32nag_int array
-
Contains updated information on the state of the generator.
- 3:
– double array
-
The first dimension,
, of the array
x will be
- if or , ;
- if or , .
The second dimension of the array
x will be
if
or
and at least
if
or
.
The array of pseudorandom multivariate Normal vectors generated by the function.
Two possible storage orders are available. If or then holds the th dimension for the th variate. If or this ordering is reversed and holds the th dimension for the th variate.
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, invalid value for
mode.
Constraint:
,
,
,
or
.
-
-
Constraint: .
-
-
Constraint: .
-
-
On entry, the covariance matrix
is not positive semidefinite to
machine precision.
-
-
Constraint: .
-
-
m is not the same as when
r was set up in a previous call.
-
-
On entry,
lr is not large enough,
: minimum length required .
-
-
On entry,
state vector has been corrupted or not initialized.
-
-
Constraint: .
Constraint: .
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
Not applicable.
Further Comments
The time taken by nag_rand_multivar_normal (g05rz) is of order .
It is recommended that the diagonal elements of
should not differ too widely in order of magnitude. This may be achieved by scaling the variables if necessary. The actual matrix decomposed is
, where
is a diagonal matrix with small positive diagonal elements. This ensures that, even when
is singular, or nearly singular, the Cholesky factor
corresponds to a positive definite covariance matrix that agrees with
within
machine precision.
Example
This example prints ten pseudorandom observations from a multivariate Normal distribution with means vector
and covariance matrix
generated by
nag_rand_multivar_normal (g05rz). All ten observations are generated by a single call to
nag_rand_multivar_normal (g05rz) with
.
The random number generator is initialized by
nag_rand_init_repeat (g05kf).
Open in the MATLAB editor:
g05rz_example
function g05rz_example
fprintf('g05rz example results\n\n');
seed = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
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];
m = size(c,1);
mode = int64(2);
lr = m*(m+1) + 2;
r = zeros(lr, 1);
[r, state, x, ifail] = g05rz( ...
mode, n, xmu, c, r, state);
disp('Variates');
disp(x);
g05rz example results
Variates
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
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015