added sketched derived mcmc classes and proposal generators
@@ -77,3 +77,52 @@ class MCMC(pyro.infer.abstract_infer.AbstractInfer):
for tr in traces:
log_z = log_z + tr.log_pdf()
return log_z / len(traces)
+
+
+##############################################
+# Non-functioning MCMC subclasses and helpers
+##############################################
+
+def hmc_proposal(model, sites=None):
+ def _fn(tr, *args, **kwargs):
+ for i in range(steps):
+ tr = poutine.block(poutine.trace(poutine.replay(model, tr, sites=sites)))(*args, **kwargs)
+ logp = tr.log_pdf()
+ samples = values(tr.filter(site_type="sample"))
+ autograd.backward(samples, logp)
+ optimizer.step(samples)
+ return tr
+ return _fn
+
+
+def single_site_proposal(model):
+ def _fn(tr, *args, **kwargs):
+ name = itertools.randomchoice(tr.filter(site_type="sample").keys())
+ new_site = propose(tr[name])
+ new_tr = tr.copy()
+ new_tr[name] = new_site
+ new_tr = poutine.trace(
+ poutine.replay(model, new_tr, sites=parents(tr, name)))(*args, **kwargs)
+ return new_tr
+ return _fn
+
+
+def mixture_guide(guides):
+ return lambda *args, **kwargs: guides[pyro.sample(gensym(), discrete, guides, ones())](*args, **kwargs)
+
+
+class MixedHMCMH(MCMC):
+ def __init__(self, model):
+ proposal = mixture_guide([hmc_proposal(model),
+ single_site_proposal(model)])
+ super(MixedHMCMH, self).__init__(model, proposal=proposal)
+
+
+class HMC(MCMC):
+ def __init__(self, model):
+ super(HMC, self).__init__(model, proposal=hmc_guide(model))
+
+
+class SingleSiteMH(MCMC):
+ def __init__(self, model):
+ super(SingleSiteMH, self).__init__(model, proposal=single_site_guide(model))
