Issue regarding indexing using integer random variable

I am having a simple indexing issue.

For my model, prior of Z is coming from Discrete Uniform.

It looks like we can not use this Z as an index of a global array.
Z in my case is an Integer Random Variable.

Here is an example of what I am trying to do:

prob = np.linspace(0,0.5,6)
def my_likelihood(z):
    def logp_(data):
        n= len(data.value)        
        return -n*prob[z] # < ==== Problem is here!!
    return logp_

obs =[1,2,3,5]
with pm.Model() as model:
    z = pm.DiscreteUniform('z',lower =0,upper=4)
    like = pm.DensityDist('like', my_likelihood(z), observed=obs)

But I am getting this error:
IndexError: only integers, slices (:), ellipsis (), numpy.newaxis (None) and integer or boolean arrays are valid indices

Thanks in advance! Appreciate the help in advance.

The issue here is that z is a Theano Variable; while it looks like prob is a numpy array. Try doing a

prob_sh = theano.shared(prob)

    reuturn -n*prob_sh[z]
1 Like