# Recreating Normal Distrbution using logp - sampling diverges

**URL:** <https://discourse.pymc.io/t/recreating-normal-distrbution-using-logp-sampling-diverges/13937>\
**Category:** General\
**Tags:** modeling\
**Created:** [March 5, 2024, 5:44am UTC](https://discourse.pymc.io/t/recreating-normal-distrbution-using-logp-sampling-diverges/13937 "2024-03-05T05:44:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![QYoda](https://avatars.discourse-cdn.com/v4/letter/q/b487fb/32.png) [@QYoda](https://discourse.pymc.io/u/QYoda)\
**Post date:** [March 5, 2024, 5:44am UTC](https://discourse.pymc.io/t/recreating-normal-distrbution-using-logp-sampling-diverges/13937/1 "2024-03-05T05:44:30Z")

</div>

To get a sense how logp and CustomDist work, I attempted to re-create the normal distribution.

In my mind, both models should create the same results:

```auto

def ndichte(value,mu,sigma):
    res = -0.5 * pt.pow((value - mu) / sigma, 2) - pt.log(pt.sqrt(2.0 * np.pi)) - pt.log(sigma)
    return res

with pm.Model() as model1:
    mu = pm.Normal("mu")
    sigma = pm.HalfNormal("sigma")
    pm.CustomDist("Y", mu, sigma, logp=ndichte)

with pm.Model() as model2:
    mu = pm.Normal("mu")
    sigma = pm.HalfNormal("sigma")
    pm.Normal("Y", mu, sigma, observed=obs)

```

I sampled from the same data obs, simple N(0,1) - distributed.

Yet, model1 produced divergences and a ‘wobbly’ trace, while model2 was reasonable.

My questions:  
-1 What happens under the hood when running model1, that is different from model2.

-2 What do I need to modify to make both models equivalent?

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [March 5, 2024, 6:02am UTC](https://discourse.pymc.io/t/recreating-normal-distrbution-using-logp-sampling-diverges/13937/2 "2024-03-05T06:02:28Z")

</div>

There’s no observed argument in model1, is that a typo? When I add it everything runs fine.

In general, it can be more difficult to sample from priors using MCMC than from posteriors. See [this discussion](https://discourse.pymc.io/t/why-does-mcmc-without-observations-have-difficulty-sampling/12809/4) for details.
