求n个骰子滚动和概率的最佳解是什么?我通过寻找
来解决它- 的意思。 标准差
- 。
-
x
以下数字的z_score - z_score:
x
- 将两者转换为概率
- 减去另一个
这是我到目前为止所做的。
# sides - number of sides on one die
def get_mean(sides)
(1..sides).inject(:+) / sides.to_f
end
def get_variance(sides)
mean_of_squares = ((1..sides).inject {|sum, side| sum + side ** 2}) / sides.to_f
square_mean = get_mean(sides) ** 2
mean_of_squares - square_mean
end
def get_sigma(variance)
variance ** 0.5
end
# x - the number of points in question
def get_z_score(x, mean, sigma)
(x - mean) / sigma.to_f
end
# Converts z_score to probability
def z_to_probability(z)
return 0 if z < -6.5
return 1 if z > 6.5
fact_k = 1
sum = 0
term = 1
k = 0
loop_stop = Math.exp(-23)
while term.abs > loop_stop do
term = 0.3989422804 * ((-1)**k) * (z**k) / (2*k+1) / (2**k) * (z**(k+1)) / fact_k
sum += term
k += 1
fact_k *= k
end
sum += 0.5
1 - sum
end
# Calculate probability of getting 'х' total points by rolling 'n' dice with 'sides' number of sides.
def probability_of_sum(x, n, sides=6)
mean = n * get_mean(sides)
variance = get_variance(sides)
sigma = get_sigma(n * variance)
# Rolling below the sum
z1 = get_z_score(x, mean, sigma)
prob_1 = z_to_probability(z1)
# Rolling above the sum
z2 = get_z_score(x+1, mean, sigma)
prob_2 = z_to_probability(z2)
prob_1 - prob_2
end
# Run probability for 100 dice
puts probability_of_sum(400, 100)
我关心的是,当我选择x = 200
时,概率是0。这是正确的解决方案吗?
存在一个二项式系数交替和的精确解。我在一些地方(Quora和MSE)写过它,你也可以在其他地方找到它,尽管有一些有缺陷的版本。要注意的是,如果你实现它,你可能需要取消比最终结果大得多的二项式系数,如果你使用浮点运算,你可能会失去太多精度。
Neil Slater使用动态规划计算卷积的建议是一个很好的建议。它比二项式系数的和慢,但相当稳健。你可以用几种方法来加速它,比如用平方求幂,或者用快速傅里叶变换,但很多人会发现这些方法有点小题大做。
为了修正你的方法,你应该使用(简单的)对正态近似的连续性校正,并限制在你有足够的骰子并且你的评估距离最大值和最小值足够远的情况下,你期望一个正态近似将是好的,无论是在绝对意义上还是在相对意义上。连续性校正就是用n-1/2到n+1/2的区间替换n的计数。
掷出总数为200的确切次数是7745954278770349845682110174816333221135826585518841002760,所以概率是除以6^100,大约是1.18563 x 10^-20。
具有简单连续性校正的正态近似为Phi((200.5-350)/sqrt(3500/12))-Phi((199.5-350)/sqrt(3500/12)) = 4.2 x 10^-19。这在绝对意义上是准确的,因为它非常接近于0,但它偏离了35倍,所以相对而言不是很好。正态近似在靠近中心的地方给出了更好的相对近似。
将两个独立概率分布的结果相加等同于对两个分布进行卷积。如果分布是离散的,那么它是一个离散卷积。
如果单个骰子表示为:
probs_1d6 = Array.new(6) { Rational(1,6) }
则2d6可以这样计算:
probs_2d6 = []
probs_1d6.each_with_index do |prob_a,i_a|
probs_1d6.each_with_index do |prob_b,i_b|
probs_2d6[i_a + i_b] = ( probs_2d6[i_a + i_b] || 0 ) + prob_a * prob_b
end
end
probs_2d6
# => [(1/36), (1/18), (1/12), (1/9), (5/36), (1/6),
# (5/36), (1/9), (1/12), (1/18), (1/36)]
虽然这是骰子的n平方,一个完全合乎逻辑的组合可以减少这一点,但这样做通常不太灵活,更复杂的设置。这种方法的好处是,你可以继续添加更多的骰子,并进行其他更奇特的组合。例如,要得到4d6,可以对两个结果进行卷积得到2d6。使用理数可以避免浮点精度问题。
我跳过了一个细节,你确实需要存储初始偏移量(对于普通的六面骰子+1)并将其加在一起,以便了解概率匹配。
我在gem games_dice中制作了一个更复杂的逻辑版本,使用浮点而不是Rational,可以处理其他一些骰子组合。
下面是使用上述方法的基本重写,以一种朴素的方式(简单地一次组合一个骰子的效果):
def probability_of_sum(x, n, sides=6)
return 0 if x < n
single_die_probs = Array.new(sides) { Rational(1,sides) }
combined_probs = [1] # Represents chance of getting 0 when rolling 0 dice :-)
# This is not the most efficient way to do this, but easier to understand
n.times do
start_probs = combined_probs
combined_probs = []
start_probs.each_with_index do |prob_a,i_a|
single_die_probs.each_with_index do |prob_b,i_b|
combined_probs[i_a + i_b] = ( combined_probs[i_a + i_b] || 0 ) + prob_a * prob_b
end
end
end
combined_probs[ x - n ] || 0
end
puts probability_of_sum(400, 100).to_f
# => 0.0003172139126369326
注意,该方法实际上计算100到600的完整概率分布,因此您只需要调用它一次并存储一次数组(加上偏移量+100),并且您可以执行其他有用的操作,例如大于某个数字的概率。由于在Ruby中使用了Rational
数字,因此具有完美的精度。
因为在你的情况下,你只有一种骰子,我们可以避免使用Rational
,直到最后,只使用整数(本质上是组合值的计数),并除以组合的总数(骰子数量的边幂)。这要快得多,并且在一秒钟内返回100个骰子的值:
def probability_of_sum(x, n, sides=6)
return 0 if x < n
combined_probs = [1] # Represents chance of getting 0 when rolling 0 dice :-)
n.times do
start_probs = combined_probs
combined_probs = []
start_probs.each_with_index do |prob_a,i_a|
sides.times do |i_b|
combined_probs[i_a + i_b] = ( combined_probs[i_a + i_b] || 0 ) + prob_a
end
end
end
Rational( combined_probs[ x - n ] || 0, sides ** n )
end
这是我的最终版本。
- 将
get_z_score
的和偏移量分别更改为x-0.5
和x+0.5
,以获得更精确的结果。 - 添加
return 0 if x < n || x > n * sides
以覆盖总和小于骰子数量且高于骰子数量乘以边数的情况。 - 添加了结果 的基准测试
主要功能
# sides - number of sides on one die
def get_mean(sides)
(1..sides).inject(:+) / sides.to_f
end
def get_variance(sides)
mean_of_squares = ((1..sides).inject {|sum, side| sum + side ** 2}) / sides.to_f
square_mean = get_mean(sides) ** 2
mean_of_squares - square_mean
end
def get_sigma(variance)
variance ** 0.5
end
# x - the number of points in question
def get_z_score(x, mean, sigma)
(x - mean) / sigma.to_f
end
# Converts z_score to probability
def z_to_probability(z)
return 0 if z < -6.5
return 1 if z > 6.5
fact_k = 1
sum = 0
term = 1
k = 0
loop_stop = Math.exp(-23)
while term.abs > loop_stop do
term = 0.3989422804 * ((-1)**k) * (z**k) / (2*k+1) / (2**k) * (z**(k+1)) / fact_k
sum += term
k += 1
fact_k *= k
end
sum += 0.5
1 - sum
end
# Calculate probability of getting 'х' total points by rolling 'n' dice with 'sides' number of sides.
def probability_of_sum(x, n, sides=6)
return 0 if x < n || x > n * sides
mean = n * get_mean(sides)
variance = get_variance(sides)
sigma = get_sigma(n * variance)
# Rolling below the sum
z1 = get_z_score(x-0.5, mean, sigma)
prob_1 = z_to_probability(z1)
# Rolling above the sum
z2 = get_z_score(x+0.5, mean, sigma)
prob_2 = z_to_probability(z2)
prob_1 - prob_2
end
require 'benchmark'
Benchmark.bm do |x|
x.report { @prob = probability_of_sum(350, 100).to_f }
puts "tWith x = 350 and n = 100:"
puts "tProbability: #{@prob}"
end
puts
Benchmark.bm do |x|
x.report { @prob = probability_of_sum(400, 100).to_f }
puts "tWith x = 400 and n = 100:"
puts "tProbability: #{@prob}"
end
puts
Benchmark.bm do |x|
x.report { @prob = probability_of_sum(1000, 300).to_f }
puts "tWith x = 1000 and n = 300:"
puts "tProbability: #{@prob}"
end
结果 user system total real
0.000000 0.000000 0.000000 ( 0.000049)
With x = 350 and n = 100:
Probability: 0.023356331366255034
user system total real
0.000000 0.000000 0.000000 ( 0.000049)
With x = 400 and n = 100:
Probability: 0.00032186531055478085
user system total real
0.000000 0.000000 0.000000 ( 0.000032)
With x = 1000 and n = 300:
Probability: 0.003232390001131513
我也用蒙特卡罗方法求解了这个问题,结果比较接近。
# x - sum of points to find probability for
# n - number of dice
# trials - number of trials
def monte_carlo(x, n, trials=10000)
pos = 0
trials.times do
sum = n.times.inject(0) { |sum| sum + rand(1..6) }
pos += 1 if sum == x
end
pos / trials.to_f
end
puts monte_carlo(300, 100, 30000)