# Sampler (NUTS) proposes the same value

**URL:** <https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142>\
**Category:** Questions\
**Created:** [April 26, 2018, 5:41pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142 "2018-04-26T17:41:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 26, 2018, 5:41pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/1 "2018-04-26T17:41:36Z")

</div>

Hi! I have the following problem and couldn’t find any way to debug it further. When I run the code:

```auto
with pm.Model() as model:
    B1 = pm.Lognormal('B1', -2, 2)
    B1_print = tt.printing.Print('B1')(B1)
    jm.B1 = B1_print
    image_model = jm.image()
    image_obs = pm.Normal('image_obs', mu=image_model, sd=1e-6, observed=image_data)
    
    trace = pm.sample(chains=1, njobs=1, step=pm.NUTS())

```

where `jm` is a largeish class so I don’t include it here (but it works correctly when I e.g. set `jm.B1 = 0.1` and call `jm.image.eval()`) it alternates between two outputs for B1:

```auto
B1 __str__ = 0.15222451156570388
B1 __str__ = nan
B1 __str__ = 0.15222451156570388
B1 __str__ = nan
B1 __str__ = 0.15222451156570388
B1 __str__ = nan

```

the number changes between code runs, but remains the same during a single run. Any ideas or suggestions what to try?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 26, 2018, 6:13pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/2 "2018-04-26T18:13:41Z")

</div>

Seems to me the sampler is rejecting all proposal. You should not call the step directly, instead do `trace = pm.sample()` which use the auto initialization with multiple chain.

Also, are you sure the input to B1 is valid?

---

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 26, 2018, 6:20pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/3 "2018-04-26T18:20:12Z")

</div>

I also used plain `pm.sample` without any parameters and got the same result.

And what do you mean by B1 validity? As I say it works when I set `jm.B1 = 0.1` and call `jm.image().eval()`.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 26, 2018, 6:24pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/4 "2018-04-26T18:24:27Z")

</div>

> [@aplavin](#):
>
> I also used plain pm.sample without any parameters and got the same result.

Then what likely happen is that the starting value is at some pathological region, which the sampler could not propose a new statue that get out from the region.

Try with other starting value (in this case using `init='advi+adapt_diag'` might helps).

---

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 26, 2018, 6:28pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/5 "2018-04-26T18:28:07Z")

</div>

Well, that seems unlikely, but will try. The B1 used to generate `image_data` is exactly in the middle of its prior (currently I test the inference on model data).

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 26, 2018, 6:30pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/6 "2018-04-26T18:30:49Z")

</div>

If `B1` is a neural network, you can easily have wrong prior that makes sampling impossible because the model is multimodal and ill defined.

---

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 26, 2018, 6:32pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/7 "2018-04-26T18:32:03Z")

</div>

Nope, nothing of this kind 🙂 its an analytical physical model and I expect a unimodal posterior.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 26, 2018, 6:34pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/8 "2018-04-26T18:34:51Z")

</div>

Hmm, it would be easier to check if you can share the full model/data in a notebook.

---

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 26, 2018, 9:58pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/9 "2018-04-26T21:58:13Z")

</div>

It turned out that the issue has something to do with theano broadcasting, and I managed to fix it (even through not completely understanding).

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 27, 2018, 6:58pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/10 "2018-04-27T18:58:41Z")

</div>

aplavin, can you please share what the error was in regard to theano broadcasting and how you fixed it. That will allow others to overcome this error if they face it in the future. Thank you

---

<div class="post-metadata">

**Author:** ![aplavin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aplavin/32/259_2.png) [@aplavin](https://discourse.pymc.io/u/aplavin)\
**Post date:** [April 27, 2018, 8:28pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/11 "2018-04-27T20:28:17Z")

</div>

As I say, it’s not clear to me what the problem was. I fixed it with manually broadcasting two numpy arrays which were 2nd and 3rd arguments to `tt.where`. Everything worked correctly before this fix when I set parameters like `jm.B1 = 1` and call `jm.image().eval()`, but somehow failed when using this with pymc. After this fix I managed to sample this model and get correct results.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 27, 2018, 8:29pm UTC](https://discourse.pymc.io/t/sampler-nuts-proposes-the-same-value/1142/12 "2018-04-27T20:29:05Z")

</div>

thank you
