Why do Bernoulli and Binomial models give different results?

I am playing around with this simple tabular problem, and I was surprised to find that the Bernoulli likelihood only worked as expected for a single outcome. If I want to condition on observing 5 events (all True for instance), then it doesn’t seem to work (i.e., the posterior for bowl1 is incorrect). The equivalent binomial model works just fine. Why is this the case?

with pm.Model() as m:
    # Prior 
    bowl1 = pm.Bernoulli('bowl1', 1/2)

    # Conditional probabality / Likelihood
    p_vanilla = pm.math.switch(bowl1, 3/4, 1/2)
    
    # vanilla = pm.Binomial('vanilla', n=5, p=p_vanilla, observed=5)
    # vanilla = pm.Binomial('vanilla', n=1, p=p_vanilla, observed=[True, True, True, True, True])
    vanilla = pm.Bernoulli('vanilla', p=p_vanilla, observed=[True, True, True, True, True])

    trace_m = pm.sample(500)

Thanks in advance for your input :slight_smile:

Most likely there is a shape error in the Bernoulli model that broadcasted the random variable.

Yes you are right.

Funnily enough, if the data is set with a pm.Data variable the problem does not come up.

1 Like