Generate random sample for continuous extension of binomial distribution

I’m using Stan to fit two variables x and y to some expressions using the binomial distribution. Since Stan can’t handle discrete x/y as parameters (right?), I figured I would approximate the problem using a continuous version of the binomial distribution: target += lchoose(x, data) + data * ln(y) + (x - data) * ln(1 - y).
(Actual example is somewhat more complex, but this suffices for the explanation).

That’s working fine, but now I want to create a posterior predictive distribution using the same process:

generated quantities {
    real future;
    future = binomial_rng(x, y);

But that’s not working because binomial_rng only takes an integer for the first parameter. There doesn’t appear to be a real to int casting operation in Stan, which would be an okay approximation. I also didn’t find a way to generate this. Can you help me with this?

The only alternative I currently see is to generate all probabilities and draw from that as a post-processing step outside of Stan.

I think post processing the samples outside of Stan, e.g. round()ing them and using rbinom() in R is the easiest thing to do. Implementing the inverse CDF method for the continuous extension to the binomial distribution, where you use the (incomplete) beta/gamma functions instead of the factorial version of the binomial coefficient, is likely to be painful.

1 Like

Ok, thanks for your advice!