在 n 次试验和 p 成功概率下,我需要找到小于或等于 x 次成功的概率
我已经建立了一个函数来查找成功次数。
我曾尝试将此功能与 MAP 一起使用
def bin_dist(n, p, x):
if x > n:
raise ValueError('the number of success cannot be greater than the number of trials')
else:
def fact2(x):
if x < 0:
return ValueError("number of successes cannot be less than 1")
else:
fact = 1
for i in range(1, x + 1):
fact = fact * i
return fact
def combination2(n, x):
return(fact2(n)/fact2(n-x)*fact2(x))
print("The probability of x successes, followed by the total number of arrangements/possible combinations used to find x:")
print(combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))
def bin_cdf(n, p, x):
li = range(0, x)
print map(bin_dist, li)
return map(bin_dist, li)
bin_cdf(3, 0.5, 2)
不幸的是,我要么收到一个无效的语法错误,要么只是一个 MAP 对象
File "<ipython-input-1-e333d90fddea>" , line 21
print map (bin_dist, li)
^
SyntaxError : invalid syntax
我还被告知用于返回 x 次成功的 p 的函数 bin_dist 在将 MAP 转换为列表时缺少两个位置参数
----> 3 print ( list ( map ( bin_dist , li ) ) )
4 return ( list ( map ( bin_dist , li ) ) )
5
TypeError : bin_dist () missing 2 required positional arguments: 'p' and 'x'
我也尝试过从 0 迭代到 x 并找到所有值的总和,但似乎发现了可疑的结果
def bin_cdf(n, p, x):
if x > n:
raise ValueError('the number of success cannot be greater than the number of trials')
else:
def fact2(x):
if x < 0:
return ValueError("number of successes cannot be less than 1")
else:
fact = 1
for i in range(1, x + 1):
fact = fact * i
return fact
def combination2(n, x):
return(fact2(n)/(fact2(n-x)*fact2(x)))
bin_dist = (combination2(n, x) * (p)**(x) * ((1-p)**(n-x)))
li = range(0, x)
bin_cdf = sum([element * bin_dist for element in li])
print(bin_cdf)
return bin_cdf
bin_cdf(3, 0.5, 3)
Out: 0.375
至少在我看来,堆栈溢出社区是互联网上最聪明的。任何建议都非常推荐