Draw multiple samples using numpy.random.multivariate_normal(mean, cov[, size])
15,145
Did you notice the size
argument in the docstring?
For example, this call generates 5 samples from a 3-d distribution:
In [22]: np.random.multivariate_normal(np.zeros(3), np.eye(3), size=5)
Out[22]:
array([[ 1.08534253, 0.70492174, -0.8625333 ],
[ 0.16955737, -0.89453284, 0.8347796 ],
[ 0.49506717, -1.18087912, -0.89118919],
[-0.97837406, -0.42304268, 0.4326744 ],
[-1.18836816, 1.33389231, 0.23616035]])
Response to edited question:
-
np.random.rand(d0, d1, d2)
makesd0*d1*d2
random draws from the univariate uniform distribution on [0, 1), and returns them in an array with shape(d0, d1, d2)
. -
np.random.multivariate_normal(mean, cov, size=n)
, wheremean
is an array with shape(m,)
andcov
is an array with shape(m, m)
, makesn
draws from a multivariate normal distribution, and returns them as the rows of an array with shape(n, m)
. Your list comprehensiondraw_B
also draws samples from a multivariate normal distribution, with one sample per function call, and it puts the samples into a list instead of an array.
Author by
ShanZhengYang
Updated on June 04, 2022Comments
-
ShanZhengYang almost 2 years
Using the numpy function
numpy.random.multivariate_normal()
, if I give the mean and covariance, I am able to draw random samples from a multivariate Gaussian.As an example,
import numpy as np mean = np.zeros(1000) # a zero array shaped (1000,) covariance = np.random.rand(1000, 1000) # a matrix of random values shaped (1000,1000) draw = np.random.multivariate_normal(mean, covariance) # this outputs one "draw" of a multivariate norm, shaped (1000,)
The above function outputs one "draw" from a multivariate Gaussian, shaped
(1000,)
(as the covariance matrix is shaped1000,1000)
).I would like 200 draws. How does one do this? I would create a list comprehension, but I don't see how to create the iteration.
EDIT: Is there a difference between
draw_A = np.random.rand(1000, 1000, 200)
and
draw_B = [np.random.multivariate_normal(mean, covariance) for i in range(200)]
?
Yes,
draw_B
is a list, but are they both 200 independent draws shaped1000,1000)
?