non-uniform distribution

J

Javier Montoya

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
 
E

Etienne Rousee

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.
 
S

Steven D'Aprano

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?

You haven't explained your requirements in anywhere near enough detail.
Any of these match your description:

[1.0]
[0.0, 0.0, 0.0, 0.0, 1.0]
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3]
[0.01, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.84]

and many, many more. What do you mean by "non-uniformly"? Do you have any
specific distribution in mind? Do you have to have a particular number of
items?

For instance, you could do this:

L = []
total = 0.0
while total < 1:
x = random.uniform(0, 1-total)
L.append(x)
total += x

assert sum(L) == total == 1.0
 
J

Javier Montoya

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.

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
 
J

Javier Montoya

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.

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
 
J

Javier Montoya

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?

You haven't explained your requirements in anywhere near enough detail.
Any of these match your description:

[1.0]
[0.0, 0.0, 0.0, 0.0, 1.0]
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3]
[0.01, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.84]

and many, many more. What do you mean by "non-uniformly"? Do you have any
specific distribution in mind? Do you have to have a particular number of
items?

For instance, you could do this:

L = []
total = 0.0
while total < 1:
    x = random.uniform(0, 1-total)
    L.append(x)
    total += x

assert sum(L) == total == 1.0

Hi Steven,

The number of items in the vector is usually between [4,15]. I was
having in mind sth. like:
[0.25, 0.23, 0.30, 0.22] for the 4 elems case.

Best wishes
 
I

Ian

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
Hi Javier,

The answer to your question is "No", and the reason is that your
requirement is impossible.

Whatever distribution you choose - and you have not said what you
require - will be altered by any scaling to meet the constraint that the
total is 1.

What exactly are you trying to achieve?

Regards

Ian
 
J

Javier Montoya

On 12/06/10 11:05, Javier Montoya wrote:> 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

Hi Javier,

The answer to your question is "No", and the reason is that your
requirement is impossible.

Whatever distribution you choose - and you have not said what you
require - will be altered by any scaling to meet the constraint that the
total is 1.

What exactly are you trying to achieve?

Regards

Ian

Hi Ian, I found that the Dirichlet distribution is suitable for my
case.
Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top