python与stan的接口,一个用于贝叶斯推理的包
pystan的Python项目详细描述
pystan为stan提供了一个python接口,一个用于贝叶斯推理的包 使用不掉头取样器,哈密顿蒙特卡罗的一种变体。
有关Stan及其建模语言的更多信息, 请参阅http://mc-stan.org/上的STAN用户指南和参考手册。
重要链接
- HTML文档:https://pystan.readthedocs.org
- 问题跟踪器:https://github.com/stan-dev/pystan/issues
- 源代码库:https://github.com/stan-dev/pystan
- 斯坦:http://mc-stan.org/
- 斯坦用户指南和参考手册(pdf)可在http://mc-stan.org
类似项目
详细的安装说明
有关详细的安装说明,请参见 doc/installation_beginner.md文件。
快速安装(Linux和MacOS)
NumPy和Cython (0.22或更高版本)是必需的。matplotlib 是可选的。
pystan和所需的包可以使用pip从Python Package Index安装。
pip install pystan
或者,如果cython(0.22或更高版本)和numpy已经 pystan可以通过以下命令从源代码安装
git clone --recursive https://github.com/stan-dev/pystan.git cd pystan python setup.py install
如果在从源代码处编译后遇到ImportError,请尝试更改 在尝试import pystan之前离开源目录。在Linux和 os x cd /tmp将起作用。
示例
import pystan import numpy as np import matplotlib.pyplot as plt schools_code = """ data { int<lower=0> J; // number of schools real y[J]; // estimated treatment effects real<lower=0> sigma[J]; // s.e. of effect estimates } parameters { real mu; real<lower=0> tau; real eta[J]; } transformed parameters { real theta[J]; for (j in 1:J) theta[j] = mu + tau * eta[j]; } model { eta ~ normal(0, 1); y ~ normal(theta, sigma); } """ schools_dat = {'J': 8, 'y': [28, 8, -3, 7, -1, 1, 18, 12], 'sigma': [15, 10, 16, 11, 9, 11, 10, 18]} sm = pystan.StanModel(model_code=schools_code) fit = sm.sampling(data=schools_dat, iter=1000, chains=4) print(fit) eta = fit.extract(permuted=True)['eta'] np.mean(eta, axis=0) # if matplotlib is installed (optional, not required), a visual summary and # traceplot are available fit.plot() plt.show()