vendredi 20 juillet 2018

Generating a list of random number, using custom bounds and summing to a desired value

I want to do practically something very similar as described in this answer. I want to create a list of random numbers that sum up to a given target value. If I would not care about the bounds, I could use what the answer suggests:

>>> print np.random.dirichlet(np.ones(10),size=1)
[[ 0.01779975  0.14165316  0.01029262  0.168136  0.03061161  0.09046587  0.19987289  0.13398581  0.03119906 0.17598322]]

However, I want to be able to control the ranges and the target of the individual parameters. I want to provide the bounds of each parameter. For instance, I would pass a list of three tuples, with each tuple specifying the lower and upper boundary of the uniform distribution. The target keyword argument would describe what the sum should add up to.

get_rnd_numbers([(0.0, 1.0), (0.2, 0.5), (0.3, 0.8)], target=0.9)

The output could for example look like this:

[0.2, 0.2, 0.5]

How could that be achieved?




Aucun commentaire:

Enregistrer un commentaire