我目前使用参数列表和theano开关功能尝试了多个开关点。如果没有这些丑陋的清单,我怎么能以更好的方式做到这一点。
with model:
switchpoints = []
mus = []
sds = []
for point in range(num_switch):
if point == 0:
switchpoints.append(pm.DiscreteUniform("switchpoint{0}".format(point),
lower=indices[0], upper=indices[-1]))
else:
switchpoints.append(pm.DiscreteUniform("switchpoint{0}".format(point),
lower=switchpoints[point - 1], upper=indices[-1]))
for point in range(num_switch + 1):
mus.append(pm.Exponential("mu_{0}".format(point), alpha))
sds.append(pm.Exponential("sd_{0}".format(point), beta))
tau_mus = []
tau_sds = []
for point in range(num_switch):
if point == 0:
tau_mus.append(pm.math.switch(switchpoints[point] >= indices, mus[0], mus[1]))
tau_sds.append(pm.math.switch(switchpoints[point] >= indices, sds[0], sds[1]))
else:
tau_mus.append(pm.math.switch(switchpoints[point] >= indices, tau_mus[point - 1], mus[point]))
tau_sds.append(pm.math.switch(switchpoints[point] >= indices, tau_sds[point - 1], sds[point]))
likelihood = pm.Normal('likelihood', mu=tau_mus[-1], sd=tau_sds[-1], observed=self.data)
step1 = pm.Metropolis()
trace = pm.sample(self.number_samples, step=step1)