Composing categorical distributions: identity CPT

I’m building a simple Bayes net, with an identity CPT, as in the attached file.
weird_example.py (629 Bytes)
I expect the resulting distribution of samples of a to reflect p, but they do a weird thing instead unless you do something like line 12, in which case everything works as expected.

Is the sampling algorithm doing something unexpected, or is there a bug somewhere?

pm.sample() with no observed does not always work, as the metropolis proposal is getting rejected ~100%, if you check the trace you can see a is always 0
We are working on a PR to make sampling from the prior and prior prediction easier: https://github.com/pymc-devs/pymc3/pull/2876

I tried to look at the acceptance rate, but it appears that stat_names for the trace is empty…

Also, observing b does not appear to give the expected results either.
weird_example2.py (669 Bytes)

Unless I do something like line 12.

The reason is that the model initial logp is inf:

model.logp(model.test_point)
# array(-inf)

And the reason is that the initial value of a is 0, which gives b logp -inf
A workaround is providing a test value to a

a = pm.Categorical('a',
        p=p,
        testval=1)

Which when you sample it will return the correct answer (i.e., all 1)

That makes sense. Thanks!