I am want to sample from the binomial distribution B(n,p) but with an additional constraint that the sampled value belongs in the range [a,b] (instead of the normal 0 to n range). In other words, I have to sample a value from binomial distribution given that it lies in the range [a,b]. Mathematically, I can write the pmf of this distribution (f(x)) in terms of the pmf of binomial distribution bin(x) = [(nCx)*(p)^x*(1-p)^(n-x)] as
sum = 0
for i in range(a,b+1):
sum += bin(i)
f(x) = bin(x)/sum
One way of sampling from this distribution is to sample a uniformly distributed number and apply the inverse of the CDF(obtained using the pmf). However, I don't think this is a good idea as the pmf calculation would easily get very time-consuming.
The values of n,x,a,b are quite large in my case and this way of computing pmf and then using a uniform random variable to generate the sample seems extremely inefficient due to the factorial terms in nCx.
What's a nice/efficient way to achieve this?
pmfandcdffor millions of items. It requires to usescipy.stats.binom. This is a new thing for me because I knew so far only ascipy.special.combwhich is misleading. - mathfuxxin my project to get another RVywhich is a function ofx. However,yalso has range[0,n]. In order to ensure thaty = f(x)is within the acceptable range, a condition needs to be imposed onx, which boils down to choosingxbinomially between[a,b]. - Black Jack 21