In [2]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as spsp
import scipy.stats as spst
%matplotlib inline
In [3]:
def poisson_sampling(lm):
#starting value of search
x=int(lm)
# cdf corresponding to the starting value?
cdf=spst.poisson.cdf(x,lm)
# compare u with cdf
u=np.random.rand()
# if u>= cdf: upward
if u>=cdf:
x=x+1
cdf=spst.poisson.cdf(x,lm)
while u>=cdf:
x=x+1
cdf=spst.poisson.cdf(x,lm)
return x
#else downward
else:
x=x-1
cdf=spst.poisson.cdf(x,lm)
while u