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 6.17 Negative binomial model in Python using Stan
====================================================
import numpy as np
import pystan
from scipy.stats import uniform, binom, nbinom
import statsmodels.api as sm
​
# Data
np.random.seed(141) # set seed to replicate example
nobs= 2500 # number of obs in model
​
x1 = binom.rvs(1, 0.6, size=nobs) # categorical explanatory variable
x2 = uniform.rvs(size=nobs) # real explanatory variable
theta = 0.303
X = sm.add_constant(np.column_stack((x1, x2)))
beta = [1.0, 2.0, -1.5]
xb = np.dot(X, beta) # linear predictor
exb = np.exp(xb)
nby = nbinom.rvs(exb, theta)
​
mydata = {} # build data dictionary
mydata['N'] = nobs # sample size
mydata['X'] = X # predictors
mydata['Y'] = nby # response variable
mydata['K'] = len(beta)
# Fit
stan_code = """
data{
int N;
int K;
matrix[N,K] X;
int Y[N];
}
parameters{
vector[K] beta;
real<lower=0, upper=5> alpha;
}
transformed parameters{
vector[N] mu;
mu = exp(X * beta);
}
model{
Y ~ neg_binomial(mu, alpha);
}
"""
# Run mcmc
fit = pystan.stan(model_code=stan_code, data=mydata, iter=10000, chains=3,
warmup=5000, n_jobs=3)
# Output
nlines = 9 # 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_cd0568292a99c9c183c40b9d48fced78.
3 chains, each with iter=10000; warmup=5000; thin=1;
post-warmup draws per chain=5000, total post-warmup draws=15000.
​
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
beta[0] 1.05 6.0e-4 0.05 0.95 1.02 1.05 1.08 1.14 6702.0 1.0
beta[1] 1.97 3.3e-4 0.03 1.91 1.95 1.97 1.99 2.03 8181.0 1.0
beta[2] -1.52 3.3e-4 0.03 -1.58 -1.54 -1.52 -1.5 -1.46 8921.0 1.0
alpha 0.44 2.3e-4 0.02 0.41 0.43 0.44 0.46 0.48 7339.0 1.0