Skip to content

Test multinomial distribution with probability vector containing zeros #4487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ricardoV94 opened this issue Feb 24, 2021 · 5 comments
Closed

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Feb 24, 2021

A previous bug in the logp of the Multinomial made it return nan when the probability vector contained zeros. This was behind this issue #3069, and was fixed by #3059

The minimal example of issues #3069 is shown below. It had to do with with the last row of the state_label_tran which includes a zero-probability for the first class.

import numpy as np
import pymc3 as pm

state_label_tran = np.array([
    [0.3, 0.2, 0.5],
    [0.1, 0.5, 0.4],
    [0.0, 0.05, 0.95],  # Would work fine without this row
])
shape = len(state_label_tran)

with pm.Model() as m:
    label_dist = [
        pm.Multinomial.dist(p=state_label_tran[i], n=1, shape=shape)
        for i in range(len(state_label_tran))
    ]
    label = pm.Mixture('label', w=np.ones(shape)/shape, comp_dists=label_dist, observed=[0, 1, 0])

print(label.logp())  # nan before PR #3059, and -1.386... afterwards

But it's enough to check the Multinomial directly:

with pm.Model() as m:
    label = pm.Multinomial('m', n=1, p=[0, 0.05, 0.95], observed=[0, 1, 0])
print(label.logp())  # nan before, and -2.9957 afterwards

None of our current Multinomial tests check for a probability vector that includes zeros (except for test_batch_dirichlet_multinomial which sometimes does so randomly). We should add one.

Originally posted by @ricardoV94 in #3069 (comment)

@ricardoV94 ricardoV94 changed the title Add multinomial distribution test where probability vector contains zeros Test multinomial distribution with probability vector containing zeros Feb 24, 2021
@Devanshu24
Copy link

Hi! I think I understand what needs to be done. If it's okay with you and no one else is working on it, I would like to work on it :)

@ricardoV94
Copy link
Member Author

@Devanshu24 Sure, go ahead.

The test should go into test_distributions.py. Look for the other Multinomial distribution tests, as maybe it's enough to tweak one of the existing tests.

Feel free to ask any questions if you need.

@Devanshu24
Copy link

Just to give an update.
Really sorry I took this long in fixing it, was caught up in midterms. Hopefully will send a PR tomorrow :)

@ricardoV94
Copy link
Member Author

No problem!

@ricardoV94
Copy link
Member Author

Closed via #4758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants