python - PyMC2 and PyMC3 give different results...? -
i'm trying simple pymc2 model working in pymc3. i've gotten model run models give different map estimates variables. here pymc2 model:
import pymc theta = pymc.normal('theta', 0, .88) x1 = pymc.bernoulli('x2', p=pymc.lambda('a', lambda theta=theta:1./(1+np.exp(-(theta-(-0.75))))), value=[1],observed=true) x2 = pymc.bernoulli('x3', p=pymc.lambda('b', lambda theta=theta:1./(1+np.exp(-(theta-0)))), value=[1],observed=true) model = pymc.model([theta, x1, x2]) mcmc = pymc.mcmc(model) mcmc.sample(iter=25000, burn=5000) trace = (mcmc.trace('theta')[:]) print "\nthe map value theta is", trace.sum()/len(trace)
that seems work expected. had sorts of trouble figuring out how use equivalent of pymc.lambda object in pymc3. came across deterministic object. following code:
import pymc3 pymc3.model() model: theta = pymc3.normal('theta', 0, 0.88) x1 = pymc3.bernoulli('x1', p=pymc3.deterministic('b', 1./(1+np.exp(-(theta-(-0.75))))), observed=[1]) x2 = pymc3.bernoulli('x2', p=pymc3.deterministic('c', 1./(1+np.exp(-(theta-(0))))), observed=[1]) start=pymc3.find_map() step=pymc3.nuts(state=start) trace = pymc3.sample(20000, step, njobs=1, progressbar=true) pymc3.traceplot(trace)
the problem i'm having map estimate theta using pymc2 ~0.68 (correct), while estimate pymc3 gives ~0.26 (incorrect). suspect has way i'm defining deterministic function. pymc3 won't let me use lambda function, have write expression in-line. when try use lambda theta=theta:... error:
astensorerror: ('cannot convert <function <lambda> @ 0x157323e60> tensortype', <type 'function'>)
something theano?? suggestions appreciated!
it works when use theano tensor
instead of numpy function in deterministic.
import pymc3 import theano.tensor tt pymc3.model() model: theta = pymc3.normal('theta', 0, 0.88) x1 = pymc3.bernoulli('x1', p=pymc3.deterministic('b', 1./(1+tt.exp(-(theta-(-0.75))))), observed=[1]) x2 = pymc3.bernoulli('x2', p=pymc3.deterministic('c', 1./(1+tt.exp(-(theta-(0))))), observed=[1]) start=pymc3.find_map() step=pymc3.nuts(state=start) trace = pymc3.sample(20000, step, njobs=1, progressbar=true) print "\nthe map value theta is", np.median(trace['theta']) pymc3.traceplot(trace);
here's output:
Comments
Post a Comment