Saving Sampling Data of PyMC3 Using the Pickle Library
Sampling data of PyMC3 can be saved using the pickle library. The saved sampling data can be read out, and be used to analyze in various ways. This website presents sample scripts in Listings 1 and 2. These scripts execute Bayesian analysis of a binomial distribution. The first script in Listing 1 runs sampling by PyMC3 and saves the sampling data using the pickle module. It also shows some results obtained by analyzing the sampling data. The script in Listing 2 reads out the saved sampling data, and analyze it in the same way as the script in Listing1. The results by the script in Listing 2 is exactly the same as those by that in Listing 1.
The scripts were run in Ubuntu. When you execute script in PyMC3 in Windows, it will run in Jupyter Notebook. Jupyter Notebook is easy to install. How to install Jupyter Notebook and How to use PyMC3 in Jupyter Notebook are explained in this website.
The script in Listing 1 is this:
Listing 1 The script, which saves the sampling data into a file.
import matplotlib.pyplot as plt
import pymc3 as pm
import pickle
import arviz as az
N = 10
k = 7
with pm.Model() as BinModel:
theta
= pm.Uniform('theta', lower = 0.0, upper = 1.0)
k_obs
= pm.Binomial('k_obs', n = N, p = theta, observed = k)
trace
= pm.sample()
with
open('sample.pkl', 'wb') as f: # The file name is arbitrary
pickle.dump(trace,
f)
pm.traceplot(trace)
plt.show()
smry
= pm.summary(trace)
print(smry)
v_waic = az.waic(trace)
print('\nWAIC statistics...\n',
v_waic)
The sampling data is saved in the file
named 'sample.pkl'
by this code.
with open('sample.pkl', 'wb') as f:
pickle.dump(trace,
f)
The file name is arbitrary.
Traceplot of the sampling is shown in Figure 1.
Figure 1
After the execution of the script ends, you can see the console display as shown in Figure 2.
Figure 2
Summary of the MCMC sampling and WAIC index are shown.
After saving the sampling data generated executing the script in Listing 1, run the script in Listing 2.
Listing 2 A script, which will be executed after saving the sampling data by that in Listing 1.
import matplotlib.pyplot as plt
import pymc3 as pm
import pickle
import arviz as az
N = 10
k = 7
with pm.Model() as BinModel:
theta
= pm.Uniform('theta', lower = 0.0, upper = 1.0)
k_obs
= pm.Binomial('k_obs', n = N, p = theta, observed = k)
#trace
= pm.sample()
#with
open('sample.pkl', 'wb') as f:
# pickle.dump(trace,
f)
trace
= pickle.load(open('sample.pkl', 'rb'))
pm.traceplot(trace)
plt.show()
smry
= pm.summary(trace)
print(smry)
v_waic = az.waic(trace)
print('\nWAIC statistics...\n',
v_waic)
This script is the same as that in Listing 1, except that it reads out the sampling data from the file, instead of doing MCMC sampling. The sampling data is read out by this code.
trace = pickle.load(open('sample.pkl', 'rb'))
As in the script in Listing 1, traceplot is displayed as in Figure 3. This traceplot is identical to that in Figure 1.
Figure 3
After execution of the script in Linsting 2, we can see the output displayed in the console terminal (Figure 4).
Figure 4
Although the sampling process is not shown (MCMC sampling is not executed, but the sampling data is read in from the file), the results of analyses of the sampling data read in from the file are exactly the same as those produced by the script in Listing 1 (see Figure 2).
When we develop a program to analyze the data by Bayesian method, we will repeat the same MCMC sampling to brush up representation of results of the analyses. If each sampling takes long time, this repetition would be tedious. However, the sampling data is saved in a file, then we can avoid this repetition of MCMC sampling.