Skip to content

Instantly share code, notes, and snippets.

@joezuntz
Created March 1, 2012 11:24
Show Gist options
  • Save joezuntz/1949164 to your computer and use it in GitHub Desktop.
Save joezuntz/1949164 to your computer and use it in GitHub Desktop.
Problem loading AdaptiveMetropolis sampler state from db
import os
import pymc
database_file = "mc.pickle"
number_samples = 1000
a = pymc.Normal("a",mu=0, tau=1)
b = pymc.Normal("b",mu=0, tau=1)
mu = a+b
c = pymc.Normal("c", mu=mu, tau=1, observed=True, value=0.5)
if os.path.exists(database_file): #restore existing chain and do another number_samples samples
db = pymc.database.pickle.load(database_file)
mcmc = pymc.MCMC([a,b,c], verbose=2, db=db)
# I cannot find any combination of the following lines that works!
# mcmc.assign_step_methods()
# mcmc.use_step_method(pymc.AdaptiveMetropolis, [a,b], delay=100, interval=100)
# mcmc.restore_sm_state()
# mcmc.restore_sampler_state()
else: #Start new chain
mcmc = pymc.MCMC([a,b,c], verbose=2, db='pickle', dbname=database_file)
mcmc.use_step_method(pymc.AdaptiveMetropolis, [a,b], delay=100, interval=100)
print "Initial covmat:"
print mcmc.step_method_dict[a][0].C
mcmc.sample(number_samples)
print "Final covmat:"
print mcmc.step_method_dict[a][0].C
mcmc.db.commit()
mcmc.db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment