问题
I am given the log10 mean and log10 standard deviation of a log-normal distribution. I want to get a random number from this log-normal distribution.
Can this be accomplished with numpy.random.lognormal, even though that function's inputs are the mean and standard of the underlying normal distribution (I do not have that)?
Also, will the random number that I get back from the function be log10, natural log, or regular?
回答1:
Wikipedia says that the parameters of lognormal distribution are expressed in terms of underlying normal distribution thus:
lognormal_mean = np.exp(normal_mean + normal_std**2 / 2)
lognormal_std = np.sqrt(np.exp(normal_std**2) - 1) * np.exp(normal_mean + normal_std**2 / 2)
With a bit of algebra these can be reversed:
normal_std = np.sqrt(np.log(1 + (lognormal_std/lognormal_mean)**2))
normal_mean = np.log(lognormal_mean) - normal_std**2 / 2
And then you can use those to generate a sample. Here is an example:
lognormal_mean = 3
lognormal_std = 5
normal_std = np.sqrt(np.log(1 + (lognormal_std/lognormal_mean)**2))
normal_mean = np.log(lognormal_mean) - normal_std**2 / 2
sample = np.random.lognormal(normal_mean, normal_std, size=10000000)
print(sample.mean(), sample.std())
In a trial run, the output was 3.00126241708, 4.99737569477 - in agreement with the parameters 3, 5.
The "log" in "lognormal" always stands for natural logarithm (base e), so this is what you will get.
Finally, if your input data is log10(lognormal_mean) and log10(lognormal_std) then the first step would be
lognormal_mean = 10**log10_lognormal_mean_
lognormal_std = 10**log10_lognormal_std
I would also check the source to find if they use the ambiguous phrase "log10 mean" to mean "log10 of mean" or "mean of log10". If it was "mean of log10" then you don't need anything above; you already have the parameters of underlying normal distribution, they just need to be multiplied by log(10) to convert from log10 to natural.
来源:https://stackoverflow.com/questions/48014712/get-lognormal-random-number-given-log10-mean-and-log10-standard-deviation