Yasuharu Okamoto, 2017.07

Pickle and Stan

 

To explain how to use pickle package of Python, simple examples of using pickle with Stan are presented. By saving a Stan model and sampling results by Stan, we can reduce the time to develop Python script.

For explanation, a simple Stan script is prepared as follows.

 

=================   sample.stan   ==============

data {

    int<lower = 2> N;

    real X[N];

}

 

parameters {

    real mu;

    real<lower = 0.0> sigma;

}

 

model {

    for (i in 1:N)

        X[i] ~ normal(mu, sigma);

}

============================================

 

When we do not use pickle, a Python script , which uses the above Stan script, is this.

 

======================================================

import pystan;

 

X = [3, 7, 5]

N = 3

Data = {'N': N, 'X': X}

fit = pystan.stan(file = 'sample.stan', data = Data, seed = 123, n_jobs = 1)

print(fit)

=======================================================

 

Run this script, then we have the results in Figure 1.

 

Figure 1

 

Now, letfs use pickle as follows.

 

=========================================

import pystan;

from pystan import StanModel

import pickle

 

X = [3, 7, 5]

N = 3

Data = {'N': N, 'X': X}

sm = StanModel(file = 'sample.stan')

with open('model.pkl', 'wb') as f:

    pickle.dump(sm, f)

 

fit = sm.sampling(data = Data, seed = 123, n_jobs = 1)

with open('fit.pkl', 'wb') as g:

    pickle.dump(fit, g)

 

print(fit)

==========================================

 

The script, which builds code named gsmh by StanModel and saves the code in a file named gmodel.pklh, is this.

 

sm = StanModel(file = 'sample.stan')

with open('model.pkl', 'wb') as f:

    pickle.dump(sm, f)

 

The following script does sampling using model sm and saves the results named gfith in a file named gfit.pklh.

 

fit = sm.sampling(data = Data, seed = 123, n_jobs = 1)

with open('fit.pkl', 'wb') as g:

    pickle.dump(fit, g)

 

The content of fit is printed out by this script.

 

print(fit)

 

Of course, the printed out results (Figure 2) is the same as that in Figure 1.

 

Figure 2

 

Now, the code of the Stan model is saved in the file gmodel.pklh and the sampling results is saved in the file gfit.pklh. You can use the saved model and get sampling by loading the model. An example script is this.

 

===========================================

import pystan;

from pystan import StanModel

import pickle

 

X = [3, 7, 5]

N = 3

Data = {'N': N, 'X': X}

sm1 = pickle.load(open('model.pkl', 'rb'))

fit = sm1.sampling(data = Data, seed = 123, n_jobs = 1)

 

print(fit)

============================================

 

Run this script, you get the following (Figure 3).

 

Figure 3

 

If you want the sampling results saved in the file gfit.pklh, run this script.

 

==================================

import pystan;

from pystan import StanModel

import pickle

 

sm2 = pickle.load(open('model.pkl', 'rb'))

fit = pickle.load(open('fit.pkl', 'rb'))

                 

print(fit)

==================================

 

Notice that the Stan model is loaded before the sampling results is loaded.

The loaded sampling results is printed out by function print (Figure 4). We can see the same results as that in Figure 3.

 

Figure 4

 

The saved model can be used to get sampling for other data.

This script loads the saved model gmodel.pklh and executes sampling by the model.

 

==========================================

import pystan;

from pystan import StanModel

import pickle

 

X = [30, 70, 50]

N = 3

Data = {'N': N, 'X': X}

sm3 = pickle.load(open('model.pkl', 'rb'))

fit = sm3.sampling(data = Data, seed = 123, n_jobs = 1)

 

print(fit)

===========================================

 

The sampling results is printed out by function print (Figure 5).

 

Figure 5

 

 

Home