Mixture of Beta fails (because no mode exists) while Gamma is ok

The following code executes without problems.

import pymc3 as pm
alpha1_, beta1_ = pm.Exponential.dist(0.1).random(size=2)
alpha2_, beta2_ = pm.Exponential.dist(0.1).random(size=2)
w_ = pm.Dirichlet.dist(a=np.ones(2), shape=2).random(size=1)
p0 = pm.Gamma.dist([alpha1_, alpha2_], [beta1_, beta2_], shape=(n,2))
p_ = pm.Mixture.dist(w = w_, comp_dists = p0, shape=n).random(size=1)

But the same code with Beta instead of Gamma gives me AttributeError: 'Beta' object has no attribute 'mode'.

I looked a bit at the code and the exception is thrown in pymc3\distributions\mixture.py in _comp_modes(self) which is called from the constructor of Mixture. As a backup strat, it tries to compute the modes of the separate distributions as if it were an iterable of distributions. But that fails too, because p0 is not iterable and it throws TypeError: 'Beta' object is not iterable . Then it goes back to the constructor of the class Mixture which catches AttributeError, ValueError and IndexError. But because the actual error is a TypeError is isn’t caught there and the exception halts the execution.

I guess for Gamma the mode is defined so this doesn’t happen. Is this a bug or is there a reason why this is a bad idea with Beta?

edit: was posted as https://github.com/pymc-devs/pymc3/issues/3126.

1 Like

Yeah this is a bug as the mixture class is wrongly rely on the mode to exist - could you file a bug on Github?

Seems like I cannot edit my original post anymore, but it has been posted as:

It is different from the issue https://github.com/pymc-devs/pymc3/issues/3126, which has been fixed.

Thanks for reporting back - I think the issue could be leave open as is.