# Nan while running find\_MAP

**URL:** <https://discourse.pymc.io/t/nan-while-running-find-map/4275>\
**Category:** Questions\
**Created:** [December 30, 2019, 3:55pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275 "2019-12-30T15:55:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [December 30, 2019, 3:55pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/1 "2019-12-30T15:55:15Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [December 30, 2019, 4:15pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/2 "2019-12-30T16:15:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelosthege](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/michaelosthege/32/371_2.png) [@michaelosthege](https://discourse.pymc.io/u/michaelosthege)\
**Post date:** [December 30, 2019, 4:42pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/3 "2019-12-30T16:42:23Z")

</div>

Could you post a full example code of your model?

Also note that the uniform distribution has no maximum.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [December 30, 2019, 4:57pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/4 "2019-12-30T16:57:49Z")

</div>

@michaelosthege, yes, it looks like my guess was correct. I had such problem because I used previously obtained solution for `testval`s. 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:

```auto
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).

---

<div class="post-metadata">

**Author:** ![michaelosthege](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/michaelosthege/32/371_2.png) [@michaelosthege](https://discourse.pymc.io/u/michaelosthege)\
**Post date:** [December 30, 2019, 5:17pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/5 "2019-12-30T17:17:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [December 30, 2019, 5:23pm UTC](https://discourse.pymc.io/t/nan-while-running-find-map/4275/6 "2019-12-30T17:23:32Z")

</div>

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