Yes, I have tried it with a pymc3 distribution. I came up with an easy example (similar to this ) where it actually works with a Normal distribution as prior for the mean parameter.
import pymc3 as pm
import numpy as np
import pymc3.backends.ndarray
import matplotlib.pyplot as plt
from pymc3.step_methods.arraystep import ArrayStep
from pymc3 import Continuous
class MyStepMethod(ArrayStep):
def __init__(self, vars, model=None):
self.vars = vars
model = pm.modelcontext(model)
super(MyStepMethod, self).__init__(vars, [model.fastlogp])
self.accrate = 0
self.count = 0
def astep(self, q0, logp):
q = q0.copy()
q[0] = np.random.normal(q[-1], 1) # using current gamma value as mean
logalt, logneu = logp(q0), logp(q)
accept = logneu - logalt
if (accept > 0) or (np.random.uniform(low=0, high=1) < np.exp(accept)):
self.accrate += 1
return q
else: return q0
with pm.Model() as model:
mean = pm.Normal("test", 5, 0.001)
I = pm.Normal("I", mean,1)
updI = MyStepMethod(vars=[I, mean])
step1 = pm.NUTS(vars=[mean])
trace = pm.sample(5000, [updI, step1], start={'I':0.5}, njobs=1, chains=1, tune=1000)
pm.traceplot(trace)
plt.show()
However, if I use a different distribution for my gamma parameter and e.g. change the line
mean = pm.Normal(“test”, 5, 0.001)
to
mean = pm.Gamma(“test”, 81, 100)
I get the shape error:
Traceback (most recent call last):
File "/home/test.py", line 50, in <module>
updI = MyStepMethod(vars=[I, mean])
File "/home/test.py", line 26, in __init__
super(MyStepMethod, self).__init__(vars, [model.fastlogp])
File "/home/user/.local/lib/python3.6/site-packages/pymc3/step_methods/arraystep.py", line 113, in __init__
self.ordering = ArrayOrdering(vars)
File "/home/user/.local/lib/python3.6/site-packages/pymc3/blocking.py", line 36, in __init__
raise ValueError('Shape of variable not known %s' % name)
ValueError: Shape of variable not known test
Any idea on why it only works for normal distributions or how I can get it to work for a gamma distributed variable?