Simple question: correct way to save traces?

I am having issues restoring PyMC3 cross-platform results using pickle. Which is, at this time, the recommended strategy to save/load pymc3 results? (May there be an example in one of the tutorials?)

I personally recommend saving the samples in netCDF format using az.InferenceData, which in addition to the to_netcdf also has a to_zarr method available. And storing the model code instead of pickling the model, you can then recompile it and use it for posterior predictive sampling for example. As far as I know, recompiling the model is the only way to not have any assumptions on the data or data shape with pymc3<4.0 (incoming next major release). Functions that recompile the model based on some input data are generally called model factories, there are some examples in this PyMCon talk: Posterior Predictive Sampling in PyMC3 by Luciano Paz and you can also search for “model factory” to find other related Discourse topics.

As a general endorsed or recommended approach, the only thing I have clear is that it should not involve pm.MultiTrace

1 Like

Hi @OriolAbril, @AlexAndorra ,
Good day.
I saved my model and trace using to_netcdf.

In a new script or I called it my prediction script,
I import the using from_netcdf and I wish to perform sample_posterior_predictive.
However, I encountered some errors.
Is model.posterior is equivalent to trace in this case?

from arviz import from_netcdf
model = from_netcdf(‘’)

trace = model.posterior

import pymc3 as pm
import numpy as np

data = np.random.randn(20)

with pm.Model() as abc_model:
mu = pm.Normal(‘mu’, 0, 1)
sigma = 1.
returns = pm.Normal(‘returns’, mu=mu, sd=sigma, observed=data)

step = pm.Metropolis()

with abc_model:
mu_sim = sample_posterior_predictive(trace, model=abc_model, samples=500)

But I get error of keyerror is 0.
May I know why?
Thank you very much

This way works for me :

saving trace

directory= r"c:\Users\xxx\Documents\xxx\traces\trace_nb")

loading saved traces

with model_nb:
t_nb = pm.load_trace(