Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-uniform distributed random array

Tags:

python

random

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

like image 529
Javier Avatar asked Dec 02 '22 05:12

Javier


2 Answers

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.

like image 140
Tamás Avatar answered Dec 27 '22 19:12

Tamás


There is a nicer example in Wikipedia page: Dirichlet distribution. The code below generate a k dimension sample:

params = [a1, a2, ..., ak]
sample = [random.gammavariate(a,1) for a in params]
sample = [v/sum(sample) for v in sample]
like image 37
Shuai Zhang Avatar answered Dec 27 '22 18:12

Shuai Zhang