如何找到3种情况的概率
How to find the probability of 3 scenarios
我有一桶网球 (2) 和棒球 (22),垃圾桶里总共有 24 个球。
我想知道 3 种情况的概率是多少。
每次我要随机抽出12个球
我想知道把12个球全部拉出来后的概率是多少:
1.) 我拿出两(2)个网球
2.) 我拿出 0 个网球
3.) 我只拉1个网球?
显然这 3 个问题的概率加起来必须为 1 或 100%
谢谢
这是 scipy
在 python 中的 hypergeometric distribution when you sample without replacement. So let's if we use hypergeom:
from scipy.stats import hypergeom
import seaborn as sns
# M is total pool, n is number of successes, N is the number of draws
[M, n, N] = [22, 2, 12]
rv = hypergeom(M, n, N)
#the range of values we are interested in
x = np.arange(0, n+1)
pmf_tballs = rv.pmf(x)
0,1,2 的概率
pmf_tballs
array([0.19480519, 0.51948052, 0.28571429])
sns.barplot(x=x,y=pmf_tballs,color="b")
可以通过暴力计算:
import itertools
balls = [int(i) for i in '1'*2 + '0'*20]
draws = itertools.combinations(balls, 12)
counts_by_tballs = {0:0,1:0,2:0}
for i in draws:
counts[sum(i)] +=1
You get a tally of 0, 1 and 2:
counts
{0: 251940, 1: 671840, 2: 369512}
并且概率与上面的超几何相同:
[i/sum(counts.values()) for i in counts.values()]
[0.19480519480519481, 0.5194805194805194, 0.2857142857142857]
我有一桶网球 (2) 和棒球 (22),垃圾桶里总共有 24 个球。
我想知道 3 种情况的概率是多少。
每次我要随机抽出12个球
我想知道把12个球全部拉出来后的概率是多少:
1.) 我拿出两(2)个网球 2.) 我拿出 0 个网球 3.) 我只拉1个网球?
显然这 3 个问题的概率加起来必须为 1 或 100%
谢谢
这是 scipy
在 python 中的 hypergeometric distribution when you sample without replacement. So let's if we use hypergeom:
from scipy.stats import hypergeom
import seaborn as sns
# M is total pool, n is number of successes, N is the number of draws
[M, n, N] = [22, 2, 12]
rv = hypergeom(M, n, N)
#the range of values we are interested in
x = np.arange(0, n+1)
pmf_tballs = rv.pmf(x)
0,1,2 的概率
pmf_tballs
array([0.19480519, 0.51948052, 0.28571429])
sns.barplot(x=x,y=pmf_tballs,color="b")
可以通过暴力计算:
import itertools
balls = [int(i) for i in '1'*2 + '0'*20]
draws = itertools.combinations(balls, 12)
counts_by_tballs = {0:0,1:0,2:0}
for i in draws:
counts[sum(i)] +=1
You get a tally of 0, 1 and 2:
counts
{0: 251940, 1: 671840, 2: 369512}
并且概率与上面的超几何相同:
[i/sum(counts.values()) for i in counts.values()]
[0.19480519480519481, 0.5194805194805194, 0.2857142857142857]