Question

I'm trying to generate random numbers from a gaussian distribution. Python has the very useful random.gauss() method, but this is only a one-dimensional random variable. How could I programmatically generate random numbers from this distribution in n-dimensions?

For example, in two dimensions, the return value of this method is essentially distance from the mean, so I would still need (x,y) coordinates to determine an actual data point. I suppose I could generate two more random numbers, but I'm not sure how to set up the constraints.

I appreciate any insights. Thanks!

Was it helpful?

Solution

Numpy has multidimensional equivalents to the functions in the random module

The function you're looking for is numpy.random.normal

OTHER TIPS

You can do this using the np.random.multivariate_normal() function. It works not only for 2-dimensional data but for any number of dimensions.

For example if you would like to have 100 2-dimensional points centered around the point (1,3) you can do the following.

mean = [1, 3]
cov = [[8, -5], [0.2, 0.2]]
x, y = np.random.multivariate_normal([0, 2], cov, 100).T

And for 100 3-dimensional points centered around the point (1,10,100) you can do this.

mean = [1, 10, 100]
cov = [[1,1,1], [1,1,1], [1,1,1]]
x, y, z = np.random.multivariate_normal(mean, cov, 100).T

For more info here is the documentation but you can also ask me. http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multivariate_normal.html

You need to properly decompose your multi-dimensional distribution into a composition of one-dimensional distributions. For example, if you want a point at a Gaussian-distributed distance from a given center and a uniformly-distributed angle around it, you'll get the polar coordinates for the delta with a Gaussian rho and a uniform theta (between 0 and 2 pi), then, if you want cartesian coordinates, you of course do a coordinate transformation.

It sounds like you are asking for a Multivariate Normal Distribution. To generate a value from that distribution, you need to have a covariance matrix that spells out the relationship between x and y. How are your x and y related? If x and y are independent, you can just generate two values with random.gauss().

If you're not sure what your covariance matrix is, then you have a math problem that you need to solve before you can work on the software problem. If you provide more information about what you're trying to model, we might be able to help (and I see that Alex Martelli just posted some solutions for common models).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top