[英]How to get the probability of each possible outcome for a multinomial case?
Using stats.binom.pmf(np.arange(0,11),10,0.5)
, I can get an array of numbers representing the probability of an event happening n times(0 to 10) in 10 times given the probability of a single occurrence 0.5, like below:使用
stats.binom.pmf(np.arange(0,11),10,0.5)
,我可以得到一个数字数组,表示一个事件在 10 次中发生 n 次(0 到 10)的概率,给定 a 的概率单次出现 0.5,如下所示:
array([0.00097656, 0.00976563, 0.04394531, 0.1171875 , 0.20507813,
0.24609375, 0.20507813, 0.1171875 , 0.04394531, 0.00976563,
0.00097656])
Now I want to get a similar output for a multinomial case.现在我想为多项式获得类似的输出。
Saying I'm rolling a dice three times, how can I get such a probability array representing the probability of the total points(3 to 18) using an existing package?说我掷骰子三次,如何使用现有包获得表示总点数(3 到 18)的概率的概率数组?
The trick is to consider the appropriate Laplace space, where each event has the same probability.诀窍是考虑适当的拉普拉斯空间,其中每个事件具有相同的概率。 In the case of three dice, this means considering the tuples (1, 1, 1), (1, 1, 2), ... (6, 6, 6) as elementary events.
在三个骰子的情况下,这意味着将元组 (1, 1, 1), (1, 1, 2), ... (6, 6, 6) 视为基本事件。 So you can count up how many of these tuples result in each of the sums from 3 to 18, and then divide those numbers by the total number of tuples to get the probabilities:
因此,您可以计算出从 3 到 18 的每个总和中有多少这些元组,然后将这些数字除以元组总数以获得概率:
from itertools import product
from collections import defaultdict
counts = defaultdict(int)
for dice in product(range(1, 7), repeat=3):
counts[sum(dice)] += 1
probabilities = {n: counts[n] / 6 ** 3 for n in range(3, 19)}
print(probabilities)
{3: 0.004629629629629629,
4: 0.013888888888888888,
5: 0.027777777777777776,
6: 0.046296296296296294,
7: 0.06944444444444445,
8: 0.09722222222222222,
9: 0.11574074074074074,
10: 0.125,
11: 0.125,
12: 0.11574074074074074,
13: 0.09722222222222222,
14: 0.06944444444444445,
15: 0.046296296296296294,
16: 0.027777777777777776,
17: 0.013888888888888888,
18: 0.004629629629629629}
To illustrate my comment, here is the function:为了说明我的评论,这里是函数:
def discrete_uniform_sum_pmf(a, b, n):
du_pmf = {i: 1/(b-a+1) for i in range(a, b+1)}
du_sum_pmf = {0: 1}
for i in range(n):
new_sum_pmf = defaultdict(float)
for prev_sum, dice in product(du_sum_pmf, du_pmf):
new_sum_pmf[prev_sum + dice] += du_sum_pmf[prev_sum] * du_pmf[dice]
du_sum_pmf = new_sum_pmf
return du_sum_pmf
It would be a lot simpler with numpy and has a lot of space for improvement (using CDF to avoid nested loops, exploiting symmetry, etc.) - but should be sufficient to illustrate the idea.使用 numpy 会简单得多,并且有很大的改进空间(使用 CDF 避免嵌套循环、利用对称性等) - 但应该足以说明这个想法。
The output of discrete_uniform_sum_pmf(1, 6, 3)
is the same shown by Arne discrete_uniform_sum_pmf(1, 6, 3)
的输出与 Arne 显示的相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.