Observed variable using custom function

I would suggest you to turn the function q_r into a matrix and index to it for boolean computation. Specifically, you dont need to compare the actually string within q_r for your purpose, as long as you have the index it is sufficient. Working with matrix also allow you to avoid the for loop and easier to use theano operation.