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.5 Simple Poisson model in Python
=============================================
import numpy as np
from scipy.stats import uniform, poisson
import statsmodels.api as sm
​
# Data
np.random.seed(2016) # set seed to replicate example
nobs= 1000 # number of obs in model
x = uniform.rvs(size=nobs)
xb = 1 + 2 * x # linear predictor
py = poisson.rvs(np.exp(xb)) # create y as adjusted
X = sm.add_constant(x.transpose())
# Fit
#build model
myp = sm.GLM(py, X, family=sm.families.Poisson())
# find parameter values
res = myp.fit()
# print summary to screen
print(res.summary())
=============================================
Output on screen:
​
Generalized Linear Model Regression Results
==============================================================================
Dep. Variable: y No. Observations: 1000
Model: GLM Df Residuals: 998
Model Family: Poisson Df Model: 1
Link Function: log Scale: 1.0
Method: IRLS Log-Likelihood: -2378.8
Date: Sat, 24 Dec 2016 Deviance: 985.87
Time: 01:05:38 Pearson chi2: 939.
No. Iterations: 8
==============================================================================
coef std err z P>|z| [95.0% Conf. Int.]
-------------------------------------------------------------------------------------------------------------------------------------
const 0.9921 0.029 34.332 0.000 0.935 1.049
x1 2.0108 0.041 48.899 0.000 1.930 2.091
==============================================================================