Hi,
I would like to get some advice on how to optimize a transformed parameters’ block.
I have a P by Q matrix M, that I need to fill with some transformation of my parameters as follows. param_1 is a vector of length P and param_2 is a P by Q matrix.
For all p, the pth diagonal element of M is 1 / (1 + exp(-param_1[p])).
For all p and q such that p != q, I have M[p, q] = -2*M[p, p] * (exp(param_2[p,q])/(1+sum(exp(param_2[p,])) - exp(param_2[p,q])).
Would anyone have an idea of how to code this efficiently in rstan? I am doing the following but this doesn’t seem to be optimal. M_intermediate is a P by Q matrix, and M_pp is a vector of length P.
for (p in 1:P){
M_intermediate[p,] = to_row_vector(exp(param_2[p,]));
M_pp[p] = 1 / (1 + exp(-param_1[p]));
for (q in 1:Q){
M[p,q] = - 2 * M_pp[p] * M_intermediate[p,q] / (1 + sum(M_intermediate[p,q]) - M_intermediate[p,q]);
}
M[p,p] = M_pp[p];
}
Thank you very much!
[edit: escaped code]