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
Code 8.18 Random-intercept–random-slopes Poisson model in Python using Stan
=============================================================
import numpy as np
import pystan
import statsmodels.api as sm
from scipy.stats import norm, uniform, poisson
# Data
np.random.seed(1656) # set seed to replicate example
N = 5000 # number of obs in model
NGroups = 10
x1 = uniform.rvs(size=N)
x2 = np.array([0 if item <= 0.5 else 1 for item in x1])
Groups = np.array([500 * [i] for i in range(NGroups)]).flatten()
a = norm.rvs(loc=0, scale=0.1, size=NGroups)
b = norm.rvs(loc=0, scale=0.35, size=NGroups)
eta = 1 + 4 * x1 - 7 * x2 + a[list(Groups)] + b[list(Groups)] * x1
mu = np.exp(eta)
y = poisson.rvs(mu)
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;
vector[NGroups] b;
real<lower=0> sigma_ri;
real<lower=0> sigma_rs;
}
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] + b[re[i] + 1] * X[i,2]);
}
}
model{
sigma_ri ~ gamma(0.01, 0.01);
sigma_rs ~ gamma(0.01, 0.01);
beta ~ multi_normal(b0, B0);
a ~ multi_normal(a0, sigma_ri * A0);
b ~ multi_normal(a0, sigma_rs * A0);
Y ~ poisson(mu);
}
"""
fit = pystan.stan(model_code=stan_code, data=model_data, iter=4000, chains=3, thin=10,
warmup=3000, n_jobs=3)
# Output
nlines = 30 # 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_ee235870bb0cf4542a61932af837de61.
3 chains, each with iter=4000; warmup=3000; 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.04 2.1e-3 0.04 0.96 1.02 1.04 1.06 1.11 300 1.0
beta[1] 4.12 7.9e-3 0.13 3.86 4.05 4.11 4.19 4.37 257 1.0
beta[2] -6.88 4.7e-3 0.08 -7.05 -6.94 -6.88 -6.83 -6.75 300 1.0
a[0] 0.06 3.2e-3 0.05 -0.04 0.03 0.06 0.1 0.17 270 1.01
a[1] 0.04 3.1e-3 0.05 -0.07 4.8e-4 0.04 0.07 0.15 300 0.99
a[2] -0.01 3.1e-3 0.05 -0.11 -0.04 -0.02 0.02 0.08 231 1.02
a[3] 0.07 3.3e-3 0.05 -0.03 0.04 0.07 0.1 0.18 248 1.0
a[4] -0.14 3.6e-3 0.06 -0.26 -0.18 -0.13 -0.1 -0.01 300 1.0
a[5] -0.1 3.1e-3 0.05 -0.21 -0.12 -0.1 -0.06 0.01 300 1.0
a[6] -0.04 3.2e-3 0.05 -0.14 -0.07 -0.04 -3.9e-3 0.07 265 1.01
a[7] 0.07 3.0e-3 0.05 -0.02 0.03 0.07 0.1 0.17 300 1.0
a[8] 0.05 3.0e-3 0.05 -0.05 9.4e-3 0.04 0.08 0.16 300 0.99
a[9] -0.03 3.1e-3 0.05 -0.12 -0.06 -0.03 8.4e-3 0.07 267 1.0
b[0] 0.47 8.9e-3 0.15 0.17 0.37 0.46 0.58 0.76 300 1.0
b[1] 0.28 8.8e-3 0.15 -0.03 0.19 0.28 0.38 0.57 300 1.0
b[2] 0.13 9.7e-3 0.16 -0.2 0.02 0.14 0.25 0.45 284 1.01
b[3] -0.09 10.0e-3 0.16 -0.42 -0.18 -0.08 9.4e-3 0.19 251 1.01
b[4] -0.2 0.01 0.2 -0.62 -0.33 -0.2 -0.07 0.21 300 1.0
b[5] 0.33 0.01 0.18 -0.05 0.22 0.34 0.45 0.68 215 1.0
b[6] -0.48 0.01 0.18 -0.86 -0.6 -0.46 -0.35 -0.15 299 1.0
b[7] 0.06 9.8e-3 0.17 -0.27 -0.05 0.07 0.18 0.4 300 1.01
b[8] -0.28 9.5e-3 0.16 -0.61 -0.4 -0.29 -0.17 0.02 300 1.01
b[9] -0.25 9.9e-3 0.17 -0.57 -0.36 -0.25 -0.15 0.09 300 1.0
sigma_ri 9.3e-3 4.1e-4 7.2e-3 1.3e-3 4.9e-3 7.4e-3 0.01 0.03 300 0.99
sigma_rs 0.14 4.2e-3 0.07 0.04 0.09 0.12 0.16 0.33 300 0.99