Sampling Error: GEV PyMC3

I am modeling Generalised Extreme Value Distribution in PYMC3 but I keep on getting SamplingError: Initial evaluation of model at starting point failed! I am implementing my own distribution. The logp equation is the PDF of GEV. I took the inital values for the shape, location and scale parameters from the best guess by scipystats.

with pm.Model() as model:
shape =  pm.Normal('shape', mu=shape1, sd=1.8*shape1)
loc = pm.Normal('loc', mu=loc1, sd=1.8*loc1)
scale = pm.Normal('scale', mu=scale1, sd=1.8*scale1)
def logp(value):

    if (shape == 0):
        lp=(np.log(1/scale) + (shape+1) * (-(value-loc/scale))- (-(value-loc/scale))).sum()
        lp=np.log(1/scale) + ((shape+1)*((-1/shape)*np.log(1+ shape*(value-loc/scale))))-       ((-1/shape)*np.log(1+shape*(value-loc/scale))).sum()
    return lp

gev = pm.DensityDist('gev', logp, observed = {'value':np.array(datin.data1)})
trace = pm.sample(3000, tune=500)

I am getting the following error:

SamplingError: Initial evaluation of model at starting point failed!
Starting values:

{'shape': array(-0.07499058), 'loc': array(14.728077), 'scale': array(5.38682189)}

Initial evaluation results:
shape    -inf
loc     -4.20
scale   -3.19
gev       NaN
Name: Log-probability of test_point, dtype: float64

It looks like someone has some working code for a model using the GEV distribution here. Does sampling work if you use this version + your data?

Thanks for the reply. I tried this version but it doesn’t work. It returns error messages
TypeError: must be real number, not FreeRV

Can you share your code that’s giving you this error?

Also, if you’re able to share your data datain that would be useful for debugging.

I am using the exact code you shared. I have tried this in the past with little success. Here is my data
data.txt (203 Bytes)

I think the problem is coming from your priors, you will need to make sure they make sense (I’m not very familiar with the GEV distribution so I’m not of much help). When I was messing with the priors I got the same

SamplingError: Initial evaluation of model at starting point failed!

issue you initially got. But here is a Collab notebook where it seems to be working. Just to note, I was getting a huge number of divergences when I ran inference initially so I set target_accept=0.99. You may want to revisit why these divergences are happening after you sort out your priors.

1 Like

Many thanks!
I agree, there is definitely some problem with my priors. I will try to play with different set of priors and see if it converges.

The GEV shape parameter rarely goes outside +/- 0.3 in practical applications. It’s hard to know what your prior is doing - what is shape1?

just wanted to update that I corrected my model by reducing the standard deviation and giving informed priors as calculated by the maximum likelihood. It looks like the model converges well with this set-up. I am attaching the trace plot. Could you please have a look at it if it looks decent. Also, Since I am new to PYMC3, I am confused about the posterior predictive analysis for this model. I tried to search for it and found some code , but it didn’t work out very well.
Many Thanks!