Using aesara tensor as indices

I have data that can be represented as a 2d matrix, eg like an actual map. I would like to model random variables for x and y coordinates, whose likelihood increases the closer they are to some quantity represented on the map. I am using pm.Potential to create this likelihood, but how to I use tensors for x and y as indices to the map data. I get errors saying the slicing the array can only be done by integers, and integer tensors do not suffice.

eg. some pseudocode

import pymc as pm
import numpy as np
import aesara.tensor as at

map = np.zeros((100, 100))
map[40:60, 40:60]=1

def mapPot(xpos, ypos, map):
   #here we want to slice the map and get the sum of the slice to represent the likelihood.
     xmin = xpos.astype(np.int32)
     ymin = ypos.astype(np.int32)
     
     xmax = xmin +5
     ymax = ymin +5
     count = np.sum(map[xmin:xmax, ymin:ymax])
     return -np.log(count)

with pm.Model() as model:
     x=pm.Uniform('xpos', lower=0, upper=99)
     y=pm.Uniform('ypos', lower=0, upper=99)
     pm.Potential('mappot', mapPot(x, y, map))

I think you just need to convert your map variable to a tensor variable, so you can slice it with Aesara variables

import aesara.tensor as at
...
map = at.as_tensor(map)
...
1 Like

Yes thanks, I also had to change

 count = np.sum(map[xmin:xmax, ymin:ymax])

to

mapt = at.as_tensor(map)
count = mapt[xmin:xmax, ymin:ymax].sum()

The only addition is to ensure that count is 1 or more otherwise it returns inf