Search code examples
pythonrandomsampling

How to choose sample according to distribution?


I have an array of elements [a_1, a_2, ... a_n] and array ofprobabilities associated with this elements [p_1, p_2, ..., p_n].

I want to choose "k" elements from [a_1,...a_n], k << n, according to probabilities [p_1,p_2,...,p_n].

How can I code it in python? Thank you very much, I am not experienced at programming


Solution

  • use numpy.random.choice

    example:

    from numpy.random import choice
    
    sample_space = np.array([a_1, a_2, ... a_n]) # substitute the a_i's 
    discrete_probability_distribution = np.array([p_1, p_2, ..., p_n]) # substitute the p_i's 
    
    # picking N samples
    N = 10
    for _ in range(N): 
        print(choice(sample_space, discrete_probability_distribution)