# Can a generated quantity be used in the model block

**URL:** <https://discourse.mc-stan.org/t/can-a-generated-quantity-be-used-in-the-model-block/32673>\
**Category:** General\
**Created:** [September 6, 2023, 12:58pm UTC](https://discourse.mc-stan.org/t/can-a-generated-quantity-be-used-in-the-model-block/32673 "2023-09-06T12:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjib](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sanjib/32/17074_2.png) [@sanjib](https://discourse.mc-stan.org/u/sanjib)\
**Post date:** [September 6, 2023, 12:58pm UTC](https://discourse.mc-stan.org/t/can-a-generated-quantity-be-used-in-the-model-block/32673/1 "2023-09-06T12:58:12Z")

</div>

Can a generated quantity be used in a model block.

I have a mixture structure, and I can write the mixture in a marginalized form (as a sum) in the model block.

I can then generate the mixture component indicator in the generated quantities block from its posterior predictive distribution…

I would then like to use the generated mixture component indicator (from the posterior predictive distribution) to describe a separate part of the model.

Is that possible/allowed?

Sanjib

```plaintext

```

---

<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:** [September 6, 2023, 1:39pm UTC](https://discourse.mc-stan.org/t/can-a-generated-quantity-be-used-in-the-model-block/32673/2 "2023-09-06T13:39:39Z")

</div>

no, but you can use the transformed parameters block to do the computation.  
all variables declared in the model block are local variables, not available in GQs.  
top-level variables (i.e., not declared inside a loop) in the transformed parameters block are global variables. the downside is that this increases the size of the output.csv file.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [September 6, 2023, 1:44pm UTC](https://discourse.mc-stan.org/t/can-a-generated-quantity-be-used-in-the-model-block/32673/3 "2023-09-06T13:44:27Z")

</div>

> [@sanjib](#):
>
> I can then generate the mixture component indicator in the generated quantities block from its posterior predictive distribution…
> 
> I would then like to use the generated mixture component indicator (from the posterior predictive distribution) to describe a separate part of the model.

I assume the mixture component indicator is generated via random number generation. This is disallowed in the model and transformed blocks because the resulting discrete parameter breaks the gradients that Hamiltonian Monte Carlo relies on to work its magic. To fit this model in Stan, you need to marginalize over the mixture component indicator in any downstream parts of the likelihood that depend on the indicator.
