Hi Chris,
thank you for your replay.
I added an observed node but the sampler still ignore my step method.
it seems that the sampler ignore new step methods after it sampled for
the first time.
in the first part of the following code I change the step method and
then sample, and everything is working as expected. In the second part
I initialize the model, sample once, and then try different sampling
method which do not affect the sampler, the only effect I can notice
is a reduction in the number of rejected samples (instead of
increment) which is probably due to tuning.
import pymc as pm
from pylab import *
import sys
N = 500
sds = [0.001, 0.1, 10 ,1e6]
data = randn(50)
##### First Part
print "**** using step method before sampling for the first time ****"
for i_sd in sds:
x = pm.Uniform('x',-5000,5000, value=0)
y = pm.Normal('y', mu=x, tau=1, value=data, observed=True)
m = pm.MCMC([x,y])
m.use_step_method(pm.Metropolis, x,
proposal_distribution='Normal', proposal_sd=i_sd)
m.sample(N, progress_bar=False)
print "**** used prposal_sd=%f" % i_sd
print "precent of rejected samples: %f" % (sum(diff(x.trace()
[:])==0)*1./N)
print
sys.stdout.flush()
###### Second Part
x = pm.Uniform('x',-5000,5000, value=0)
y = pm.Normal('y', mu=x, tau=1, value=data, observed=True)
m = pm.MCMC([x,y])
m.sample(1, progress_bar=False) #!#!#!#!#!#!#!#! taking one sample
print "**** changing the step method after sampling ****"
for i_sd in sds:
m.use_step_method(pm.Metropolis, x,
proposal_distribution='Normal', proposal_sd=i_sd)
m.sample(N, progress_bar=False)
print "**** used prposal_sd=%f" % i_sd
print "precent of rejected samples: %f" % (sum(diff(x.trace()
[:])==0)*1./N)
print
output:
**** using step method before sampling for the first time ****
**** used prposal_sd=0.001000
precent of rejected samples: 0.000000
**** used prposal_sd=0.100000
precent of rejected samples: 0.200000
**** used prposal_sd=10.000000
precent of rejected samples: 0.978000
**** used prposal_sd=1000000.000000
precent of rejected samples: 0.998000
**** changing the step method after sampling ****
**** used prposal_sd=0.001000
precent of rejected samples: 0.632000
**** used prposal_sd=0.100000
precent of rejected samples: 0.578000
**** used prposal_sd=10.000000
precent of rejected samples: 0.476000
**** used prposal_sd=1000000.000000
precent of rejected samples: 0.360000