Almost there. How many groups do you have? If it’s quite a few then the recursion limit probably comes from these self-referential set_subtensor calls. You can do some thing like
g_idx, g_vals, gam_vals = list(), list(), list()
for index, group in enumerate(GroupId1):
sub_group = xxdat_matrix[GroupId1 == group]
sum_ad = tt.sum(sub_group[:,-1].tolist())
sum_ad_gammma = sum_ad**gamma
g_idx.extend(index)
g_vals.extend([group]*len(index)]
gam_vals.extend(sum_ad_gamma)
yydat_matrix = tt.set_subtensor(yydat_matrix[g_idx,[0,1,2,3]], g_vals)
yydat_matrix = tt.set_subtensor(yydat_matrix[g_idx,4], gam-vals)
Now list() likely won’t work. My guess is you’ll need a correctly-shaped matrix or array; but you get the idea. This way you’re only adding two set nodes to the computation graph.