jeudi 5 novembre 2020

Weighed numpys random.choice for intervals

I want to create, pseudo-randomly, an array of transaction values from half-open intervals, for example [1,5), [5,10), [10,25) and so on. Each number in the interval has the same chance to be picked, but I want to adjust the probability of getting some intervals over others. The flow should look like this way:

1: Pick any of the defined intervals > Pick a random number in the interval > Append number in array
2: Pick any of the defined intervals > Pick a random number in the interval > Append number in array
...
n: Pick any of the defined intervals > Pick a random number in the interval > Append number in array

My code is:

import numpy as np
# set interval limits
a,b,c,d,e,f,g,h,i,j = 1,5,10,25,50,100,200,300,500,1000
# start picking random numbers
np.random.seed(33)
trx_value = np.random.choice([np.random.uniform(a,b), 
                              np.random.uniform(b,c),
                              np.random.uniform(c,d),
                              np.random.uniform(d,e),
                              np.random.uniform(e,f),
                              np.random.uniform(f,g),
                              np.random.uniform(g,h),
                              np.random.uniform(h,i),
                              np.random.uniform(i,j)], 20, 
                              p=[0.1, 0.15, 0.2, 0.2, 0.15, 0.1, 0.05, 0.03, 0.02])
trx_value

It seems that it is working in a different way, as the result is not displaying the uniqueness in the values I was expecting:

> array([  8.22611761,   8.22611761, 127.05325134,   8.22611761,
>        266.53172436,  35.43526485,  35.43526485,  88.57366253,
>        127.05325134,  12.38946805, 266.53172436, 127.05325134,
>         12.38946805,  35.43526485, 127.05325134,  12.38946805,
>        266.53172436,   8.22611761])

It seems it is doing something like this:

1: For each interval, pick a random number > Store this number in a pool
2: Pick a random number from the pool > Append number in array
...
n: Pick a random number from the pool > Append number in array

I am sure it is a thinking-problem, but I want to know if this is possible without using a for loop




Aucun commentaire:

Enregistrer un commentaire