I am doing a simple negative binomial regression (I am following along with the text “Bayes Rules!” except using PYMC/bambi for the exercises).
The data is 37 rows of data that consists of counts of Eagle sitings, year of the data, and hours of observation during that year. Bambi quickly devours this (58 seconds on my Macbook) with this model
alpha_prior = bmb.Prior('Exponential', lam= 1/50.)
model2 = bmb.Model('count ~ year + hours', eagles, family='negativebinomial',priors = {'alpha': alpha_prior})
For ‘educational’ purposes I wanted to see if I could understand this by building my own model. After many iterations I ended up with this beast:
COORDS = {"regressors": ["year", "hours"], "obs_idx" : eagles.index}
with pm.Model(coords= COORDS) as negBin:
# define priors, weakly informative Normal
b0 = pm.Normal("Intercept", mu=0, sigma=500)
b = pm.Normal("slopes", mu=0, sigma=.4, dims="regressors")
alpha_p = pm.Exponential("alpha",lam = 1/50. )
Y = pm.ConstantData("year", eagles["year"].to_numpy(), dims = "obs_idx")
H = pm.ConstantData("hours", eagles["hours"].to_numpy(), dims = "obs_idx")
C = pm.ConstantData("count", eagles["count"].to_numpy(), dims = "obs_idx")
# define linear model and exp link function
theta = pm.math.exp(b0 + b[0] * Y + b[1] * H)
## Define Poisson likelihood"
y = pm.NegativeBinomial("y", mu=theta, alpha =alpha_p, observed=C, dims="obs_idx")
This takes about 20 minutes ! ! And sometimes diverges . I am sure I am doing something bone-headed but after hours of banging my head against this I am reaching out to hope for help.