# Is it possible to make Generated Quantities runtime-optional?

**URL:** <https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156>\
**Category:** General\
**Created:** [June 22, 2020, 8:40pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156 "2020-06-22T20:40:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![8one6](https://avatars.discourse-cdn.com/v4/letter/8/ea5d25/32.png) [@8one6](https://discourse.mc-stan.org/u/8one6)\
**Post date:** [June 22, 2020, 8:40pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/1 "2020-06-22T20:40:07Z")

</div>

Simple linear regression with predictions looks like this:

```stan
data {
    int<lower=1> D;

    int<lower=1> N;
    matrix[N, D] X;
    vector[N] Y;

    int<lower=1> N_pred;
    matrix[N_pred, D] X_pred;
}

parameters {
    real a;
    vector[D] b;
    real<lower=1> s;
}

model {
    vector[N] mu = (X * b) + a;
    Y ~ normal(mu, s);
}

generated quantities {
    vector[N_pred] Y_pred;
    {
        vector[N_pred] mu_pred = (X_pred * b) + a;
        Y_pred = normal_rng(mu_pred, s);
    }
}

```

Sometimes, though, after compiling the model specified by this code, I just want to fit some parameters, and don’t want to generate any posterior predictive distributions. But because it’s been specified as above, if I don’t pass `N_pred` and `X_pred` variables to STAN at runtime, it yells at me (totally fairly!). I usually get around this by just passing in data which has `N_pred` set equal to `N` and `X_pred` equal to `X` but that seems really dumb and a waste of compute cycles when I don’t care about that stuff at all.

Is there any way to code this model so that, at sampling time, after compilation is long-since-finished, I have the option to either pass in `N_pred` and `X_pred` and have the `generated quantities` code run as written _or_ pass in neither of those pieces of data and have the sampling process completely skip the generated quantities section entirely? I’d be totally fine if to achieve that goal I was required to have a variable in the `data` block called `should_i_generate_quantities` that I would need to be set to `1` when I want to give both extra bits of data and run the last block and be set to `0` when I don’t want to bother with anything related to the last block.

Thanks!

---

<div class="post-metadata">

**Author:** ![erognli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erognli/32/5181_2.png) [@erognli](https://discourse.mc-stan.org/u/erognli)\
**Post date:** [June 23, 2020, 5:52am UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/2 "2020-06-23T05:52:42Z")

</div>

You pretty much answer your own question - enclose the generated quantities in an if-statement, and include a control variable in the data block. But don’t expect that much of a speedup. The generated quantities block is computationally cheap because it is only executed once per iteration, ulike all the stuff going on in the model block. Still, it will reduce the size of the fitted model object, and may save some time by reducing the amount of diagnostics.

---

<div class="post-metadata">

**Author:** ![ssp3nc3r](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ssp3nc3r/32/120_2.png) [@ssp3nc3r](https://discourse.mc-stan.org/u/ssp3nc3r)\
**Post date:** [June 23, 2020, 1:38pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/3 "2020-06-23T13:38:19Z")

</div>

Enclosing code inside generated quantities with an if statement can avoid the computation but you still need to pass in variables for that prediction (even if dummy or repeating data used for fitting) .

Another approach, if using RStan, is to put prediction code into a Stan function, expose the function in R, and call the function, passing it new data and fitted parameters. If not using RStan, you can put the prediction code in a separate file in generated quantities (omit the parameter and model blocks) and do the same.

---

<div class="post-metadata">

**Author:** ![8one6](https://avatars.discourse-cdn.com/v4/letter/8/ea5d25/32.png) [@8one6](https://discourse.mc-stan.org/u/8one6)\
**Post date:** [June 23, 2020, 1:59pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/4 "2020-06-23T13:59:11Z")

</div>

Thanks for that reply! I’m on pyStan, does the same option exist there?

Either way, a follow up question: How does STAN respond, in general, to degenerate data structures? I.e. in the above setup could `N_pred` be `0` and if so, what data structure would you need to feed in to `X_pred` to keep the program happy?

---

<div class="post-metadata">

**Author:** ![ssp3nc3r](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ssp3nc3r/32/120_2.png) [@ssp3nc3r](https://discourse.mc-stan.org/u/ssp3nc3r)\
**Post date:** [June 23, 2020, 5:28pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/5 "2020-06-23T17:28:17Z")

</div>

You can pass in `N_pred` as `0` and `X_pred` as a `[0,]` matrix, and it’s easy to test; _e.g._:

```
data {
  int<lower=0> N_pred;
  matrix[N_pred, 1] X_pred;
}

generated quantities {
  print(X_pred);
}

```

In R (I don’t have python setup for Stan at the moment):

`f <- rstan::stan(file = "test.stan", data = list(N_pred = 0, X_pred = matrix(1, nrow = 0, ncol = 1)), algorithm = "Fixed_param", iter = 1, chains = 1)`

---

<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:** [June 23, 2020, 7:27pm UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/6 "2020-06-23T19:27:57Z")

</div>

> [@erognli](#):
>
> The generated quantities block is computationally cheap because it is only executed once per iteration, ulike all the stuff going on in the model block

it’s also cheap because it doesn’t have to compute any gradients, unlike work done in the transformed parameter and model block.

> [@erognli](#):
>
> Still, it will reduce the size of the fitted model object,

this is a good point.

you could try running CmdStanPy which is going to be more memory efficient.  
but it sounds like you want to have a pair of models where the 2nd model is run using  
the `generate_quantities` method - [https://cmdstanpy.readthedocs.io/en/latest/generate\_quantities.html](https://cmdstanpy.readthedocs.io/en/latest/generate_quantities.html)

---

<div class="post-metadata">

**Author:** ![erognli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erognli/32/5181_2.png) [@erognli](https://discourse.mc-stan.org/u/erognli)\
**Post date:** [June 24, 2020, 9:29am UTC](https://discourse.mc-stan.org/t/is-it-possible-to-make-generated-quantities-runtime-optional/16156/7 "2020-06-24T09:29:13Z")

</div>

> [@mitzimorris](#):
>
> it’s also cheap because it doesn’t have to compute any gradients, unlike work done in the transformed parameter and model block.

Ah, yes. That’s actually what I meant by my _very_ much less precise “stuff going on” in the model block 😊. Or what I was thinking about. Thanks for making it clearer, anyway!
