Skip to main content

Python interface to Stan, a package for Bayesian inference

Project description

PyStan is a Python interface to Stan, a package for Bayesian inference.

Stan® is a state-of-the-art platform for statistical modeling and high-performance statistical computation. Thousands of users rely on Stan for statistical modeling, data analysis, and prediction in the social, biological, and physical sciences, engineering, and business.

Notable features of PyStan include:

  • Automatic caching of compiled Stan models

  • Automatic caching of samples from Stan models

  • An interface similar to that of RStan

  • Open source software: ISC License

Getting started

Install PyStan with pip install pystan. PyStan requires Python ≥3.7 running on Linux or macOS. You will also need a C++ compiler such as gcc ≥9.0 or clang ≥10.0.

The following block of code shows how to use PyStan with a model which studied coaching effects across eight schools (see Section 5.5 of Gelman et al (2003)). This hierarchical model is often called the “eight schools” model.

import stan

schools_code = """
data {
  int<lower=0> J;         // number of schools
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}
"""

schools_data = {"J": 8,
                "y": [28,  8, -3,  7, -1,  1, 18, 12],
                "sigma": [15, 10, 16, 11,  9, 11, 10, 18]}

posterior = stan.build(schools_code, data=schools_data)
fit = posterior.sample(num_chains=4, num_samples=1000)
eta = fit["eta"]  # array with shape (8, 4000)
df = fit.to_frame()  # pandas `DataFrame`

Citation

We appreciate citations as they let us discover what people have been doing with the software. Citations also provide evidence of use which can help in obtaining grant funding.

To cite PyStan in publications use:

Riddell, A., Hartikainen, A., & Carter, M. (2021). PyStan (3.0.0). https://pypi.org/project/pystan

Or use the following BibTeX entry:

@misc{pystan,
  title = {pystan (3.0.0)},
  author = {Riddell, Allen and Hartikainen, Ari and Carter, Matthew},
  year = {2021},
  month = mar,
  howpublished = {PyPI}
}

Please also cite Stan.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pystan-3.3.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

pystan-3.3.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file pystan-3.3.0.tar.gz.

File metadata

  • Download URL: pystan-3.3.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.9.7 Linux/5.8.0-1042-azure

File hashes

Hashes for pystan-3.3.0.tar.gz
Algorithm Hash digest
SHA256 44a26a88b70f9f3b0d20864fb0f72366b902b58908d6ad4360758d037c0ed3f4
MD5 c7d2e2965f1617af1af2367cb688ffdd
BLAKE2b-256 240c544aa1e85624125a08944491c36057179bab281ba571a1e6b2677d9af7b0

See more details on using hashes here.

File details

Details for the file pystan-3.3.0-py3-none-any.whl.

File metadata

  • Download URL: pystan-3.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.9.7 Linux/5.8.0-1042-azure

File hashes

Hashes for pystan-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fe0c761d8f13bfc2ea370e81ecc64ec4163d5b87470bad2a6f723178c9a1923
MD5 075c498cf6a1add34cddc1e8b00d91b7
BLAKE2b-256 e77615cb959d93e49b473a1b188d3c3086a0bc7f2c2d30b5565abdb0ceb9cf16

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