Skip to content

Raise when trying to sample a Multinomial variable #7691

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@ def dist(cls, n, p, *args, **kwargs):
return super().dist([n, p], *args, **kwargs)

def support_point(rv, size, n, p):
observed = getattr(rv.tag, "observed", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense here. support_point is well defined, and may be used for other purposes other than sampling.

Also it is possible (although unlikely) that someone outside of PyMC implemented a sampler that works for Multinomial variables.

Finally Categorical is only a valid substitute to Multinomial when n=1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right place is perhaps in whatever default sampler is given to MultinomialRVs, when that is initialized

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or to define a CannotSampleRV sampler that is given priority for Multinomial (or whatever RVs we have) that can't be sampled correctly, that does the raising

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, I've added CannotSampleRV and added the condition where samplers are being initialized, can you please check and tell if anything needs a change?

if observed is None:
raise ValueError(
"Latent Multinomial variables are not supported for sampling. "
"Use a Categorical variable instead."
)
n = pt.shape_padright(n)
mean = n * p
mode = pt.round(mean)
Expand Down
9 changes: 9 additions & 0 deletions tests/distributions/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def test_issue_4499(self):
x = pm.DiracDelta("x", 1, size=10)
npt.assert_almost_equal(m.compile_logp()({"x": np.ones(10)}), 0 * 10)

def test_issue_7548(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give test an informative name, and it wasn't really a bug but missing functionality

# Test for bug in Multinomial, it should raise when trying to sample a Multinomial variable
with pm.Model() as model:
p = [0.3, 0.4, 0.3]
n = 10
x = pm.Multinomial("x", n=n, p=p)
with pytest.raises(ValueError, match="Latent Multinomial variables are not supported"):
pm.sample(draws=100, chains=1)


def test_all_distributions_have_support_points():
import pymc.distributions as dist_module
Expand Down