首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对数正态分布

对数正态分布
EN

Stack Overflow用户
提问于 2019-01-12 04:09:32
回答 2查看 1.4K关注 0票数 1

我有一个关于对数正态分布的问题。我想创建和集成“质量”从10到10**5的正态分布的对象。我认为这将是一个对数正态分布,所以我开始尝试在python中这样做:

代码语言:javascript
复制
mu, sigma = 3., 1. # mean and standard deviation
s = np.random.lognormal(mu, sigma, 1000)
count, bins, ignored = plt.hist(s, 1000, density=True, align='mid')
x = np.linspace(min(bins), max(bins), 1000)
pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))
plt.plot(x, pdf, linewidth=2, color='r')
plt.xscale('log')
plt.show()

如numpy的示例所示,但是更改µ和sigma并查看图表,我真的不能确定将m和v(遵循下面链接的wikipedia文章)设置为10**5和1000是否满足了我的需要

我查看了https://en.wikipedia.org/wiki/Log-normal_distribution以了解如何计算µ和sigma,但可能我做错了其他事情。这是解决这个问题的正确方法吗?

我读过之前关于对数正态分布的问题/答案,但我不认为他们问了同样的事情。如果此类型的问题已经得到回答,请在高级中表示抱歉。

mu,sigma = 3.,1.是示例中给出的值,可以很好地工作,但当我将mu和sigma更改为如下值时:

代码语言:javascript
复制
m=10**3.5 #where I want the distribution to be centered
v=10000   #the "spread" that I want 
f=1.+(v/m2)
mu=np.log(m/np.sqrt(f))
sigma=np.sqrt(np.log(f))

我没有得到我所期望的..。这是一个以10**3.5为中心的分布,标准差为10000。

尝试建议的内容:

代码语言:javascript
复制
mu=np.log(3000)
sigma=np.log(10)
s = np.random.lognormal(mu, sigma, 1000)
count, bins, ignored = plt.hist(s, 500, density=True, align='mid')
x = np.linspace(min(bins), max(bins), 1000)
pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))
plt.semilogx(x, pdf, linewidth=2, color='r')

这似乎也不起作用,除非我误解了直方图histogram

EN

回答 2

Stack Overflow用户

发布于 2019-01-12 05:26:32

我认为您在解释分布的参数时遇到了困难。

np.random.lognormal的文档在这里:https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.lognormal.html

特别是,均值不是mu10**mu,而是exp(mu),因此给定的分布的均值为e**3 ≈ 20

您似乎希望平均值约为1000,因此将µ和sigma设置为

代码语言:javascript
复制
mu, sigma  = np.log(1000), np.log(10)`

将生成您所期望的发行版。

票数 0
EN

Stack Overflow用户

发布于 2019-01-12 07:15:38

如果您知道您想要1000个对数正态分布的值(即,log(x)给出正态分布),并且您希望您的数据在10到10^5的范围内,那么您必须进行一些计算以获得µ和sigma。但是,您必须插入到np.random.lognormal中的值是基础的、相关的对数正态分布的平均值和标准差,而不是对数正态分布的。您可以从您看到的Wikipedia页面上给出的均值和方差公式中推导出这些公式。

代码语言:javascript
复制
# Parameters
xmax = 10**5
xmin = 10
n = 1000

# Get original mean and variance
# mu: We want normal distribution, so just take the average of the extremes.
# sigma: use the z = (x - mu)/sigma formula and approximation that 
#        the extremes are a deviation of z=3 away.
mu = (xmax + xmin)/2.0
sigma = (xmax - mu)/3.0
m = mu
v = sigma**2

# Get the mean and standard deviation of the underlying normal distribution
norm_mu = np.log(m**2 / np.sqrt(v + m**2))
norm_sigma = np.sqrt((v / m**2)+1)

# Generate random data and an overlying smooth curve
# (This is the same as your code, except I replaced the parameters
# in the 'pdf =' formula.)
s = np.random.lognormal(norm_mu, norm_sigma, n)
count, bins, ignored = plt.hist(s, n, density=True, align='mid')
x = np.linspace(min(bins), max(bins), n)
pdf = (np.exp(-(np.log(x) - norm_mu)**2 / (2 * norm_sigma**2)) / (x * norm_sigma * np.sqrt(2 * np.pi)))
plt.plot(x, pdf, linewidth=2, color='r')
plt.xscale('log')
plt.show()

这就是我所得到的。请注意,x轴上的缩放比例呈指数级上升,而不是线性上升。这就是你要找的东西吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54153401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档