hide long namesshow long names
hide short namesshow short names
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)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 and covariance matrix 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
fx = C-1 2πm exp - 12 x-aT C-1 x-a  
where m is the number of dimensions, C is the covariance matrix, a is the vector of means and x is the vector of positions.
Covariance matrices are symmetric and positive semidefinite. Given such a matrix C, there exists a lower triangular matrix L such that LLT=C. L is not unique, if C is singular.
nag_rand_multivar_normal (g05rz) decomposes C to find such an L. It then stores m, a and L in the reference vector r which is used to generate a vector x of independent standard Normal pseudorandom numbers. It then returns the vector a+Lx, which has the required multivariate Normal distribution.
It should be noted that this function will work with a singular covariance matrix C, provided 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
Set up reference vector only.
mode=1 or 3
Generate variates using reference vector set up in a prior call to nag_rand_multivar_normal (g05rz).
mode=2 or 4
Set up reference vector and generate variates.
The variates are stored differently in x for mode=1 or 2 compared with mode=3 or 4.
Constraint: mode=0, 1, 2, 3 or 4.
2:     n int64int32nag_int scalar
n, the number of random variates required.
Constraint: n0.
3:     xmum – double array
a, the vector of means of the distribution.
4:     cldcm – double array
ldc, the first dimension of the array, must satisfy the constraint ldcm.
The covariance matrix of the distribution. Only the upper triangle need be set.
Constraint: C must be positive semidefinite to machine precision.
5:     rlr – double array
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, the number of dimensions of the distribution.
Constraint: m>0.
2:     lr int64int32nag_int scalar
Default: the dimension of the array r.
The dimension of the array r. if mode=1 or 3, it must be the same as the value of lr specified in the prior call to nag_rand_multivar_normal (g05rz) with mode=0, 2 or 4.
Constraint: lrm×m+1+1.

Output Parameters

1:     rlr – double array
2:     state: int64int32nag_int array
Contains updated information on the state of the generator.
3:     xldx: – double array
The first dimension, ldx, of the array x will be
  • if mode=1 or 2, ldx=n;
  • if mode=3 or 4, ldx=m.
The second dimension of the array x will be m if mode=1 or 2 and at least n if mode=3 or 4.
The array of pseudorandom multivariate Normal vectors generated by the function.
Two possible storage orders are available. If mode=1 or 2 then xij holds the jth dimension for the ith variate. If mode=3 or 4 this ordering is reversed and xji holds the jth dimension for the ith variate.
4:     ifail int64int32nag_int scalar
ifail=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
On entry, invalid value for mode.
Constraint: mode=0, 1, 2, 3 or 4.
   ifail=2
Constraint: n0.
   ifail=3
Constraint: m > 0.
   ifail=5
On entry, the covariance matrix C is not positive semidefinite to machine precision.
   ifail=6
Constraint: ldcm.
   ifail=7
m is not the same as when r was set up in a previous call.
   ifail=8
On entry, lr is not large enough, lr=_: minimum length required .
   ifail=9
On entry, state vector has been corrupted or not initialized.
   ifail=11
Constraint: ldxm.
Constraint: ldxn.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

The time taken by nag_rand_multivar_normal (g05rz) is of order nm3.
It is recommended that the diagonal elements of 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, where E is a diagonal matrix with small positive diagonal elements. This ensures that, even when C is singular, or nearly singular, the Cholesky factor L corresponds to a positive definite covariance matrix that agrees with C within machine precision.

Example

This example prints ten pseudorandom observations from a multivariate Normal distribution with means vector
1.0 2.0 -3.0 0.0  
and covariance matrix
1.69 0.39 -1.86 0.07 0.39 98.01 -7.07 -0.71 -1.86 -7.07 11.56 0.03 0.07 -0.71 0.03 0.01 ,  
generated by nag_rand_multivar_normal (g05rz). All ten observations are generated by a single call to nag_rand_multivar_normal (g05rz) with mode=2. The random number generator is initialized by nag_rand_init_repeat (g05kf).
function g05rz_example


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

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

% Number of variates
n = int64(10);

% Distribution means
xmu = [1; 2; -3; 0];

% Upper triangular part of covariance matrix
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);

% Setup and generate in one go
mode = int64(2);

% Generate variates from a multivariate Normal distribution
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)
Chapter Contents
Chapter Introduction
NAG Toolbox

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