Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulties on pymc3 vs. pymc2 when discrete variables are involved

Tags:

python

pymc

mcmc

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)

Aqp

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?

like image 952
Rafael S. Calsaverini Avatar asked Jan 25 '14 15:01

Rafael S. Calsaverini


1 Answers

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)
like image 139
Chris Fonnesbeck Avatar answered Oct 05 '22 01:10

Chris Fonnesbeck