I'm a little confused by lognormal distributions.

Let's say I have a parameter that is distributed as:

\begin{equation}
V \sim \mathcal{N}(5, 2) \, ,
\end{equation}

how would I represent this in a lognormal distribution so it can't fall below zero? A lognormal distribution is defined as:

\begin{equation}
\ln(V) \sim \ln(\mathcal{N}(5, 2)) \, ,
\end{equation}

> In probability theory, a lognormal distribution is a ... probability distribution o a random variable who's *logarithm is normally distributed*.

> Thus if $X$ is log-normally distributed, then $Y - \ln(X)$ is normally distributed.


```python
import numpy as np
import seaborn as sns
import pylab as plt
```


```python
npts = 5000
mu = 10
sig = 2
v = np.random.normal(mu, sig, npts)
```


```python
sns.distplot(v)
plt.xlabel('V')
plt.axvline(mu)
```

This extends below 0, which we don't want it to do. What does the logarithm look like?


```python
sns.distplot(np.log(v))
plt.xlabel(r'$\ln(V)$')
plt.axvline(np.log(mu))
plt.show()
```

If we wanted a lognormal distribution to recapture this same distribution, should we be using

\begin{equation}
\ln(V) \sim \log\mathcal{N}(\ln(10), \ln(2)) \, ?
\end{equation}


```python
lnv = np.random.lognormal(np.log(mu), np.log(2), npts)
```


```python
sns.distplot(lnv, label='LogNormal')
sns.distplot(np.log(v), label='ln(V)')
plt.legend()
```

Okay, these don't line up...


```python
sns.distplot(lnv, label='LogNormal')
sns.distplot(v, label='V')
```

I guess this only truly works if the logarithm of V is normally distributed, but in this case we want it to be the other way round?


```python
import mystyle as ms
with plt.style.context(ms.ms):
    sns.distplot(v)
    sns.distplot(lnv)
    plt.axvline(np.median(v), c='r', label='Median V')
    plt.axvline(np.median(lnv), c='g', label='Median LnV')
    plt.axvline(np.mean(lnv), c='b', label='Mean LnV')
    plt.legend()
```

In both cases the median values intersect at $\mu$, but with $V$ distributed lognormally, it can not fall below zero.

Let's try a more realistic example.


```python
npts = 10000
mu = 0.7
sigma = .1
v = np.random.normal(mu, sigma, npts)
lnv = np.random.lognormal(np.log(mu), sigma, npts)
```


```python
with plt.style.context(ms.ms):
    sns.distplot(v, label='Normally distributed')
    sns.distplot(lnv, label='Lognormally distributed')
    plt.legend()
print(f'Median of Normal: {np.median(v)}')
print(f'Median of LogNormal: {np.median(lnv)}')
```

This example indicates that $\sigma$ should remain as is.

The equations seem to agree. Let's check our previous example:


```python
npts = 10000
mu = 10.
sigma = 2.
v = np.random.normal(mu, sigma, npts)
lnv = np.random.lognormal(np.log(mu), sigma, npts)
```


```python
with plt.style.context(ms.ms):
    sns.distplot(v, label='Normally distributed')
    sns.distplot(lnv, label='Lognormally distributed')
    plt.legend()
print(f'Median of Normal: {np.median(v)}')
print(f'Median of LogNormal: {np.median(lnv)}')
```

Yep, the plots are unsightly but otherwise okay.

Conclusions: our parameters that can't go below zero should be distributed as 

\begin{equation}
V \sim \rm{LogNormal}(\ln(\mu), \sigma)
\end{equation}

deec48dbf4ae8283172fed25d3837d85ab232643
ipynb
Jupyter Notebook
code/tests/examples/investigate_lognormal.ipynb
ojhall94/malatium
156e44b9ab386eebf1d4aa05254e1f3a7b255d98
MIT
2021-04-25T07:45:20.000Z
2021-04-25T07:45:20.000Z
code/tests/examples/investigate_lognormal.ipynb
ojhall94/malatium
156e44b9ab386eebf1d4aa05254e1f3a7b255d98
MIT
code/tests/examples/investigate_lognormal.ipynb
ojhall94/malatium
156e44b9ab386eebf1d4aa05254e1f3a7b255d98
MIT
2022-02-19T09:38:35.000Z
2022-02-19T09:38:35.000Z
Qwen/Qwen-72B
1. YES
2. YES