mercredi 26 février 2020

Numpy random sampling from multiple distributions

What is an efficient way to sample N numbers, where each number is a sampled by first choosing a random distribution from within a fixed predetermined list (using some specific discrete distribution), and then sampling from that chosen distribution.

So, for instance, if we want to choose 0 with probability 0.30, 1 with probability 0.30, and with probability 0.40 we want to choose any real number in [0,1) with uniform distribution, we can write:

np.choose(
    np.random.choice(2, size=N, p=[0.6, 0.4]),
    np.vstack((
        np.random.choice(2, size=(1,N)),
        np.random.uniform(size=(1,N))
    )))

However this generates NxD random numbers (where D is the number of distributions) and uses NxD space. Is there a more efficient vectorized (i.e no O(N) python for-loops) way of achieving this?

If not in the general case, can the above specific combined distribution be somehow efficiently generated otherwise?




Aucun commentaire:

Enregistrer un commentaire