# Boradcasting in Stan

**URL:** <https://discourse.mc-stan.org/t/boradcasting-in-stan/19420>\
**Category:** General\
**Created:** [November 21, 2020, 4:27am UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420 "2020-11-21T04:27:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [November 21, 2020, 4:27am UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/1 "2020-11-21T04:27:15Z")

</div>

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:

```stan
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:

```stan
 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:

```stan
 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!

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 21, 2020, 8:46am UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/2 "2020-11-21T08:46:40Z")

</div>

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);

```

---

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [November 21, 2020, 3:59pm UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/3 "2020-11-21T15:59:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 21, 2020, 4:09pm UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/4 "2020-11-21T16:09:24Z")

</div>

> 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](https://mc-stan.org/docs/2_25/reference-manual/data-types-chapter.html)

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[]`)

---

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [November 21, 2020, 4:46pm UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/5 "2020-11-21T16:46:44Z")

</div>

That’s very helpful!

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [November 21, 2020, 5:14pm UTC](https://discourse.mc-stan.org/t/boradcasting-in-stan/19420/6 "2020-11-21T17:14:11Z")

</div>

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](https://mc-stan.org/docs/functions-reference/index.html)

 ![Untitled](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/9/962345da310f2c5b8b787aeb949912ae5db353b6.png)
