Skip to main content

PyStan

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 runs on Linux and 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
  array[J] real y;              // estimated treatment effects
  array[J] real<lower=0> sigma; // 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.

Metadata

Release files for pystan 3.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystan 3.9.0
File Size Uploaded Provenance Actions
pystan-3.9.0.tar.gz 13.8 kB Without provenance Details for pystan-3.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pystan 3.9.0
File Interpreter ABI Platform Actions
pystan-3.9.0-py3-none-any.whl Python 3 none any Details for pystan-3.9.0-py3-none-any.whl

Total release size: 27.7 kB

Release files / pystan-3.9.0.tar.gz

Download URL pystan-3.9.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
07493f90537215b8de8577d8905cccbaa209835106437c5a0035d9dc89215417
BLAKE2b-256 checksum
How to use checksums
332c4da7111fdc703bbd810caf766a8d9e2a83ad8192ccccb9d0f26a5570e418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1019-azure

Release files / pystan-3.9.0-py3-none-any.whl

Download URL pystan-3.9.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4f312efaeb54be878bc3a1762c3ea166711e4dc671a58862ee9e282297f3df4
BLAKE2b-256 checksum
How to use checksums
36dce84dcee309f00384674bd4571085a1daada182552a0fbc83231672539b4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.7 Linux/6.2.0-1019-azure

Security for pystan 3.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.9.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page