计算累积二项式概率时R中的奇怪精度问题 [英] Strange precision issues in R when computing cumulative binomial probability
问题描述
使用此代码时,我遇到了一些奇怪的问题:
I've been running into some weird problems when using this code:
positions<-c(58256)
occurrencies<-c(30)
frequency<-c(11/5531777)
length<-c(4)
prob<-c(0)
for(i in 0:(occurrencies-1))
{
pow<-frequency^i
pow1<-(1-frequency)^(positions-i)
bin<-choose(positions, i)
prob<<-prob+(bin*pow*pow1)
}
此for循环的每次迭代应计算在给定频率下事件发生次数 i
的二项式概率。每次迭代还会汇总结果。这应该导致 prob
变量永远不会超过1,但是在7次左右的循环迭代之后,一切都陷入地狱,而 prob
超越1。
Each iteration of this for loop should calculate the binomial probability that, i
number of occurrences of the event occur given the frequency. Each iteration also sums up the result. This should result in the prob
variable never exceeding 1, but after 7 or so for loop iterations, everything goes to hell and prob
excedes 1.
我认为这可能是精度数字的问题,因此我尝试使用 Rmpfr 但无济于事-仍然存在相同的问题。
I thought it might be a question of precision digits, so i tried using Rmpfr but to no avail- the same problem persisted.
我想知道是否有任何技巧或软件包可以克服这种情况,或者我是否坚持下去。
I was wondering if there are any tips or packages to overcome this situation, or if I'm stuck with this.
推荐答案
按照Ben Bolker的建议看?pbinom
Following Ben Bolker's advice to see ?pbinom
pbinom(q = occurencies, size = positions, prob = frequency, lower.tail = FALSE)
这篇关于计算累积二项式概率时R中的奇怪精度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!