Skip to content
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

ValueError when using check_bounds=False in a model with DiscreteUniform #4499

Closed
ricardoV94 opened this issue Mar 3, 2021 · 0 comments · Fixed by #4541
Closed

ValueError when using check_bounds=False in a model with DiscreteUniform #4499

ricardoV94 opened this issue Mar 3, 2021 · 0 comments · Fixed by #4541

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Mar 3, 2021

I was poking around to see if we should change the docstrings of check_bounds in pm.Model to mention this might not be safe in the presence of discrete variables and I found this strange error:

with pm.Model(check_bounds=False) as m:
    y = pm.DiscreteUniform('y', 0, 5)
    trace = pm.sample()
Traceback (most recent call last):
  File "/home/ricardo/Documents/Projects/pymc3-venv/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-60-ca8c3478b1dc>", line 3, in <module>
    trace = pm.sample()
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/sampling.py", line 515, in sample
    step = assign_step_methods(model, step, step_kwargs=kwargs)
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/sampling.py", line 217, in assign_step_methods
    return instantiate_steppers(model, steps, selected_steps, step_kwargs)
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/sampling.py", line 138, in instantiate_steppers
    step = step_class(vars=vars, **args)
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/step_methods/metropolis.py", line 187, in __init__
    self.delta_logp = delta_logp(model.logpt, vars, shared)
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/step_methods/metropolis.py", line 888, in delta_logp
    logp1 = pm.CallableTensor(logp0)(inarray1)
  File "/home/ricardo/Documents/Projects/pymc3/pymc3/aesaraf.py", line 308, in __call__
    (oldinput,) = inputvars(self.tensor)
ValueError: not enough values to unpack (expected 1, got 0)

I don't see why changing the check_bounds flag would lead to any issues in sampling. I can call the logp method for any value and I get a finite (and wrong) evaluation as would be expected from removing the bounds:

np.array([m.logp({'y': y}) for y in np.arange(-10, 10)])
array([-1.79175947, -1.79175947, -1.79175947, -1.79175947, -1.79175947,
       -1.79175947, -1.79175947, -1.79175947, -1.79175947, -1.79175947,
       -1.79175947, -1.79175947, -1.79175947, -1.79175947, -1.79175947,
       -1.79175947, -1.79175947, -1.79175947, -1.79175947, -1.79175947])

I tried with a couple of other Discrete Variables and I had no issues either. Obviously it works as expected with the flag set to True

with pm.Model(check_bounds=True) as m:
    y = pm.DiscreteUniform('y', 0, 5)
    trace = pm.sample()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant