Skip to content

random.multivariate_normal is extremely slow #14193

@zoj613

Description

@zoj613

It seems as though using np.random.multivariate_normal to generate a random vector of a fairly moderate size (1881) is very slow. generating the random variables via cholesky decomposition is much faster.

Reproducing code example:

import numpy as np

mean = np.random.rand(1881)
a = np.random.rand(1881, 1881)
cov = a @ a.T / mean.size

# here I define a custom function to generate the random vector using Cholesky decompostion.
def chol_sample(mean, cov):
    return mean + np.linalg.cholesky(cov) @ np.random.standard_normal(mean.size)

Timing the speed in an ipython session gives:

In [7]: %timeit chol_sample(mean, cov)                                                                                                                        
184 ms +- 4.78 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)

In [8]: %timeit np.random.multivariate_normal(mean, cov)                                                                                                      
6.45 s +- 32.3 ms per loop (mean +- std. dev. of 7 runs, 1 loop each

The Cholesky factorization method is 35 times faster. I think this is a serious problem when one needs to call this function repeatedly for an MCMC sampling problem.

Numpy/Python version information:

('1.17.0', '3.6.8 (default, Apr 28 2019, 00:51:53) \n[GCC 8.2.1 20181127]')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions