Hello!
I am struggling to figure out where the source of my error is. I am able to fit the model with no issue but get an error when I sample from the posterior predictive. Any assistance would be greatly appreciated. Thank you.
with pm.Model() as model:
# Data Objects ------------------------------
x = pm.MutableData(name = “x”, value = x_pre)
y = pm.MutableData(name = “y”, value = y_pre)
# Priors -------------------------------------
beta = pm.Dirichlet(name = “beta”, a = (1 / k)*pt.ones(k))
alpha = pm.Exponential(name = “alpha”, lam = 12)
psi = pm.Beta(name = “psi”, alpha = 2, beta = 2)
# Conditional Expectation ---------------------
mu = pm.Deterministic(name = “mu”, var = pm.math.dot(x, beta))
# Likelihood ----------------------------------
likelihood = pm.ZeroInflatedNegativeBinomial(name = “likelihood”, psi = psi, mu = pm.math.exp(mu), alpha = alpha, observed = y)
with model:
posterior_predictive_pre = pm.sample_posterior_predictive(trace = idata)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\compile\function\types.py:970](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/compile/function/types.py:970) , in Function.call**(self, *args, kwargs)
968 try:
969 outputs = (
→ 970 self.vm()
971 if output_subset is None
972 else self.vm(output_subset=output_subset)
973 )
974 except Exception:
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\graph\op.py:543](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/graph/op.py:543) , in Op.make_py_thunk…rval**(p, i, o, n, params)**
539 @is_thunk_type
540 def rval(
541 p=p, i=node_input_storage, o=node_output_storage, n=node, params=None
542 ):
→ 543 r = p(n, [x[0] for x in i], o)
544 for o in node.outputs:
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\op.py:378](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/op.py:378) , in RandomVariable.perform**(self, node, inputs, outputs)**
376 rng_var_out[0] = rng
→ 378 smpl_val = self.rng_fn(rng, *(args + [size]))
380 if (
381 not isinstance(smpl_val, np.ndarray)
382 or str(smpl_val.dtype) != out_var.type.dtype
383 ):
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\basic.py:55](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/basic.py:55) , in ScipyRandomVariable.rng_fn**(cls, *args, kwargs)
54 size = args[-1]
—> 55 res = cls.rng_fn_scipy(*args, **kwargs)
57 if np.ndim(res) == 0:
58 # The sample is an
np.number
, and is not writeable, or non-NumPy59 # type, so we need to clone/create a usable NumPy result
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\basic.py:1612](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/basic.py:1612) , in NegBinomialRV.rng_fn_scipy**(cls, rng, n, p, size)**
1610 @classmethod
1611 def rng_fn_scipy(cls, rng, n, p, size):
→ 1612 return stats.nbinom.rvs(n, p, size=size, random_state=rng)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_distn_infrastructure.py:3343](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_distn_infrastructure.py:3343) , in rv_discrete.rvs**(self, *args, kwargs)
3342 kwargs[‘discrete’] = True
→ 3343 return super().rvs(*args, **kwargs)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_distn_infrastructure.py:1067](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_distn_infrastructure.py:1067) , in rv_generic.rvs**(self, *args, kwds)
1065 random_state = self._random_state
→ 1067 vals = self._rvs(*args, size=size, random_state=random_state)
1069 vals = vals * scale + loc
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_discrete_distns.py:320](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_discrete_distns.py:320) , in nbinom_gen._rvs**(self, n, p, size, random_state)**
319 def _rvs(self, n, p, size=None, random_state=None):
→ 320 return random_state.negative_binomial(n, p, size)
File _generator.pyx:3143 , in numpy.random._generator.Generator.negative_binomial**()**
ValueError : n too large or p too small, see Generator.negative_binomial Notes
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Cell In[43], line 2
1 with model:
----> 2 posterior_predictive_pre = pm.sample_posterior_predictive(trace = idata)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pymc\sampling\forward.py:644](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pymc/sampling/forward.py:644) , in sample_posterior_predictive**(trace, model, var_names, sample_dims, random_seed, progressbar, return_inferencedata, extend_inferencedata, predictions, idata_kwargs, compile_kwargs)**
639 # there’s only a single chain, but the index might hit it multiple times if
640 # the number of indices is greater than the length of the trace.
641 else:
642 param = _trace[idx % len_trace]
→ 644 values = sampler_fn(**param)
646 for k, v in zip(vars_, values):
647 ppc_trace_t.insert(k.name, v, idx)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pymc\util.py:391](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pymc/util.py:391) , in point_wrapper…wrapped**(kwargs)
389 def wrapped(**kwargs):
390 input_point = {k: v for k, v in kwargs.items() if k in ins}
→ 391 return core_function(**input_point)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\compile\function\types.py:983](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/compile/function/types.py:983) , in Function.call**(self, *args, kwargs)
981 if hasattr(self.vm, “thunks”):
982 thunk = self.vm.thunks[self.vm.position_of_error]
→ 983 raise_with_op(
984 self.maker.fgraph,
985 node=self.vm.nodes[self.vm.position_of_error],
986 thunk=thunk,
987 storage_map=getattr(self.vm, “storage_map”, None),
988 )
989 else:
990 # old-style linkers raise their own exceptions
991 raise
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\link\utils.py:535](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/link/utils.py:535) , in raise_with_op**(fgraph, node, thunk, exc_info, storage_map)**
530 warnings.warn(
531 f"{exc_type} error does not allow us to add an extra error message"
532 )
533 # Some exception need extra parameter in inputs. So forget the
534 # extra long error message in that case.
→ 535 raise exc_value.with_traceback(exc_trace)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\compile\function\types.py:970](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/compile/function/types.py:970) , in Function.call**(self, *args, kwargs)
967 t0_fn = time.perf_counter()
968 try:
969 outputs = (
→ 970 self.vm()
971 if output_subset is None
972 else self.vm(output_subset=output_subset)
973 )
974 except Exception:
975 restore_defaults()
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\graph\op.py:543](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/graph/op.py:543) , in Op.make_py_thunk…rval**(p, i, o, n, params)**
539 @is_thunk_type
540 def rval(
541 p=p, i=node_input_storage, o=node_output_storage, n=node, params=None
542 ):
→ 543 r = p(n, [x[0] for x in i], o)
544 for o in node.outputs:
545 compute_map[o][0] = True
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\op.py:378](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/op.py:378) , in RandomVariable.perform**(self, node, inputs, outputs)**
374 rng = copy(rng)
376 rng_var_out[0] = rng
→ 378 smpl_val = self.rng_fn(rng, *(args + [size]))
380 if (
381 not isinstance(smpl_val, np.ndarray)
382 or str(smpl_val.dtype) != out_var.type.dtype
383 ):
384 smpl_val = _asarray(smpl_val, dtype=out_var.type.dtype)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\basic.py:55](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/basic.py:55) , in ScipyRandomVariable.rng_fn**(cls, *args, kwargs)
52 @classmethod
53 def rng_fn(cls, *args, **kwargs):
54 size = args[-1]
—> 55 res = cls.rng_fn_scipy(*args, **kwargs)
57 if np.ndim(res) == 0:
58 # The sample is an
np.number
, and is not writeable, or non-NumPy59 # type, so we need to clone/create a usable NumPy result
60 res = np.asarray(res)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\pytensor\tensor\random\basic.py:1612](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/pytensor/tensor/random/basic.py:1612) , in NegBinomialRV.rng_fn_scipy**(cls, rng, n, p, size)**
1610 @classmethod
1611 def rng_fn_scipy(cls, rng, n, p, size):
→ 1612 return stats.nbinom.rvs(n, p, size=size, random_state=rng)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_distn_infrastructure.py:3343](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_distn_infrastructure.py:3343) , in rv_discrete.rvs**(self, *args, kwargs)
3314 “”"Random variates of given type.
3315
3316 Parameters
(…)
3340
3341 “”"
3342 kwargs[‘discrete’] = True
→ 3343 return super().rvs(*args, **kwargs)
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_distn_infrastructure.py:1067](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_distn_infrastructure.py:1067) , in rv_generic.rvs**(self, *args, kwds)
1064 else:
1065 random_state = self._random_state
→ 1067 vals = self._rvs(*args, size=size, random_state=random_state)
1069 vals = vals * scale + loc
1071 # do not forget to restore the _random_state
File [c:\Users\j.dekermanjian\Anaconda3\envs\pymc5\Lib\site-packages\scipy\stats_discrete_distns.py:320](file:///C:/Users/j.dekermanjian/Anaconda3/envs/pymc5/Lib/site-packages/scipy/stats/_discrete_distns.py:320) , in nbinom_gen._rvs**(self, n, p, size, random_state)**
319 def _rvs(self, n, p, size=None, random_state=None):
→ 320 return random_state.negative_binomial(n, p, size)
File _generator.pyx:3143 , in numpy.random._generator.Generator.negative_binomial**()**
ValueError : n too large or p too small, see Generator.negative_binomial Notes
Apply node that caused the error: nbinom_rv{0, (0, 0), int64, True}(RandomGeneratorSharedVariable(), MakeVector{dtype=‘int64’}.0, 4, alpha, Composite{(i1 / (exp(i0) + i1))}.0)
Toposort index: 11
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(1,)), TensorType(int64, shape=()), TensorType(float64, shape=()), TensorType(float64, shape=(None,))]
Inputs shapes: [‘No shapes’, (1,), (), (), (114,)]
Inputs strides: [‘No strides’, (8,), (), (), (8,)]
Inputs values: [Generator(PCG64) at 0x2168374ECE0, array([114], dtype=int64), array(4, dtype=int64), array(0.04567795), ‘not shown’]
Outputs clients: [[‘output’], [ExpandDims{axis=1}(nbinom_rv{0, (0, 0), int64, True}.out)]]
HINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag ‘optimizer=fast_compile’. If that does not work, PyTensor optimizations can be disabled with ‘optimizer=None’.
HINT: Use the PyTensor flag
exception_verbosity=high
for a debug print-out and storage map footprint of this Apply node.