Search code examples
pythonstatisticsdistributionlogarithm

Is there a way to generate a lognormal distribution from a pre-defined normal distribution?


I have the code which generates a normal distribution as a pdf, centered at the mean 400, with st

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats

muPrev, sigmaPrev = 400, 40.
a = np.random.normal(muPrev, sigmaPrev, 100000)
count, bins, ignored = plt.hist(a, 1000, density=True)
plt.plot(bins, 1/(sigmaPrev * np.sqrt(2 * np.pi)) *
           np.exp( - (bins - muPrev)**2 / (2 * sigmaPrev**2) ),linewidth=3, color='r')

enter image description here

and I can visualise it. But what if I wanted to convert this into a lognormal distribution? So that I now get values of mu and sigma that correspond to this as a log distribution?


Solution

  • What is posted by @SamMason is not correct. It is somewhat working because your mean and sd are relative large.

    Ok, here is what would be correct way to get parameters of the Log-Normal distribution.

    You have predefined values of mean (corresponding to your Gaussian mean) and sd (again, your Gaussian sd).

    Mean=exp(μ+σ2/2)

    Var =(exp(σ2) - 1)(exp(2μ+σ2))

    Here μ and σ are log-normal (NOT gaussian) parameter. You have to find them.

    1. Compute mean from your Gaussian mean (ok, that one is easy, they are equal)
    2. Compute variance from your Gaussian sd (square)
    3. Using formulas above solve two non-linear equations system and get your μ and σ
    4. Plug μ and σ into your sampling routine and draw samples

    UPDATE

    Mean2=exp(2μ+σ2)

    Var/Mean2 = (exp(σ2) - 1)

    So here is your σ. To be more elaborate

    Sd2/Mean2 = exp(σ2) - 1

    exp(σ2) = 1 + Sd2/Mean2

    σ2 = ln(1 + Sd2/Mean2)

    From first equation now you could get μ

    2μ+σ2 = ln(Mean2)

    2μ=ln(Mean2) - σ2 = ln(Mean2) - ln(1 + Sd2/Mean2) = ln((Mean2)/(1 + Sd2/Mean2))

    Please, check the math, but this is the way to get PRECISE log-normal μ,σ parameters to match desired Mean and Sd.

    @SamMason approximation works, I believe, only if in the expression for

    σ2 = ln(1 + Sd2/Mean2)

    one have second term much larger than 1. THen you could drop 1 and have log of ratios.

    UPDATE II

    2μ=ln((Mean2)/(1 + Sd2/Mean2)) = ln(Mean4/(Mean2 + Sd2))

    μ=1/2 ln(Mean4/(Mean2 + Sd2))=ln(Mean2/Sqrt(Mean2 + Sd2))