This is an old version, view current version.

17 Multiple Indexing and Range Indexing

Stan allows multiple indexes to be provided for containers (i.e., arrays, vectors, and matrices) in a single position, using either an array of integer indexes or range bounds. This allows many models to be vectorized. For instance, consider the likelihood for a varying-slope, varying-intercept hierarchical linear regression, which could be coded as

for (n in 1:N)
  y[n] ~ normal(alpha[ii[n]] + beta[ii[n]] * x[n], sigma);

With multiple indexing, this can be coded in one line, leading to more efficient vectorized code.

y ~ normal(alpha[ii] + rows_dot_product(beta[ii], x), sigma);

This latter version is equivalent in speed to the clunky assignment to a local variable.

{
  vector[N] mu;
  for (n in 1:N)
    mu[n] = alpha[ii[n]] + beta[ii[n]] * x[n];
  y ~ normal(mu, sigma);
}