Is it possible to get scipy.stats.binom.pmf(x, n, p)
to return the number of trials ( n ) with the probabaility, number of successes ( x ), and probability of successes ( p ) known?
Example Problem:
How many throws does Alex need to do in order to be 90% sure of hitting the target at least 10 times?
where:
You can do something like this. What you want to compute is what scipy.stats
calls the survival function ( sf
) which is 1-cdf
. Since you are interested in greater than or equal to 10 success--that is the sum of the probabilities of 10 or more successes, that is precisely 1-cdf
. The sf
function can take a numpy array for the arguments, so we pass in an array for n
(varying the number of trials). We then look for the number of trials that gave us a value greater than the defined confidence
.
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
# Defining model parameters
p = 0.5
k = 10
confidence = 0.9
n = np.arange(k, 5*k)
# Generating survival function distribution and computing the number of required trials
binomSurvivalDist = scipy.stats.binom.sf(k, n, p)
nrequired = n[binomSurvivalDist >= confidence][0]
# Plotting the results to Verify that this works
fig, ax = plt.subplots(1, 1, figsize=(12, 10))
x = np.arange(0, nrequired+1, 1)
ax.plot(x, scipy.stats.binom.sf(x, nrequired, p), lw=2)
ax.set_xlabel("Number of Successes", fontsize=16)
ax.set_ylabel("Probability of Getting At Least this Many Success", fontsize=16)
ax.set_title("Distribution for %i Trials" % nrequired, fontsize=18)
print(nrequired)
plt.show()
The plot is not necessary for the computation, but allows us to verify that the procedure does give us the correct answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.