I am using scipy.stats.binom
to work with the binomial distribution. Given n
and p
, the probability function is
A sum over k
ranging over 0
to n
should (and indeed does) give 1. Fixing a point x_0
, we can add the probabilities in both directions and the two sums ought to add to 1. However the code below yields two different answers when x_0
is close to n
.
from scipy.stats import binom
n = 9
p = 0.006985
b = binom(n=n, p=p)
x_0 = 8
# Method 1
cprob = 0
for k in range(x_0, n+1):
cprob += b.pmf(k)
print('cumulative prob with method 1:', cprob)
# Method 2
cprob = 1
for k in range(0, x_0):
cprob -= b.pmf(k)
print('cumulative prob with method 2:', cprob)
I expect the outputs from both methods to agree. For x_0 < 7
it agrees but for x_0 >= 8
as above I get
>> cumulative prob with method 1: 5.0683768775504006e-17
>> cumulative prob with method 2: 1.635963929799698e-16
The precision error in the two methods propagates through my code (later) and gives vastly different answers. Any help is appreciated.