Transformed parameters with target +=



I am fitting a model where, conceptually, the following is happening: a parameter A needs to be transformed to B via a transformation that is data-dependent. I only care about B, and the transformation is a necessity to avoid rejecting parameter values (unconstrained B makes no sense in my application).

The transformation involves vectors, and is somewhat complex (about 20 lines of Stan code). It also needs to be done about 1500 times, and I would like to record the bs. Finally, of course it involves a Jacobian correction, which is the part of the problem.

Ideally, I would encode it in the transformed parameters block, which would make sure the bs are recorded. But I get

Sampling statements (~) and increment_log_prob() are
only allowed in the model block or lp functions.

However, if I encode in in a function with an _lp name, it compiles.

I am asking to check that the target += statement is executed in a function called in the transformed parameters block.

Cf #889 which is about this, but was closed (without resolution).


It should be executed. I believe the _lp functions were originally included precisely for this use-case.

This also relates the following issue: . I am unsure whether the distinction between the transformed parameters and model blocks is a useful one in the first place.