1

I'm trying to draw an numpy 2D-array of shape (N,5) from a certain probability distribution (in my case a Zipf distribution) but with the condition that the sum of all array elements ends up being equal to a certain value of type float. (in the following named K)

I have currently not really an idea how it could be done beside just redrawing until it appears randomly. (But that is of course an insane inefficient idea in that sense). I am currently using the builtin numpy function for random arrays of the Zipf distribution and I am running python 3.7:

v2 =  K * numpy.random.zipf(2, (N,5))

So is there any way to do what I want to do in an elegant/efficient manner? (also nonefficient but practical solutions are welcome). Maybe there is some nice python (or julia) module that could help?

EDIT: Something I did not think about was of course just scaling the resulting array of my random draw above. Anyway, if there are more "elegant"/modular solutions, it might be more pleasing as a solution.

Scipham
  • 133
  • 7
  • 1
    How scaling not elegant enough? v2 = K * v2 / v2.sum() – stmax Oct 26 '19 at 18:25
  • Normalizing a probability distribution and then scaling for some overall amplitude (often number of events) is standard and quite elegant. – alexpiers Oct 26 '19 at 19:43
  • have a look at the top answer [here](https://stackoverflow.com/questions/31811267/how-to-generate-a-list-of-random-numbers-so-their-sum-would-be-equal-to-a-random). Its done with **ints** but dont see whay it woudn't work for floats as well (Disclaimer I havent tried) – DrBwts Oct 29 '19 at 14:02

0 Answers0