3

是否可以scipy.stats.binom.pmf(x, n, p)返回已知概率、成功次数 ( x ) 和成功概率 ( p ) 的试验次数 ( n ) ?

示例问题:

为了有 90% 的把握击中目标至少 10 次,Alex 需要投掷多少次?

在哪里:

  • 概率 = 0.90
  • p = 0.50
  • x = 10
4

1 回答 1

5

你可以做这样的事情。您要计算的是scipy.stats所谓的生存函数 ( sf),即1-cdf. 由于您对大于或等于 10 次成功感兴趣——这是 10 次或更多成功的概率之和,这正是1-cdf. 该sf函数可以接受一个 numpy 数组作为参数,因此我们传入一个数组 for 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()

该图对于计算不是必需的,但可以让我们验证该过程确实给了我们正确的答案。

于 2019-12-16T17:34:00.193 回答