I am using pystan to produce samples from the posterior predictive distribution on new data, given a set of parameter samples that were produced by a fit to some training data.

The idea is to store the parameter samples from MCMC in a python object and then pass them to a different piece of stan code in the data block, alongside the new data. The samples from the posterior predictive distribution are then created in the generated quantities block. This works fine, except `fit.extract()`

takes a very long time for larger samples.

As a simple example, take the following stan code

```
data {
int<lower=0> n;
}
generated quantities {
vector[n] theta;
for (j in 1:n) theta[j] = normal_rng(0., 1.);
}
```

I run stan in `Fixed_param`

mode and then do `fit.extract()`

. For reference, I attach a graph of the time it takes to do the sampling in stan, and how long `fit.extract()`

takes, as a function of the length of the generated vector (i.e. `n`

in the above stan code). When the vector is ~ 100,000 elements long, the sampling still only takes 0.2s or so, but extract takes a few hundred seconds.

I wanted to post in this forum before I dug into the problem further - am I doing something wrong / is this behaviour expected?

pystan_timings.pdf (13.2 KB)