Stan samples from prior predictive distribution the same number of samples as posterior draws. How restrict it to only one?

Hi everyone,

I am working on an unpoolded model in which I am drawing 4000 samples from the posterior distribution of the parameters of the individual models. There are a total of 6 modelling distributions.

I want to now sample new data from a new event. In this case I need to sample once from the prior distribution ( which would be my new event) and now condition on this parameter I want to sample N times from the sampling distribution.

To do so I have:

generated quantities {
  
  real mu_7    = normal_rng(0,1);
  real sigma_7 = inv_chi_square_rng(10);
  real y_7     = normal_rng( mu_7, sigma_7 );

}

With this I have observed that mu_7 is sampled 4000 times and for each of this sample one y_7 is sampled. I have been working on other alternatives such as:

  real mu_7    = normal_rng(0,1);
  real sigma_7 = inv_chi_square_rng(10);

  array[4000] real y_7;
  for (n in 1:4000){
    y_7[n] = normal_rng( mu_7, sigma_7 ); 
  }

However this creates 4000 y_7 variables each one with 4000 samples. How can I just sample once from the prior distribution and then N times from the sampling distribution?

Thank you.

I think you need to do this in parts.

First the the prior with gq and then give the gq result as a data. Maybe there are ways to handle this with transformed data block?

Hi thanks from the reply,

from what I have seen the gq block is executed as many times as the number of posterior samples drawn, so I think I have to do this outside Stan’s code. I’ve found related thread in this forum which I will post in this answer since I closed it already.

Yes but you would not need to run sampling against hmc but use fixed_parans options