我想计算事件的概率,n
骰子与s
边(编号从1到s
)的所有眼睛的总和等于{{1} }。我的语言是Python 3。
我目前的方法几乎是一个尝试计数的解决方案,只适用于小数字(运行t
已经占用了我所有的RAM并强迫我硬重置):
probability(10, 10, 50)
但老实说,我不知道如何解决这个问题。你能帮助我走上正轨吗?
答案 0 :(得分:1)
首先关闭:总可能的滚动组合将始终为s**n
,因此您无需存储所有可能性的列表以获得其长度。类似地,你可以保持一个预期结果的运行总数,而不是保留它们的列表以节省内存空间,但它仍然不会加速功能:
def probability(n, s, t):
all_rolls = itertools.product(range(1,s+1), repeat=n) #no list, leave it a generator
target_rolls=sum(1 for l in all_rolls if sum(l)==t) #just total them up
return round(target_rolls/s**n, 4)
一种更有效的计算可能性的方法是使用dict
和一些聪明的迭代。每个字典将使用roll值作为键,频率作为值,每次迭代prev
将成为前一个X骰子的dict,cur
将通过添加另一个die来更新它:< / p>
import collections
def probability(n, s, t):
prev = {0:1} #previous roll is 0 for first time
for _ in range(n):
cur = collections.defaultdict(int) #current probability
for r,times in prev.items():
for i in range(1,s+1):
#if r occured `times` times in the last iteration then
#r+i have `times` more possibilities for the current iteration.
cur[r+i]+=times
prev = cur #use this for the next iteration
return cur[t] / s**n
#return round(cur[t] / s**n , 4)
注1:由于cur
是一个defaultdict,试图查找一个用给定输入不可能的数字将返回0
注意2:由于此方法将包含所有可能结果的字典放在一起,您可以返回cur
并在同一个骰子卷上计算多个不同的可能结果。
答案 1 :(得分:1)
停止制作清单。只需使用延迟评估。
from itertools import product
def prob(dice, pips, target):
rolls = product(range(1, pips+1), repeat=dice)
targets = sum(1 for roll in rolls if sum(roll) == target)
return targets / pips**dice
试验:
for i in range(5, 26):
print(i, prob(5, 5, i))
print('sum: ', sum(prob(5, 5, i) for i in range(5, 26)))
# prints
5 0.00032
6 0.0016
7 0.0048
8 0.0112
9 0.0224
10 0.03872
11 0.0592
12 0.0816
13 0.1024
14 0.1168
15 0.12192 # symmetrical, with peak in middle
16 0.1168
17 0.1024
18 0.0816
19 0.0592
20 0.03872
21 0.0224
22 0.0112
23 0.0048
24 0.0016
25 0.00032
sum: 1.0000000000000002
编辑:删除未使用的def
答案 2 :(得分:1)
itertools.product对于太多的边数> 5和边数> 6来说太慢了。在我的具有dice_number:10和sides:10的机器上,花了一个半小时来计算。 取而代之的是,我使用numpy.polypow函数来计算目标,并且花费了不到一秒钟的时间。
from numpy.polynomial.polynomial import polypow
def probability(dice_number, sides, target):
"""
Using numpy polynomial
The number of ways to obtain x as a sum of n s-sided dice
is given by the coefficients of the polynomial:
f(x) = (x + x^2 + ... + x^s)^n
"""
# power series (note that the power series starts from x^1, therefore
# the first coefficient is zero)
powers = [0] + [1] * sides
# f(x) polynomial, computed used polypow in numpy
poly = polypow(powers, dice_number)
return poly[target] / sides ** dice_number if target < len(poly) else 0