Mu of pm.Normal() depends on output of RandomVariable on previous time step. How do I implement this?

If you have random variables inside a scan, like pm.Normal.dist(...), you need to help pytensor pass along the random states from step to step. There’s a helper function for this in pymc.pytensorf called collect_default_updates. Here’s an example of how to do this.

Next you should make the whole scanned trajectory a single random variable. One thing that isn’t good about that example is that it does this model.register_rv thing to do that; this is not the recommended method. Use a pm.CustomDist, passing the scan output as the dist argument, as shown here.

One other thing to do is always pass strict=True to your scans. This will prevent scan from looking outside the local namespace of stepFunc when doing its thing, which can be a source of errors. I really recommend that you always explicitly pass everything to scan. It looks like you do that here, but it’s still good to have that check active.

5 Likes