HSI
From: Bayesian Models for Astrophysical Data, Cambridge Univ. Press
(c) 2017, Joseph M. Hilbe, Rafael S. de Souza and Emille E. O. Ishida
you are kindly asked to include the complete citation if you used this material in a publication
​
​
# Data from code 8.21
import numpy as np
import statsmodels.api as sm
from scipy.stats import norm, uniform, nbinom
​
np.random.seed(1656) # set seed to replicate example
N = 2000 # number of obs in model
NGroups = 10
​
x1 = uniform.rvs(size=N)
x2 = uniform.rvs(size=N)
​
Groups = np.array([200 * [i] for i in range(NGroups)]).flatten()
a = norm.rvs(loc=0, scale=0.5, size=NGroups)
eta = 1 + 0.2 * x1 - 0.75 * x2 + a[list(Groups)]
mu = np.exp(eta)
y = nbinom.rvs(mu, 0.5)
​
​
​
​
​
​
​
Code 8.23 Bayesian random intercept negative binomial in Python using Stan
==========================================================
import pystan
​
X = sm.add_constant(np.column_stack((x1,x2)))
K = X.shape[1]
model_data = {}
model_data['Y'] = y
model_data['X'] = X
model_data['K'] = K
model_data['N'] = N
model_data['NGroups'] = NGroups
model_data['re'] = Groups
model_data['b0'] = np.repeat(0, K)
model_data['B0'] = np.diag(np.repeat(100, K))
model_data['a0'] = np.repeat(0, NGroups)
model_data['A0'] = np.diag(np.repeat(1, NGroups))
# Fit
stan_code = """
data{
int<lower=0> N;
int<lower=0> K;
int<lower=0> NGroups;
matrix[N, K] X;
int Y[N];
int re[N];
vector[K] b0;
matrix[K, K] B0;
vector[NGroups] a0;
matrix[NGroups, NGroups] A0;
}
parameters{
vector[K] beta;
vector[NGroups] a;
real<lower=0> sigma_re;
real<lower=0> alpha;
}
transformed parameters{
vector[N] eta;
vector[N] mu;
eta = X * beta;
for (i in 1:N){
mu[i] = exp(eta[i] + a[re[i]+1]);
}
}
model{
sigma_re ~ cauchy(0, 25);
alpha ~ cauchy(0, 25);
beta ~ multi_normal(b0, B0);
a ~ multi_normal(a0, sigma_re * A0);
Y ~ neg_binomial(mu, alpha/(1.0 - alpha));
}
"""
​
fit = pystan.stan(model_code=stan_code, data=model_data, iter=5000, chains=3, thin=10,
warmup=4000, n_jobs=3)
​
# Output
nlines = 20 # number of lines in screen output
​
output = str(fit).split('\n')
​
for item in output[:nlines]:
print(item)
==========================================================
Output on screen:
​
Inference for Stan model: anon_model_778557c369c27d3e94602081bd2e5cba.
3 chains, each with iter=5000; warmup=4000; thin=10;
post-warmup draws per chain=100, total post-warmup draws=300.
​
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
beta[0] 1.08 0.01 0.17 0.71 0.99 1.09 1.18 1.42 256 1.0
beta[1] 0.2 4.0e-3 0.07 0.07 0.15 0.2 0.24 0.34 300 1.01
beta[2] -0.82 4.2e-3 0.07 -0.97 -0.87 -0.82 -0.78 -0.69 275 1.0
a[0] 0.33 9.5e-3 0.16 -2.1e-4 0.23 0.33 0.43 0.73 300 1.0
a[1] 0.23 0.01 0.17 -0.08 0.14 0.22 0.31 0.64 255 1.0
a[2] 0.12 9.5e-3 0.16 -0.17 0.01 0.11 0.21 0.49 290 1.0
a[3] -0.93 0.01 0.19 -1.29 -1.06 -0.93 -0.82 -0.55 288 0.99
a[4] 0.17 0.01 0.17 -0.15 0.08 0.17 0.26 0.55 275 1.01
a[5] -0.34 10.0e-3 0.17 -0.65 -0.44 -0.35 -0.25 0.05 300 1.01
a[6] 0.22 9.6e-3 0.16 -0.11 0.12 0.22 0.31 0.62 294 1.01
a[7] 0.44 0.01 0.16 0.11 0.35 0.44 0.54 0.79 266 1.0
a[8] 0.06 0.01 0.17 -0.26 -0.04 0.05 0.14 0.46 271 1.0
a[9] -0.3 0.01 0.17 -0.7 -0.41 -0.3 -0.21 0.07 254 1.0
sigma_re 0.3 0.01 0.21 0.1 0.17 0.23 0.37 0.83 300 0.99
alpha 0.5 1.1e-3 0.02 0.47 0.49 0.5 0.52 0.54 279 1.0