PyStan: Way to save individual fits of STAN models?

772 views
Skip to first unread message

Evan Warfel

unread,
Nov 3, 2014, 7:51:09 PM11/3/14
to stan-...@googlegroups.com
Hello,


I'm using pystan, and I'm wondering if there is a way to save the output from a pystan.stan( ) call?

I see ways to pickle the model itself, but I'm fitting the same model to each individual in my dataset - does pystan have a method to save the mcmc trace and the sampled parameter values in, say, a .csv file?


Thanks,
Evan

Allen Riddell

unread,
Nov 4, 2014, 6:34:04 AM11/4/14
to stan-...@googlegroups.com
Hi Evan,

In PyStan 2.5.0.0 you can pickle the entire fit object which is probably the
easiest way to accomplish what you're after.

The other strategy I've encountered is to pickle the output of the extract()
method or save it to a numpy savez file.

Something along the lines of:

extr = fit.extract()
pickle.dump(extr, open("/tmp/fit.pkl", 'wb'))

I think the following would also work:

numpy.savez('/tmp/fit.npz', extr)

Best wishes,

Allen
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nathan Lemoine

unread,
Nov 4, 2014, 2:42:40 PM11/4/14
to stan-...@googlegroups.com
I don't know about saving the MCMC trace plots, but I usually save all my posterior samples in a csv by putting them into a pandas data frame and the using to_csv().

allnut_regress_fit = regressMod.sampling(data = regressData, iter = 50000, chains = 4)
aChains = allnut_regress_fit.extract(permuted = True)['alpha']
aChains = pd.DataFrame({'Coral-Zoox' : aChains[:,0],
                        'Lichen' : aChains[:,1],
                        'Clam-Zoox' : aChains[:,2],
                        'Plant-Mych' : aChains[:,3],
                        'Plant-Rhiz' : aChains[:,4]})
aChains.to_csv(file_path_here)

Then I can read the csv back in and use pandas to make density and trace plots without running the model again.

Allen Riddell

unread,
Nov 5, 2014, 9:20:54 AM11/5/14
to stan-...@googlegroups.com
Hi,

It seems that pickling the entire fit object is broken. Right now the
recommended strategy is to pickle the output of `extract`, as in

extr = fit.extract()
pickle.dump(extr, open("/tmp/fit-extract.pkl", 'wb'))

I'll make a release of PyStan when this is fixed.

Best,

-a
Reply all
Reply to author
Forward
0 new messages