Python interface to Stan, a package for Bayesian inference
Project description
NOTE: This documentation describes a BETA release of PyStan 3.
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
NOTE: BETA versions of PyStan 3 must be installed with ``pip install –pre pystan``.
Install PyStan with pip install pystan. (PyStan requires Python 3.7 or higher running on a Linux or macOS system.)
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`
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
Built Distribution
File details
Details for the file pystan-3.0.0rc1.tar.gz
.
File metadata
- Download URL: pystan-3.0.0rc1.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d897dfc0dd92b8feacc062177ac660473618012aaa043647e778f715d1ab4170 |
|
MD5 | 6d33e8c33312545f44ee33b4f58993a9 |
|
BLAKE2b-256 | 8b311d3d32f2b75840b13eb319a6c02385ca22b00e312521ad2d394d6c3cdc47 |
File details
Details for the file pystan-3.0.0rc1-py3-none-any.whl
.
File metadata
- Download URL: pystan-3.0.0rc1-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45a0b5fe0e5ba3ab240cfb96588536e5a586f72a7e7be689e90d5b745a6ec532 |
|
MD5 | 56525c8dd1b656042cd966bbfde808f2 |
|
BLAKE2b-256 | f2fa54e4f2e228e826c657a0ad1d5586ddad3950aed926da54a1fdff7bdf37d0 |