randomNormalMultivariate¶
Generates pseudorandom numbers from a multivariate normal distribution.
Synopsis¶
randomNormalMultivariate (nVectors, covariances)
Required Arguments¶
- int
nVectors
(Input) - Number of random multivariate normal vectors to generate.
- float
covariances
(Input) - Array of size
length
×length
containing the variance-covariance matrix.
Return Value¶
An array of length nVectors
× length
containing the random
multivariate normal vectors stored consecutively.
Description¶
Function randomNormalMultivariate
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 randomNormalMultivariate
and then by adding the
vectors of means to each row of the result.
Example¶
In this example, randomNormalMultivariate
generates five pseudorandom
normal vectors of length 2 with variance-covariance matrix equal to the
following:
from numpy import *
from pyimsl.stat.randomNormalMultivariate import randomNormalMultivariate
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix
n_random = 5
length = 2
covariances = [[.5, .375], [.375, .5]]
randomSeedSet(123457)
r = randomNormalMultivariate(n_random, covariances)
writeMatrix("multivariate normal random deviates", r)
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