Skip to main content

Stan 的 Python 接口,一个用于贝叶斯推理的包

项目描述

PyStan是 Stan 的 Python 接口,Stan 是一个用于贝叶斯推理的包。

Stan® 是用于统计建模和高性能统计计算的最先进平台。数以千计的用户依靠 Stan 在社会、生物和物理科学、工程和商业领域进行统计建模、数据分析和预测。

PyStan 的显着特点包括:

  • 自动缓存已编译的 Stan 模型

  • 自动缓存来自 Stan 模型的样本

  • 类似于 RStan 的接口

  • 开源软件:ISC 许可证

入门

使用pip install pystan 安装 PyStan。PyStan 在 Linux 和 macOS 上运行。您还需要一个 C++ 编译器,例如 gcc ≥9.0 或 clang ≥10.0。

以下代码块展示了如何将 PyStan 与一个模型一起使用,该模型研究了八所学校的教练效果(参见 Gelman 等人 (2003) 的第 5.5 节)。这种分层模型通常被称为“八校”模型。

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`

引文

我们感谢引用,因为它们让我们发现人们一直在使用该软件做什么。引文还提供了可以帮助获得赠款资金的使用证据。

要在出版物中引用 PyStan,请使用:

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

或使用以下 BibTeX 条目:

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

还请引用斯坦。

项目详情


发布历史 发布通知| RSS订阅