Search code examples
pythonrandomprobability-density

Lognormal Distribution


When defining a lognormal function with a mean of 15.1466 and a standard deviation of 0.3738, the result you should get is the following:

enter image description here

However when I run the following code to do it with python the result I get is not the same.

mu, sigma = 15.1466, 0.3738
s = np.random.lognormal(mu, sigma, 10000)
count, bins, ignored = plt.hist(s, 30,
                                density=True, 
                                color='blue')
x = np.linspace(min(bins),
                max(bins), 10000)
  
pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))
       / (x * sigma * np.sqrt(2 * np.pi)))
  
plt.plot(x, pdf, color='black')
plt.grid()
plt.show()

enter image description here


Solution

  • Mean and standard deviation are NOT THE SAME as distribution parameters mu and sigma for log-normal

    Check wiki

    Basically, given mean and std.dev, you compute mean and variance, and then solve system of two equations to find mu and sigma

    Only then you feed mu and sigma into routines for sampling, PDF etc