# Vectorizing parameters by indices

Hi there,

I am trying to reduce computation time and was wondering whether vectorization of the loop in the following model would help:

``````model {
theta ~ std_normal();         // informative true prior
beta ~ normal(0, sigma_beta);
gamma ~ normal(0, sigma_gamma);
sigma_beta ~ cauchy(0, 5);
sigma_gamma ~ cauchy(0, 5);

for (n in 1:N){
real p;
real g;
p = inv_logit(theta[jj[n]] - beta[ii[n]]);
g = fabs(int_step(z[n])-inv_logit(gamma[jj[n]]));
y[n] ~ bernoulli(p + (1-p)*g);
}
}
``````

I tried:

``````model {
vector[N] p;
vector[N] g;
theta ~ std_normal();         // informative true prior
beta ~ normal(0, sigma_beta);
gamma ~ normal(0, sigma_gamma);
sigma_beta ~ cauchy(0, 5);
sigma_gamma ~ cauchy(0, 5);
p <- inv_logit(theta[jj]-beta[ii]);
g <- fabs(z-inv_logit(gamma[jj]));
y ~ bernoulli(p + (1-p).*g);
}
``````

I edited this post, the above code does actually work. I am however not sure why the parameters theta and gamma take on dimension N. Are the indices ii and jj implicitly converting them to vectors of length N?

Yes

2 Likes