Mixture of Beta Binomials

Hi. Assume I have a dataframe consisting of baseball players and their respective num_bats and num_hits. I would like to fit a Mixture of Beta Binomials, but am getting a shape mismatch. Any help would be greatly appreciated.

The model:

num_mixtures = 3
with pm.Model() as mix_bb_model:
    mixing_proportions = pm.Dirichlet("mixing_proportions", a=np.ones(num_mixtures))
    # Now generate prior Beta hyperparameters for each of the num_mixture Betas
    phi = pm.Uniform("phi", lower=0.0, upper=1.0, shape=num_mixtures)
    kappa_log = pm.Exponential("kappa_log", lam=2.5, shape=num_mixtures)
    kappa = pm.Deterministic("kappa", at.exp(kappa_log))

    theta = pm.Beta(
    components = pm.Binomial.dist(

    lik = pm.Mixture('lik', w=mixing_proportions, comp_dists=components, observed=df["num_hits"].values)

and this is returning an error of:

ValueError: Incompatible Elemwise input shapes [(18, 18), (18, 3)]

where 18 is the number of players and 3 is the number of mixtures.

The problem is in your Binomial distribution. n has shape (18,), and p has shape (18, 3), which can’t be broadcasted together. You can fix this by setting n=df["num_bats"].values[:, None], so that it has shape (18, 1) instead.

You can find more info about parameter broadcasting behavior in: Distribution Dimensionality — PyMC 5.0.2 documentation


Thanks! This is the solution, how do I mark it as such?