Fail when trying to sample model from PyMC

37 Views Asked by At

I'm learning about Bayesian modeling and PyMC, and I'm trying to build my first model. I have structured my model as follows:

with pm.Model(coords={"obs_id": range(len(df))}) as model:
    p = pm.MutableData("p", value=df["p"])
    
    b0 = pm.Normal("b0", mu=3.77, sigma=2)
    b1 = pm.HalfNormal("b1", sigma=2)
    
    alpha = pm.Gamma("alpha", alpha=0.95, beta=1)
    beta = pm.Deterministic("beta", var=pm.math.exp(-b0 - b1 * p), dims="obs_id")
    
    y = pm.Gamma("y", alpha=alpha, beta=beta, observed=df["y"], dims="obs_id")

But when I try to pm.sample(1000) from this model, I get the following error:

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'b0': array(4.31820668), 'b1_log__': array(1.1156248), 'alpha_log__': array(-0.08505816)}

Logp initial evaluation results:
{'b0': -1.65, 'b1': -0.97, 'alpha': -1.03, 'y': -inf}
You can call `model.debug()` for more details.

My initial guess was that the y distribution might get negative or zero parameters from either alpha or beta, or from the observed df["y"] - but it doesn't look like that. The smallest value of df["y"] is 0.5. The alpha should be strictly positive, since it is drawn from a Gamma distribution; and the beta should also be positive, since pm.math.exp(...) should only return positive values.

Is there something that I'm missing about this formulation?

0

There are 0 best solutions below