Translation from PyMC3 to PyMC

Hello, I am porting an old PyMC3 model (written in 2019, so I assume it was v3.7 or similar) to the PyMC and am struggling. Does there exist a vocabulary from ‘old’ to ‘new’ way of doing things? For example, what should I use instead of

  • theano.shared
  • .astype(floatX) in an expression like np.random.normal(loc=0, scale=1, size=[2, 3]).astype(floatX)
  • how to get access to posterior of a specific parameter? I reran this example in Colab, and it breaks with the new PyMC at the expression trace2['alpha']

Most of the times it’s enough to replace theano.x by pytensor.x. In your case pytensor.shared

1 Like

Re: the last point, all the pm.sample functions return arviz InferenceData by default now, so you can grab variables as if you had first done az.from_pymc3 in older versions. This notebook also does a lot grabbing stuff out of InferenceData, so it might be good to check out.

1 Like

Thanks both @ricardoV94 @jessegrabowski, pytensor got me a long way. The item I am stuck with now is
1). getting samples of a particular variable from trace,
2). getting samples of a particular variable from prior/posterior predictive.

For example, the code below produces errors on the last two lines. What am I doing wrong? Here is a link to Colab with the same code for reproducibility.

import pymc as pm

n = 250
with pm.Model() as model:
    alpha = pm.Gamma('alpha', alpha=4, beta=0.5)
    beta = pm.Gamma('beta', alpha=4, beta=0.5)
    x1 = pm.Beta('x1', alpha, beta)
    x2 = pm.Beta('x2', alpha, beta)
    k1 = pm.Binomial('k1', n=n, p=x1, observed=140)
    k2 = pm.Binomial('k2', n=n, p=x2, observed=110)

with model:
    trace_prior = pm.sample_prior_predictive(500)
with model:
    trace_sample = pm.sample(500)



trace_prior will have groups prior and prior_predictive. Since alpha isn’t observed, you can get it out of the prior group like this: trace_prior.prior['alpha'].

trace_sample will have a bunch of groups, but the relevant one here is posterior: trace_sample.posterior['alpha']

If you’re working in a notebook, you run a cell with only trace_prior or trace_sample to get a nice interactive object you can click around inside to explore the structure.

1 Like