stan icon indicating copy to clipboard operation
stan copied to clipboard

Merge Transformed Parameters and Model Blocks

Open VMatthijs opened this issue 7 years ago • 1 comments

Summary:

The point of this feature request is to merge the transformed parameters and model blocks, as the added complexity of having them be separate entities seems unnecessary.

Description:

The idea will be to make the following changes to the language:

  • Make global variables in model block be in scope in generated quantities block, as would be the case for transformed parameters;
  • Write them to your sample, as you would for transformed parameters;
  • At this point, there is not more distinction between the transformed parameters and model blocks except that target is in scope in model but not transformed parameters, so we might as well merge the two blocks.

Observe that the current behaviour can be reproduced by using a local scope, to ensure that variables do not get written to the sample or are in scope in generated quantities.

Reproducible Steps:

Run

model { real x = 1; } generated quantities { print(x); }

Current Output:

This throws an error as x is not in scope in generated quantities. Nor does it get written to the sample. This is confusing.

Expected Output:

No error. A program that prints 1 in the generated quantities phase and writes 1 to the sample.

Additional Information:

People I have spoken to have been confused about the distinction between transformed parameters and model and have been confused that model level global vars are not in scope in the generated quantities block.

The scoping mechanism should moreover control whether a variable ends up in the sample.

Current Version:

v2.18.0

VMatthijs avatar Oct 10 '18 17:10 VMatthijs

This doesn't quite match the current (2.18) syntax, in which local variables in the model block may not have constraints.

It also doesn't match the semantics, in that constraints on transformed parameters are validated at the end of the block and a rejection occurs if they fail. Arguably, we shouldn't have constraints on transformed parameters---it's up to the program to define them (same for transformed data).

In terms of the existing implementation, the model class write_array() method does not need to execute the model block to produce the generated quantities at the end of an iteration---it only needs to produce the transformed parameters. If these were mixed, there'd be no way to do this without executing the entire model block.

bob-carpenter avatar Oct 11 '18 03:10 bob-carpenter