Suppose you have a number that you want to represent a total -- let's say it's 123,456,789.
Now, suppose you want to generate some numbers that add up to that number, but with fuzzy weights.
For instance, suppose I want to generate three numbers. The first should be around 60% of the total, but with some small level of variance. The second should be 30% of the total, again with some variance. And the third would end up being about 10%, depending on the other two.
I tried doing it this way:
percentages = [0.6, 0.3]
total = 123456789
still_need = total
values = []
for i in range(2):
x = int(total * (percentages[i] + np.random.normal(scale=0.05)))
values.append(x)
still_need = still_need - x
values.append(still_need)
But that doesn't seem very elegant.
Is there a better way?
A clean way to do it would be to draw from a multinomial distribution
total = 123456789
percentages = [0.6, 0.3, 0.1]
values = np.random.multinomial(total, percentages)
In this case, the multinomial distribution models rolling a 3-sided die 123456789 times, where the probability of each face turning up is [0.6, 0.3, 0.1]. Calling multinomial() is like running a single trial of this experiment. It returns 3 random integers that sum to 123456789. They represent the number of times that each face of the die turned up. If you want multiple draws, you can use the size parameter`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With