# [SciPy-user] Generating random variables in a joint normal distribution?

Robert Kern robert.kern@gmail....
Sun Oct 28 21:42:06 CDT 2007

```Parvel Gu wrote:
> Hi all,
>
> Since I am fresh to use SciPy for simulation, I am not sure about how
> to get a sequence of pairs of random variables in a joint normal
> distribution.
>
> I have read the info docs in the module scipy.stats. It seems that
> only the normal distribution rvs for single variable is provided. Thus
> one sequence of random variables could be get by calling
> stats.norm.rvs() for times (right?). But if I want pairs of random
> variables, for example, (p, s), which are expected to be in the joint
> normal distribution of some given coefficient ro, would there be any
> routine provided to do this just like stats.norm.rvs?
>
> If currently there is no such routine, would there be some workaround
> to achieve this by combining the existing routines? I am not very good
> at numerical probabilities...

In [1]: from numpy import random

In [2]: random.multivariate_normal?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
Namespace:      Interactive
Docstring:
Return an array containing multivariate normally distributed random numbers
with specified mean and covariance.

multivariate_normal(mean, cov) -> random values
multivariate_normal(mean, cov, [m, n, ...]) -> random values

mean must be a 1 dimensional array. cov must be a square two dimensional
array with the same number of rows and columns as mean has elements.

The first form returns a single 1-D array containing a multivariate
normal.

The second form returns an array of shape (m, n, ..., cov.shape[0]).
In this case, output[i,j,...,:] is a 1-D array containing a multivariate
normal.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma