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

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.4.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

pycmdstan-0.4.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pycmdstan-0.4.1.tar.gz
Algorithm Hash digest
SHA256 298decb91e388cd4dffb5e6253110f88adde3903e385513b6991720be618c101
MD5 9a40b46a3792713bb885c91dbbdd5c35
BLAKE2b-256 fc28423a37ac153802aa71363d1bd4288a6c9041db1dcd99baca6d7e430606eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycmdstan-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 446c4d213e64b9c224d33a55254e465217b9372387728bcc9d813e5079c63592
MD5 bc8f1bd461ea507ad4cbbc8aed22507b
BLAKE2b-256 073e609a4eaa3ebad007ff06fcea8185709cc11e085cc7f188f9b4a31320c747

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