Problem with PyMC3 and CategoricalData


I’m having problems with the find_MAP function with this simple model

import pymc3 as pm
import theano
import numpy as np

p_1 = 0.46
p_2 = 0.52
number_tosses = 1
heads = 1
with pm.Model() as ht_coin_flipping:
    coin = pm.Categorical('coin', p=[0.5, 0.5])
    p = theano.shared(np.array([p_1, p_2]))[coin]

    y = pm.Binomial('y', p=p, n=number_tosses, observed=[heads])


it’s raising

~\miniconda3\envs\bayesianStats\lib\site-packages\theano\compile\ in call(self, *args, **kwargs)
901 try:
902 outputs =
→ 903 self.fn() if output_subset is None else
904 self.fn(output_subset=output_subset)
905 except Exception:

IndexError: index out of bounds
Apply node that caused the error: Subtensor{int64}(<TensorType(float64, vector)>, ScalarFromTensor.0)
Toposort index: 1
Inputs types: [TensorType(float64, vector), Scalar(int64)]
Inputs shapes: [(2,), ()]
Inputs strides: [(8,), ()]
Inputs values: [array([0.46, 0.52]), 2]
Outputs clients: [[Elemwise{Composite{Switch(Cast{int8}((GE(i0, i1) * LE(i0, i2))), (Switch(EQ(i0, i1), i3, log(i0)) + Switch(EQ((i4 - i0), i1), i5, i6)), i3)}}(Subtensor{int64}.0, TensorConstant{0}, TensorConstant{1}, TensorConstant{-inf}, TensorConstant{1.0}, TensorConstant{0.0}, TensorConstant{0.0})]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\ipykernel\”, line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 2878, in run_cell
raw_cell, store_history, silent, shell_futures)
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 2923, in _run_cell
return runner(coro)
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 68, in pseudo_sync_runner
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 3147, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 3338, in run_ast_nodes
if (await self.run_code(code, result, async
File “C:\Users\carlo\miniconda3\envs\bayesianStats\lib\site-packages\IPython\core\”, line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 11, in
p = theano.shared(np.array([p_1, p_2]))[coin]

HINT: Use the Theano flag ‘exception_verbosity=high’ for a debugprint and storage map footprint of this apply node.

I can obtain samples from the posterior without problems (and it matches the theoretical distribution)…

find_MAP is not really designed for models with only discrete unobserved variables. Newer versions of PyMC3 actually raise a ValueError if you use the default pm.find_MAP() in your model.

You can trust the results of normal sampling and use those.

Thank you @ricardoV94