Best Way to Handle a for Loop in pymc4

For your specific error, you need to explicitly set the dtype of the values passed to outputs_info, i.e. at.as_tensor_variable(np.asarray(0, dtype='float64')). Aesara is very strict about dtypes and shapes, and this is doubly true for scan. As the error explains, it will not permit the result of the scan operation (the ‘inner function’) to be of a different datatype than the first value that “kicks off” the loop (outputs_info). I find it good to always be extra explicit in my code when working with scan.

One other note: you shouldn’t compile the aesara function yourself. Instead, let PyMC compile the entire computational graph by itself. In this case, it seems that “result” is a symbolic representation of “virality”, so you can use that directly in your deterministic function. If delta * prior_result * data.gamma() is a scalar, result is k x 1 vector. If it’s a vector, then result is k x v matrix (assuming k = n_rows and v = n_cols in your data)

1 Like