ABC*_*ABC 3 python statistics scipy python-3.x binomial-theorem
是否有可能在概率、成功次数 ( x ) 和成功概率 ( pscipy.stats.binom.pmf(x, n, p) ) 已知的情况下返回试验次数 ( n ) ?
问题示例:
Alex 需要投掷多少次才能有 90% 的把握至少击中目标 10 次?
在哪里:
小智 5
你可以做这样的事情。您要计算的是所谓scipy.stats的生存函数 ( sf),即1-cdf。由于您感兴趣的是大于或等于 10 次成功——即 10 次或更多成功的概率之和,即1-cdf。该sf函数可以采用 numpy 数组作为参数,因此我们传入一个数组n(改变试验次数)。然后我们寻找给我们的值大于定义的试验次数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()
Run Code Online (Sandbox Code Playgroud)
该图对于计算来说不是必需的,但可以让我们验证该过程是否给出了正确的答案。
| 归档时间: |
|
| 查看次数: |
1561 次 |
| 最近记录: |