Search code examples
pythonpython-3.xpymc3

Creating a matrix of deterministic variables in pyMC3


I am trying to implement the following model with pyMC3

https://i.sstatic.net/YcJiw.jpg

I am unable to define an 8x8 matrix of deterministic variables.

Let's say the name of the 2D matrix that I want is s.

I want s[i, j] to be equal to np.exp(-c*(w*d1[i, j] + (1-w)*d2[i, j]))

where,

d1[i, j], d2[i, j] are np.arrays

c ~ Uniform(0,5)


w ~ Uniform(0,1)

I tried the following:

s = np.zeros((d1.shape[0],d1.shape[1]))

with pm.Model() as model:
    c = pm.Uniform("c", 0, 5)
    w = pm.Uniform("w", 0, 1)

    for i in range(8):
        for j in range(8):
            name_str = "s["+str(i)+","+str(j)+"]"
            s[i,j] = pm.Deterministic(name_str, tt.exp(-c*(w*d1[i,j]+(1-w)*d2[i,j])))

When I try to run this, I get the error:

ValueError: setting an array element with a sequence.

I am new to pyMC3. Is there any way to realize the above graphical model?


Solution

  • PyMC3 supports vectorized operations, so you should be able to just use

    with pm.Model() as model:
        c = pm.Uniform("c", 0, 5)
        w = pm.Uniform("w", 0, 1)
    
        s = pm.Deterministic('s', tt.exp(-c * (w * d1 + (1 - w) * d2)))