Hi Jesse,
Thank you so much for your advice. I learned about pytensor.scan and the “boilerplate” code you provided in Declaring Priors using Loops for State Space Models - #2 by jessegrabowski, and they solved my problem.
Revised code which worked without error:
import numpy as np
import pymc as pm
from pymc.pytensorf import collect_default_updates
import pytensor
import pytensor.tensor as pt
N = 32
Y = np.random.randint(0, 20, size=N)
# Helper function for pm.CustomDist
n_steps = N - 1
def statespace_dist(mu_init, sigma_level, size):
def grw_step(mu_tm1, sigma_level):
mu_t = mu_tm1 + pm.Normal.dist(sigma=sigma_level)
return mu_t, collect_default_updates(outputs=[mu_t])
mu, updates = pytensor.scan(fn=grw_step,
outputs_info=[{"initial": mu_init}],
non_sequences=[sigma_level],
n_steps=n_steps,
name='statespace',
strict=True)
return mu
with pm.Model() as model:
alpha1 = pm.Uniform('alpha[1]', 0, 10)
sigma = pm.Uniform('sigma', 0, 100)
alpha = pm.CustomDist('alpha', alpha1, sigma, dist=statespace_dist)
alpha = pt.concatenate([[alpha1], alpha])
l = pm.Deterministic('lambda', alpha.exp())
Yobs = pm.Poisson('Y', mu=l, observed=Y)
with model:
trace = pm.sample(1000, progressbar=False)