non-uniform distribution

Javier Montoya jmontoyaz at gmail.com
Sat Jun 12 08:27:39 EDT 2010


On Jun 12, 1:08 pm, Etienne Rousee <etie... at rousee.org> wrote:
> Le 12/06/2010 12:05, Javier Montoya a écrit :
>
> > 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?
>
> Let f any function (injective is better).
> Let a1,...,an n numbers uniformly distributed.
> Let s the sum of f(a1),...,f(an).
>
> f(a1)/s, ... , f(an)/s is what you want.
>
> You have to choose f to obtain the distribution you prefer.
>
> --
>
> Etienne

Hi Etienne,

Thanks for your suggestion. I ended with following code:
N=5
a=np.random.random_integers(2*N, size=(1.,N))
a=a/float(sum(a))
However, in some cases, the random numbers are even repeated, for
example, I obtained the following sequence:
[0.03846154,  0.03846154,  0.23076923,  0.34615385,  0.34615385]
This is mainly because in random_integers the integers generated might
be repeated.
One solution, would be to set the first parameter in random_integers
to a larger number.
What do you think? Do you suggest any other function instead of
random_integers?

Best wishes




More information about the Python-list mailing list