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 / runs.niter) > 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.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

pycmdstan-0.2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pycmdstan-0.2.tar.gz
Algorithm Hash digest
SHA256 b4c71c7ae019b466f338edd34b5004f706dd65d74d854c077ffa0c7c73046e5e
MD5 97bc8cfeb4bc019cb4e92d855dc53bab
BLAKE2b-256 cf4847b4cb3be4372830f571cd4e4aba95f642da37572a9ff3014b8234d65142

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pycmdstan-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 acf1c0be11b5f9b4140a08fce57153e0749ed4168a2a1632a3e3f2941d5effd3
MD5 7c8860a4b0bc8100bce90eeb4cce4622
BLAKE2b-256 c98e01414c3ae92b6399fffc833c41ee49a42ba04777e8d2ee39a3056937166c

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