Nan while running find_MAP

Hello all,

I run find_MAP with uniform priors such as Uniform('x', 0, 500), but sometimes it fails and gives me logp=inf with the outcome 'x': nan. Just wondring, is it possible? Because even if it fails, the variable should be bounded (in this case b/w 0 and 500)

It looks like if I define:
x = Uniform('x', 0, 500, testval = 500)
find_MAP then fails and it gives me nan for x

Could you post a full example code of your model?

Also note that the uniform distribution has no maximum.

@michaelosthege, yes, it looks like my guess was correct. I had such problem because I used previously obtained solution for testvals. This means that if I would get an upper bound in my previous solution, I potentially encounter the inf problem.

If desirably, I may try to write a reproducible example, but the excerpt looks like that:

Rmn = .5
Rmx = 2.8
with pm.Model() as model:
    Rmin = pm.Uniform('Rmin', Rmn, Rmx, testval=inits['Rmin'][-nprefects:], shape=nprefects)
    ΔR = pm.Uniform('ΔR', 0, Rmx, testval=inits['ΔR'][-nprefects:], shape=nprefects)
    ε = pm.Uniform('ε', 0, .5, testval=inits['ε'][-nprefects:], shape=nprefects)
    ρ = pm.Uniform('ρ', 0, 1, testval=inits['ρ'][-nprefects:], shape=nprefects)
    logφ = pm.Uniform('logφ', 4, 9, testval=inits['logφ'][-nprefects:], shape=nprefects)
    popSize = tt.as_tensor_variable(np.array(popSizes,dtype=np.float64))
    Infected0 = pm.Uniform('Infected0', 0, 500, 
                           testval=inits['Infected0'][-nprefects:], shape=nprefects)
    Immune_fraction0 = tt.as_tensor_variable(np.dtype('float64').type(0.2))

    # Solving ODEs
    initial = tt.stack([Rmin, ΔR, ε, popSize, Infected0, 
                        tt.repeat(Immune_fraction0, nprefects)], axis=1)
    forward = forward_model(initial, theano.shared(Σ))

    μ = ρ*forward
    α = popSize/tt.exp(logφ)
    pm.TruncatedNormal('Y_obs', μ, α + 1e-6, lower=0, observed=Y) 

    solMLE_raw = pm.find_MAP(return_raw = True, include_transformed = False, progressbar = True, maxeval=1000000)

solMLE = solMLE_raw[0]
solMLE 

To note that inf arises when I have also a problematic fit (trying to fit something that looks not so good).

When logp=-inf happens, it is often one of these:

  1. observed data that is outside of the support of the likelihood function (e.g. a nan in the data)
  2. a parameter set that makes no sense for the model (e.g. a negative value for a parameter that may only be positive, or a standard deviation of 0)
  3. a parameter combination that, while mathematically possible, is numerically impossible to work with

Since you’re using a TruncatedNormal as a likelihood function, problem 1. may be what you’re looking at.

2 Likes

Thank you a lot! I will check carefully (it is always challenging for larger datasets)