Hi,
I am trying to write a code where I pass a free random variable to a custom step method in order to calculate the new proposal value (in other words, the new proposal value is depending on the current value of the free random variable). I am doing a single site update, meaning that in my array q of length 100, I chose a position randomly and then only update only that position at a time.
I have tried various ways, e.g.
ni = some constant
R = Array of length 100
with Model() as model:
gamma = pm.Gamma('gamma', 81, 100)
I = MyDistr('I', gamma=gamma, ni=ni, testval=0.5, shape=100)
updI = MyStepMethod(vars=[I], gamma=gamma, ni=ni)
trace = pm.sample(10000, step=[updI], tune=2000)
class MyStepMethod(ArrayStep):
def __init__(self, vars, R, ni, gamma, model=None):
self.vars = vars
self.R = R
self.ni = ni
self.gamma = gamma
model = pm.modelcontext(model)
super(MyStepMethod, self).__init__(vars, [model.fastlogp])
def astep(self, q0, logp):
q = q0.copy()
ind = np.random.randint(low=0, high=self.ni, size=1)
q[ind] = self.R[ind] - self.gamma # setting new value
lognew = logp(q)
logalt = logp(q0)
# an then do a Metroplois Hastings with logold and lognew
The error message I get is: ValueError: setting an array element with a sequence. I dont understand this, because I thought gamma is not a sequence but rather a 1 dim element…?
I also tried passing gamma as vars parameter to the stepmethod function:
updI = MyStepMethod(vars=[I, gamma], R=R, ni=ni)
and then the idea is to use the last element of q as my gamma value:
def astep(self, q0, logp):
q = q0.copy()
ind = np.random.randint(low=0, high=self.ni, size=1)
q[ind] = self.R[ind] - q[-1] # setting new value
lognew = logp(q[:-1])
logalt = logp(q0[:-1])
However, I get the error message already at the updI = MyStepMethod(vars=[I, gamma] … line saying ValueError: Shape of variable not known gamma.
Also, setting gamma = pm.Gamma(‘gamma’, 81, 100, shape=1) does not resolve the problem…
Does anyone has any suggestion on how to go about it? Thank you!