【发布时间】:2019-01-11 20:09:31
【问题描述】:
我有一个关于对数正态分布的问题。我想创建和集合具有从 10 到 10**5 的“质量”通常分布的对象。我认为这将是一个对数正态分布,所以我开始尝试在 python 中这样做:
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 的示例所示,但更改 mu 和 sigma 并查看图表,我无法确定是否将 m 和 v(按照下面链接的 wikipedia 文章)设置为 10**5 和 1000说给了我想要的东西
我查看了https://en.wikipedia.org/wiki/Log-normal_distribution 以弄清楚如何计算 mu 和 sigma,但也许我做错了其他事情。这是解决这个问题的正确方法吗?
我阅读了以前关于对数正态分布的问题/答案,但我认为他们问的不是同一件事。如果此类问题已得到解答,请提前致歉。
mu, sigma = 3., 1. 示例中给出的是什么,这很好用,但是当我将 mu 和 sigma 更改为以下值时:
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。
尝试建议的方法:
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
【问题讨论】:
-
您的问题缺乏明确的问题陈述?你到底想要什么? wiki 给出了 m,v 和 mu, sigma 之间的关系。您在问题中提到了 m, v 的值,但您的代码中没有任何地方使用它们。请具体明确。为什么选择 mu=3 和 sigma=1?
-
谢谢,我更新了问题。 3 和 1 只是 np.random.lognormal() 文档中示例中的值
标签: python statistics