Is there a way to only write some results to disk in cmdstanpy / cmdstanr?

Is there a way to save a subset of parameters using cmdstanpy / cmdstanr? Sometimes, it’s nice to not have something be locally scoped, but not necessarily need it in the output (i.e., a big matrix defined in the transformed parameters block that’s needed in the model/gq block, but not wanted in the output).

In rstan, this is available via the pars argument, but not sure what the equivalent would be in cmdstanpy / cmdstanr

Yeah this would be nice. Unfortunately CmdStan still does not support this. There’s been an open issue on GitHub for a while now

and various developers (@WardBrian @Bob_Carpenter @mitzimorris and others) have also expressed an interest in this. There’s also been some discussion of adding annotations to the Stan language so that which parameters to save could be indicated directly inside a Stan program instead of via the interface.

1 Like

Thanks for pointing me to the issue in gh! I should have done a more-than-cursory-look for similar topics here ahead of posting.

Using annotations/decorators within the Stan program itself is conceptually appealing from the user level, since it universalizes the solution regardless of interface. From poking around, it looks like @gpu is potentially the first decorator to be soft-released for feedback. I’m not super familiar with opencl / gpu usage, but I’ll keep an eye out to see if that leads to the @silent decorator down the line.

Thanks again!

For this case, you can move the declaration and definitions from the transformed parameter block to the model block. The only difference between declaring in the model or transformed parameters is whether they will get printed. If you need local variables in the transformed parameters that aren’t saved, you can put them in their own block. For example, the following program uses a to compute b but saves both to output.

transformed parameters {
  real a;
  real b;
  a = 5;
  b = a^2;
}

If you only want to save b, you can declare a in a locally scoped block as follows.

transformed parameters {
  real b;
  {
    real a;  // a is local, so won't be saved
    a = 5;
    b = a^2;
  }
}

Sometimes you want a both in the transformed parameters and in the model or generated quantities, but you don’t want to save it. In that case, you can redefine it as a local variable. It may not be the most efficient approach, but if there’s not a lot involved in comuting a, it can be fine. Especially in generated quantities.

2 Likes