# Why are \_rng functions necessary for generating posterior predictive distributions?

**URL:** <https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169>\
**Category:** Modeling\
**Created:** [February 1, 2018, 2:20pm UTC](https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169 "2018-02-01T14:20:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![IambicPentameter](https://avatars.discourse-cdn.com/v4/letter/i/74df32/32.png) [@IambicPentameter](https://discourse.mc-stan.org/u/IambicPentameter)\
**Post date:** [February 1, 2018, 2:20pm UTC](https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169/1 "2018-02-01T14:20:23Z")

</div>

Hello,

I am building some AR(1) process models, and I want to generate some posterior predictive distributions. In my reading of the documentation/internet I see everyone use \_rng functions. Why is that necessary? What is the difference between

```
generated quantities {
 matrix[T, NC] Y_pred; //predictions 
 
for (c in 1:NC){
Y_pred[1,c]=Y[1,c];
        for (t in 2:T){
        Y_pred[t,c] = normal_rng(alpha[c] + beta[c] * Y[t-1,c],sigma[c]);
        }
}
}

```

and

```
generated quantities {
 matrix[T, NC] Y_pred; //predictions 
 
for (c in 1:NC){
Y_pred[1,c]=Y[1,c];
        for (t in 2:T){
        Y_pred[t,c] = alpha[c] + beta[c] * Y[t-1,c];
        }
}
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [February 1, 2018, 2:46pm UTC](https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169/2 "2018-02-01T14:46:21Z")

</div>

The former reflects the fact that even if two observations have the same value of `alpha[c] + beta[c] * Y[t - 1, c]` they will have different values of `Y[t, c]` due to noise / measurement error / etc. It also reflects your uncertainty in `sigma`, which is the standard deviation of said noise / measurement error / etc. As such, the posterior predictive distribution should closely resemble the marginal distribution of the data, whereas it will not — even when the model fits well — for the latter way you describe, which is not even a posterior predictive distribution but rather the posterior distribution of the conditional mean of the outcome. So, the first way is right and the second way is wrong for most purposes.

---

<div class="post-metadata">

**Author:** ![tlyim](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tlyim/32/3834_2.png) [@tlyim](https://discourse.mc-stan.org/u/tlyim)\
**Post date:** [November 16, 2019, 10:46pm UTC](https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169/3 "2019-11-16T22:46:47Z")

</div>

I am curious about the following. Would appreciate your straightening out any misconception.

If in the end, one would only use the mean of the posterior predictive distribution to compare against point forecasts from other non-Bayesian methods, does it mean starting with `rng` is redundant? Because the random number generated is independent of the estimated parameters. Taking expectations successively (first one the random number, then on the parameters) will end up with the same mean (right?).

(I know focusing only on point forecasts is odd to Bayesians. But I am coming from the non-Bayesian world and using Stan for its convenience in modeling multiple aspects of a problem in an integral way. I believe my audience would care more about whether the complexity would take them somewhere in terms of assessment criteria that they are more familiar with than using density-forecast based approaches.)

---

<div class="post-metadata">

**Author:** ![ldeschamps](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ldeschamps/32/4463_2.png) [@ldeschamps](https://discourse.mc-stan.org/u/ldeschamps)\
**Post date:** [November 17, 2019, 1:05pm UTC](https://discourse.mc-stan.org/t/why-are-rng-functions-necessary-for-generating-posterior-predictive-distributions/3169/4 "2019-11-17T13:05:26Z")

</div>

Hello!

Even in non-bayesian area, you can compute a predictive distribution!

The point/distribution difference is not about prediction, but about parameters. You can get point estimates maximazing the likelihood for mean and dispersion, or you can get a posterior distribution of mean and dispersion. In both cases, you can use mean(s) and dispersion(s) to generate a predictive distribution to compare to your data.

Now, you could be only interested in the value of mu and the uncertainty around it. You could use for that point estimate with a non-really-interpretable confidence interval. Or use the ready to interpret posterior distribution.

The major differences, I think, is that we tend to give more importance to modelling the noise in the bayesian area. The noise around the mean is part of the data generating process, and failing to model it is simply failing to model the process we are interested in. For exemple, I stopped recently to be interested by r2, because I don’t want my data point to be closely around my means, I want instead to be sure to unterstand how the dispersion occured. A high r2 would have given a less interesting result!

Hope that helped!

Have a good day!  
Lucas
