Great! This does indeed work. Two related questions have come up for me:
-
While implementing your solution I once accidentally fitted the model with
y[i] = -1but without your proposed change to the model. Surprisingly, this worked without warnings or errors even though this means that at some pointobs = numpyro.sample("y", dist.Bernoulli(probs=probs), obs=-1)must have been called. What happens there? Does it treat this as ifobs is None? A shallow look at my results seems to suggest this. -
An alternative solution I considered before you answered was to use the
obs_maskargument ofsample(...), where I denote the left-out sample by aFalsein theobs_mask.
def model(X, y=None, obs_mask=None):
# Model where y_i depends on x_i and y_{i-1}
...
def transition(y_prev, data_curr):
x_curr, y_curr, obs_mask_curr = data_curr
probs = foo(y_prev, x_curr)
obs = numpyro.sample(
"y", dist.Bernoulli(probs=probs), obs=y_curr, obs_mask=obs_mask_curr
)
return obs, (obs)
_, (obs) = scan(transition, (init_y), (X, y, obs_mask), length=len(y))
But this doesn’t seem to work. Can you say what my mistake here is?
Thanks so much!