Problem with DiscreteUniform giving indices out of bounds

I have the following code:

with pm.Model() as model:
    sep = pm.DiscreteUniform('sep', 0, nTemp - 2*padding) 
    sep1 = pm.DiscreteUniform('sep1', sep+1, nTemp - padding)
    d0 = pm.DiscreteUniform('d0', lower = 0, upper = sep)
    d1 = pm.DiscreteUniform('d1', lower = sep+1, upper = sep1)
    d2 = pm.DiscreteUniform('d2', lower = sep1+1, upper = nTemp-2)
    pred = np.zeros((nTemp, len(box.match)), np.float64)
    for iwvl, amatch in enumerate(box.match):
        pred[0:,iwvl] += amatch['intensitySum']
    xpred = theano.shared(pred, name='p0')
    idx0 = tensor.as_tensor_variable(d0)
    idx1 = tensor.as_tensor_variable(d1)
    idx2 = tensor.as_tensor_variable(d2)
    em = pm.Normal('em', mu=emLog[0], sigma=0.02, shape=3)
    predicted = xpred[idx0]*10.**em[0] + xpred[idx1]*10.**em[1]
    nObs = len(box.match)
    intensity = np.zeros(nObs, np.float64)
    for iwvl in range(nObs):
        intensity[iwvl] = box.match[iwvl]['obsIntensity']
    wght = np.sqrt(2.)*0.2
    Y_obs = pm.Normal('Y_obs', mu=predicted, sigma=(wght*intensity)**2, observed=intensity)
    tune = 1000
    samples = 10000
    cores = 4    
    trace = pm.sample(samples, tune=tune, cores=cores, start={'sep': (Tindex[0]+Tindex[1])//2, 'sep1': (Tindex[1]+Tindex[2])//2})

This code actually runs. but what I want to do is
predicted = xpred[idx0]*10.**em[0] + xpred[idx1]*10.**em[1] + xpred[idx2]*10.**em[2]
and that fails with an index out of bounds
if I look at the trace of d2, it never goes out of bounds
Did something like this with PyMC2 that ran
Any suggestions?

Couple of questions regarding the code: what’s the value of nTemp? Also what does box.match do?

first, nTemp is the number of temperatures in the model. box.match is a list of spectral lines, each member of the list is a dict containing such things as a ‘wvl’ for the wavelength, ‘obsIntensity’, the observed intensity, the ‘intensitySum’, the predicted intensity for the line, a numpy array of size nTemp. em stands for emission-measure, basically the amount of material at 3 temperatures
basically, I am trying to use a three temperature model to reproduce the observed intensities.
what I need to have is 0 < idx0 < idx1 < idx2 <= nTemp - 1

the sampling will run for about 100-200 samples before idx2 goes out of bounds.
I am currently above to run the same problem with Pymc 2.3

I trying to run this in a jupyter ipython notebook
I am using
Python 3.6.10
Pymc3 3.7
Jupyter 1.0.0
jupyter Notebook 5.7.8
Ipython 7.13.0
all running on OpenSuse Leap 15.2 linux

the fact that the Pymc2 code runs makes me believe there may be a bug somewhere in pymc3

thanks for the interest

I’m having trouble replicating the model without the values you’re using, but with nTemp=3, padding=1 and some mock data from np.random.rand I’m getting chain failures, not out of bounds errors, when using predicted = xpred[idx0]*10.**em[0] + xpred[idx1]*10.**em[1] + xpred[idx2]*10.**em[2]. d2 gives me constant values of 2, which matches the dimensions of pred.

If I change to padding=0 I get out of bounds errors. I think the problem is in your nTemp and padding values

thanks for looking into this. I think a large part of my problem is trying to index theano variables and that does not work well. Also pymc2 did not actually work as well as I thought.

actually it does now work with Pymc2 but the coding is pretty ugly

It turns out that I was feeding the final pm.Normal as very bad value for sigma. It is a miracle that it worked for 2 temperature variables. Anyway, I seem to have pymc3 working for me now.
thanks for all the help