user25640
user25640

Reputation: 235

How to fit a lognormal distribution

I want to fit a lognormal distribution in Python. My question is why should I use scipy.lognormal.fit instead of just doing the following:

from numpy import log
mu = log(data).mean()
sigma = log(data).std()

which gives the MLE of mu and sigma so that the distribution is lognormal(mu, sigma**2)?

Also, once I get mu and sigma how can I get a scipy object of the distribution lognormal(mu, sigma**2)? The arguments passed to scipy.stats.lognorm are not clear to me.

Thanks

Upvotes: 2

Views: 2639

Answers (1)

Severin Pappadeux
Severin Pappadeux

Reputation: 20130

Wrt fitting, you could use scipy.lognormal.fit, you could use scipy.normal.fit applied to log(x), you could do what you just wrote, I believe you should get pretty much the same result.

The only thing I could state, that you have to fit two parameters (mu, sigma), so you have to match two values. Instead of going for mean/stddev, some people might prefer to match peaks, thus getting (mu,sigma) from mode/stddev.

Wrt using lognorm with known mean and stddev

from scipy.stats import lognorm

stddev = 0.859455801705594
mean = 0.418749176686875

dist=lognorm([stddev],loc=mean) # will give you a lognorm distribution object with the mean and standard deviation you specify.

# You can then get the pdf or cdf like this:

import numpy as np
import pylab as pl
x=np.linspace(0,6,200)
pl.plot(x,dist.pdf(x))
pl.plot(x,dist.cdf(x))

pl.show()

enter image description here

Upvotes: 1

Related Questions