Selective write/save

Is there a way to selectively write or save variables?

I am using cmdstanr.

I have a large dimensional variable shared between the “model” block and the “generated quantities” block. Based on my understanding, I need to declare such a variable in the “parameters” block (for it to be shared between “model” and “generated quantities” blocks), and a variable declared in “parameters” block is written (saved).

My interest is not in this large dimensional variable, but quantities computed in “generated quantities” using this variable. I am trying to see how I can avoid writing/saving this large dimensional variable.

I am not sure if I can move the “generated quantities” computation to the “model” block (and then the large dimensional variable can be declared as local to model block). My understanding is that quantities in model block may get computed in each leapfrog step (rather than once in every iteration as is the case for generated quantities) - thus this may significantly slow down computation.

Thank you

1 Like

You have two options:

  1. Define & compute the intermediate quantity in the transformed parameters block, for use then in the model block & generated quantities block.

    • Computation: least redundant
    • Storage: most writing
  2. Define & compute the intermediate quantity as a local variable (i.e. its definition and use is encapsulated by a { } set) in both the model block & generated quantities block

    • Computation: more redundant
    • Storage: least writing

But note the amount of compute dedicated to the intermediate quantity is not 2x in the latter option as one might naively expect because the code in the TP/model blocks actually gets run many times per sample while the code in the GQ block only gets run once. So I recommend you go the 2 route.

Note also that there’s a variant of 2 whereby you remove the GQ block altogether, sample the model with the intermediate variable defined locally in the model block, then when you’re happy with the sampling diagnostics, pass the posterior samples as input to a separate run of the $generate_quantities method.