前言

PyStan 为 Stan 提供了一个 Python 接口,这是一个使用 No-U-Turn 采样器进行贝叶斯推理的软件包,这是Hamiltonian Monte Carlo 的一种变体。

PyStan具有以下依赖项:

Python:2.7,> = 3.3
Cython:> = 0.22
NumPy:> = 1.7
PyStan还要求在安装和运行时可以使用C ++编译器。 在基于Debian的系统上,这是通过发出命令apt-get install build-essential来完成的。

例子:

importpystanschools_code="""data{int<lower=0>J;//numberofschoolsvector[J]y;//estimatedtreatmenteffectsvector<lower=0>[J]sigma;//s.e.ofeffectestimates}parameters{realmu;real<lower=0>tau;vector[J]eta;}transformedparameters{vector[J]theta;theta=mu+tau*eta;}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)

需要资料,看个简介

素材来源中:开源中国