The results of a switch are observed directly (beginner)

It would help to think of it in a generative way, so the first step is try to express the data generation process and generate some fake data.
The procedure of generating data sounds like you can frame it under coin flipping:

  1. first flip a coin (A) with p=\theta_2, if coin A comes up head, set observed=0
  2. if coin A comes up tail, flip a second coin B with p=\theta_1, set observed=outcome of coin B.

So a generation process to get sample would be:

theta1, theta2 = .7, .3
obs = []
for i in range(10):
    if np.random.binomial(1, theta2) == 1:
        obs.append(0)
    else:
        obs.append(np.random.binomial(1, theta1))

To model it in pymc3, the most straightforward way is to have a latent variable to represent the unknown result of coin flip of coin A.

import theano.tensor as tt
with pm.Model() as m:
    theta_2 = pm.Uniform('theta_2', 0., 1.)
    coinA = pm.Bernoulli('latent', p=theta_2, shape=10)
    theta_1 = pm.Uniform('theta_1', 0., 1.)
    p = tt.stack([theta_1, 0.])
    observed = pm.Bernoulli('obs', p=p[coinA], observed=obs)

FYI: What we have here is a mixture model, or a zero inflated binomial. Rewriting the above model to a marginalized mixture model would be much better for inference. You can also have a look at related literature and examples.

1 Like