Difficulties on pymc3 vs. pymc2 when discrete variables are involved
Asked Answered
S

1

6

I'm updating some calculations where I used pymc2 to pymc3 and I'm having some problems with samplers behavior when I have some discrete random variables on my model. As an example, consider the following model using pymc2:

import pymc as pm

N = 100
data = 10

p = pm.Beta('p', alpha=1.0, beta=1.0)
q = pm.Beta('q', alpha=1.0, beta=1.0) 
A = pm.Binomial('A', N, p)
X = pm.Binomial('x', A, q, observed=True, value=data)

It's not really representative of anything, it's just a model where one of the unobserved variables is discrete. When I sample this model with pymc2 I get the following results:

mcmc = pm.MCMC(model)
mcmc.sample(iter=100000, burn=50000, thin=100)
plot(mcmc)

A q p

But when I try the same with PYMC3, I get this:

with pm.Model() as model:
    N = 100
    p = pm.Beta('p', alpha=1.0, beta=1.0)
    q = pm.Beta('q', alpha=1.0, beta=1.0) 
    A = pm.Binomial('A', N, p)
    X = pm.Binomial('x', A, q, observed=10)

with model:
    start = pm.find_MAP()

with model:
    step = pm.NUTS()
    trace = pm.sample(3000, step, start)

pm.traceplot(trace)

qa

It looks like the variable A is not being sampled at all. I didn't read a lot about the sampling method used in pymc3, but I noticed it seems to be particulary aimed for continuous models. Does this means it rules out discrete unobserved variables on the model or is there some way to do what I'm trying to do?

Sirotek answered 25/1, 2014 at 15:50 Comment(0)
F
10

The NUTS sampler does not work with discrete variables (though folks are working on generalizing it to do so). What you'd want to do is assign different step methods to different types of variables. For example:

step1 = pm.NUTS(vars=[p, q])
step2 = pm.Metropolis(vars=[A])

trace = pm.sample(3000, [step1, step2], start)
Foy answered 27/1, 2014 at 17:17 Comment(1)
I suspected it was something like this. Anyway, the ability to add specific samplers to each variable is very nice! Thanks for this great lib.Sirotek

© 2022 - 2024 — McMap. All rights reserved.