Calculating log_likelihood for model with scan

Great! This does indeed work. Two related questions have come up for me:

  1. While implementing your solution I once accidentally fitted the model with y[i] = -1 but without your proposed change to the model. Surprisingly, this worked without warnings or errors even though this means that at some point obs = numpyro.sample("y", dist.Bernoulli(probs=probs), obs=-1) must have been called. What happens there? Does it treat this as if obs is None? A shallow look at my results seems to suggest this.

  2. An alternative solution I considered before you answered was to use the obs_mask argument of sample(...), where I denote the left-out sample by a False in the obs_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!