pystan貝葉斯推理的軟件包
PyStan 為 Stan 提供了一個(gè) Python 接口,這是一個(gè)使用 No-U-Turn 采樣器進(jìn)行貝葉斯推理的軟件包,這是Hamiltonian Monte Carlo 的一種變體。
PyStan具有以下依賴項(xiàng):
Python:2.7,> = 3.3
Cython:> = 0.22
NumPy:> = 1.7
PyStan還要求在安裝和運(yùn)行時(shí)可以使用C ++編譯器。 在基于Debian的系統(tǒng)上,這是通過發(fā)出命令apt-get install build-essential來完成的。
例子:
import pystan
schools_code = """
data {
int<lower=0> J; // number of schools
vector[J] y; // estimated treatment effects
vector<lower=0>[J] sigma; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
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)
評論
圖片
表情
