Hi,
Yes, one should save the model with the fit. (Model is actually a python module and you need to load it for the fit)
There are a couple of ways.
I recommend that you split model compilation and fitting.
stan_model = pystan.StanModel(file=model_path)
outputs = []
for ...:
fit = stan_model.sampling(data=...)
outputs.append(fit)
# you can use compression too
import gzip
with gzip.open(...pickle.gz, "wb") as f:
pickle.dump({"model" : stan_model, "outputs" : outputs})
This was assuming you use the same model for each fit; if not true, compile new model in a for loop and move model + fit together
.append([model, output])
And you can then just pickle the output list.
There are a couple of different ways to save the fit info too: arviz.InferenceData == NetCDF
or pandas.DataFrame
import arviz as az
idata = az.from_pystan(fit)
idata.to_netcdf(path)
# latest summary info (updated rhat + ess)
az.summary(idata) # or az.summary(fit)
# plotting
az.plot_pair(idata)
https://arviz-devs.github.io/arviz/
# pandas
dataframe = fit.to_dataframe()
dataframe.to_csv(path)
To load data from old fits that don’t have the model saved, see https://pystan.readthedocs.io/en/latest/unpickling_fit_without_model.html
I recommend that you then save these values with arviz, because the model is not really related to the fit, so any additional calculation could be wrong.