# Applying Vectorization in generated quantities section

**URL:** <https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769>\
**Category:** General\
**Created:** [March 20, 2020, 5:18pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769 "2020-03-20T17:18:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 20, 2020, 5:18pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/1 "2020-03-20T17:18:35Z")

</div>

Hi Everyone,

I am trying to convert all the for loops into vectorized form so that the calculations are done faster.

But, when I try to change the for loops to vectorized form in generated quantities section, Im getting a dimension mismatch in assignment error.

My question is, vectorization is not applicable to generated quantities section?

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 20, 2020, 7:56pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/2 "2020-03-20T19:56:27Z")

</div>

Could you please post some code so we can take a look at how you’re currently attempting to do this?

---

<div class="post-metadata">

**Author:** ![tiagocc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tiagocc/32/85_2.png) [@tiagocc](https://discourse.mc-stan.org/u/tiagocc)\
**Post date:** [March 20, 2020, 8:48pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/3 "2020-03-20T20:48:52Z")

</div>

Yes, it is possible to vectorize rng functions, but as the error tells you,

> [@Murali\_037](#):
>
> Im getting a dimension mismatch in assignment error

you need to ensure you have matching dimensions on both sides of the rng function.

For example, given the data

```stan
data {
int N;
vector[N] y;
}

```

You can create the following vector of ones which you can then use on the `generated quantities` block to ensure your functions have the correct dimensions.

```stan
transformed data {
vector[N] ones_N = rep_vector(1, N);
}
parameters {
real <lower = 0> lambda;
}

model {
lambda ~ exponential(1);
target += exponential_lpdf(y | lambda);
}

generated quantities {
real y_rep[N] = exponential_rng(ones_N * lambda);
}

```

Does, avoiding looping over every data point.

Hope it helps!

---

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 20, 2020, 11:25pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/4 "2020-03-20T23:25:57Z")

</div>

Hi@tiagocc

I will try to implement this and update tomorrow If I’m able to make my model work. Thanks for the tip

---

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 24, 2020, 2:58pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/5 "2020-03-24T14:58:43Z")

</div>

Hi @tiagocc,

Can you show an example of using the ones for normal\_rng function?

---

<div class="post-metadata">

**Author:** ![tiagocc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tiagocc/32/85_2.png) [@tiagocc](https://discourse.mc-stan.org/u/tiagocc)\
**Post date:** [March 31, 2020, 5:01pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/6 "2020-03-31T17:01:43Z")

</div>

Sorry it took so long to respond.

I would do something like:

```stan

transformed data {
  int N = 10;
  vector[N] ones_N = rep_vector(1, N);
}

generated quantities {
  real yrep [N] = normal_rng(ones_N * 1, ones_N * 10);  
}

```

---

<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:** [July 1, 2020, 4:49pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/7 "2020-07-01T16:49:50Z")

</div>

a little clarification on the above example - which is entirely correct -  
as this issue just bubbled up again - [Minor typos in Stan user guide · Issue #208 · stan-dev/docs · GitHub](https://github.com/stan-dev/docs/issues/208)

intuitively, one would want `yrep` to be of type `vector[N]` just as `y` is.  
however, this doesn’t compile.

the definition of `normal_rng` in the Stan Functions Reference Manual - [Stan Functions Reference](https://mc-stan.org/docs/2_23/functions-reference/normal-distribution.html)  
says:

> `R` **`normal_rng`** `(reals mu, reals sigma)`  
> Generate a normal variate with location mu and scale sigma; may only be used in transformed data and generated quantities blocks. For a description of argument and return types, see section [vectorized PRNG functions](https://mc-stan.org/docs/2_23/functions-reference/vectorization.html#prng-vectorization).

that definition uses type `R` - in the prng-vectorization section, 10.8.3.3 Return type - it says:

> The result of a vectorized PRNG function depends on the size of the arguments and the distribution’s support. If all arguments are scalars, then the return type is a scalar. For a continuous distribution, if there are any non-scalar arguments, the return type is a real array ( `real[]` ) matching the size of any of the non-scalar arguments, as all non-scalar arguments must have matching size. Discrete distributions return `ints` and continuous distributions return `reals` , each of appropriate size. The symbol `R` denotes such a return type.

in short, the vectorized RNG function `normal_rng` return scalars or `int` or `real` arrays. not `matrix`, `vector`, or `row_vector`.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 7, 2020, 7:58pm UTC](https://discourse.mc-stan.org/t/applying-vectorization-in-generated-quantities-section/13769/8 "2020-07-07T19:58:44Z")

</div>

> [@Murali\_037](#):
>
> trying to convert all the for loops into vectorized form so that the calculations are done faster.

That only helps if there’s autodiff inside the loop that is more efficient outside the loop. Loops themselves are very fast in Stan. That means this

```no-highlight
real y[N];
for (n in 1:N)
  y[n] ~ normal(mu, sigma);

```

is a lot slower than the vectorized form

```no-highlight
y ~ normal(mu, sigma);

```

tghat doesnt’ carry over everywhere. For example,

```no-highlight
real x[N];
real y[N] = exp(x);

```

isn’t any faster than

```no-highlight
real x[N];
real y[N];
for (n in 1:N) 
  y[n] = exp(x[n]);

```

because all the vectorized `exp` function does is a loop down in C++ just like the loop code.

_Edit: There’s no autodiff in transformed parameters or generated quantities, so there won’t be a speed difference between loops or vectorized operations for those functions. There still will be a big difference between matrix operations and loops. You definitely want to call matrix operations where possible because they have lots of optimizations built in._
