Skip to content
Advertisement

Scipy – probability in binomial distribution

I’m trying to use scipy in order to calculate a probability, given a binomial distribution:

The probability: in an exam with 45 questions, each one with 5 items, what is the probability of randomly choose right (instead of wrong) more than half the exam, that is, 22.5?

I’ve tried:

from scipy.stats import binom

n = 45
p = 0.20
mu = n * p 

p_x = binom.pmf(1,n,p)

How do I calculate this with scipy?

Advertisement

Answer

Assuming there’s exactly one correct choice for each question, the random variable X which counts the number of correctly answered questions by choosing randomly is indeed binomial distributed with parameters n=45 and p=0.2. Hence, you want to calculate P(X >= 23) = P(X = 23 ) + ... + P(X = 45 ) = 1 - P(X <= 22), so there are two ways to compute it:

from scipy.stats import binom

n = 45
p = 0.2

# (1)
prob = sum(binom.pmf(k, n, p) for k in range(23, 45 + 1))

# (2)
prob = 1 - binom.cdf(22, n, p)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement