SamplingError: Initial evaluation of model at starting point failed!

I am trying to implement a code created originally here.

What I have is this model:

with pm.Model() as model:
    sigma = pm.HalfNormal('sigma', 1, testval=.1)
    mu = pm.Normal('mu', 0, sigma, testval=.1)

    nu = pm.Exponential('nu', 1./10)
    logs = pm.GaussianRandomWalk('logs', sigma**-2, shape=n)

    #lam uses variance in pymc3, not sd like in scipy
    r = pm.StudentT('r', nu, mu=mu, lam=1/pm.math.exp(-2*logs), observed=returns.values[train]) 

import scipy as sp

with model:
    start = pm.find_MAP(vars=[logs])

with model:
    step = pm.NUTS(vars=[logs, mu, nu,sigma],scaling=start, gamma=.25)
    start2 = pm.sample(100, step, start=start)[-1]

    # Start next run at the last sampled position.
    step = pm.NUTS(vars=[logs, mu, nu,sigma],scaling=start2, gamma=.55)
    trace = pm.sample(2000, step, start=start2)

However, I am getting the following error:

SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'sigma_log__': array(-2.30258509), 'mu': array(0.1), 'nu_log__': array(2.30258509), 'logs': array([    0.,   100.,   200.,   300.,   400.,   500., ...  44700.,
   44800., 44900.])}

Initial evaluation results:
{'sigma': -2.53, 'mu': 0.88, 'nu': -1.0, 'logs': -418.13, 'r': -inf}

I am not sure what is going on. How do I fix this? I am using pymc rather than pymc3

There’s something weird going on here with the starting values of logs. Doing something like pm.math.exp on a large number like 44700 might be leading to numerical issues. I can’t see why the value of logs initialized to the values shown here. Do you know why they might be so large?

I will have to check all the data being fed and the models used. I’m not so sure where the large values are coming from. The link to the article I am following is in the question if you are interested in reading further :slight_smile:

I think the content is 90% similar, but you might also check out the stochastic volatility example in the PyMC docs: Stochastic Volatility model — PyMC example gallery

1 Like

Hi @traderman ! To build on @ckrapu’s answer, are there any specific reasons why you’re specifying test values, starting at MAP and tweaking the step sampler?

The best practice is usually to let PyMC handle that, as it can have a big impact on sampling – which could explain the error you’re currently having.

So in other words: have you try sampling this model with PyMC’s default settings?

2 Likes

@AlexAndorra I’ll try that out, but I’m not too familiar with pymc so I’m basically testing out existing examples and trying to understand them.

Ah then going with the defaults should make your life easier – and that’s a general rule with PyMC :wink: