Python接口到Stan,一个用于贝叶斯推断的软件包
项目描述
PyStan 是一个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
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`
引用
我们感谢引用,因为它们让我们了解到人们如何使用此软件。引用还可以提供使用证据,有助于获得资助。
在出版物中引用PyStan使用以下格式:
Riddell, A., Hartikainen, A., & Carter, M. (2021). PyStan (3.0.0). https://pypi.ac.cn/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} }
请同时引用Stan。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
pystan-3.10.0.tar.gz (13.8 kB 查看散列值)
构建分发
pystan-3.10.0-py3-none-any.whl (13.9 kB 查看散列值)