top of page

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.20 Generalized Poisson model in Python using Stan

====================================================

import numpy as np
import pystan
import statsmodels.api as sm

from  scipy.misc import factorial
from scipy.stats import uniform, rv_discrete

def sign(delta):
"""Returns a pair of vectors to set sign on
generalized Poisson distribution.

â€‹

input: delta, scalar
extra parameter from generalized Poisson

â€‹

output: value, sig
pair of scalars
value -> absolute value of delta
sig -> if delta < 0, sig = 0.5
else sign > 1.5
"""

if delta > 0:
value = delta
sig = 1.5
else:
value = abs(delta)
sig = 0.5

return value, sig

â€‹

â€‹

class gpoisson(rv_discrete):
"""Generalized Poisson distribution."""

def _pmf(self, n, mu, delta, sig):

if sig < 1.0:
delta1 = -delta
else:
delta1 = delta

term1 = mu * ((mu + delta1 * n) ** (n - 1))
term2 = np.exp(-mu- n * delta1) / factorial(n)

return term1 * term2

â€‹

â€‹

# Data
np.random.seed(160)                                                        # set seed to replicate example
nobs= 1000                                                                       # number of obs in model

â€‹

x1 = uniform.rvs(size=nobs)

xb = 1.0 + 3.5 * x1                                                           # linear predictor
delta = -0.3

â€‹

exb = np.exp(xb)

gen_poisson = gpoisson(name="gen_poisson", shapes='mu, delta, sig'

gpy = [gen_poisson.rvs(exb[i],
sign(delta)[0],  sign(delta)[1]) for i in range(nobs)]

mydata = {}                                                                     # build data dictionary
mydata['N'] = nobs                                                          # sample size
mydata['Y'] = gpy                                                            # response variable
mydata['K'] = 2                                                                # number of coefficients

# Fit
stan_code = """
data{
int N;
int K;
matrix[N, K] X;
int Y[N];
}
parameters{
vector[K] beta;
real<lower=-1, upper=1> delta;
}
transformed parameters{
vector[N] mu;

mu = exp(X * beta);
}
model{
vector[N] l1;
vector[N] l2;
vector[N] LL;

delta ~ uniform(-1, 1);

â€‹

for (i in 1:N){
l1[i] = log(mu[i]) + (Y[i] - 1) * log(mu[i] + delta * Y[i]);
l2[i] = mu[i] + delta * Y[i] + lgamma(Y[i] + 1);
LL[i] = l1[i] - l2[i];
}

target += LL;
}
generated quantities{
vector[N] ExpY;
vector[N] VarY;
vector[N] Pres;

â€‹

for (i in 1:N){
ExpY[i] = mu[i] / (1 - delta);
VarY[i] = mu[i] / pow(1-delta, 3);
Pres[i] = (Y[i] - ExpY[i]) / sqrt(VarY[i]);
}
}
"""

# Run mcmc
fit = pystan.stan(model_code=stan_code, data=mydata, iter=5000, chains=3,
warmup=4000, n_jobs=3)

â€‹

# Output
nlines = 8                                                                # 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_0315c6c87b1d597a303c44dcb1dd148b.
3 chains, each with iter=5000; warmup=4000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=3000.

â€‹

mean   se_mean          sd       2.5%     25%        50%       75%   97.5%       n_eff      Rhat
beta[0]          1.0       9.9e-4       0.03       0.94      0.98          1.0       1.02       1.05       892.0         1.0
beta[1]         3.51      7.4e-4       0.02       3.47        3.5        3.51       3.53       3.56     1030.0         1.0
delta           -0.31      8.5e-4       0.03      -0.37     -0.33      -0.31      -0.29      -0.26     1012.0         1.0

bottom of page