Adding a Deterministic variable after sampling

Oh, I failed to notice that one can’t simply refer to variables as normal when cloning but have to refer to them as elements of the cloned model object, so this works as expected:

import pymc as pm
from pymc.model.fgraph import clone_model

with pm.Model() as model:
    data = pm.ConstantData('data',[0])
    mu = pm.Normal('mu', mu=0, sigma=1)
    mu_squared1 = pm.Deterministic('mu_squared1', mu**2)
    y = pm.Normal('y', mu=mu, sigma=1, observed=data)


with clone_model(model) as model2:
    mu_squared2 = pm.Deterministic('mu_squared2', model2['mu']**2)


with model2:
    trace = pm.sample_prior_predictive(
        samples = 1
        , var_names = ['mu','mu_squared1','mu_squared2']
    )
1 Like