I was wondering why the Metropolis implementation of pymc3 performs the model forward calculation twice per step:
Running this simple example and investigating the output shows that “Executed” is printed twice per step.
import numpy as np import pymc3 as pm import theano.tensor as tt from theano.compile.ops import as_op @as_op(itypes=[tt.dscalar, tt.dscalar], otypes=[tt.dvector]) def test_dist(x, y): radius = (x - y)**3 print("Executed") return np.array([radius]) with pm.Model() as test_model: a1 = pm.Normal("a1", mu=5, sigma=2) a2 = pm.Normal("a2", mu=10, sigma=2) a3 = test_dist(a1, a2) a3_obs = pm.Normal("a3_obs", mu=a3, sigma=130, observed=10.0) step = pm.Metropolis([a1, a2], blocked=True) trace = pm.sample(10, tune=50, init=None, chains=1, cores=1, step=step, return_inferencedata=False)
From my understanding, shouldn’t it only be computed once, and then compared to the previous result?
Or is this for some more advanced feature that goes beyond the basic Metropolis algorithm?
I also noticed that the model is evaluated once if I load an existing trace using pm.load_trace. Why is that needed?
Thanks for the help!