I'm trying to implement a model in PyMC3 which relies on a switch with a stochastic condition in the final step, and hence can't pass observed values to the model.

Question. What is the "correct" way to pass observations in a PyMC3 model, when the last step is not a stochastic variable?

Some context: I'm attempting to model intermittent demand using PyMC3 (demand is mostly zero, but occasionally a positive integer). The statistical model I am trying to implement is to model the demand $X_t$ at time $t$ by $$X_t\sim\begin{cases} 0 & \text{with probability } 1 - p \\ Poisson(\mu) & \text{with probability } p \end{cases}$$ (no time dependence), or perhaps more formally, $$\begin{eqnarray}\alpha & \sim & Bernoulli(p) \\ X_t & \sim & \begin{cases} Poisson(\mu) & \text{if } \alpha=1 \\ 0 & \text{if } \alpha=0 \end{cases} \end{eqnarray}$$

I attempted to implement this model in PyMC3 by

import pymc3 as pm from theano import tensor as tt with pm.Model() as model: p = pm.Beta('p', alpha=1, beta=1) mu = pm.Gamma('mu', alpha=0.001, beta=0.001) alpha = pm.Bernoulli('alpha', p) demand_pos = pm.Poisson('demand', mu) obs = pm.math.switch(tt.eq(alpha, 1), 0, demand_pos)

The issue is that obs is not a distribution, and so has no observed parameter.


1 Answer 1


You can use a potential for this, which just gets multiplied to your likelihood. See e.g. https://discourse.pymc.io/t/how-to-set-up-a-custom-likelihood-function-for-two-variables/906/2.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.