Answers inline.
On 3/9/13 5:11 PM, Rudolf Cardinal wrote:
> Dear all,
>
> I've just come across Stan and am very impressed; thanks to all those who have developed it!
>
> I'm trying to use it for reinforcement learning models in which some information is stored locally (in the "model"
> block) and used to calculate things on a trial-by-trial, cumulative basis. Stan seems potentially ideal for this, given
> that you can do imperative coding within the "model" block.
>
> The Stan manual (v1.2.0) says (under "Variable scope") that "*The variables declared in each block have scope over all
> subsequent statements.* Thus a variable declared in the transformed data block may be used in the model block. But a
> variable declared in the generated quantities block may not be used in any earlier block, including the model block."
>
> [There's one qualification I've noticed: variables in an earlier block are read-only for later blocks (with the
> exception of parameter sampling statements, which I'm aware are not the same sort of thing as conventional assignments).]
Right -- variables can only be assigned to (show up on the
left side of an "<-") in the block in which they are declared.
> However, *variables declared in the "model" block (even at its top level) are not in scope in the "generated quantities"
> block.* Accordingly, if I want to retrieve values of my calculated probabilities of choosing the better stimulus on each
> trial ("p_best" in the skeleton code below), I can't retrieve it from the "model" block, and if I declare it in an
> earlier block, I can't write to it from the "model" block -- so my only option is to recalculate it within the
> "generated quantities" block -- which is somewhat inefficient since I had to calculate it anyway as part of the model.
> (Not as inefficient as it could be, as I understand that the "generated quantities" block is executed less often than
> the "model" block -- but it's still noticeable.)
I'll try to clarify in the manual.
The variables in the model block are all local variables, which only persist
in the block in which they are defined.
To define a variable you need in the model block, use the
transformed parameter block.
> *Could "model" variables be put in read-only scope for the "generated quantities" block* (clearly, with the caveat that
> only variables declared in the "generated quantities" should be reported back to e.g. RStan or the final output, or
> there'd be too much junk coming back in many cases -- but a simple copy would suffice, e.g.: *generated quantities {
> real my_copy; my_copy <- my_model_var; }*)?
The could be, but they're not going to be :-)
> Or is there some other obvious way of retrieving posterior distributions of things calculated within the model block
> that I've not seen?
Yes, put them in the transformed parameter block.
> Simplest code to reproduce is:
>
> model {
> real modelthing;
> modelthing <- 1.0;
> }
> generated quantities {
> real thing;
> thing <- modelthing;
> }
>
> ... generates error:
> thing <- modelthing;
> ^-- here
> DIAGNOSTIC(S) FROM PARSER:
> variable "modelthing" does not exist.
>
> More realistic skeleton code below.
In this case, you'd do this:
transformed parameters {
real modelthing;
modelthing <- 1.0;
}
Then modelthing is available in the model and will
also be printed.
If the variable being defined does not depend on any parameters (implicitly,
this includes other transformed parameters or local variables), as
in this example, then it's more efficient to define it in the transformed data block.
- Bob