maxframe.tensor.random.laplace#

maxframe.tensor.random.laplace(loc=0.0, scale=1.0, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#

从具有指定位置(或均值)和尺度(衰减)的拉普拉斯或双指数分布中抽取样本。

拉普拉斯分布与高斯/正态分布相似,但在峰值处更尖锐,尾部更厚。它表示两个独立且同分布的指数随机变量之间的差值。

参数:
  • loc (float or array_like of floats, optional) -- 分布峰值的位置 \(\mu\)。默认为 0。

  • scale (float or array_like of floats, optional) -- \(\lambda\),指数衰减。默认为 1。

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

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

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

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

返回:

out -- 从参数化的拉普拉斯分布中抽取的样本。

返回类型:

Tensor or scalar

备注

其概率密度函数为

\[f(x; \mu, \lambda) = \frac{1}{2\lambda} \exp\left(-\frac{|x - \mu|}{\lambda}\right).\]

拉普拉斯第一定律(1774年)指出,误差的频率可以表示为误差绝对值的指数函数,这导致了拉普拉斯分布。在经济学和健康科学的许多问题中,该分布似乎比标准高斯分布更好地拟合数据。

引用

示例

从分布中抽取样本

>>> import maxframe.tensor as mt
>>> loc, scale = 0., 1.
>>> s = mt.random.laplace(loc, scale, 1000)

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

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s.execute(), 30, normed=True)
>>> x = mt.arange(-8., 8., .01)
>>> pdf = mt.exp(-abs(x-loc)/scale)/(2.*scale)
>>> plt.plot(x.execute(), pdf.execute())

绘制高斯分布进行比较:

>>> g = (1/(scale * mt.sqrt(2 * np.pi)) *
...      mt.exp(-(x - loc)**2 / (2 * scale**2)))
>>> plt.plot(x.execute(),g.execute())