Metropolis step method initialization being called repeatedly and slow

I have checked this related post, but the problem has not been solved.
I added some print statements in Metropolis init method, and found that the init method is called repeatedly for many many times, each time for one variable. This does not finish until 6-7h later!
Strangely, I have passed all vars to Metropolis at once, but why does it only get one var and where is it called repeated? I tried search Metropolis entry point on GtiHub source repo but couldn’t find any.

s = pm.step_methods.metropolis.Metropolis(vars=self.model.free_RVs)

For some vars, it takes really long to initialize. Can I do anything to avoid it? Thanks.

Where did you add the print statement? And what is your model like? It is a bit difficult to say without more information.

Yep I agree. I added quite a few print statements before and after this line. Every statement in this init method has been executed many times.
I also print(vars) at the beginning of the init method. It gives only one variable, while in my model I pass all variables to it. I think maybe there is some wrapper for this step method but I couldn’t find any.

s = pm.step_methods.metropolis.Metropolis(vars=self.model.free_RVs)

For example, a simple model as follows.

with pm.Model() as model:
    mu = pm.Normal('mu', mu=0, sd=1)
    sd = pm.Normal('sd', mu=0, sd=1)
    obs = pm.Normal('obs', mu=mu, sd=sd, observed=np.random.randn(100))
    s = pm.Metropolis(vars=model.free_RVs)
    trace = pm.sample(100, tune=100, step=s, cores=1)

The print statements are added as such.
The result follows.

My questions are that, where the wrapper for the Metropolis is since I don’t see a for loop processing one variable at a time, and why the step after print('DEBUG: 1') can be extremely slow for some variable, e.g., a hierarchical parameter in my project model.

Hmm, maybe you can try passing blocked=True (although that is the default already)

1 Like

Oh damn! It works! Now all variables gather together!
Thanks a lot but i have to say, Metropolis has a poor document about its interface…Perhaps most people just stick to the default NUTS sampler and don’t pay as much attention to Metropolis.

BTW, does HMC or NUTS do the same thing, sort of cloning the vars into shared, which takes a really long time? Because i want fast and massive sampling (tho i know the effective number may be small) I turn to Metropolis, but now it turns out Metropolis is even slower because of preprocessing vars. Do you think this can be considered as a performance issue?

Metropolis is not a good sampler in high dimensions, so you really shouldn’t be using that. And yes all sampler clone the vars to generate a logp function.

Yes I agree. I just want to at least give it a try. The result is as bad as you say :(. Thanks for the clarification.

1 Like