https://github.com/johannesbuchner/cmdstancache
Caches Stan MCMC for quicker model iterations and enhanced productivity
https://github.com/johannesbuchner/cmdstancache
Last synced: 3 months ago
JSON representation
Caches Stan MCMC for quicker model iterations and enhanced productivity
- Host: GitHub
- URL: https://github.com/johannesbuchner/cmdstancache
- Owner: JohannesBuchner
- License: gpl-3.0
- Created: 2023-01-28T08:50:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-28T09:04:07.000Z (over 1 year ago)
- Last Synced: 2024-11-09T09:40:51.053Z (11 months ago)
- Language: Python
- Size: 68.4 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
CmdStanCache
=============Quicker model iterations and enhanced productivity for Stan MCMC by
* caching model compilation in a smart way
* caching sampling results in a smart wayNo waiting for the resampling the same model with the same data.
Install
-------First install `CmdStanPy `_ and
CmdStan and make sure it works.::
$ pip install cmdstancache
Usage
-----
::model = """
data {
int N;
}
parameters {
real x[N];
}
model {
for (i in 1:N-1) {
target += -2 * (100 * square(x[i+1] - square(x[i])) + square(1 - x[i]));
}
}
"""
data = dict(N=2)import cmdstancache
stan_variables, method_variables = cmdstancache.run_stan(
model,
data=data,
# any other sample() parameters go here
seed=42
)**Now comes the trick**:
* If you run this code twice, the second time the stored result is read.
* If you add or modify a code comment, the same result is returned without having to rerun.
.. image:: https://coveralls.io/repos/github/JohannesBuchner/CmdStanCache/badge.svg?branch=main
:target: https://coveralls.io/github/JohannesBuchner/CmdStanCache?branch=main
.. image:: https://github.com/JohannesBuchner/CmdStanCache/actions/workflows/testing.yml/badge.svg
:target: https://github.com/JohannesBuchner/CmdStanCache/actions/workflows/testing.yml
.. image:: https://img.shields.io/pypi/v/cmdstancache.svg
:target: https://pypi.python.org/pypi/cmdstancacheHow it works
-------------cmdstancache keeps a cache of code and data that has previously been used for MCMC sampling.
If it already has the results, it returns it from the cache.Here are the details:
1. The code is normalised (stripped of comments and indents)
2. A hash of the normalised code is computed
3. The model code is stored in ~/.stan_cache/.stan
4. The model is compiled, if it is not already there
5. The data are sorted by key, exported to json, and a hash computed
6. The data are stored in ~/.stan_cache/.json
7. cmdstanpy MCMC is run with code=.stan and data=.json
8. fit.stan_variables() and fit.method_variables() are returned
9. joblib memoizes steps 7 and 8, avoiding resampling when the same data and code hash are seen.Plotting
--------Make a quick corner plots of only the scalar model variables::
cmdstancache.plot_corner(stan_variables)
In case some chains are stuck, and you want to remove their samples for plotting::
cleaned_variables = remove_stuck_chains(stan_variables, method_variables)
plot = plot_corner(cleaned_variables)Since this is optional, the dependency of corner is pulled in if installed with::
$ pip install cmdstancache[plot]
Contributors
-------------* @JohannesBuchner
Contributions are welcome.