Vectorization of target

According to this documentation

A faster alternative is to copy the values into a vector, then apply the sum() operator, as in the following refactoring.

{
  vector[N] summands;
  for (n in 1:N)
    summands[n] = foo(n,...);
  total = sum(summands);
}

Is this also true for incrementing the log density? I.e. is the gradient calculation more efficient in

target = sum(lp)

vs

target += lp[n]
2 Likes

Yes, it is a bit more efficient. Things like this may not be that important on simple vector math (I wouldn’t worry about the particular transformation you’re quoting), but on the probability statements they are important.

So, if y is a vector, something like this:

y ~ binomial_logit(N, a * x  + b);

should be preferred to something like:

for(i in 1:N) {
  y[n] ~ binomial_logit(N, a * x[n]  + b);
}

The difference is that with the probability statements there are often shared computations.

1 Like