# Sampling with a general lognormal model

**URL:** <https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145>\
**Category:** Questions\
**Created:** [November 27, 2019, 12:01am UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145 "2019-11-27T00:01:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [November 27, 2019, 12:01am UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/1 "2019-11-27T00:01:31Z")

</div>

Hey everyone

For the project i have been working on i am trying to implement a model which is a sum of lognormals and so i wanted to test how Pymc3 handles a general lognormal of the form ![lognorm](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/5/5464b9aee35d9ed289fe1b98d96b14ec63edb0a6.png)

here is my implementation in Pymc

```
with pm.Model() as model:
mval = pm.Normal('mval', mu=5.0, sigma=5.0)
sval = pm.Normal('sval',mu=2.0,sigma=2.0)
tval = pm.Normal('tval', mu=5.0, sigma=5.0)

LN = tt.exp(-tt.log((x-tval)/mval)**2/(2*sval**2))/((x-tval)*sval*tt.sqrt(2*np.pi))
LN = tt.switch(tt.isnan(LN), 0.0, LN)

y_ = pm.Normal("y", mu=LN, observed=lognorm)

trace = pm.sample(1000,tune=1000)

```

the switch is there so i do not get NaN values when tval\>x. I also took a note from the Stan people and used normal priors instead of uniform ones .“lognorm” is obtained from using the same equation with the parameters mval=2.5, sval=1.5 and tval=7.5. The number of points here is set using numpy.linspace

```
x=np.linspace(0,50,1000)

```

When i run the sampler i am surprised to find that is diverges between 60-90 times for each chain after tuning. The mean parameter values found are off from the ones i inputted for lognorm and with a relatively high sd. I also find that the number of effective samples is low (less than 10%)

 ![lognormstats](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/6/654347771829aa3cfdd71cdb8899f3a2c4b10d3c.png)

 ![lognormtrace](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/eb2a6314e32943293c392dd268ea0f8239c115ea.png)

Can someone help me understand why the sampler is having trouble with this lognormal? Again i am surprised by the result because a lognormal only has 3 parameters. Is there some way to define the lognormal differently so it keeps the sampler from diverging?

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [November 27, 2019, 1:54pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/2 "2019-11-27T13:54:56Z")

</div>

Hi there!  
I’m not well versed into lognormals, but is there any reason why you’re not using [the built-in distribution](https://docs.pymc.io/api/distributions/continuous.html#pymc3.distributions.continuous.Lognormal)?

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [November 27, 2019, 11:18pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/3 "2019-11-27T23:18:14Z")

</div>

Hey Alex

Thanks for linking the built-in distributions. I took a look over them and the lognormal there is missing the location parameter (theta) to shift the function on the x-axis. It also has mu, which is the mean of the log of the distribution, instead of the median of the distribution m. From trying different priors, it seems to be the location parameter and median that are the most obvious problem here. Theta just takes on a value close to the middle of the prior you define (if it is uniform) and m is just off.

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [November 28, 2019, 8:20pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/4 "2019-11-28T20:20:57Z")

</div>

Okay i have tried to limit the function to only the median and sigma. I tried a couple of different parameter values and priors and i think i understand where the problems lies. I just have to specify sigma for the likelihood. I am not sure what happens if sigma is not specified (i would suspect some like sigma=1.0). I just used the standard deviation of the input data and that seemed to work.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [November 29, 2019, 5:54pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/5 "2019-11-29T17:54:08Z")

</div>

Out of curiosity, you used your custom function or the built-in one?

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [November 29, 2019, 6:45pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/6 "2019-11-29T18:45:05Z")

</div>

The custom one. I needed the location paramter theta for what i am working on, so i wanted to try using my custom function.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [November 30, 2019, 4:37pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/7 "2019-11-30T16:37:52Z")

</div>

Thanks, good to know!  
I’m not used to log-normals, but if you feel like the location parameter is customary and would be useful to have in the built-in distribution, I think a PR would be greatly appreciated 😉

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [December 2, 2019, 5:13pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/8 "2019-12-02T17:13:46Z")

</div>

I would be glad to 😁 I have not tried to do a PR before, but i will try it. The lognormal can be written up a couple of ways, but let me implement a modified version of the one already used.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [December 3, 2019, 1:16pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/9 "2019-12-03T13:16:19Z")

</div>

Awesome, thank you! Don’t hesitate if you have questions – not a PR expert either, but maybe I’ll be able to help 🙂

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [December 3, 2019, 6:43pm UTC](https://discourse.pymc.io/t/sampling-with-a-general-lognormal-model/4145/10 "2019-12-03T18:43:57Z")

</div>

Okay i have opened up a issue on the github with a first try [here](https://github.com/pymc-devs/pymc3/issues/3703). Not entirely sure how to test if this works, but it might still need some checks to see if x\>theta.
