maxframe.tensor.random.lognormal#

maxframe.tensor.random.lognormal(mean=0.0, sigma=1.0, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#

从对数正态分布中抽取样本。

从具有指定均值、标准差和数组形状的对数正态分布中抽取样本。请注意,这里的均值和标准差不是分布本身的值,而是其来源的正态分布的值。

参数:
  • mean (float or array_like of floats, optional) -- 底层正态分布的均值。默认为0。

  • sigma (float or array_like of floats, optional) -- 底层正态分布的标准差。应大于零。默认为1。

  • size (int or tuple of ints, optional) -- 输出形状。如果给定的形状是例如 (m, n, k),则抽取 m * n * k 个样本。如果 size 是 None``(默认),且 ``meansigma 都是标量,则返回单个值。否则,抽取 np.broadcast(mean, sigma).size 个样本。

  • chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小

  • gpu (bool, optional) -- 如果为 True,则将张量分配在 GPU 上,默认为 False

  • dtype (data-type, optional) -- 返回张量的数据类型。

返回:

out -- 从参数化的对数正态分布中抽取的样本。

返回类型:

Tensor or scalar

参见

scipy.stats.lognorm

概率密度函数、分布、累积密度函数等。

备注

如果变量 x 的对数 log(x) 服从正态分布,则 x 服从对数正态分布。对数正态分布的概率密度函数为:

\[p(x) = \frac{1}{\sigma x \sqrt{2\pi}} e^{(-\frac{(ln(x)-\mu)^2}{2\sigma^2})}\]

其中 \(\mu\) 是变量对数的正态分布均值,\(\sigma\) 是其标准差。当一个随机变量是大量独立同分布变量的 乘积 时,其结果服从对数正态分布,这与变量是大量独立同分布变量的 时服从正态分布类似。

引用

示例

从分布中抽取样本:

>>> import maxframe.tensor as mt
>>> mu, sigma = 3., 1. # mean and standard deviation
>>> s = mt.random.lognormal(mu, sigma, 1000)

显示样本的直方图以及概率密度函数:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s.execute(), 100, normed=True, align='mid')
>>> x = mt.linspace(min(bins), max(bins), 10000)
>>> pdf = (mt.exp(-(mt.log(x) - mu)**2 / (2 * sigma**2))
...        / (x * sigma * mt.sqrt(2 * mt.pi)))
>>> plt.plot(x.execute(), pdf.execute(), linewidth=2, color='r')
>>> plt.axis('tight')
>>> plt.show()

演示从均匀分布中随机抽取样本并取其乘积后,可以很好地拟合对数正态概率密度函数。

>>> # Generate a thousand samples: each is the product of 100 random
>>> # values, drawn from a normal distribution.
>>> b = []
>>> for i in range(1000):
...    a = 10. + mt.random.random(100)
...    b.append(mt.product(a).execute())
>>> b = mt.array(b) / mt.min(b) # scale values to be positive
>>> count, bins, ignored = plt.hist(b.execute(), 100, normed=True, align='mid')
>>> sigma = mt.std(mt.log(b))
>>> mu = mt.mean(mt.log(b))
>>> x = mt.linspace(min(bins), max(bins), 10000)
>>> pdf = (mt.exp(-(mt.log(x) - mu)**2 / (2 * sigma**2))
...        / (x * sigma * mt.sqrt(2 * mt.pi)))
>>> plt.plot(x.execute(), pdf.execute(), color='r', linewidth=2)
>>> plt.show()