pm.Categorical for matrix of probabilities

Dear all,

I want to sample_prior_predictive from pm.Categorical with p as a vector, but it gives me only one sample rathar than an array of samples.

For example, the code:

P = [[.5,.5,0],[0,.01,.99]]
with pm.Model() as model:
    output = pm.Categorical('output',p=P,shape=(2,))
    sampling = pm.sample_prior_predictive(5)
    
sampling['output']

gives only:

array([1, 2])

However, the use of only one P gives an expected result:

P = [.5,.5,0]
with pm.Model() as model:
    output = pm.Categorical('output',p=P)
    sampling = pm.sample_prior_predictive(5)
    
sampling['output']

the output:

array([0, 1, 1, 1, 1])

As a workaround, I can of course try to implement a for-loop in my pm.Model and loop over all Ps, but it would not look very nice. Alternativale, pm.Multinomial provides the desired result:

P = [[.5,.5,0],[0,.01,.99]]
with pm.Model() as model:
    output = pm.Multinomial('output',n=1,p=P,shape=(2,3))
    sampling = pm.sample_prior_predictive(5)
    
[(np.flatnonzero(x)%3).tolist() for x in sampling['output']]

with output:

[[1, 2], [1, 2], [0, 2], [1, 2], [1, 2]]

but again it looks less nicer as it would be with one-line Categorical.

Could you please point out on my mistake in the first code-snippet? Thanks in advance.

Seems to be a shape related bug - @lucianopaz could you take a look when you get a chance?

1 Like

Yup, it’s a shape bug. It seems to be caused by distributions.dist_math.random_choice. I’ll try to fix it soonish, but I’m a bit swamped with other things right now.

2 Likes

Thanks! I opened the bug report on github. I use a workaround with pm.Multinomial at the moment.

2 Likes