如何在 python 中生成具有特定均值和标准差的对数正态分布?
How to generate lognormal distribution with specific mean and std in python?
我需要生成均值为 1 且标准差为 1 的对数正态分布。即:w~logN(1,1)
。我需要变量 w
有 mu=1
和 sigma=1
。但是,当我使用 scipy.stats.lognorm
时,我在操作参数 s,loc,sigma
时遇到了麻烦。代码如下:
import numpy as np
from scipy.stats import lognorm
lo = np.log(1/(2**0.5))
sig = (np.log(2.0))**0.5
print(lognorm.stats(s=sig,loc=lo,scale=1.0,moments='mv'))
结果是:
(array(1.06763997), array(2.))
这显然不是我想要的。我想要 mean=1 和 sigma=1.
谁能告诉我如何使用 s、loc 和 scale 进行操作以获得所需的结果?
编辑:也许看看这个答案:
现在可能为时已晚,但我可以解决您的问题。我不知道对数正态分布是如何工作的,也不知道您如何通过数学方式推导出值以达到您想要的结果。但是你可以使用标准化以编程方式做你想做的事。
示例:
我假设你有这样的东西:
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
plt.hist(dist, bins=100)
print(np.mean(dist))
print(np.std(dist))
输出:
mean: 1.0200
std: 0.2055
现在我不知道您需要将哪些参数输入 lognorm 才能获得您想要的均值 1 和标准差 1。我会对此感兴趣。
但是,您可以标准化此分布。
标准化意味着最终分布的均值为 0,标准差为 1。
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
# standardisation to get mean = 0, std = 1
dist = (dist - np.mean(dist)) / np.std(dist)
plt.hist(dist, bins=100)
print(f"mean: {np.mean(dist):.4f}")
print(f"std: {np.std(dist):.4f}")
mean: 0.0000
std: 1.0000
现在您可以反转此过程以获得您想要的任何平均值。假设你想要 mean = 123, std = 456:
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
# standardisation to get mean = 0, std = 1
dist = (dist - np.mean(dist)) / np.std(dist)
# get desired mean + std
dist = (dist * 456) + 123
plt.hist(dist, bins=100)
print(f"mean: {np.mean(dist):.4f}")
print(f"std: {np.std(dist):.4f}")
产出
mean: 123.0000
std: 456.0000
形状本身与最初相同。
我需要生成均值为 1 且标准差为 1 的对数正态分布。即:w~logN(1,1)
。我需要变量 w
有 mu=1
和 sigma=1
。但是,当我使用 scipy.stats.lognorm
时,我在操作参数 s,loc,sigma
时遇到了麻烦。代码如下:
import numpy as np
from scipy.stats import lognorm
lo = np.log(1/(2**0.5))
sig = (np.log(2.0))**0.5
print(lognorm.stats(s=sig,loc=lo,scale=1.0,moments='mv'))
结果是:
(array(1.06763997), array(2.))
这显然不是我想要的。我想要 mean=1 和 sigma=1.
谁能告诉我如何使用 s、loc 和 scale 进行操作以获得所需的结果?
编辑:也许看看这个答案:
现在可能为时已晚,但我可以解决您的问题。我不知道对数正态分布是如何工作的,也不知道您如何通过数学方式推导出值以达到您想要的结果。但是你可以使用标准化以编程方式做你想做的事。
示例:
我假设你有这样的东西:
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
plt.hist(dist, bins=100)
print(np.mean(dist))
print(np.std(dist))
输出:
mean: 1.0200
std: 0.2055
现在我不知道您需要将哪些参数输入 lognorm 才能获得您想要的均值 1 和标准差 1。我会对此感兴趣。 但是,您可以标准化此分布。
标准化意味着最终分布的均值为 0,标准差为 1。
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
# standardisation to get mean = 0, std = 1
dist = (dist - np.mean(dist)) / np.std(dist)
plt.hist(dist, bins=100)
print(f"mean: {np.mean(dist):.4f}")
print(f"std: {np.std(dist):.4f}")
mean: 0.0000
std: 1.0000
现在您可以反转此过程以获得您想要的任何平均值。假设你想要 mean = 123, std = 456:
dist = scipy.stats.lognorm.rvs(0.2, 0, 1, size=100000)
# standardisation to get mean = 0, std = 1
dist = (dist - np.mean(dist)) / np.std(dist)
# get desired mean + std
dist = (dist * 456) + 123
plt.hist(dist, bins=100)
print(f"mean: {np.mean(dist):.4f}")
print(f"std: {np.std(dist):.4f}")
产出
mean: 123.0000
std: 456.0000
形状本身与最初相同。