Feature request: if statements in parameter declarations


#1

I am setting up some sort of simulation study here, and it would be very convenient to be able to specify an ‘if x, declare parameter y’ type statement. The alternative seems to be to code the if statements in R, which will require re-compiling for every condition, or to sample parameters that are pointless under some conditions. Any alternative approaches would be very welcome!


#2
parameters {
  real theta[condition ? size_if_true : 0];
}

We do this a lot in the rstanarm package.


#3

great - I assume then that all object types can have dimensions zero…


#4

If not, it is a bug in Stan, although 2.16.x actually has such a bug with simplex vectors of size zero.


#5

As Bob notes in the the GitHub issue a simplex of size zero doesn’t make any sense as it can’t satisfy the sum-to-one constraint so it is unlikely that we will support that in the near future. All of the other containers, however, should support size 0.


#6

The bug with simplexes was just failure to catch higher up. It’s fixed in the develop branch.

I’m not sure about the covariance and correlation matrices or Cholesky factors; those might not allow size zero (though they should).


#7

We’ve been thinking more lately about how to allow exploration of bunches of models.

We should put Ben’s trick in the manual for now:

https://github.com/stan-dev/stan/issues/2336#issuecomment-319441156

It’s possible at least in theory to support these conditionals but it would have to be with the condition that only data could be involved in the conditionals—otherwise, the number of variables could vary iteration to iteration, which we can’t allow.


#8

Yeah, it would be a pleasant addition, as (from a user standpoint) there’s no obvious reason why I couldn’t put an if statement referencing data in there.