Porting stan model to PyMC

Hi Blake,

I’m pretty sure I wrote this model! Where exactly are you stuck, is it the random walk part? I think this should get you started:

import pymc as pm

# Change these to your values
n_players = 10
n_periods = 20

with pm.Model() as model:
    
    # Initial variance
    sigma_s = pm.HalfNormal('sigma_s', sigma=1.)
    
    # Random walk variance:
    sigma_t_s = pm.HalfNormal('sigma_t_s', sigma=1.)

    s = pm.GaussianRandomWalk('s', sigma=sigma_t_s, shape=(n_periods, n_players),
                              init_dist=pm.Normal.dist(0, sigma_s))
    
    # ... The other random walk should be similar.

I haven’t tested this a great deal, so there may be a mistake, but I think this should work. This will give you the matrix s, which should be the same as the Stan one. I was a little bit unsure about whether the random walk is correctly vectorised, but according to this: Can I instantiate multiple random walks with different drifts it seems that this should return n_players independent random walks, which is what you want. You then need to construct r also in the same way way. Let me know if that helps.

Best,
Martin

3 Likes