stan-dev / docs

Documentation for the Stan language and CmdStan
https://mc-stan.org/docs/
Other
37 stars 107 forks source link

Document that non-data variables are not allowed in top level size declarations - consequences for `generated quantities` block #774

Closed mitzimorris closed 3 months ago

mitzimorris commented 3 months ago

Summary:

The Stan Reference Manual doesn't mention that the Stan compiler doesn't allow this:

generated quantities {
  int N = 10;
  array[N] int foo;
}

The compiler generates error message:

Semantic error in '/Users/mitzi/github/zmorris/epistan/general/foo.stan', line 3, column 8 to column 9:
   -------------------------------------------------
     1:  generated quantities {
     2:    int N = 10;
     3:    array[N] int foo;
                 ^
     4:  }
   -------------------------------------------------

Non-data variables are not allowed in top level size declarations.

Document, explain why.

WardBrian commented 3 months ago

The wording of the issue isn't clear to me if you're asking someone else to document this or if it's just a TODO item for yourself, so forgive me if you already know, but I think the simple reason is "we don't ever want the possibility of things changing sizes between iterations"

mitzimorris commented 3 months ago

right - I think this should go in the Generated Quantities block section of the Reference Manual and I will add it.

WardBrian commented 3 months ago

Note that this is also important in e.g. transformed parameters

parameters {
  array[3] real y;

}
transformed parameters {
  corr_matrix[size(y)] z;
}

generates the same error, since size(y) references y. Arguably we should make an exception for the size family of functions, but at the moment we do not.

There is also a related, but slightly different error, for directly using an _rng in a decl:

Semantic error in 'foo.stan', line 2, column 9 to column 26:
   -------------------------------------------------
     1:  transformed data {
     2:     array[binomial_rng(3,3)] int x;
                  ^
     3:  }
     4:  
   -------------------------------------------------

Random number generators are not allowed in top level size declarations.

This is fine, though:

transformed data {
   int N = binomial_rng(3,3);
   array[N] int x;
}
bob-carpenter commented 3 months ago

I don't see any reason why we have to disallow

array[binomial_rng(3, 0.3)] int<lower=0, upper=3> x;

binomial_rng(3, 3) would throw a runtime error because the second argument needs to be a probability.

WardBrian commented 3 months ago

We could allow that in transformed data, but not generated quantities. My guess is it was just easier to disallow everywhere

bob-carpenter commented 3 months ago

We could allow that in transformed data, but not generated quantities.

Good point. I was only thinking about transformed data given the example.