The distributions in scipy are coded in a generic way wrt two parameter location and scale so that location is the parameter (loc
) which shifts the distribution to the left or right, while scale
is the parameter which compresses or stretches the distribution.
For the two parameter lognormal distribution, the "mean" and "std dev" correspond to log(scale
) and shape
(you can let loc=0
).
The following illustrates how to fit a lognormal distribution to find the two parameters of interest:
In [56]: import numpy as np
In [57]: from scipy import stats
In [58]: logsample = stats.norm.rvs(loc=10, scale=3, size=1000) # logsample ~ N(mu=10, sigma=3)
In [59]: sample = np.exp(logsample) # sample ~ lognormal(10, 3)
In [60]: shape, loc, scale = stats.lognorm.fit(sample, floc=0) # hold location to 0 while fitting
In [61]: shape, loc, scale
Out[61]: (2.9212650122639419, 0, 21318.029350592606)
In [62]: np.log(scale), shape # mu, sigma
Out[62]: (9.9673084420467362, 2.9212650122639419)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…