maxframe.tensor.random.gamma#
- maxframe.tensor.random.gamma(shape, scale=1.0, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#
从 Gamma 分布中抽取样本。
样本从具有指定参数的 Gamma 分布中抽取,`shape`(有时记为 "k")和 `scale`(有时记为 "theta"),这两个参数都大于 0。
- 参数:
shape (float or array_like of floats) -- Gamma 分布的形状参数。应大于零。
scale (float or array_like of floats, optional) -- Gamma 分布的尺度参数。应大于零。默认值为 1。
size (int or tuple of ints, optional) -- 输出形状。如果给定形状为,例如
(m, n, k),则抽取m * n * k个样本。如果 size 为None``(默认),当 ``shape和scale都为标量时返回单个值。否则,抽取np.broadcast(shape, scale).size个样本。chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的 chunk 大小
gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False
dtype (data-type, optional) -- 返回张量的数据类型。
- 返回:
out -- 从参数化的 Gamma 分布中抽取的样本。
- 返回类型:
Tensor or scalar
参见
scipy.stats.gamma概率密度函数、分布函数或累积密度函数等。
备注
Gamma 分布的概率密度为
\[p(x) = x^{k-1}\frac{e^{-x/\theta}}{\theta^k\Gamma(k)},\]其中 \(k\) 是形状参数,\(\theta\) 是尺度参数,\(\Gamma\) 是 Gamma 函数。
Gamma 分布通常用于建模电子元件的失效时间,并在泊松分布事件之间的等待时间相关的自然过程中出现。
引用
示例
从分布中抽取样本:
>>> import maxframe.tensor as mt
>>> shape, scale = 2., 2. # mean=4, std=2*sqrt(2) >>> s = mt.random.gamma(shape, scale, 1000).execute()
显示样本的直方图以及概率密度函数:
>>> import matplotlib.pyplot as plt >>> import scipy.special as sps >>> import numpy as np >>> count, bins, ignored = plt.hist(s, 50, normed=True) >>> y = bins**(shape-1)*(np.exp(-bins/scale) / ... (sps.gamma(shape)*scale**shape)) >>> plt.plot(bins, y, linewidth=2, color='r') >>> plt.show()