-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Raise when trying to sample a Multinomial variable #7691
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2025 - present The PyMC Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from pymc.step_methods.arraystep import ArrayStep | ||
|
||
|
||
class CannotSampleRV(ArrayStep): | ||
"""A step method that raises an error when sampling a latent Multinomial variable.""" | ||
|
||
name = "cannot_sample_rv" | ||
|
||
def __init__(self, vars, **kwargs): | ||
# Remove keys that ArrayStep.__init__ does not accept. | ||
kwargs.pop("model", None) | ||
kwargs.pop("initial_point", None) | ||
kwargs.pop("compile_kwargs", None) | ||
self.vars = vars | ||
super().__init__(vars=vars, fs=[], **kwargs) | ||
|
||
def astep(self, q0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should happen in init, it shouldn't hardcode Multinomial, but read the name of the variable. This can also be used for the Wishart distribution instead of the eager warning we have now |
||
# This method is required by the abstract base class. | ||
raise ValueError("Latent Multinomial variables are not supported") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an interface for samplers to say they prefer a variable of a given type, it shouldn't happen here.