4

While working through problems in "Statistical Rethinking" - I was using pymc3 to work through the "Salamanders" problem 10H4. (code below)

The sampler always failed with an error :

The derivative of RV alpha.ravel()[0] is zero.

But when I passed the init='map' option to the sampler, I got similar results to others who used R code. The 'map' init option is heavily discouraged in the output of the sampler, but many of the other init options also had problems.

The data is here

I have tried changing the priors as suggested in a web search related to the error message.

with pm.Model() as hw_10_4:
    alpha = pm.Normal('alpha',mu=0,sd=5)
    beta = pm.Normal('beta',mu=0,sd=5)
    l = pm.Deterministic('lambda',pm.math.exp(alpha + beta*pctcover))
    s = pm.Poisson('salam',l,observed=salaman)
    trace_10_4 = pm.sample(1000, tune=1000, init='map')

I have the result I want, but I am interested in understanding more about why I had the problem in the first place. I am new to PYMC3 and would like to fully understand what is going on.

Thanks!

Yilmaz
  • 35,338
  • 10
  • 157
  • 202

1 Answers1

2

Yeah, PyMC3 really doesn't seem to handle global scaling very well there. It's nice that Stan appears not to choke on that like PyMC3 does, but really in both cases ideal sampling will occur when regression predictors are standardized. The unofficial port of some of the Statistical Rethinking code to PyMC3 does exactly this for that problem. FWIW, I usually import scale from sklearn.preprocessing, which behaves identically to the R scale used in the text.

As for using a MAP init, that's mostly an issue at high-dimensional predictor space. I wouldn't sweat it here with only the one predictor.

merv
  • 67,214
  • 13
  • 180
  • 245
  • I'll give scaling a try - The answers to the questions weren't in the repository when I pulled it, so I've updated that - that should help to answer any questions. Thanks for the reply! – user1810588 Jul 06 '19 at 12:08