Skip to main content

Python interface to CmdStan

Project description

pycmdstan

pipeline status coverage report PyPI package version

Python interface to CmdStan.

Install

Pycmdstan is a pure-Python package which can be installed from PyPI

pip install --upgrade pycmdstan

or from sources

pip install -e git+https://gitlab.thevirtualbrain.org/tvb/pycmdstan

Usage

import os
os.environ['CMDSTAN'] = '~/src/cmdstan-2.17.1'
from pycmdstan import Model, Run

model = Model('''
data { vector[20] x; real mu; }
parameters { real sig; }
model { x ~ normal(mu, sig); }
generate quantities {
    vector[20] log_lik;
    for (i in 1:20) log_lik[i] = normal_lpdf(x[i] | mu, sig);
}
''')

runs = model.sample(
	data=dict(mu, **data),
	chains=4
)
assert runs.N_eff_per_iter.min() > 0.2
assert runs.R_hat.max() < 1.2

data = {'x': np.random.randn(20) + 5.0}
loo = []
mus = np.r_[1.0, 3.0, 5.0, 7.0, 9.0]
for mu in mus:
    run = model.sample(
        data=dict(mu=mu, **data), num_warmup=200, num_samples=200)
    loo.append(run['loo'])
assert mus[np.argmin(loo)] == 5.0

CmdStan's command line arguments are structured as a tree, so while child parameters of the method argument can be passed directly,

model.sample(num_samples=500)

equivalent to

./$model sample num_samples=500

A more complex case with nested parameters looks like

./$model id=$i \
    sample save_warmup=1 num_warmup=200 num_samples=200 \
        adapt \
            delta=0.8 \
        algorithm=hmc \
            engine=nuts \
                max_depth=12

Pycmdstan doesn't do anything clever (yet), so full set of subarguments need to be passed as equivalent strings

model.sample(
	save_warmup=1,
	num_warmup=200,
	num_samples=200,
	adapt_='delta=0.8',
	algorithm='hmc engine=nuts max_depth=12'
)

Here, the _ postfix on adapt_ means adapt doesn't take a value, but subarguments. In doubt, the command line used to call the model is available as an attribute of the Run instance,

run = model.sample(...)
print(run.cmd)

Contributing

Contributions are welcome, please start in the issue tracker. Use YAPF to format the code. The Dockerfile can ease local development,

docker build -t pycmdstan .
docker run --rm -it pycmdstan pytest -n4 pycmdstan/tests.py

Acknowledgements

  • PSIS code is by Aki Vehtari & Tuomas Sivula (BSD licensed, repo here)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycmdstan-0.6.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

pycmdstan-0.6-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pycmdstan-0.6.tar.gz.

File metadata

  • Download URL: pycmdstan-0.6.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pycmdstan-0.6.tar.gz
Algorithm Hash digest
SHA256 2578ce863f36c43a0e66224fd671de30aec77df32b60500047c553e7a55fab03
MD5 f968f3943246992c8ba9dc494ca823de
BLAKE2b-256 84c49c5e244955e82a906a3ad81e8321c1c89f259fb005c123991a1f6bfcf49d

See more details on using hashes here.

File details

Details for the file pycmdstan-0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pycmdstan-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 eead216ba8cec43e121419ec9e3263566706cc1c6ce6777202e18de2dc013fff
MD5 7e1f9355cd3261729a79b454bc72b3dd
BLAKE2b-256 039d9eaea2fefa67e91fd4acc150e7d165bd1552211236ad8a7d5a3acff21ff8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page