Skip to content
Prev 7434 / 63421 Next

pbinom gotcha (PR#1569)

This came up due to a question from Anders Hald:

Bernoulli calculated an approximation to the smallest n so that

P(0.58 <= x/n <= 0.62) >= 1000/1001

What is the exact value?

Now try

n <- 6350:6500
Pr <- function(n)pbinom(0.62*n,n,0.6) -  
  pbinom(0.58*n,n,0.6) + dbinom(0.58*n,n,0.6)
plot(n,Pr(n),type="b")
abline(h=1000/1001)
min(n[Pr(n)>1000/1001])

Next, try

Pr <- function(n)pbinom(0.62*n,n,0.6) - 
        pbinom(0.58*n + 1e-7,n,0.6) + dbinom(0.58*n,n,0.6)
plot(n,Pr(n),type="b")
abline(h=1000/1001)
min(n[Pr(n)>1000/1001])

At least on Linux, you get quite different results.
The reason is of course that
[1] -4.547474e-13

and dbinom has a fuzz factor, but pbinom unceremoniously takes
floor(x). I suppose we ought to build the same fuzz into pbinom?