How does pymc3 handle n=0 in Binomial discrete likelihood function

Do I need to be concerned about how the NUTS sampler handles no samples for a few array items being used for ‘n’ in the Binomial likelihood?

Also, are floats provided as trials (n) and successes (observed) converted to integer? (e.g., 0.6 => 0 )

Yes n is automatically converted to int. n=0 should be fine.

You can always try yourself by calling

value = 0
pm.Binomial.dist(n=0, p=0.5).logp(value).eval()  # array(0.)

Thanks for noting how I can check it.
Now I can check the arrays of trials and observations with the zeros, shortened without the zeros, and with NaN.

Note that I updated my answer. It was missing the .dist part.

Does this extend to pymc v4 (4.0.0b2)? I can run something like pm.Binomial.dist(n=np.array([0,2,3,4,5]),p=at.expit(-0.1)).eval() with expected results, but when I put it in a model e.g.

with pm.Model() as model:
    alpha = pm.Normal("alpha", mu=0, sigma=1)
    mu = at.expit(alpha)

    pm.Binomial(
        "y",
        n=np.array([0,2,3,4,5]),
        p=mu,
        observed=np.array([0,2,2,0,2]) # or even if you omit observed
    )

I get

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'alpha': array(0.28858972)}

Initial evaluation results:
{'alpha': -0.96, 'y': -inf}

Any ideas?

The logp seems to have the constraint that n must be nonzero:

import pymc as pm
pm.logp(pm.Binomial.dist(n=0, p=0.5), 0).eval()

Raises

aeppl.logprob.ParameterValueError: n > 0, 0 <= p <= 1

But it doesn’t seem necessary I think. We could just modify these lines to include 0 <= n instead of 0 < n:

@theo Do you mind opening an issue on Github?

2 Likes