BUG: `KeyError: 'diverging'` when setting up `CategoricalGibbsMetropolis` step method with `proposal=‘proportional’`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 9.8k
- Forks
- 2.3k
- Avg merge
- 21h 39m
- Merged PRs (30d)
- 5
Description
Describe the issue:
Trying to set up CategoricalGibbsMetropolis step method for a categorical variable with the proposal parameter set to 'proportional' causes an error KeyError: 'diverging' just as sampling starts.
Reproduceable code example:
import pymc as pm
with pm.Model() as model:
# Prior for categorical
p = pm.Dirichlet('p', a=[1., 1., 1.], shape=3)
# Categorical variable
category = pm.Categorical('category', p=p, shape=100)
# CategoricalGibbsMetropolis with proportional proposal
step = pm.CategoricalGibbsMetropolis(vars=[category], proposal = 'proportional')
trace = pm.sample(500, cores=1, chains=1, step=step, return_inferencedata=False)
Error message:
Traceback (most recent call last):
File "h:\Fear_Generalization\import pymc as pm.py", line 14, in <module>
trace = pm.sample(500, cores=1, chains=1, step=step, return_inferencedata=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\fbabb\.conda\envs\pymc_env\Lib\site-packages\pymc\sampling\mcmc.py", line 929, in sample
_sample_many(**sample_args)
File "C:\Users\fbabb\.conda\envs\pymc_env\Lib\site-packages\pymc\sampling\mcmc.py", line 1072, in _sample_many
_sample(
File "C:\Users\fbabb\.conda\envs\pymc_env\Lib\site-packages\pymc\sampling\mcmc.py", line 1157, in _sample
for it, diverging in enumerate(sampling_gen):
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\fbabb\.conda\envs\pymc_env\Lib\site-packages\pymc\sampling\mcmc.py", line 1224, in _iter_sample
trace.record(point, stats)
File "C:\Users\fbabb\.conda\envs\pymc_env\Lib\site-packages\pymc\backends\ndarray.py", line 116, in record
data[key][draw_idx] = val
~~~~^^^^^
KeyError: 'diverging'
PyMC version information:
PyMC Version: 5.19.1
PyTensor Version: 2.26.3
Python Version: 3.12.7
Context for the issue:
Editing the pymc source code by copying this pattern at the end of astep_prop as suggested by Luciano Paz resolved the issue for me.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example with PyMC 5.19.1 and trace the CategoricalGibbsMetropolis proportional-proposal path through astep_prop. Compare its returned statistics with the pattern in pymc/step_methods/metropolis.py#L769-L772 and inspect how pymc/backends/ndarray.py records them. Done means the example samples without KeyError: 'diverging'.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100