掷大数

问题描述 投票:6回答:1

我保证这不是 只是 又是一道掷骰子的作业题。我实现了一个函数来计算获得小于和的概率。s 轧制 n m-侧面的骰子。我的函数适用于小值的 n 但我发现对于大值的 n. 见附图。有谁能洞察到这是怎么回事?

我的概率函数

从此执行 堆栈交换

probability <- function(s, m, n) {

  i <- 0:((s-1-n) / m)
  m^(-n) * sum((-1)^i * choose(n, i) * choose(s - 1 - i * m, n))

}

开始断裂~ n > 80。

n <- 1:90 # number of dice
m <- 6 # number of sides
s <- floor(mean(1:m)*n) # sum of faces
p <- mapply(probability, s = s, m = m, n = n)
plot(n, p, main = paste("probability of rolling less than floor(", mean(1:m),"* n) as sum of n rolls of a", m, "sided dice"))

enter image description here

r dice
1个回答
1
投票

正如在最初问题的评论中提到的,问题是概率函数要求R计算非常巨大的数字(choose(80,40) = 1.075072e+23),我们正在冲击R的数值精度极限。

另一种不涉及巨大数字而使用大量数字的方法是运行蒙特卡洛模拟。这将生成一个掷骰子总和的分布,并将观察到的总和与分布进行比较。这将花费更长的时间来运行,但更容易做到,并且不会有数值精度问题。

mc <- Vectorize(function(s, m, n, reps = 10000) {
  x <- replicate(reps, sum(sample(m, n, replace = TRUE)))
  ecdf(x)(s-1)
})



n <- 1:90 # number of dice
m <- 6 # number of sides
s <- floor(mean(1:m)*n) # sum of faces
analytic_prob <- mapply(probability, s = s, m = m, n = n)
mc_prob <- mapply(mc, s = s, m = m, n = n)


plot(n, analytic_prob, main = paste("probability of rolling less than floor(", mean(1:m),"* n) as sum of n rolls of a", m, "sided dice"),
     sub = "monte carlo in red")
points(n, mc_prob, col = "red")

enter image description here


0
投票

这个问题是由R的数值精度限制造成的。正如评论者指出的,我上面计算的n选k值真的非常非常大(choose(80,40) = 1.075072e+23).

我们可以使用日志来尝试将问题控制在R的计算范围内。这就是Ramanujan的方法的实现。不幸的是,近似中的误差复合了,精度衰减得更快。概率函数需要对一连串非常大的数字进行加减,才能得到0和1之间的最终值,而且不能容忍任何不精确的情况。

0)将概率函数改写为分成几步走

probability <- function(s, m, n) {

  # Probability of getting less than s
  i <- 0:((s-1-n) / m)

  c1 <- choose(n, i)
  c2 <- choose(s - 1 - i * m, n)

  seq <- (-1)^i * (c1 * c2)

  m^(-n) * sum(seq)

}

1)实现对数(x!)的近似。)

# using the 'ramanujan' method
ramanujan <- function(n){
  n * log(n) - n + log(n * (1 + 4*n * (1 + 2*n))) / 6 + log(pi) / 2
}

# confirm Ramanujan works correctly
n <- 1:200
diff <- log(factorial(n)) - ramanujan(n)
plot(n, diff) # r returns inf for factorial(171), but up to there the numbers match

2) 重写 choose 使用对数近似的函数。

#' This function returns log(choose(n,k)) 
log_nck <- Vectorize(function(n, k) {
  if(n <= k | n < 1 | k < 1) return(log(choose(n,k))) # logs don't like 0 or neg numbers

  return((ramanujan(n) - ramanujan(k) - ramanujan(n-k)))
})

# Check that choose function works
n <- seq(10, 100, 10)
k <- seq(5, 50, 5)
c_real <- log(choose(n, k))
c_approx <- log_nck(n, k)
# If we print them, they appear to match
print(c_real)
print(c_approx)
# and the difference shows pretty small errors. 
print(c_real - c_approx)

3)用对数选择重写概率函数。

new_probability <- function(s, m, n) {

  # Probability of getting less than s
  i <- 0:((s-1-n) / m)

  c1 <- log_nck(n, i)
  c2 <- log_nck(s - 1 - i * m, n)

  seq <- (-1)^i * exp(c1 + c2)

  return(m^(-n) * sum(seq))

}

最后测试

n <- 1:90 # number of dice
m <- 6 # number of sides
s <- floor(mean(1:m)*n) # sum of faces

p <- mapply(probability, s = s, m = m, n = n)
newp <- mapply(new_probability, s = s, m = m, n = n)

plot(n, p, main = "Original in black, approximation in red")
points(n, newp, col = "red")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.