tags:

views:

154

answers:

1

Dear all,

I need to generate a vector of random float numbers between [0,1] such that their sum equals 1 and that are distributed non-uniformly. Is there any python function that generates such a vector?

Best wishes

+7  A: 

The distribution you are probably looking for is called the Dirichlet distribution. There's no built-in function in Python for drawing random numbers from a Dirichlet distribution, but NumPy contains one:

>>> from numpy.random.mtrand import dirichlet
>>> print dirichlet([1] * n)

This will give you n numbers that sum up to 1, and the probability of each such combination will be equal.

Alternatively, if you don't have NumPy, you can make use of the fact that a random sample drawn from an n-dimensional Dirichlet distribution can be generated by drawing n independent samples from a gamma distribution with shape and scale parameters equal to 1 and then dividing the samples with the sum:

>>> from random import gammavariate
>>> def dirichlet(n):
...     samples = [gammavariate(1, 1) for _ in xrange(n)]
...     sum_samples = sum(samples)
...     return [x/sum_samples for x in samples]

The reason why you need a Dirichlet distribution is because if you simply draw random numbers uniformly from some interval and then divide them by the sum of them, the resulting distribution will be biased towards samples consisting of roughly equal numbers. See Luc Devroye's book for more on this topic.

Tamás
Thanks Tamás, that's what I was exactly looking for!
Javier
Great! Please consider marking the solution as "accepted" if you are satisfied with this one.
Tamás
Why not use the simpler `random.expovariate`?
Mark Dickinson
Sure, `random.expovariate` is fine as well in this case; the only reason I prefer to use `random.gammavariate` is that it works even in the more general case when the Dirichlet distribution has different `alpha` parameters along different dimensions. The scenario described by Javier corresponds to the case when every `alpha` parameter in the Dirichlet distribution is 1.
Tamás