Error IndexError: index out of bounds

Hi,
I’m having a problem with indexing.
i want sample the longitude_i and latitude_i in the range of (1-79)
but if i make the draws or chains big, there will be an IndexError, index out of bounds.
i donot want the sampling happen out of the range (1-79), but i donot know how i should set.

m_srs_sum=np.zeros((2*num_lat,2*num_lon,num_Conc)) 
for i_num in range(num_Conc):
    for j_lat in range(num_lat):
        for k_lon in range(num_lon):
            for q_dur in range(time_dur):                
                m_srs_sum[j_lat][k_lon][i_num]=m_srs_sum[j_lat][k_lon][i_num]+m_srs[time_dur*i_num+q_dur][j_lat][k_lon]


lat_lower_bound=0
lat_upper_bound=120
lon_lower_bound=0
lon_upper_bound=80

with basic_model:

    # Priors for unknown model parameters   
    longitude_i = pm.DiscreteUniform("longitude_i", lower=1, upper=79)   
    latitude_i = pm.DiscreteUniform("latitude_i", lower=1, upper=79)
    releaserate = pm.Uniform("releaserate", lower=1, upper=100,transform=None)
    sigma_model=1
    m_srs_model = pm.Data('m_srs_model', m_srs_sum)

    
    lon_index=pm.theanof.intX(longitude_i)
    lat_index=pm.theanof.intX(latitude_i)

    grid_area = 100 * 1000 * lon_res * 100 * 1000 * lat_res 
    grid_volume = grid_area * level_height
    
    
    matrix_temp = m_srs_model[lat_index][lon_index]
    #factor=releaserate*1000*time_release_interval/3600  / grid_volume / time_res * 10 ** 9
    factor=releaserate*1000/time_release_interval  / grid_volume  * 10 ** 9
    result_ConcCal=factor*matrix_temp
    
    
    Y_pre=result_ConcCal
  
    
    Y_obs=pm.Normal("Y_obs", mu=Y_pre, sigma=sigma_model, observed=Y)
    trace_g=pm.sample(draws=20000,tune=1000,chains=20,cores=4)
    
CompoundStep
>CompoundStep
>>Metropolis: [latitude_i]
>>Metropolis: [longitude_i]
>NUTS: [releaserate]
Sampling 40 chains, 0 divergences:  76%|███████▌  | 638193/840000 [24:18<07:41, 437.53draws/s]  
RemoteTraceback: 

Traceback (most recent call last):
  File "D:\Program\anaconda\lib\site-packages\theano\compile\function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\
IndexError: index out of bounds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 110, in run
    self._start_loop()
  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 160, in _start_loop
    point, stats = self._compute_point()
  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 191, in _compute_point
    point, stats = self._step_method.step(self._point)
  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\compound.py", line 27, in step
    point, state = method.step(point)
  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\compound.py", line 27, in step
    point, state = method.step(point)
  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\arraystep.py", line 175, in step
    apoint, stats = self.astep(self.bij.map(point))
  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\metropolis.py", line 162, in astep
    accept = self.delta_logp(q, q0)
  File "D:\Program\anaconda\lib\site-packages\theano\compile\function_module.py", line 914, in __call__
    gof.link.raise_with_op(
  File "D:\Program\anaconda\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "D:\Program\anaconda\lib\site-packages\six.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "D:\Program\anaconda\lib\site-packages\theano\compile\function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\
IndexError: index out of bounds
Apply node that caused the error: Subtensor{int32, int32}(m_srs_model, ScalarFromTensor.0, ScalarFromTensor.0)
Toposort index: 18
Inputs types: [TensorType(float64, 3D), Scalar(int32), Scalar(int32)]
Inputs shapes: [(200, 320, 192), (), ()]
Inputs strides: [(491520, 1536, 8), (), ()]
Inputs values: ['not shown', 79, 362]
Outputs clients: [[Elemwise{Composite{(i0 + (-sqr((i1 - (i2 * i3 * i4)))))}}(TensorConstant{(1,) of -1..0664093453}, TensorConstant{[0.   0.  ..0.   0.  ]}, TensorConstant{(1,) of 0...8888888889}, InplaceDimShuffle{x}.0, Subtensor{int32, int32}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "D:\Program\anaconda\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2894, in run_cell
    result = self._run_cell(
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2940, in _run_cell
    return runner(coro)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3165, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3357, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-257-acb4e119d140>", line 44, in <module>
    matrix_temp = m_srs_model[lat_index][lon_index]
  File "D:\Program\anaconda\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2894, in run_cell
    result = self._run_cell(
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2940, in _run_cell
    return runner(coro)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3165, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3357, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-257-acb4e119d140>", line 44, in <module>
    matrix_temp = m_srs_model[lat_index][lon_index]

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
"""


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 110, in run
    self._start_loop()

  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 160, in _start_loop
    point, stats = self._compute_point()

  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 191, in _compute_point
    point, stats = self._step_method.step(self._point)

  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\compound.py", line 27, in step
    point, state = method.step(point)

  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\compound.py", line 27, in step
    point, state = method.step(point)

  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\arraystep.py", line 175, in step
    apoint, stats = self.astep(self.bij.map(point))

  File "D:\Program\anaconda\lib\site-packages\pymc3\step_methods\metropolis.py", line 162, in astep
    accept = self.delta_logp(q, q0)

  File "D:\Program\anaconda\lib\site-packages\theano\compile\function_module.py", line 914, in __call__
    gof.link.raise_with_op(

  File "D:\Program\anaconda\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)

  File "D:\Program\anaconda\lib\site-packages\six.py", line 702, in reraise
    raise value.with_traceback(tb)

  File "D:\Program\anaconda\lib\site-packages\theano\compile\function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\

IndexError: index out of bounds
Apply node that caused the error: Subtensor{int32, int32}(m_srs_model, ScalarFromTensor.0, ScalarFromTensor.0)
Toposort index: 18
Inputs types: [TensorType(float64, 3D), Scalar(int32), Scalar(int32)]
Inputs shapes: [(200, 320, 192), (), ()]
Inputs strides: [(491520, 1536, 8), (), ()]
Inputs values: ['not shown', 79, 362]
Outputs clients: [[Elemwise{Composite{(i0 + (-sqr((i1 - (i2 * i3 * i4)))))}}(TensorConstant{(1,) of -1..0664093453}, TensorConstant{[0.   0.  ..0.   0.  ]}, TensorConstant{(1,) of 0...8888888889}, InplaceDimShuffle{x}.0, Subtensor{int32, int32}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "D:\Program\anaconda\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2894, in run_cell
    result = self._run_cell(
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2940, in _run_cell
    return runner(coro)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3165, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3357, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-257-acb4e119d140>", line 44, in <module>
    matrix_temp = m_srs_model[lat_index][lon_index]
  File "D:\Program\anaconda\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2894, in run_cell
    result = self._run_cell(
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2940, in _run_cell
    return runner(coro)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3165, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3357, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "D:\Program\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-257-acb4e119d140>", line 44, in <module>
    matrix_temp = m_srs_model[lat_index][lon_index]

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "<ipython-input-257-acb4e119d140>", line 54, in <module>
    trace_g=pm.sample(draws=20000,tune=1000,chains=40,cores=4)

  File "D:\Program\anaconda\lib\site-packages\pymc3\sampling.py", line 469, in sample
    trace = _mp_sample(**sample_args)

  File "D:\Program\anaconda\lib\site-packages\pymc3\sampling.py", line 1059, in _mp_sample
    for draw in sampler:

  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 394, in __iter__
    draw = ProcessAdapter.recv_draw(self._active)

  File "D:\Program\anaconda\lib\site-packages\pymc3\parallel_sampling.py", line 297, in recv_draw
    raise error from old_error

RuntimeError: Chain 32 failed.

It’s a little tricky to figure out what’s happening if we can’t run your code. It looks like you have given us almost a complete script but some variables like num_lat and num_lon are missing. Can you provide a completely runnable version of this snippet?

here is the code.

thanks a lot for your help.

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
import arviz as az
import numpy as np
import pymc3 as pm
import os
import theano.tensor as tt
from theano.compile.ops import as_op

lon_start=-10
lon_res=0.25
lat_start=40
lat_res=0.25
level_height=500
time_res=10800   #3h
time_start_index=88
time_dur=12
time_release_interval=3600

num_lon=80
num_lat=80
num_Conc=10


m_srs_sum=np.zeros((2*num_lat,2*num_lon,num_Conc)) 

for i in range(num_Conc):
    m_srs_sum[20][20][i]=1


Y=np.ones(num_Conc)
basic_model = pm.Model()
with basic_model:

    # Priors for unknown model parameters   
    longitude_i = pm.DiscreteUniform("longitude_i", lower=1, upper=79)   
    latitude_i = pm.DiscreteUniform("latitude_i", lower=1, upper=79)
    releaserate = pm.Uniform("releaserate", lower=1, upper=100,transform=None)
    sigma_model=1
    m_srs_model = pm.Data('m_srs_model', m_srs_sum)

    
    lon_index=pm.theanof.intX(longitude_i)
    lat_index=pm.theanof.intX(latitude_i)

    grid_area = 100 * 1000 * lon_res * 100 * 1000 * lat_res 
    grid_volume = grid_area * level_height
    
    
    matrix_temp = m_srs_model[lat_index][lon_index]
    #factor=releaserate*1000*time_release_interval/3600  / grid_volume / time_res * 10 ** 9
    factor=releaserate*1000/time_release_interval  / grid_volume  * 10 ** 9
    result_ConcCal=factor*matrix_temp
    
    
    Y_pre=result_ConcCal
  
    
    Y_obs=pm.Normal("Y_obs", mu=Y_pre, sigma=sigma_model, observed=Y)
    trace_g=pm.sample(draws=20000,tune=1000,chains=20,cores=4)