MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / gamma

Method gamma

imperative/python/megengine/random/rng.py:376–433  ·  view source on GitHub ↗

r"""Random variable with Gamma distribution :math:`\Gamma(k, \theta)`. The corresponding probability density function is .. math:: p(x)=x^{k-1} \frac{e^{-x / \theta}}{\theta^{k} \Gamma(k)} \quad \text { for } x>0 \quad k, \theta>0, where :math:`\Ga

(
        self,
        shape: Union[Tensor, float],
        scale: Union[Tensor, float] = 1,
        size: Optional[Iterable[int]] = None,
    )

Source from the content-addressed store, hash-verified

374 )
375
376 def gamma(
377 self,
378 shape: Union[Tensor, float],
379 scale: Union[Tensor, float] = 1,
380 size: Optional[Iterable[int]] = None,
381 ):
382 r"""Random variable with Gamma distribution :math:`\Gamma(k, \theta)`.
383
384 The corresponding probability density function is
385
386 .. math::
387
388 p(x)=x^{k-1} \frac{e^{-x / \theta}}{\theta^{k} \Gamma(k)}
389 \quad \text { for } x>0 \quad k, \theta>0,
390
391 where :math:`\Gamma(k)` is the gamma function,
392
393 .. math::
394 \Gamma(k)=(k-1) ! \quad \text { for } \quad k \quad \text{is positive integer}.
395
396 Args:
397 shape(Union[Tensor, float]): the shape parameter (sometimes designated "k") of the distribution.
398 Must be positive.
399 scale(Union[Tensor, float]): the scale parameter (sometimes designated "theta") of the distribution.
400 Must be positive. Default: 1.
401 size(Optional[Iterable[int]]): the size of output tensor. If shape and scale are scalars and given size is, e.g.,
402 `(m, n)`, then the output shape is `(m, n)`. If shape or scale is a Tensor and given size
403 is, e.g., `(m, n)`, then the output shape is `(m, n) + broadcast(shape, scale).shape`.
404 The broadcast rules are consistent with `numpy.broadcast`. Default: None.
405
406 Returns:
407 Return type: tensor. The random variable with Gamma distribution.
408
409 Examples:
410 >>> import megengine.random as rand
411 >>> x = rand.gamma(shape=2, scale=1, size=(2, 2))
412 >>> x.numpy() # doctest: +SKIP
413 array([[0.97447544, 1.5668875 ],
414 [1.0069491 , 0.3078318 ]], dtype=float32)
415 >>> shape = mge.Tensor([[ 1],
416 ... [10]], dtype="float32")
417 >>> scale = mge.Tensor([1,5], dtype="float32")
418 >>> x = rand.gamma(shape=shape, scale=scale)
419 >>> x.numpy() # doctest: +SKIP
420 array([[ 0.11312152, 3.0799196 ],
421 [10.973469 , 29.596972 ]], dtype=float32)
422 >>> x = rand.gamma(shape=shape, scale=scale, size=2)
423 >>> x.numpy() # doctest: +SKIP
424 array([[[4.35868073e+00, 1.22415285e+01],
425 [1.02696848e+01, 4.19773598e+01]],
426
427 [[7.73875117e-02, 6.06766164e-01],
428 [1.22881927e+01, 8.13445740e+01]]], dtype=float32)
429 """
430 _seed = self._seed() if callable(self._seed) else self._seed
431 return _gamma(
432 shape=shape, scale=scale, size=size, seed=_seed, handle=self._handle
433 )

Callers 2

test_GammaRNGFunction · 0.95
fnFunction · 0.80

Calls 1

_gammaFunction · 0.85

Tested by 2

test_GammaRNGFunction · 0.76
fnFunction · 0.64