pymc-devs / pymc-devs/pymc

BUG: `KeyError: 'diverging'` when setting up `CategoricalGibbsMetropolis` step method with `proposal=‘proportional’`

Open
#7,628 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.