How to build the MMSB model with pymc3?

If myBeta and epsilon is not a tensor, you can move them outside of the with pm.Model()... - it’s much easier to check the correctness of the output and much faster.

In general, you can get rid of the two forloop with some matrix tricks (usually that is way faster than for loop). For example, in your case you can try something like:

import theano.tensor as tt
z = np.random.randint(0, 5, size=(5, 5))
eps = .1
with pm.Model() as m:
    myBeta = pm.Beta('b', 1., 1., shape=5)
    pD = tt.switch(tt.eq(z, z.T), myBeta[z], eps)
    y = pm.Bernoulli('D', p=pD, observed=np.random.rand(*z.shape)>.3)

The nice thing of the code above is that, it works with z being either a numpy array or a theano tensor.

1 Like