2

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 shaped 1000,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 shaped 1000,1000)?

1 Answer 1

6

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) makes d0*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), where mean is an array with shape (m,) and cov is an array with shape (m, m), makes n draws from a multivariate normal distribution, and returns them as the rows of an array with shape (n, m). Your list comprehension draw_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.
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I noticed this. Please see my edit above for clarification. Is draw_A the same as draw_B?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.