# Weird result in Generated Quantities

**URL:** <https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333>\
**Category:** Modeling\
**Created:** [November 14, 2023, 3:40pm UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333 "2023-11-14T15:40:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommaso11397](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tommaso11397/32/20246_2.png) [@Tommaso11397](https://discourse.mc-stan.org/u/Tommaso11397)\
**Post date:** [November 14, 2023, 3:40pm UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/1 "2023-11-14T15:40:01Z")

</div>

Hi, I have a quick question about how exactly Stan computes the mean and quantiles for generated quantities. Let’s say I have the following model, I simplify the one I am currently using.

```stan
model {
  vector[N] mu = alpha + b_1 x_1 + b_2 x_2;
  y ~ normal(mu, sigma); 
} 
generated quantities{
 g = a + b_1*5 + b_2*10
}

```

The issue arises from the fact that if I mechanically calculate g using the output means for a, b\_1 and b\_2 I do not get the same as the mean that Stan output for g, why is that? Theoretically the mean of g taken over all the simulations and chains should be the same as summing the output means for the three parameters.

Another concern is the quantification of uncertainty, I understand that in this case covariance also plays a role and one cannot simply mechanically construct the variance (which is why I am using the generated quantity function, but are the ouput quantiles taken over all the simulations?

Thanks in advance for the help

---

<div class="post-metadata">

**Author:** ![Corey.Plate](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/corey.plate/32/20600_2.png) [@Corey.Plate](https://discourse.mc-stan.org/u/Corey.Plate)\
**Post date:** [November 15, 2023, 6:28am UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/2 "2023-11-15T06:28:34Z")

</div>

Try:

```stan
generated quantities {
 g = a + b_1*5.0 + b_2*10.0
}

```

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 15, 2023, 7:43am UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/3 "2023-11-15T07:43:54Z")

</div>

Do you get the same values if you calculate g over all draws in R/Python first and then calculate its mean.

---

<div class="post-metadata">

**Author:** ![Tommaso11397](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tommaso11397/32/20246_2.png) [@Tommaso11397](https://discourse.mc-stan.org/u/Tommaso11397)\
**Post date:** [November 15, 2023, 8:28am UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/4 "2023-11-15T08:28:34Z")

</div>

I use the command print and summary to get results, can that be an issue?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 15, 2023, 7:58pm UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/5 "2023-11-15T19:58:03Z")

</div>

Probably not. Do you use cmdstan interface?

Is there a programming language you use to do calculations? (E.g. python / R)

If you calculate g per draw from posterior, do you get the same g as in Stan? If you do then g from means is different from mean of g.

---

<div class="post-metadata">

**Author:** ![Tommaso11397](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tommaso11397/32/20246_2.png) [@Tommaso11397](https://discourse.mc-stan.org/u/Tommaso11397)\
**Post date:** [November 16, 2023, 10:45am UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/6 "2023-11-16T10:45:08Z")

</div>

I use Stan through RStudio. I use the print command from the Stan package and I compare the value Stan prints g^{print} with g^{manual} = a^{print} + 5 b\_1^{print} + 10b\_2^{print}, where I manually construct the latter value, using the mean parameter values Stan prints.

My question is, should they be equal by construction or not? And if not why?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 16, 2023, 11:42am UTC](https://discourse.mc-stan.org/t/weird-result-in-generated-quantities/33333/7 "2023-11-16T11:42:23Z")

</div>

So I guess you use CmdStanR interface?

I think you could use posterior package to extract draws and then calculate g manually.

You could also do some plotting to see if there is correlation with parameters.
