# LogNormal-Distribution - how to set mu and sigma

**URL:** <https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101>\
**Category:** Statistics\
**Tags:** question, statistics\
**Created:** [November 16, 2017, 3:54pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101 "2017-11-16T15:54:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 16, 2017, 3:54pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/1 "2017-11-16T15:54:31Z")

</div>

Hey there! I’m trying to draw random numbers from a Log-Normal distribution with a given mean and standard-deviation. As far as i know, the `LogNormal(\mu \sigma)` lets you set the mean and standard deviation of the distribution. I am not really clear however, if I should feed the function with log(valuex) or just with value x, for either mu or sigma. I’m not so sure, what the documentation tries to explain (maybe it’s lack of maths…). it states:

> LogNormal(mu, sig) # Log-normal distribution with log-mean mu and scale sig

So how do I do this? Say, I want to use x as mean and y as scale, do I draw from `LogNormal(log(x), y)`? In R I would use the log(x), but how about Julia? Thanks so much!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2017, 4:00pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/2 "2017-11-16T16:00:54Z")

</div>

You have to calculate `μ` if you want a given mean. See the [wikipedia page of about the lognormal](https://en.wikipedia.org/wiki/Log-normal_distribution). For example, you could do

```julia
using Distributions

μ_for_mean(m, σ) = log(m) - σ^2/2

m = 2
σ = 0.5
d = LogNormal(μ_for_mean(m, σ), σ)

mean(d) ≈ m # voila

```

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 16, 2017, 5:44pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/3 "2017-11-16T17:44:07Z")

</div>

Thanks! That answers my question nicely 🙂 one of topic question: how did you make the mu ign a mu sign? I tried `\mu` but that didn’t work… Thanks again!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2017, 6:32pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/4 "2017-11-16T18:32:43Z")

</div>

Press `[TAB]` after `\mu` in the REPL (I actually made it in Emacs though, using [company-math](https://github.com/vspinu/company-math)). See [unicode input](https://docs.julialang.org/en/latest/manual/unicode-input/#Unicode-Input-1).

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 18, 2017, 1:33pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/5 "2017-11-18T13:33:36Z")

</div>

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [November 18, 2017, 2:30pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/6 "2017-11-18T14:30:33Z")

</div>

Notice that σ is not the standard deviation of the LogNormal distribution, e.g.

```julia
julia> std(LogNormal(0,1))
2.1611974158950877

```

so if your inputs are the mean and standard deviation then the problem of finding μ and σ is a bit harder since there is no closed form solution. I actually had to do this recently and ended up doing something like

```julia
julia> f = (θ, lm) -> norm([θ[1] + θ[2]^2/2 - lm[1], log(exp(θ[2]^2) - 1) + 2*θ[1] + θ[2]^2 - lm[2]])^2
(::#71) (generic function with 1 method)

julia> LogNormal(Optim.minimizer(optimize(t -> f(t, log.([1, 1])), [1.0, 1.0], BFGS()))...) |> t -> (mean(t), std(t))
(1.0000000001643707, 1.000000000094841)

```

Notice that solving for the logarithm of the mean and standard deviations works much better that solving for the untransformed variables.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 21, 2017, 10:45am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/7 "2017-11-21T10:45:10Z")

</div>

Thanks so much! I ended up using the definitions given for μ and σ on Wikipedia ([Log-normal distribution - Wikipedia](https://en.wikipedia.org/wiki/Log-normal_distribution)), like follows:

> ParVal=readdlm(“StandardScenario\_alpha4\_2.txt”)  
> hind=Array{Float64,1}(ParVal[2:end, 4])  
> gind=Array{Float64,1}(ParVal[2:end, 5])  
> uniquehind=unique(hind)  
> uniquegind=unique(gind)  
> meanhindunique=mean(uniquehind)  
> meangindunique=mean(uniquegind)  
> sthindunique=std(uniquehind)  
> vargindunique=var(uniquegind)  
> **σgindunique=sqrt(log(vargindunique/meangindunique^2+1))**  
> **μgindunique=log(meangindunique) - σgindunique^2/2**

The .txt contains the data I’m using the get the distribution, I didn’t think it would be necessary to give mock-data so you can copy what I did, but rather to illustrate, how I calculated μ and σ.  
For me, it seems similar to what you did (?), but I’m not familiar with the `Optim.minimizer` function you used. However, my supervisor agreed on my calculation, so I guess, for my purposes it works.  
Thank you anyways! 🙂

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [November 21, 2017, 11:32am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/8 "2017-11-21T11:32:11Z")

</div>

Ha. Thanks for the correction. Indeed that is the closed form solution for `σ` and `μ`.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 21, 2017, 11:37am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/9 "2017-11-21T11:37:36Z")

</div>

Yeah for Wikipedia and an interest in maths 😉

---

<div class="post-metadata">

**Author:** ![kurbkid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kurbkid/32/9791_2.png) [@kurbkid](https://discourse.julialang.org/u/kurbkid)\
**Post date:** [May 21, 2019, 2:04pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/10 "2019-05-21T14:04:01Z")

</div>

Hello from the future. If you know both the mean and standard deviation you want, this function does the trick:

```julia
using Distributions

function myLogNormal(m,std)
    γ = 1+std^2/m^2
    μ = log(m/sqrt(γ))
    σ = sqrt(log(γ))

    return LogNormal(μ,σ)
end

```

I know the transformation already appeared in your later comments, but I thought it’s worth it to post some clear code.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 4, 2019, 8:13am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/11 "2019-06-04T08:13:17Z")

</div>

This seems like it would be a worthwhile addition to `Distributions.jl`, don’t you think? A super easy PR it seems.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 4, 2019, 11:21am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/12 "2019-06-04T11:21:29Z")

</div>

It would be interesting to have a wrapper for moments, and then utilize the [fit framework](https://juliastats.github.io/Distributions.jl/stable/fit/), eg have something like

```julia
fit(LogNormal, Moments(m, std))

```

do the above.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [June 4, 2019, 1:56pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/13 "2019-06-04T13:56:03Z")

</div>

Yes, that would be really neat to include this!

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [June 4, 2019, 1:57pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/14 "2019-06-04T13:57:30Z")

</div>

Perfect! Much clearer this way 🙂

---

<div class="post-metadata">

**Author:** ![John\_Coppola](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_coppola/32/16462_2.png) [@John\_Coppola](https://discourse.julialang.org/u/John_Coppola)\
**Post date:** [December 25, 2021, 2:19am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/15 "2021-12-25T02:19:32Z")

</div>

Does Moments exist in Distributions package?

---

<div class="post-metadata">

**Author:** ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)\
**Post date:** [December 25, 2021, 10:39am UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/16 "2021-12-25T10:39:11Z")

</div>

You may be interested in MeasureTheory.jl

[![](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e2fef22164b91f1331d7ecd41b0eecc0c507e17.jpeg "Applied Measure Theory for Probabilistic Modeling | Chad Scherrer | JuliaCon2021") ](https://www.youtube.com/watch?v=Jr6kp0cHiJA)

Cc @cscherrer

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [December 25, 2021, 2:24pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/17 "2021-12-25T14:24:34Z")

</div>

Thanks @EvoArt ! I don’ know if this is what you have in mind, but one important design point about MeasureTheory is to make it easy to work with different parameterizations. The setup is that a `ParameterizedMeasure` is just a struct with a `NamedTuple` field, and we use `KeywordCalls.jl` for dispatch.

So for example, we can have a `LogNormal(μ,σ)` set up to work exactly like Distributions, and also have `LogNormal(moments=(m,std))` available.

To be fair, Distributions could be adapted to do this as well, but in that case it would probably just build a LogNormal with the standard parameterization. The advantage of the MeasureTheory approach is that we allow different parameterizations to be used directly, without conversion to some standard form. Here that doesn’t help so much (I think you’d probably want to convert it anyway), but in some cases it can make things much more efficient.

---

<div class="post-metadata">

**Author:** ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)\
**Post date:** [December 30, 2021, 3:20pm UTC](https://discourse.julialang.org/t/lognormal-distribution-how-to-set-mu-and-sigma/7101/18 "2021-12-30T15:20:08Z")

</div>

@John_Coppola see this new package also [https://github.com/bgctw/DistributionFits.jl](https://github.com/bgctw/DistributionFits.jl)
