import numpy as np
from pprint import pprint
from crabbymetrics import Poisson
np.set_printoptions(precision=4, suppress=True)Poisson Example
This page mirrors examples/poisson_example.py.
1 Fit A Poisson Model
rng = np.random.default_rng(4)
n = 700
k = 2
beta = np.array([0.4, -0.6])
intercept = 0.2
x = rng.normal(size=(n, k))
logits = intercept + x @ beta
mu = np.exp(logits)
y = rng.poisson(mu).astype(float)
model = Poisson(alpha=0.0, max_iterations=200)
model.fit(x, y)
print("true intercept:", intercept)
print("true coef:", beta)
pprint(model.summary())true intercept: 0.2
true coef: [ 0.4 -0.6]
{'coef': array([ 0.3499, -0.5813]),
'coef_se': array([0.0298, 0.029 ]),
'converged': True,
'inference_available': True,
'intercept': 0.2729469240217961,
'intercept_se': 0.035418648411838095,
'iterations': 5,
'objective': 321.1718929596153,
'penalty': 0.0,
'termination_reason': 'Solver converged',
'vcov': array([[ 0.0013, -0.0003, 0.0005],
[-0.0003, 0.0009, -0. ],
[ 0.0005, -0. , 0.0008]]),
'vcov_type': 'vanilla'}
2 Prediction surfaces
For Poisson, predict_lin() returns the log-mean index and predict() exponentiates back to the conditional mean.
check = x[:8]
out = {
"log_mu": model.predict_lin(check),
"mu": model.predict(check),
}
pprint(out){'log_mu': array([ 0.1464, 0.472 , -0.2984, -0.0316, -0.4303, -0.5605, 0.087 ,
-1.5589]),
'mu': array([1.1577, 1.6032, 0.742 , 0.9689, 0.6503, 0.5709, 1.0909, 0.2104])}