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.4 Random intercept normal model in Python using Stan
===================================================
import numpy as np
import statsmodels.api as sm
import pystan
from scipy.stats import norm, uniform
# Data
np.random.seed(1656) # set seed to replicate example
N = 4500 # number of obs in model
NGroups = 20
x1 = uniform.rvs(size=N)
x2 = uniform.rvs(size=N)
Groups = np.array([225 * [i] for i in range(20)]).flatten()
# Use this if you want random values for a
# a = norm.rvs(loc=0, scale=0.5, size=NGroups)
# Use values from code 8.1
a = np.array([0.579, -0.115, -0.125, 0.169, -0.500, -1.429, -1.171, -0.205,
0.193, 0.041, -0.917, -0.353, -1.197, 1.044, 1.084, -0.085, -0.886, -0.352,
-1.398, 0.350])
mu = 1 + 0.2 * x1 - 0.75 * x2 + a[list(Groups)]
y = norm.rvs(loc=mu, scale=2, size=N)
X = sm.add_constant(np.column_stack((x1,x2)))
K = X.shape[1]
re = Groups
Nre = NGroups
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'] = re
model_data['b0'] = np.repeat(0, K)
model_data['B0'] = np.diag(np.repeat(100, K))
model_data['a0'] = np.repeat(0, Nre)
model_data['A0'] = np.diag(np.repeat(1, Nre))
# Fit
stan_code = """
data{
int<lower=0> N;
int<lower=0> K;
int<lower=0> NGroups;
matrix[N, K] X;
real 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_plot;
real<lower=0> sigma_eps;
}
transformed parameters{
vector[N] eta;
vector[N] mu;
eta = X * beta;
for (i in 1:N){
mu[i] = eta[i] + a[re[i] + 1];
}
}
model{
sigma_plot ~ cauchy(0, 25);
sigma_eps ~ cauchy(0, 25);
beta ~ multi_normal(b0, B0);
a ~ multi_normal(a0, sigma_plot * A0);
Y ~ normal(mu, sigma_eps);
}
"""
fit = pystan.stan(model_code=stan_code, data=model_data, iter=10000,
chains=3, thin=10, warmup=6000, 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_e4941f4908aa4222930f76e89e912902.
3 chains, each with iter=10000; warmup=6000; thin=10;
post-warmup draws per chain=400, total post-warmup draws=1200.
​
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
beta[0] 0.81 7.8e-3 0.2 0.39 0.68 0.81 0.94 1.22 661.0 1.0
beta[1] 0.14 3.0e-3 0.1 -0.06 0.07 0.13 0.21 0.34 1160.0 1.0
beta[2] -0.77 3.2e-3 0.1 -0.98 -0.84 -0.77 -0.7 -0.56 1022.0 1.0
a[0] 1.01 8.3e-3 0.22 0.58 0.85 1.0 1.16 1.45 730.0 1.0
a[1] 0.15 8.3e-3 0.23 -0.29 9.9e-3 0.16 0.3 0.6 742.0 1.0
a[2] 0.27 8.0e-3 0.23 -0.18 0.12 0.26 0.42 0.74 817.0 1.0
a[3] 0.37 8.2e-3 0.23 -0.08 0.21 0.37 0.51 0.8 752.0 1.0
a[4] -0.3 8.9e-3 0.23 -0.74 -0.46 -0.3 -0.15 0.17 682.0 1.0
a[5] -1.36 8.2e-3 0.22 -1.81 -1.5 -1.35 -1.21 -0.89 730.0 1.0
a[6] -1.03 8.3e-3 0.22 -1.5 -1.18 -1.03 -0.88 -0.62 726.0 1.0
a[7] 0.1 8.0e-3 0.22 -0.35 -0.05 0.1 0.24 0.53 768.0 1.0
a[8] 0.29 7.9e-3 0.22 -0.14 0.14 0.3 0.44 0.72 796.0 1.0
a[9] 0.33 8.7e-3 0.23 -0.11 0.19 0.33 0.47 0.8 682.0 1.0
a[10] -0.39 8.1e-3 0.22 -0.82 -0.54 -0.38 -0.24 0.05 726.0 1.0
a[11] -0.07 8.4e-3 0.23 -0.55 -0.21 -0.07 0.08 0.36 730.0 1.0
a[12] -0.8 8.6e-3 0.23 -1.26 -0.95 -0.8 -0.65 -0.35 700.0 1.0
a[13] 1.32 8.1e-3 0.22 0.89 1.17 1.32 1.46 1.77 736.0 1.0
a[14] 1.29 9.1e-3 0.22 0.86 1.14 1.28 1.44 1.73 610.0 1.0
a[15] 0.16 7.8e-3 0.23 -0.28 7.0e-3 0.16 0.32 0.61 823.0 1.0
a[16] -0.53 8.5e-3 0.22 -0.98 -0.68 -0.53 -0.38 -0.09 699.0 1.0
a[17] -0.14 8.1e-3 0.23 -0.58 -0.29 -0.15 0.01 0.3 781.0 1.0
a[18] -1.2 8.2e-3 0.22 -1.65 -1.35 -1.2 -1.05 -0.76 728.0 1.0
a[19] 0.55 9.1e-3 0.23 0.1 0.4 0.55 0.71 1.02 652.0 1.0
sigma_plot 0.73 8.2e-3 0.28 0.36 0.55 0.68 0.87 1.43 1151.0 1.0
sigma_eps 1.98 6.1e-4 0.02 1.94 1.97 1.98 2.0 2.03 1200.0 1.0