Hello to every one! I am trying to optimize some code, but have a hard time assessing what direction to take. In particular I have a hard time understanding if in `rstan`

it is more efficient at for loops or Hadamard products of matrices.

In recent years I have coded mostly in Python, hence I have imprinted in me the rationale to avoid for loop at all cost. In my code I have to calculate the bernoulli probability of a matrix `A`

of binary values, and hence rather than looping trough its values, I created a user-defined function that calculates the probability for the whole matrix using basically two Hadamard products between matrices of the same dimension of the data matrix `A`

to do so.

Is such an approach usually faster than the for loop, or I am actually going to be slower?

Would this situation change if in this matrix I have many missing values of which I don’t want to calculate the likelihood(let us say roughly 90%), hence rather than doing the Hadamard products, I could just loop trough a list which has just the indices of the non Nan values?

Best,

Luca