megengine.random.beta

beta(alpha, beta, size=None)

Random variable with Beta distribution \(\operatorname{Beta}(\alpha, \beta)\).

The corresponding probability density function is

\[p(x)=\frac{1}{\mathrm{~B}(\alpha, \beta)} x^{\alpha-1}(1-x)^{\beta-1} \quad \text { for } \alpha, \beta>0,\]

where \(\mathrm{~B}(\alpha, \beta)\) is the beta function,

\[\mathrm{~B}(\alpha, \beta)=\int_{0}^{1} t^{\alpha-1}(1-t)^{\beta-1} d t.\]
参数
  • alpha (Union[Tensor, float]) – the alpha parameter of the distribution. Must be non-negative.

  • beta (Union[Tensor, float]) – the beta parameter of the distribution. Must be non-negative.

  • size (Optional[Iterable[int]]) – the size of output tensor. If alpha and beta are scalars and given size is, e.g., (m, n), then the output shape is (m, n). If alpha or beta is a Tensor and given size is, e.g., (m, n), then the output shape is (m, n) + broadcast(alpha, beta).shape.

返回

the output tensor.

实际案例

import megengine as mge
import megengine.random as rand

x = rand.beta(alpha=2, beta=1, size=(2, 2))
print(x.numpy())

alpha = mge.Tensor([[0.5],
                    [  3]], dtype="float32")
beta = mge.Tensor([0.5,5], dtype="float32")

x = rand.beta(alpha=alpha, beta=beta)
print(x.numpy())

x = rand.beta(alpha=alpha, beta=beta, size=2)
print(x.numpy())

Outputs:

[[0.582565   0.91763186]
 [0.86963767 0.6088103 ]]

[[0.41503012 0.16438372]
 [0.90159506 0.47588003]]

[[[0.55195075 0.01111084]
  [0.95298755 0.25048104]]
 [[0.11680304 0.13859665]
  [0.997879   0.43259275]]]