afaulconbridge
afaulconbridge

Reputation: 1182

Sample from repeated binomial distribtion

I have a repeated binomial test (p = probability of success, n = no of trials). I want to randomly generate the number of successes.

I could just generate n random numbers, but that is too inefficient. i.e. not like this:

def successes(p, n):
    count = 0
    for i in xrange(n):
        if random.random() < p:
            count += 1
    return count

I want this to be an exact sample (not a normal or other approximation) as this is going to be used repeatedly for highly skewed distributions.

I feel that this could be done using the density functions etc ( http://en.wikipedia.org/wiki/Binomial_distribution#Probability_mass_function ) but I can't quite wrap my head around it.

Upvotes: 0

Views: 2728

Answers (1)

Janne Karila
Janne Karila

Reputation: 25197

numpy.random.binomial does it.

Upvotes: 2

Related Questions