Generate random number in transformed parameters block

Hello Stan users,

I know that generating random numbers in transformed parameters are prohibited in Stan, and somehow it will not be a full Bayesian model anymore because of ‘restricting flow of information’. But I do not see what is wrong with the following approach.

vector[n] mu;
vector[m] noise;

transformed parameters{
vector[m] rv;
vector[m] rv_mean;
vector[m] rv_var;
rv_mean = f(mu);
rv_var = f(mu);
rv = rv_mean + noise;

noise ~ normal(0, sqrt(rv_var));
data ~ poisson_log(f(rv));

I do not have a fixed model yet, hence the abstract code. f() are some function, and m>n.
My question is:

  1. would the variable rv then follow normal(rv_mean, sqrt(rv_var))?
  2. would data inform parameter mu?
  3. Is the evaluation equivalent to putting rv ~ normal(rv_mean, sqrt(rv_var)); in the model block directly?

Thanks. Craig

Yes, conditionally. Although it would be better to define rv = rv_mean + sqrt(rv_var) * noise and put a standard normal prior on noise.


Equivalent, no, because it changes the geometry and thus all aspects of the computation. It does not, however, change the marginal distribution of mu or whatever is of interest.

Thanks Ben.

Yes of course :)

That could explain the time difference, I should review my stan knowledge again.