CNL Stat : Random Number Generation : random_normal_multivariate
random_normal_multivariate
Generates pseudorandom numbers from a multivariate normal distribution.
Synopsis
#include <imsls.h>
float *imsls_f_random_normal_multivariate(int n_vectors, int length, float *covariances, 0)
The type double function is imsls_d_random_normal_multivariate.
Required Arguments
int n_vectors (Input)
Number of random multivariate normal vectors to generate.
int length (Input)
Length of the multivariate normal vectors.
float *covariances (Input)
Array of size length × length containing the variance-covariance matrix.
Return Value
An array of length n_vectors × length containing the random multivariate normal vectors stored consecutively.
Synopsis with Optional Arguments
#include <imsls.h>
float *imsls_f_random_normal_multivariate (int n_vectors, int length, float *covariances,
IMSLS_RETURN_USER, float r[],
0)
Optional Arguments
IMSLS_RETURN_USER, float r[] (Output)
User-supplied array of length n_vectors × length containing the random multivariate normal vectors stored consecutively.
Description
Function imsls_f_random_normal_multivariate generates pseudorandom numbers from a multivariate normal distribution with mean vector consisting of all zeros and variance-covariance matrix covariances. First, the Cholesky factor of the variance-covariance matrix is computed. Then, independent random normal deviates with mean 0 and variance 1 are generated, and the matrix containing these deviates is postmultiplied by the Cholesky factor. Because the Cholesky factorization is performed in each invocation, it is best to generate as many random vectors as needed at once.
Deviates from a multivariate normal distribution with means other than 0 can be generated by using imsls_f_random_normal_multivariate and then by adding the vectors of means to each row of the result.
Example 1
In this example, imsls_f_random_normal_multivariate generates five pseudorandom normal vectors of length 2 with variance-covariance matrix equal to the following:
 
#include <imsls.h>
 
int main()
{
int n_vectors = 5;
int length = 2;
float covariances[] = {.5, .375, .375, .5};
float *random;
 
imsls_random_seed_set (123457);
random = imsls_f_random_normal_multivariate (n_vectors, length,
covariances, 0);
 
imsls_f_write_matrix ("multivariate normal random deviates",
n_vectors, length, random, 0);
}
Output
multivariate normal random deviates
1 2
1 1.451 1.595
2 0.058 0.641
3 -0.867 -0.492
4 -0.933 -1.413
5 -0.325 -0.527
Example 2
Using the same variance-covariance matrix as above, imsls_f_random_normal_multivariate generates 10 pseudorandom normal vectors of length 2 in 2 blocks of 5. After resetting the random number generator, in this case the Mersenne Twister, imsls_f_random_normal_multivariate then generates all 10 random vectors at once. Because the generator is reset, the values in the third call match the combined values of the first two.
 
#include <imsls.h>
 
int main(){
int seed = 123457, j;
int n_vectors, l_vectors;
float *r1 = NULL, *r2 = NULL, *r3 = NULL;
float covariances[] = {1.0, 0.5, 0.5, 1.0};
unsigned long long *itable;
 
imsls_random_option(9);
imsls_random_seed_set (seed);
 
n_vectors = 5;
l_vectors = 2;
 
/* Generate the first matrix. */
r1 = imsls_f_random_normal_multivariate (n_vectors, l_vectors,
covariances, 0);
 
printf("multivariate random normal deviates");
imsls_f_write_matrix ("\nmatrix 1: ", n_vectors, l_vectors,
r1, 0);
 
/* Generate the second matrix.*/
r2 = imsls_f_random_normal_multivariate (n_vectors, l_vectors,
covariances, 0);
 
imsls_f_write_matrix ("\nmatrix 2: ", n_vectors, l_vectors, r2, 0);
 
/* Reset the generator. Setting itable[0] to a value > 625 resets
the generator to its original state.*/
imsls_random_MT64_table_get (&itable,0);
itable[0] = 1000;
imsls_random_MT64_table_set (itable);
 
/* Generate all rows after resetting the generator. */
n_vectors = 10;
r3 = imsls_f_random_normal_multivariate (n_vectors, l_vectors,
covariances, 0);
imsls_f_write_matrix ("\nmatrix 3: ", n_vectors, l_vectors, r3, 0);
 
imsls_free(r1);
imsls_free(r2);
imsls_free(r3);
imsls_free(itable);
}
Output
multivariate random normal deviates
matrix 1:
1 2
1 1.321 0.598
2 0.055 1.050
3 -0.546 -1.876
4 0.724 1.548
5 -0.591 0.764
matrix 2:
1 2
1 -0.0288 -0.7060
2 -0.9761 -0.0418
3 0.8074 -0.8965
4 -0.6163 -0.7335
5 -0.4368 0.0183
matrix 3:
1 2
1 1.321 0.598
2 0.055 1.050
3 -0.546 -1.876
4 0.724 1.548
5 -0.591 0.764
6 -0.029 -0.706
7 -0.976 -0.042
8 0.807 -0.896
9 -0.616 -0.734
10 -0.437 0.018