Alex
Alex

Reputation: 4264

Randomly drawing samples from matrix normal distribution in python

Is there a way to randomly draw samples from the matrix normal distribution (https://en.wikipedia.org/wiki/Matrix_normal_distribution#Drawing_values_from_the_distribution) using python? Numpy has functionality for drawing from the 1-D normal distribution and the multivariate normal distribution, but I can't find anything on the matrix normal distribution. Thanks.

Upvotes: 1

Views: 1630

Answers (2)

ychz
ychz

Reputation: 571

numpy.random.normal() could draw 2d matrix as long as you specify size like this:

numpy.random.normal(loc = mean, scale = standard deviation, size = (m,n))

Upvotes: 1

Eric
Eric

Reputation: 97661

From the wikipedia page you link:

The matrix normal is related to the multivariate normal distribution in the following way:

\mathbf{X} \sim \mathcal{MN}_{n\times p}(\mathbf{M}, \mathbf{U}, \mathbf{V}),

if and only if

\mathrm{vec}(\mathbf{X}) \sim \mathcal{N}_{np}(\mathrm{vec}(\mathbf{M}), \mathbf{V} \otimes \mathbf{U})

Translated to numpy, that should be

numpy.random.multivariate_normal(M.ravel(), np.kron(V, U)).reshape(M.shape)

Upvotes: 7

Related Questions