megengine.random.gamma

gamma(shape, scale=1, size=None)

Random variable with Gamma distribution \(\Gamma(k, \theta)\).

The corresponding probability density function is

\[p(x)=x^{k-1} \frac{e^{-x / \theta}}{\theta^{k} \Gamma(k)} \quad \text { for } x>0 \quad k, \theta>0,\]

where \(\Gamma(k)\) is the gamma function,

\[\Gamma(k)=(k-1) ! \quad \text { for } \quad k \quad \text{is positive integer}.\]
Parameters
  • shape (Union[Tensor, float]) – the shape parameter (sometimes designated “k”) of the distribution. Must be positive.

  • scale (Union[Tensor, float]) – the scale parameter (sometimes designated “theta”) of the distribution. Must be positive. Default: 1.

  • size (Optional[Iterable[int]]) – the size of output tensor. If shape and scale are scalars and given size is, e.g., (m, n), then the output shape is (m, n). If shape or scale is a Tensor and given size is, e.g., (m, n), then the output shape is (m, n) + broadcast(shape, scale).shape. The broadcast rules are consistent with numpy.broadcast. Default: None.

Returns

tensor. The random variable with Gamma distribution.

Return type

Return type

Examples

>>> import megengine.random as rand
>>> x = rand.gamma(shape=2, scale=1, size=(2, 2))
>>> x.numpy()   
array([[0.97447544, 1.5668875 ],
       [1.0069491 , 0.3078318 ]], dtype=float32)
>>> shape = mge.Tensor([[ 1],
...                     [10]], dtype="float32")
>>> scale = mge.Tensor([1,5], dtype="float32")
>>> x = rand.gamma(shape=shape, scale=scale)
>>> x.numpy()   
array([[ 0.11312152,  3.0799196 ],
       [10.973469  , 29.596972  ]], dtype=float32)
>>> x = rand.gamma(shape=shape, scale=scale, size=2)
>>> x.numpy()   
array([[[4.35868073e+00, 1.22415285e+01],
        [1.02696848e+01, 4.19773598e+01]],
[[7.73875117e-02, 6.06766164e-01],

[1.22881927e+01, 8.13445740e+01]]], dtype=float32)