Boradcasting in Stan

Hi,

I am a R user and most of time I do vector/matrix computation in R, there is something called ‘broadcasting’ happening to make the dimension match well (eg, one number + one vector). I noticed Stan has similar topics in user manula but I am still confused about it. For example in the following example:

vector[n] logit_p = beta[1] + beta[2]*x  + random_intercept1[indicator] + x.* random_intercept1[indicator];
int<lower = 0> y = bernoulli_logit_rng(logit_p);

Here beta is a vector of length two, x is a vector of length n, indicator is a vector of length n and random_intercept1[indicator] is a corresponding scaler (eg, random_intercept1 is a vector of length four, and each element of indicator is a number from 1-4 to choose the corresponding random intercept). The above expression works well since Stan will match the dimension correctly for me. However, if I rewrite the code in the following way, it will not work:

 vector[n] logit_p;
 vector[n] y;
 logit_p = beta[1] + beta[2]*x + random_intercept1[indicator] + x.* random_intercept1[indicator]];
 y = bernoulli_logit_rng(logit_p);

So I have to use the for loop to go through each position of vector y:

 vector[n] logit_p;
 vector[n] y;
 logit_p = beta[1] + beta[2]*x + random_intercept1[indicator] + x.* random_intercept1[indicator]];
 for (i in 1:n){
    y[i] = bernoulli_logit_rng(logit_p[i]);
  }

I don’t know why the above situation happens and I have encountered similar problems multiple times, so once encountering the problem I have to use the for loop to define vectors in Stan, which is quite confusing. May I ask that why this happens and is there any solution for that?

Thanks!

This isn’t a broadcasting issue, it’s a type issue. The return type of bernoulli_logit_rng(vector) is an array of integers (int[]), not a vector. So the following code should work for you:

 int y[n];
 y = bernoulli_logit_rng(logit_p);
1 Like

Thanks so much for youre reply! May I ask that what’s the difference between an array of integers and a vector in Stan? (since I thought vector is more general version of array of integer so I just simply ignored the difference and expected the code would work). Also may I ask will defineing y as a vector work if I would like to generate normal random variables?

Thanks!

I thought vector is more general version of array of integer so I just simply ignored the difference and expected the code would work

For a good coverage of the different data types, you should have a look at this chapter of the Reference Manual: 5 Data Types and Declarations | Stan Reference Manual

Stan is a strongly-typed language, which means that the specification of types and dimensions are very important. In this context, an array of integers and a vector are wholly different and not interchangeable.

will defineing y as a vector work if I would like to generate normal random variables?

When the rng functions that return continuous variables are called with vector inputs, the return type is an array of reals (i.e., real[])

1 Like

That’s very helpful!

The Stan Function’s reference documents available broadcast operations for real-valued, vector, and matrix operations - try searching the docs: https://mc-stan.org/docs/functions-reference/index.html