# Variable values in a Log normal model

**URL:** <https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562>\
**Category:** v3\
**Created:** [July 20, 2023, 5:42pm UTC](https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562 "2023-07-20T17:42:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [July 20, 2023, 5:42pm UTC](https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562/1 "2023-07-20T17:42:39Z")

</div>

I am trying to build a log Normal model. Below is an example of the code that I am working on -

```auto
a = pm.Normal("a", mu = 10, sd = 1)
b1= pm.Normal("b1", mu = 2, sd = 1)
b2= pm.Normal("b2", mu = 2, sd = 1)

mu=a+b1*df['x1']+b2*df['x2']
contrib=pm.Deterministic("contrib",b1*df['x1']+b2*df['x2'])

sd = pm.Exponential("sd", 0.5)
y_obs = pm.Lognormal("obs", mu=mu, sigma=sd, observed=df.value)

```

Since, a,b1,b2 are on the log scale, and I am using pm.LogNormal, the “obs” values in the posterior samples are on the normal scale(not log). But ‘contrib’ is on a log scale in the posterior samples. Is there a way, I can have ‘contrib’ values too on the normal scale(not Log)?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [July 21, 2023, 2:50pm UTC](https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562/2 "2023-07-21T14:50:33Z")

</div>

You can exponentiate the values inside the Deterministic with `pm.math.exp` if I understand you correctly.

---

<div class="post-metadata">

**Author:** ![Ansul](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ansul](https://discourse.pymc.io/u/Ansul)\
**Post date:** [July 21, 2023, 5:15pm UTC](https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562/3 "2023-07-21T17:15:49Z")

</div>

Am I supposed to do an adjustment of pm.math.exp(σ2/2) because here I am trying to exponentiate something which is on a log scale? Please refer to this link where it talks about getting y from log y?

> <https://stats.stackexchange.com/questions/440695/predicting-y-from-log-y-as-the-dependent-variable>

---

<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:** [July 22, 2023, 2:24am UTC](https://discourse.pymc.io/t/variable-values-in-a-log-normal-model/12562/4 "2023-07-22T02:24:24Z")

</div>

It depends what you want. In the linked post, they are talking about computing the expected value of Y, given the model. In that case the closed form solution comes from the mean of the log-normal distribution. Since you have samples, you can skip the theory and just compute it as the mean of your samples.

On the other hand, if you just want to know the value of `contrib` in levels, you can just exp it. The expected value of `exp(contrib)` should be different from the expected value of `y_obs` due to [Jensen’s Inequality](https://en.wikipedia.org/wiki/Jensen%27s_inequality#Probabilistic_form), which you could verify by comparison.
