NAG Library Function Document
nag_rgsn_matrix_multi_normal (g05lyc)
1 Purpose
nag_rgsn_matrix_multi_normal (g05lyc) sets up a reference vector and generates an array of pseudorandom numbers from a multivariate Normal distribution with mean vector and covariance matrix .
2 Specification
| #include <nag.h> |
| #include <nagg05.h> |
| void |
nag_rgsn_matrix_multi_normal (Nag_OrderType order,
Integer mode,
Integer m,
const double xmu[],
const double c[],
Integer pdc,
Integer n,
double x[],
Integer pdx,
Integer igen,
Integer iseed[],
double r[],
Integer lr,
NagError *fail) |
|
3 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_rgsn_matrix_multi_normal (g05lyc) 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_rngs_init_repeatable (g05kbc) (for a repeatable sequence if computed sequentially) or
nag_rngs_init_nonrepeatable (g05kcc) (for a non-repeatable sequence) must be called prior to the first call to nag_rgsn_matrix_multi_normal (g05lyc).
4 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
5 Arguments
- 1:
order – Nag_OrderTypeInput
-
On entry: the
order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by
. See
Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint:
or Nag_ColMajor.
- 2:
mode – IntegerInput
On entry: selects the operation to be performed.
- Initialize and generate random numbers.
- Initialize only (i.e., set up reference vector).
- Generate random numbers using previously set up reference vector.
Constraint:
, or .
- 3:
m – IntegerInput
On entry:
, the number of dimensions of the distribution.
Constraint:
.
- 4:
xmu[m] – const doubleInput
On entry: , the vector of means of the distribution.
- 5:
c[] – const doubleInput
-
Note: the dimension,
dim, of the array
c
must be at least
.
The
th element of the matrix
is stored in
- when ;
- when .
On entry: the covariance matrix of the distribution. Only the upper triangle need be set.
Constraint:
c must be positive semidefinite to
machine precision.
- 6:
pdc – IntegerInput
-
On entry: the stride separating row or column elements (depending on the value of
order) in the array
c.
Constraint:
.
- 7:
n – IntegerInput
On entry:
, the number of random variates required.
Constraint:
.
- 8:
x[] – doubleOutput
-
Note: the dimension,
dim, of the array
x
must be at least
- when ;
- when .
Where
appears in this document, it refers to the array element
- when ;
- when .
On exit: the array of pseudorandom multivariate
Normal vectors generated by the function, with holding the th dimension for the th variate.
- 9:
pdx – IntegerInput
-
On entry: the stride separating row or column elements (depending on the value of
order) in the array
x.
Constraints:
- if ,
;
- if , .
- 10:
igen – IntegerInput
On entry: must contain the identification number for the generator to be used to return a pseudorandom number and should remain unchanged following initialization by a prior call to
nag_rngs_init_repeatable (g05kbc) or
nag_rngs_init_nonrepeatable (g05kcc).
- 11:
iseed[] – IntegerCommunication Array
On entry: contains values which define the current state of the selected generator.
On exit: contains updated values defining the new state of the selected generator.
- 12:
r[lr] – doubleInput/Output
On entry: if , the reference vector as set up by nag_rgsn_matrix_multi_normal (g05lyc) in a previous call with or .
On exit: if or , the reference vector that can be used in subsequent calls to nag_rgsn_matrix_multi_normal (g05lyc) with .
- 13:
lr – IntegerInput
On entry: the dimension of the array
r. If
, it must be the same as the value of
lr specified in the prior call to nag_rgsn_matrix_multi_normal (g05lyc) with
or
.
Constraint:
.
- 14:
fail – NagError *Input/Output
-
The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
- NE_ALLOC_FAIL
Dynamic memory allocation failed.
- NE_BAD_PARAM
On entry, argument had an illegal value.
- NE_INT
On entry, invalid value for
igen. Ensure
igen has not changed since random number generator was initialized.
On entry, .
Constraint: .
On entry, .
Constraint: , or .
On entry, .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INT_2
m is not the same as when
r was set up in a previous call. Previous value
and
.
On entry, , .
Constraint: .
On entry, and .
Constraint: .
On entry, and .
Constraint: .
On entry, and .
Constraint: .
- NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
- NE_POS_DEF
The covariance matrix
c is not positive semidefinite to
machine precision.
7 Accuracy
The maximum absolute error in , and hence in the covariance matrix of the resulting vectors, is less than times the maximum element of , where is the machine precision. Under normal circumstances, the above will be small compared to sampling error.
The time taken by nag_rgsn_matrix_multi_normal (g05lyc) 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.
9 Example
This example prints ten pseudorandom observations from a multivariate Normal distribution with means vector
and covariance matrix
generated by nag_rgsn_matrix_multi_normal (g05lyc). All ten observations are generated by a single call to nag_rgsn_matrix_multi_normal (g05lyc) with
. The random number generator is initialized by
nag_rngs_init_repeatable (g05kbc).
9.1 Program Text
Program Text (g05lyce.c)
9.2 Program Data
None.
9.3 Program Results
Program Results (g05lyce.r)